lexer.lpp 12 KB

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