lexer.lpp 12 KB

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