lexer.lpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. // Prints a newline in trace mode because trace prints an incomplete line
  14. // "Reading a token: " which can prevent LIT from finding expected patterns.
  15. #define FATAL_SYNTAX_ERROR(context) \
  16. RAW_EXITING_STREAM() << (context.trace() ? "\n" : "") \
  17. << "COMPILATION ERROR: " << (context.source_loc()) \
  18. << ": "
  19. // Reads and returns a single character. Fails on EOF.
  20. char ReadChar(yyscan_t yyscanner, const Carbon::ParseAndLexContext& context);
  21. %}
  22. /* Turn off legacy bits we don't need. */
  23. %option noyywrap nounput nodefault
  24. %option reentrant
  25. /* Lexing a token immediately after consuming some whitespace. */
  26. %s AFTER_WHITESPACE
  27. /*
  28. * Lexing a token immediately after consuming an operand-ending token:
  29. * a closing bracket, identifier, or literal.
  30. */
  31. %s AFTER_OPERAND
  32. /* table-begin */
  33. AMPERSAND "&"
  34. AND "and"
  35. API "api"
  36. ARROW "->"
  37. AS "as"
  38. AUTO "auto"
  39. AWAIT "__await"
  40. BOOL "Bool"
  41. BREAK "break"
  42. CASE "case"
  43. CHOICE "choice"
  44. CLASS "class"
  45. COLON ":"
  46. COLON_BANG ":!"
  47. COMMA ","
  48. CONTINUATION "__continuation"
  49. CONTINUATION_TYPE "__Continuation"
  50. CONTINUE "continue"
  51. DEFAULT "default"
  52. DOUBLE_ARROW "=>"
  53. ELSE "else"
  54. EQUAL "="
  55. EQUAL_EQUAL "=="
  56. EXTERNAL "external"
  57. FALSE "false"
  58. FN "fn"
  59. FN_TYPE "__Fn"
  60. IF "if"
  61. IMPL "impl"
  62. IMPORT "import"
  63. INTERFACE "interface"
  64. LEFT_CURLY_BRACE "{"
  65. LEFT_PARENTHESIS "("
  66. LEFT_SQUARE_BRACKET "["
  67. LET "let"
  68. LIBRARY "library"
  69. MATCH "match"
  70. MINUS "-"
  71. NOT "not"
  72. OR "or"
  73. PACKAGE "package"
  74. PERIOD "."
  75. PLUS "+"
  76. RETURN "return"
  77. RIGHT_CURLY_BRACE "}"
  78. RIGHT_PARENTHESIS ")"
  79. RIGHT_SQUARE_BRACKET "]"
  80. RUN "__run"
  81. SEMICOLON ";"
  82. SLASH "/"
  83. STRING "String"
  84. THEN "then"
  85. TRUE "true"
  86. TYPE "Type"
  87. UNDERSCORE "_"
  88. UNIMPL_EXAMPLE "__unimplemented_example_infix"
  89. VAR "var"
  90. WHILE "while"
  91. /* table-end */
  92. /* This should be kept table-like, but isn't automatic due to spaces. */
  93. identifier [A-Za-z_][A-Za-z0-9_]*
  94. intrinsic_identifier __intrinsic_[A-Za-z0-9_]*
  95. sized_type_literal [iuf][1-9][0-9]*
  96. integer_literal [0-9]+
  97. horizontal_whitespace [ \t\r]
  98. whitespace [ \t\r\n]
  99. one_line_comment \/\/[^\n]*\n
  100. operand_start [(A-Za-z0-9_\"]
  101. /* Single-line string literals should reject vertical whitespace. */
  102. string_literal \"([^\\\"\n\v\f\r]|\\.)*\"
  103. %{
  104. // This macro is expanded immediately before each action specified below.
  105. //
  106. // Advances the current token position by yyleng columns without changing
  107. // the line number, and takes us out of the after-whitespace / after-operand
  108. // state.
  109. #define YY_USER_ACTION \
  110. context.current_token_position.columns(yyleng); \
  111. if (YY_START == AFTER_WHITESPACE || YY_START == AFTER_OPERAND) { \
  112. BEGIN(INITIAL); \
  113. }
  114. #define SIMPLE_TOKEN(name) \
  115. Carbon::Parser::make_##name(context.current_token_position);
  116. #define ARG_TOKEN(name, arg) \
  117. Carbon::Parser::make_##name(arg, context.current_token_position);
  118. %}
  119. %%
  120. %{
  121. // Code run each time yylex is called.
  122. // Begin with an empty token span starting where its previous end was.
  123. context.current_token_position.step();
  124. %}
  125. /* table-begin */
  126. {AMPERSAND} { return SIMPLE_TOKEN(AMPERSAND); }
  127. {AND} { return SIMPLE_TOKEN(AND); }
  128. {API} { return SIMPLE_TOKEN(API); }
  129. {ARROW} { return SIMPLE_TOKEN(ARROW); }
  130. {AS} { return SIMPLE_TOKEN(AS); }
  131. {AUTO} { return SIMPLE_TOKEN(AUTO); }
  132. {AWAIT} { return SIMPLE_TOKEN(AWAIT); }
  133. {BOOL} { return SIMPLE_TOKEN(BOOL); }
  134. {BREAK} { return SIMPLE_TOKEN(BREAK); }
  135. {CASE} { return SIMPLE_TOKEN(CASE); }
  136. {CHOICE} { return SIMPLE_TOKEN(CHOICE); }
  137. {CLASS} { return SIMPLE_TOKEN(CLASS); }
  138. {COLON_BANG} { return SIMPLE_TOKEN(COLON_BANG); }
  139. {COLON} { return SIMPLE_TOKEN(COLON); }
  140. {COMMA} { return SIMPLE_TOKEN(COMMA); }
  141. {CONTINUATION_TYPE} { return SIMPLE_TOKEN(CONTINUATION_TYPE); }
  142. {CONTINUATION} { return SIMPLE_TOKEN(CONTINUATION); }
  143. {CONTINUE} { return SIMPLE_TOKEN(CONTINUE); }
  144. {DEFAULT} { return SIMPLE_TOKEN(DEFAULT); }
  145. {DOUBLE_ARROW} { return SIMPLE_TOKEN(DOUBLE_ARROW); }
  146. {ELSE} { return SIMPLE_TOKEN(ELSE); }
  147. {EQUAL_EQUAL} { return SIMPLE_TOKEN(EQUAL_EQUAL); }
  148. {EQUAL} { return SIMPLE_TOKEN(EQUAL); }
  149. {EXTERNAL} { return SIMPLE_TOKEN(EXTERNAL); }
  150. {FALSE} { return SIMPLE_TOKEN(FALSE); }
  151. {FN_TYPE} { return SIMPLE_TOKEN(FN_TYPE); }
  152. {FN} { return SIMPLE_TOKEN(FN); }
  153. {IF} { return SIMPLE_TOKEN(IF); }
  154. {IMPL} { return SIMPLE_TOKEN(IMPL); }
  155. {IMPORT} { return SIMPLE_TOKEN(IMPORT); }
  156. {INTERFACE} { return SIMPLE_TOKEN(INTERFACE); }
  157. {LEFT_CURLY_BRACE} { return SIMPLE_TOKEN(LEFT_CURLY_BRACE); }
  158. {LEFT_PARENTHESIS} { return SIMPLE_TOKEN(LEFT_PARENTHESIS); }
  159. {LEFT_SQUARE_BRACKET} { return SIMPLE_TOKEN(LEFT_SQUARE_BRACKET); }
  160. {LET} { return SIMPLE_TOKEN(LET); }
  161. {LIBRARY} { return SIMPLE_TOKEN(LIBRARY); }
  162. {MATCH} { return SIMPLE_TOKEN(MATCH); }
  163. {MINUS} { return SIMPLE_TOKEN(MINUS); }
  164. {NOT} { return SIMPLE_TOKEN(NOT); }
  165. {OR} { return SIMPLE_TOKEN(OR); }
  166. {PACKAGE} { return SIMPLE_TOKEN(PACKAGE); }
  167. {PERIOD} { return SIMPLE_TOKEN(PERIOD); }
  168. {PLUS} { return SIMPLE_TOKEN(PLUS); }
  169. {RETURN} { return SIMPLE_TOKEN(RETURN); }
  170. {RUN} { return SIMPLE_TOKEN(RUN); }
  171. {SEMICOLON} { return SIMPLE_TOKEN(SEMICOLON); }
  172. {SLASH} { return SIMPLE_TOKEN(SLASH); }
  173. {STRING} { return SIMPLE_TOKEN(STRING); }
  174. {THEN} { return SIMPLE_TOKEN(THEN); }
  175. {TRUE} { return SIMPLE_TOKEN(TRUE); }
  176. {TYPE} { return SIMPLE_TOKEN(TYPE); }
  177. {UNDERSCORE} { return SIMPLE_TOKEN(UNDERSCORE); }
  178. {UNIMPL_EXAMPLE} { return SIMPLE_TOKEN(UNIMPL_EXAMPLE); }
  179. {VAR} { return SIMPLE_TOKEN(VAR); }
  180. {WHILE} { return SIMPLE_TOKEN(WHILE); }
  181. /* table-end */
  182. /* More modern Bisons provide make_EOF. */
  183. <<EOF>> { return SIMPLE_TOKEN(END_OF_FILE); }
  184. {RIGHT_PARENTHESIS} {
  185. BEGIN(AFTER_OPERAND);
  186. return SIMPLE_TOKEN(RIGHT_PARENTHESIS);
  187. }
  188. {RIGHT_CURLY_BRACE} {
  189. BEGIN(AFTER_OPERAND);
  190. return SIMPLE_TOKEN(RIGHT_CURLY_BRACE);
  191. }
  192. {RIGHT_SQUARE_BRACKET} {
  193. BEGIN(AFTER_OPERAND);
  194. return SIMPLE_TOKEN(RIGHT_SQUARE_BRACKET);
  195. }
  196. /*
  197. * For a `*` operator, we look at whitespace and local context to determine the
  198. * arity and fixity. There are two ways to write a binary operator:
  199. *
  200. * 1) Whitespace on both sides.
  201. * 2) Whitespace on neither side, and the previous token is considered to be
  202. * the end of an operand, and the next token is considered to be the start
  203. * of an operand.
  204. *
  205. * Otherwise, the operator is unary, but we also check for whitespace to help
  206. * the parser enforce the rule that whitespace is not permitted between the
  207. * operator and its operand, leading to three more cases:
  208. *
  209. * 3) Whitespace before (but implicitly not after, because that would give a
  210. * longer match and hit case 1): this can only be a prefix operator.
  211. * 4) Whitespace after and not before: this can only be a postfix operator.
  212. * 5) No whitespace on either side (otherwise the longest match would take us
  213. * to case 4): this is a unary operator and could be either prefix or
  214. * postfix.
  215. */
  216. /* `*` operator case 1: */
  217. <AFTER_WHITESPACE>"*"{whitespace}+ {
  218. BEGIN(AFTER_WHITESPACE);
  219. return SIMPLE_TOKEN(BINARY_STAR);
  220. }
  221. /* `*` operator case 2: */
  222. <AFTER_OPERAND>"*"/{operand_start} { return SIMPLE_TOKEN(BINARY_STAR); }
  223. /* `*` operator case 3: */
  224. <AFTER_WHITESPACE>"*" { return SIMPLE_TOKEN(PREFIX_STAR); }
  225. /* `*` operator case 4: */
  226. <INITIAL,AFTER_OPERAND>"*"{whitespace}+ {
  227. BEGIN(AFTER_WHITESPACE);
  228. return SIMPLE_TOKEN(POSTFIX_STAR);
  229. }
  230. /* `*` operator case 5: */
  231. <INITIAL,AFTER_OPERAND>"*" { return SIMPLE_TOKEN(UNARY_STAR); }
  232. {sized_type_literal} {
  233. BEGIN(AFTER_OPERAND);
  234. return ARG_TOKEN(sized_type_literal, yytext);
  235. }
  236. {intrinsic_identifier} {
  237. BEGIN(AFTER_OPERAND);
  238. return ARG_TOKEN(intrinsic_identifier, yytext);
  239. }
  240. {identifier} {
  241. BEGIN(AFTER_OPERAND);
  242. return ARG_TOKEN(identifier, yytext);
  243. }
  244. {integer_literal} {
  245. BEGIN(AFTER_OPERAND);
  246. int val = 0;
  247. if (!llvm::to_integer(yytext, val)) {
  248. FATAL_SYNTAX_ERROR(context) << "Invalid integer literal: " << yytext;
  249. }
  250. return ARG_TOKEN(integer_literal, val);
  251. }
  252. {string_literal} {
  253. llvm::StringRef str(yytext);
  254. CHECK(str.consume_front("\"") && str.consume_back("\""));
  255. std::optional<std::string> unescaped = Carbon::UnescapeStringLiteral(str);
  256. if (unescaped == std::nullopt) {
  257. FATAL_SYNTAX_ERROR(context) << "Invalid escaping in string: " << yytext;
  258. }
  259. return ARG_TOKEN(string_literal, *unescaped);
  260. }
  261. \"\"\" {
  262. // Block string literal.
  263. std::string s(yytext);
  264. // Scans for the closing """, checking for possible escape sequences
  265. // like \""".
  266. for (;;) {
  267. char c = ReadChar(yyscanner, context);
  268. s.push_back(c);
  269. if (c != '"' && c != '\\') {
  270. continue;
  271. }
  272. if (c == '\\') {
  273. // \" in \""" is not a terminator.
  274. s.push_back(ReadChar(yyscanner, context));
  275. continue;
  276. }
  277. c = ReadChar(yyscanner, context);
  278. s.push_back(c);
  279. if (c != '"') {
  280. continue;
  281. }
  282. c = ReadChar(yyscanner, context);
  283. s.push_back(c);
  284. if (c == '"') {
  285. break;
  286. }
  287. }
  288. llvm::Expected<std::string> block_string = Carbon::ParseBlockStringLiteral(s);
  289. if (!block_string) {
  290. FATAL_SYNTAX_ERROR(context)
  291. << "Invalid block string: " << toString(block_string.takeError());
  292. }
  293. return ARG_TOKEN(string_literal, *block_string);
  294. }
  295. {one_line_comment} {
  296. // Advance end by 1 line, resetting the column to zero.
  297. context.current_token_position.lines(1);
  298. // Make the span empty by setting start to end.
  299. context.current_token_position.step();
  300. }
  301. {horizontal_whitespace}+ {
  302. // Make the span empty by setting start to end.
  303. context.current_token_position.step();
  304. BEGIN(AFTER_WHITESPACE);
  305. }
  306. \n+ {
  307. // Advance end by yyleng lines, resetting the column to zero.
  308. context.current_token_position.lines(yyleng);
  309. // Make the span empty by setting start to end.
  310. context.current_token_position.step();
  311. BEGIN(AFTER_WHITESPACE);
  312. }
  313. . {
  314. FATAL_SYNTAX_ERROR(context)
  315. << "invalid character '\\x" << llvm::toHex(llvm::StringRef(yytext, 1))
  316. << "' in source file.";
  317. }
  318. %%
  319. char ReadChar(yyscan_t yyscanner, const Carbon::ParseAndLexContext& context) {
  320. const int c = yyinput(yyscanner);
  321. if (c == EOF) {
  322. FATAL_SYNTAX_ERROR(context) << "Unexpected end of file";
  323. }
  324. return c;
  325. }