lexer.lpp 9.9 KB

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