lexer.lpp 12 KB

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