lexer.lpp 10 KB

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