lexer.lpp 9.1 KB

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