lexer.lpp 12 KB

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