lexer.lpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. horizontal_whitespace [ \t\r]
  75. whitespace [ \t\r\n]
  76. one_line_comment \/\/[^\n]*\n
  77. operand_start [(A-Za-z0-9_"]
  78. /* Single-line string literals should reject vertical whitespace. */
  79. string_literal \"([^\\\"\n\v\f\r]|\\.)*\"
  80. %{
  81. // This macro is expanded immediately before each action specified below.
  82. //
  83. // Advances the current token position by yyleng columns without changing
  84. // the line number, and takes us out of the after-whitespace / after-operand
  85. // state.
  86. # define YY_USER_ACTION \
  87. context.current_token_position.columns(yyleng); \
  88. if (YY_START == AFTER_WHITESPACE || \
  89. YY_START == AFTER_OPERAND) { \
  90. BEGIN(INITIAL); \
  91. }
  92. #define SIMPLE_TOKEN(name) \
  93. Carbon::Parser::make_##name(context.current_token_position);
  94. #define ARG_TOKEN(name, arg) \
  95. Carbon::Parser::make_##name(arg, context.current_token_position);
  96. %}
  97. %%
  98. %{
  99. // Code run each time yylex is called.
  100. // Begin with an empty token span starting where its previous end was.
  101. context.current_token_position.step();
  102. %}
  103. {AND} { return SIMPLE_TOKEN(AND); }
  104. {ARROW} { return SIMPLE_TOKEN(ARROW); }
  105. {AUTO} { return SIMPLE_TOKEN(AUTO); }
  106. {AWAIT} { return SIMPLE_TOKEN(AWAIT); }
  107. {BOOL} { return SIMPLE_TOKEN(BOOL); }
  108. {BREAK} { return SIMPLE_TOKEN(BREAK); }
  109. {CASE} { return SIMPLE_TOKEN(CASE); }
  110. {CHOICE} { return SIMPLE_TOKEN(CHOICE); }
  111. {CLASS} { return SIMPLE_TOKEN(CLASS); }
  112. {COLON_BANG} { return SIMPLE_TOKEN(COLON_BANG); }
  113. {COLON} { return SIMPLE_TOKEN(COLON); }
  114. {COMMA} { return SIMPLE_TOKEN(COMMA); }
  115. {CONTINUATION_TYPE} { return SIMPLE_TOKEN(CONTINUATION_TYPE); }
  116. {CONTINUATION} { return SIMPLE_TOKEN(CONTINUATION); }
  117. {CONTINUE} { return SIMPLE_TOKEN(CONTINUE); }
  118. {DEFAULT} { return SIMPLE_TOKEN(DEFAULT); }
  119. {DOUBLE_ARROW} { return SIMPLE_TOKEN(DOUBLE_ARROW); }
  120. {ELSE} { return SIMPLE_TOKEN(ELSE); }
  121. {EQUAL_EQUAL} { return SIMPLE_TOKEN(EQUAL_EQUAL); }
  122. {EQUAL} { return SIMPLE_TOKEN(EQUAL); }
  123. {FALSE} { return SIMPLE_TOKEN(FALSE); }
  124. {FNTY} { return SIMPLE_TOKEN(FNTY); }
  125. {FN} { return SIMPLE_TOKEN(FN); }
  126. {IF} { return SIMPLE_TOKEN(IF); }
  127. {LEFT_PARENTHESIS} { return SIMPLE_TOKEN(LEFT_PARENTHESIS); }
  128. {LEFT_CURLY_BRACE} { return SIMPLE_TOKEN(LEFT_CURLY_BRACE); }
  129. {LEFT_SQUARE_BRACKET} { return SIMPLE_TOKEN(LEFT_SQUARE_BRACKET); }
  130. {MATCH} { return SIMPLE_TOKEN(MATCH); }
  131. {MINUS} { return SIMPLE_TOKEN(MINUS); }
  132. {NOT} { return SIMPLE_TOKEN(NOT); }
  133. {OR} { return SIMPLE_TOKEN(OR); }
  134. {PERIOD} { return SIMPLE_TOKEN(PERIOD); }
  135. {PLUS} { return SIMPLE_TOKEN(PLUS); }
  136. {RETURN} { return SIMPLE_TOKEN(RETURN); }
  137. {RUN} { return SIMPLE_TOKEN(RUN); }
  138. {SEMICOLON} { return SIMPLE_TOKEN(SEMICOLON); }
  139. {SLASH} { return SIMPLE_TOKEN(SLASH); }
  140. {STRING} { return SIMPLE_TOKEN(STRING); }
  141. {TRUE} { return SIMPLE_TOKEN(TRUE); }
  142. {TYPE} { return SIMPLE_TOKEN(TYPE); }
  143. {UNDERSCORE} { return SIMPLE_TOKEN(UNDERSCORE); }
  144. {VAR} { return SIMPLE_TOKEN(VAR); }
  145. {WHILE} { return SIMPLE_TOKEN(WHILE); }
  146. /* More modern Bisons provide make_EOF. */
  147. <<EOF>> { return SIMPLE_TOKEN(END_OF_FILE); }
  148. {RIGHT_PARENTHESIS} {
  149. BEGIN(AFTER_OPERAND);
  150. return SIMPLE_TOKEN(RIGHT_PARENTHESIS);
  151. }
  152. {RIGHT_CURLY_BRACE} {
  153. BEGIN(AFTER_OPERAND);
  154. return SIMPLE_TOKEN(RIGHT_CURLY_BRACE);
  155. }
  156. {RIGHT_SQUARE_BRACKET} {
  157. BEGIN(AFTER_OPERAND);
  158. return SIMPLE_TOKEN(RIGHT_SQUARE_BRACKET);
  159. }
  160. /*
  161. * For a `*` operator, we look at whitespace and local context to determine the
  162. * arity and fixity. There are two ways to write a binary operator:
  163. *
  164. * 1) Whitespace on both sides.
  165. * 2) Whitespace on neither side, and the previous token is considered to be
  166. * the end of an operand, and the next token is considered to be the start
  167. * of an operand.
  168. *
  169. * Otherwise, the operator is unary, but we also check for whitespace to help
  170. * the parser enforce the rule that whitespace is not permitted between the
  171. * operator and its operand, leading to three more cases:
  172. *
  173. * 3) Whitespace before (but implicitly not after, because that would give a
  174. * longer match and hit case 1): this can only be a prefix operator.
  175. * 4) Whitespace after and not before: this can only be a postfix operator.
  176. * 5) No whitespace on either side (otherwise the longest match would take us
  177. * to case 4): this is a unary operator and could be either prefix or
  178. * postfix.
  179. */
  180. /* `*` operator case 1: */
  181. <AFTER_WHITESPACE>"*"{whitespace}+ {
  182. BEGIN(AFTER_WHITESPACE);
  183. return SIMPLE_TOKEN(BINARY_STAR);
  184. }
  185. /* `*` operator case 2: */
  186. <AFTER_OPERAND>"*"/{operand_start} {
  187. return SIMPLE_TOKEN(BINARY_STAR);
  188. }
  189. /* `*` operator case 3: */
  190. <AFTER_WHITESPACE>"*" {
  191. return SIMPLE_TOKEN(PREFIX_STAR);
  192. }
  193. /* `*` operator case 4: */
  194. <INITIAL,AFTER_OPERAND>"*"{whitespace}+ {
  195. BEGIN(AFTER_WHITESPACE);
  196. return SIMPLE_TOKEN(POSTFIX_STAR);
  197. }
  198. /* `*` operator case 5: */
  199. <INITIAL,AFTER_OPERAND>"*" {
  200. return SIMPLE_TOKEN(UNARY_STAR);
  201. }
  202. {sized_type_literal} {
  203. return ARG_TOKEN(sized_type_literal, yytext);
  204. }
  205. {identifier} {
  206. BEGIN(AFTER_OPERAND);
  207. return ARG_TOKEN(identifier, yytext);
  208. }
  209. {integer_literal} {
  210. BEGIN(AFTER_OPERAND);
  211. int val;
  212. CHECK(llvm::to_integer(yytext, val));
  213. return ARG_TOKEN(integer_literal, val);
  214. }
  215. {string_literal} {
  216. llvm::StringRef str(yytext);
  217. CHECK(str.consume_front("\"") && str.consume_back("\""));
  218. std::optional<std::string> unescaped = Carbon::UnescapeStringLiteral(str);
  219. if (unescaped == std::nullopt) {
  220. if (Carbon::tracing_output) {
  221. // Print a newline because tracing prints an incomplete line
  222. // "Reading a token: ".
  223. llvm::errs() << "\n";
  224. }
  225. FATAL_COMPILATION_ERROR(context.SourceLoc())
  226. << "Invalid escaping in string: " << yytext;
  227. }
  228. return ARG_TOKEN(string_literal, *unescaped);
  229. }
  230. {one_line_comment} {
  231. // Advance end by 1 line, resetting the column to zero.
  232. context.current_token_position.lines(1);
  233. // Make the span empty by setting start to end.
  234. context.current_token_position.step();
  235. }
  236. {horizontal_whitespace}+ {
  237. // Make the span empty by setting start to end.
  238. context.current_token_position.step();
  239. BEGIN(AFTER_WHITESPACE);
  240. }
  241. \n+ {
  242. // Advance end by yyleng lines, resetting the column to zero.
  243. context.current_token_position.lines(yyleng);
  244. // Make the span empty by setting start to end.
  245. context.current_token_position.step();
  246. BEGIN(AFTER_WHITESPACE);
  247. }
  248. . {
  249. if (Carbon::tracing_output) {
  250. // Print a newline because tracing prints an incomplete line
  251. // "Reading a token: ".
  252. llvm::errs() << "\n";
  253. }
  254. FATAL_COMPILATION_ERROR(context.SourceLoc()) << "invalid character '\\x"
  255. << llvm::toHex(llvm::StringRef(yytext, 1)) << "' in source file.";
  256. }
  257. %%