lexer.lpp 10 KB

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