lexer.lpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  3. Exceptions. See /LICENSE for license information.
  4. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5. */
  6. %{
  7. #include <cstdlib>
  8. #include "common/check.h"
  9. #include "common/string_helpers.h"
  10. #include "executable_semantics/syntax/parse_and_lex_context.h"
  11. #include "executable_semantics/syntax/parser.h"
  12. #include "llvm/ADT/StringExtras.h"
  13. %}
  14. /* Turn off legacy bits we don't need. */
  15. %option noyywrap nounput nodefault noinput
  16. %option reentrant
  17. /* Lexing a token immediately after consuming some whitespace. */
  18. %s AFTER_WHITESPACE
  19. /*
  20. * Lexing a token immediately after consuming an operand-ending token:
  21. * a closing bracket, identifier, or literal.
  22. */
  23. %s AFTER_OPERAND
  24. /* table-begin */
  25. AND "and"
  26. API "api"
  27. ARROW "->"
  28. AUTO "auto"
  29. AWAIT "__await"
  30. BOOL "Bool"
  31. BREAK "break"
  32. CASE "case"
  33. CHOICE "choice"
  34. CLASS "class"
  35. COLON ":"
  36. COLON_BANG ":!"
  37. COMMA ","
  38. CONTINUATION "__continuation"
  39. CONTINUATION_TYPE "__Continuation"
  40. CONTINUE "continue"
  41. DEFAULT "default"
  42. DOUBLE_ARROW "=>"
  43. ELSE "else"
  44. EQUAL "="
  45. EQUAL_EQUAL "=="
  46. FALSE "false"
  47. FN "fn"
  48. FN_TYPE "__Fn"
  49. IF "if"
  50. IMPL "impl"
  51. IMPORT "import"
  52. LEFT_CURLY_BRACE "{"
  53. LEFT_PARENTHESIS "("
  54. LEFT_SQUARE_BRACKET "["
  55. LIBRARY "library"
  56. MATCH "match"
  57. MINUS "-"
  58. NOT "not"
  59. OR "or"
  60. PACKAGE "package"
  61. PERIOD "."
  62. PLUS "+"
  63. RETURN "return"
  64. RIGHT_CURLY_BRACE "}"
  65. RIGHT_PARENTHESIS ")"
  66. RIGHT_SQUARE_BRACKET "]"
  67. RUN "__run"
  68. SEMICOLON ";"
  69. SLASH "/"
  70. STRING "String"
  71. TRUE "true"
  72. TYPE "Type"
  73. UNDERSCORE "_"
  74. UNIMPL_EXAMPLE "__unimplemented_example_infix"
  75. VAR "var"
  76. WHILE "while"
  77. /* table-end */
  78. /* This should be kept table-like, but isn't automatic due to spaces. */
  79. identifier [A-Za-z_][A-Za-z0-9_]*
  80. intrinsic_identifier __intrinsic_[A-Za-z0-9_]*
  81. sized_type_literal [iuf][1-9][0-9]*
  82. integer_literal [0-9]+
  83. horizontal_whitespace [ \t\r]
  84. whitespace [ \t\r\n]
  85. one_line_comment \/\/[^\n]*\n
  86. operand_start [(A-Za-z0-9_\"]
  87. /* Single-line string literals should reject vertical whitespace. */
  88. string_literal \"([^\\\"\n\v\f\r]|\\.)*\"
  89. %{
  90. // This macro is expanded immediately before each action specified below.
  91. //
  92. // Advances the current token position by yyleng columns without changing
  93. // the line number, and takes us out of the after-whitespace / after-operand
  94. // state.
  95. #define YY_USER_ACTION \
  96. context.current_token_position.columns(yyleng); \
  97. if (YY_START == AFTER_WHITESPACE || YY_START == AFTER_OPERAND) { \
  98. BEGIN(INITIAL); \
  99. }
  100. #define SIMPLE_TOKEN(name) \
  101. Carbon::Parser::make_##name(context.current_token_position);
  102. #define ARG_TOKEN(name, arg) \
  103. Carbon::Parser::make_##name(arg, context.current_token_position);
  104. %}
  105. %%
  106. %{
  107. // Code run each time yylex is called.
  108. // Begin with an empty token span starting where its previous end was.
  109. context.current_token_position.step();
  110. %}
  111. /* table-begin */
  112. {AND} { return SIMPLE_TOKEN(AND); }
  113. {API} { return SIMPLE_TOKEN(API); }
  114. {ARROW} { return SIMPLE_TOKEN(ARROW); }
  115. {AUTO} { return SIMPLE_TOKEN(AUTO); }
  116. {AWAIT} { return SIMPLE_TOKEN(AWAIT); }
  117. {BOOL} { return SIMPLE_TOKEN(BOOL); }
  118. {BREAK} { return SIMPLE_TOKEN(BREAK); }
  119. {CASE} { return SIMPLE_TOKEN(CASE); }
  120. {CHOICE} { return SIMPLE_TOKEN(CHOICE); }
  121. {CLASS} { return SIMPLE_TOKEN(CLASS); }
  122. {COLON_BANG} { return SIMPLE_TOKEN(COLON_BANG); }
  123. {COLON} { return SIMPLE_TOKEN(COLON); }
  124. {COMMA} { return SIMPLE_TOKEN(COMMA); }
  125. {CONTINUATION_TYPE} { return SIMPLE_TOKEN(CONTINUATION_TYPE); }
  126. {CONTINUATION} { return SIMPLE_TOKEN(CONTINUATION); }
  127. {CONTINUE} { return SIMPLE_TOKEN(CONTINUE); }
  128. {DEFAULT} { return SIMPLE_TOKEN(DEFAULT); }
  129. {DOUBLE_ARROW} { return SIMPLE_TOKEN(DOUBLE_ARROW); }
  130. {ELSE} { return SIMPLE_TOKEN(ELSE); }
  131. {EQUAL_EQUAL} { return SIMPLE_TOKEN(EQUAL_EQUAL); }
  132. {EQUAL} { return SIMPLE_TOKEN(EQUAL); }
  133. {FALSE} { return SIMPLE_TOKEN(FALSE); }
  134. {FN_TYPE} { return SIMPLE_TOKEN(FN_TYPE); }
  135. {FN} { return SIMPLE_TOKEN(FN); }
  136. {IF} { return SIMPLE_TOKEN(IF); }
  137. {IMPL} { return SIMPLE_TOKEN(IMPL); }
  138. {IMPORT} { return SIMPLE_TOKEN(IMPORT); }
  139. {LEFT_CURLY_BRACE} { return SIMPLE_TOKEN(LEFT_CURLY_BRACE); }
  140. {LEFT_PARENTHESIS} { return SIMPLE_TOKEN(LEFT_PARENTHESIS); }
  141. {LEFT_SQUARE_BRACKET} { return SIMPLE_TOKEN(LEFT_SQUARE_BRACKET); }
  142. {LIBRARY} { return SIMPLE_TOKEN(LIBRARY); }
  143. {MATCH} { return SIMPLE_TOKEN(MATCH); }
  144. {MINUS} { return SIMPLE_TOKEN(MINUS); }
  145. {NOT} { return SIMPLE_TOKEN(NOT); }
  146. {OR} { return SIMPLE_TOKEN(OR); }
  147. {PACKAGE} { return SIMPLE_TOKEN(PACKAGE); }
  148. {PERIOD} { return SIMPLE_TOKEN(PERIOD); }
  149. {PLUS} { return SIMPLE_TOKEN(PLUS); }
  150. {RETURN} { return SIMPLE_TOKEN(RETURN); }
  151. {RUN} { return SIMPLE_TOKEN(RUN); }
  152. {SEMICOLON} { return SIMPLE_TOKEN(SEMICOLON); }
  153. {SLASH} { return SIMPLE_TOKEN(SLASH); }
  154. {STRING} { return SIMPLE_TOKEN(STRING); }
  155. {TRUE} { return SIMPLE_TOKEN(TRUE); }
  156. {TYPE} { return SIMPLE_TOKEN(TYPE); }
  157. {UNDERSCORE} { return SIMPLE_TOKEN(UNDERSCORE); }
  158. {UNIMPL_EXAMPLE} { return SIMPLE_TOKEN(UNIMPL_EXAMPLE); }
  159. {VAR} { return SIMPLE_TOKEN(VAR); }
  160. {WHILE} { return SIMPLE_TOKEN(WHILE); }
  161. /* table-end */
  162. /* More modern Bisons provide make_EOF. */
  163. <<EOF>> { return SIMPLE_TOKEN(END_OF_FILE); }
  164. {RIGHT_PARENTHESIS} {
  165. BEGIN(AFTER_OPERAND);
  166. return SIMPLE_TOKEN(RIGHT_PARENTHESIS);
  167. }
  168. {RIGHT_CURLY_BRACE} {
  169. BEGIN(AFTER_OPERAND);
  170. return SIMPLE_TOKEN(RIGHT_CURLY_BRACE);
  171. }
  172. {RIGHT_SQUARE_BRACKET} {
  173. BEGIN(AFTER_OPERAND);
  174. return SIMPLE_TOKEN(RIGHT_SQUARE_BRACKET);
  175. }
  176. /*
  177. * For a `*` operator, we look at whitespace and local context to determine the
  178. * arity and fixity. There are two ways to write a binary operator:
  179. *
  180. * 1) Whitespace on both sides.
  181. * 2) Whitespace on neither side, and the previous token is considered to be
  182. * the end of an operand, and the next token is considered to be the start
  183. * of an operand.
  184. *
  185. * Otherwise, the operator is unary, but we also check for whitespace to help
  186. * the parser enforce the rule that whitespace is not permitted between the
  187. * operator and its operand, leading to three more cases:
  188. *
  189. * 3) Whitespace before (but implicitly not after, because that would give a
  190. * longer match and hit case 1): this can only be a prefix operator.
  191. * 4) Whitespace after and not before: this can only be a postfix operator.
  192. * 5) No whitespace on either side (otherwise the longest match would take us
  193. * to case 4): this is a unary operator and could be either prefix or
  194. * postfix.
  195. */
  196. /* `*` operator case 1: */
  197. <AFTER_WHITESPACE>"*"{whitespace}+ {
  198. BEGIN(AFTER_WHITESPACE);
  199. return SIMPLE_TOKEN(BINARY_STAR);
  200. }
  201. /* `*` operator case 2: */
  202. <AFTER_OPERAND>"*"/{operand_start} { return SIMPLE_TOKEN(BINARY_STAR); }
  203. /* `*` operator case 3: */
  204. <AFTER_WHITESPACE>"*" { return SIMPLE_TOKEN(PREFIX_STAR); }
  205. /* `*` operator case 4: */
  206. <INITIAL,AFTER_OPERAND>"*"{whitespace}+ {
  207. BEGIN(AFTER_WHITESPACE);
  208. return SIMPLE_TOKEN(POSTFIX_STAR);
  209. }
  210. /* `*` operator case 5: */
  211. <INITIAL,AFTER_OPERAND>"*" { return SIMPLE_TOKEN(UNARY_STAR); }
  212. {sized_type_literal} {
  213. BEGIN(AFTER_OPERAND);
  214. return ARG_TOKEN(sized_type_literal, yytext);
  215. }
  216. {intrinsic_identifier} {
  217. BEGIN(AFTER_OPERAND);
  218. return ARG_TOKEN(intrinsic_identifier, yytext);
  219. }
  220. {identifier} {
  221. BEGIN(AFTER_OPERAND);
  222. return ARG_TOKEN(identifier, yytext);
  223. }
  224. {integer_literal} {
  225. BEGIN(AFTER_OPERAND);
  226. int val;
  227. CHECK(llvm::to_integer(yytext, val));
  228. return ARG_TOKEN(integer_literal, val);
  229. }
  230. {string_literal} {
  231. llvm::StringRef str(yytext);
  232. CHECK(str.consume_front("\"") && str.consume_back("\""));
  233. std::optional<std::string> unescaped = Carbon::UnescapeStringLiteral(str);
  234. if (unescaped == std::nullopt) {
  235. if (context.trace()) {
  236. // Print a newline because trace prints an incomplete line
  237. // "Reading a token: ".
  238. llvm::errs() << "\n";
  239. }
  240. FATAL_COMPILATION_ERROR(context.source_loc())
  241. << "Invalid escaping in string: " << yytext;
  242. }
  243. return ARG_TOKEN(string_literal, *unescaped);
  244. }
  245. {one_line_comment} {
  246. // Advance end by 1 line, resetting the column to zero.
  247. context.current_token_position.lines(1);
  248. // Make the span empty by setting start to end.
  249. context.current_token_position.step();
  250. }
  251. {horizontal_whitespace}+ {
  252. // Make the span empty by setting start to end.
  253. context.current_token_position.step();
  254. BEGIN(AFTER_WHITESPACE);
  255. }
  256. \n+ {
  257. // Advance end by yyleng lines, resetting the column to zero.
  258. context.current_token_position.lines(yyleng);
  259. // Make the span empty by setting start to end.
  260. context.current_token_position.step();
  261. BEGIN(AFTER_WHITESPACE);
  262. }
  263. . {
  264. if (context.trace()) {
  265. // Print a newline because trace prints an incomplete line
  266. // "Reading a token: ".
  267. llvm::errs() << "\n";
  268. }
  269. FATAL_COMPILATION_ERROR(context.source_loc())
  270. << "invalid character '\\x" << llvm::toHex(llvm::StringRef(yytext, 1))
  271. << "' in source file.";
  272. }
  273. %%