lexer.lpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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/error.h"
  10. #include "common/string_helpers.h"
  11. #include "executable_semantics/syntax/parse_and_lex_context.h"
  12. #include "executable_semantics/syntax/parser.h"
  13. #include "llvm/ADT/StringExtras.h"
  14. #include "llvm/Support/FormatVariadic.h"
  15. // Reads and returns a single character. Reports an error on EOF.
  16. auto ReadChar(yyscan_t yyscanner, Carbon::ParseAndLexContext& context) -> int;
  17. %}
  18. /* Turn off legacy bits we don't need. */
  19. %option noyywrap nounput nodefault
  20. %option reentrant
  21. /* Lexing a token immediately after consuming some whitespace. */
  22. %s AFTER_WHITESPACE
  23. /*
  24. * Lexing a token immediately after consuming an operand-ending token:
  25. * a closing bracket, identifier, or literal.
  26. */
  27. %s AFTER_OPERAND
  28. /* table-begin */
  29. AMPERSAND "&"
  30. AND "and"
  31. API "api"
  32. ARROW "->"
  33. AS "as"
  34. AUTO "auto"
  35. AWAIT "__await"
  36. BOOL "Bool"
  37. BREAK "break"
  38. CASE "case"
  39. CHOICE "choice"
  40. CLASS "class"
  41. COLON ":"
  42. COLON_BANG ":!"
  43. COMMA ","
  44. CONTINUATION "__continuation"
  45. CONTINUATION_TYPE "__Continuation"
  46. CONTINUE "continue"
  47. DEFAULT "default"
  48. DOUBLE_ARROW "=>"
  49. ELSE "else"
  50. EQUAL "="
  51. EQUAL_EQUAL "=="
  52. EXTERNAL "external"
  53. FALSE "false"
  54. FN "fn"
  55. FN_TYPE "__Fn"
  56. FORALL "forall"
  57. IF "if"
  58. IMPL "impl"
  59. IMPORT "import"
  60. INTERFACE "interface"
  61. LEFT_CURLY_BRACE "{"
  62. LEFT_PARENTHESIS "("
  63. LEFT_SQUARE_BRACKET "["
  64. LET "let"
  65. LIBRARY "library"
  66. MATCH "match"
  67. MINUS "-"
  68. NOT "not"
  69. OR "or"
  70. PACKAGE "package"
  71. PERIOD "."
  72. PLUS "+"
  73. RETURN "return"
  74. RIGHT_CURLY_BRACE "}"
  75. RIGHT_PARENTHESIS ")"
  76. RIGHT_SQUARE_BRACKET "]"
  77. RUN "__run"
  78. SEMICOLON ";"
  79. SLASH "/"
  80. STRING "String"
  81. THEN "then"
  82. TRUE "true"
  83. TYPE "Type"
  84. UNDERSCORE "_"
  85. UNIMPL_EXAMPLE "__unimplemented_example_infix"
  86. VAR "var"
  87. WHILE "while"
  88. /* table-end */
  89. /* This should be kept table-like, but isn't automatic due to spaces. */
  90. identifier [A-Za-z_][A-Za-z0-9_]*
  91. intrinsic_identifier __intrinsic_[A-Za-z0-9_]*
  92. sized_type_literal [iuf][1-9][0-9]*
  93. integer_literal [0-9]+
  94. horizontal_whitespace [ \t\r]
  95. whitespace [ \t\r\n]
  96. one_line_comment \/\/[^\n]*\n
  97. operand_start [(A-Za-z0-9_\"]
  98. /* Single-line string literals should reject vertical whitespace. */
  99. string_literal \"([^\\\"\n\v\f\r]|\\.)*\"
  100. %{
  101. // This macro is expanded immediately before each action specified below.
  102. //
  103. // Advances the current token position by yyleng columns without changing
  104. // the line number, and takes us out of the after-whitespace / after-operand
  105. // state.
  106. #define YY_USER_ACTION \
  107. context.current_token_position.columns(yyleng); \
  108. if (YY_START == AFTER_WHITESPACE || YY_START == AFTER_OPERAND) { \
  109. BEGIN(INITIAL); \
  110. }
  111. #define SIMPLE_TOKEN(name) \
  112. Carbon::Parser::make_##name(context.current_token_position);
  113. #define ARG_TOKEN(name, arg) \
  114. Carbon::Parser::make_##name(arg, context.current_token_position);
  115. %}
  116. %%
  117. %{
  118. // Code run each time yylex is called.
  119. // Begin with an empty token span starting where its previous end was.
  120. context.current_token_position.step();
  121. %}
  122. /* table-begin */
  123. {AMPERSAND} { return SIMPLE_TOKEN(AMPERSAND); }
  124. {AND} { return SIMPLE_TOKEN(AND); }
  125. {API} { return SIMPLE_TOKEN(API); }
  126. {ARROW} { return SIMPLE_TOKEN(ARROW); }
  127. {AS} { return SIMPLE_TOKEN(AS); }
  128. {AUTO} { return SIMPLE_TOKEN(AUTO); }
  129. {AWAIT} { return SIMPLE_TOKEN(AWAIT); }
  130. {BOOL} { return SIMPLE_TOKEN(BOOL); }
  131. {BREAK} { return SIMPLE_TOKEN(BREAK); }
  132. {CASE} { return SIMPLE_TOKEN(CASE); }
  133. {CHOICE} { return SIMPLE_TOKEN(CHOICE); }
  134. {CLASS} { return SIMPLE_TOKEN(CLASS); }
  135. {COLON_BANG} { return SIMPLE_TOKEN(COLON_BANG); }
  136. {COLON} { return SIMPLE_TOKEN(COLON); }
  137. {COMMA} { return SIMPLE_TOKEN(COMMA); }
  138. {CONTINUATION_TYPE} { return SIMPLE_TOKEN(CONTINUATION_TYPE); }
  139. {CONTINUATION} { return SIMPLE_TOKEN(CONTINUATION); }
  140. {CONTINUE} { return SIMPLE_TOKEN(CONTINUE); }
  141. {DEFAULT} { return SIMPLE_TOKEN(DEFAULT); }
  142. {DOUBLE_ARROW} { return SIMPLE_TOKEN(DOUBLE_ARROW); }
  143. {ELSE} { return SIMPLE_TOKEN(ELSE); }
  144. {EQUAL_EQUAL} { return SIMPLE_TOKEN(EQUAL_EQUAL); }
  145. {EQUAL} { return SIMPLE_TOKEN(EQUAL); }
  146. {EXTERNAL} { return SIMPLE_TOKEN(EXTERNAL); }
  147. {FALSE} { return SIMPLE_TOKEN(FALSE); }
  148. {FN_TYPE} { return SIMPLE_TOKEN(FN_TYPE); }
  149. {FN} { return SIMPLE_TOKEN(FN); }
  150. {FORALL} { return SIMPLE_TOKEN(FORALL); }
  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. {LET} { return SIMPLE_TOKEN(LET); }
  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. Carbon::ErrorOr<Carbon::IntrinsicExpression::Intrinsic> intrinsic =
  237. Carbon::IntrinsicExpression::FindIntrinsic(yytext, context.source_loc());
  238. if (intrinsic.ok()) {
  239. return ARG_TOKEN(intrinsic_identifier, *intrinsic);
  240. } else {
  241. return context.RecordSyntaxError(intrinsic.error().message());
  242. }
  243. }
  244. {identifier} {
  245. BEGIN(AFTER_OPERAND);
  246. return ARG_TOKEN(identifier, yytext);
  247. }
  248. {integer_literal} {
  249. BEGIN(AFTER_OPERAND);
  250. int val = 0;
  251. if (!llvm::to_integer(yytext, val)) {
  252. return context.RecordSyntaxError(
  253. llvm::formatv("Invalid integer literal: {0}", yytext));
  254. }
  255. return ARG_TOKEN(integer_literal, val);
  256. }
  257. {string_literal} {
  258. llvm::StringRef str(yytext);
  259. CHECK(str.consume_front("\"") && str.consume_back("\""));
  260. std::optional<std::string> unescaped = Carbon::UnescapeStringLiteral(str);
  261. if (unescaped == std::nullopt) {
  262. return context.RecordSyntaxError(
  263. llvm::formatv("Invalid escaping in string: {0}", yytext));
  264. }
  265. return ARG_TOKEN(string_literal, *unescaped);
  266. }
  267. \"\"\" {
  268. // Block string literal.
  269. std::string s(yytext);
  270. // Scans for the closing """, checking for possible escape sequences
  271. // like \""".
  272. for (;;) {
  273. int c = ReadChar(yyscanner, context);
  274. if (c <= 0) {
  275. return SIMPLE_TOKEN(END_OF_FILE);
  276. }
  277. s.push_back(c);
  278. if (c != '"' && c != '\\') {
  279. continue;
  280. }
  281. if (c == '\\') {
  282. // \" in \""" is not a terminator.
  283. c = ReadChar(yyscanner, context);
  284. if (c <= 0) {
  285. return SIMPLE_TOKEN(END_OF_FILE);
  286. }
  287. s.push_back(c);
  288. continue;
  289. }
  290. c = ReadChar(yyscanner, context);
  291. if (c <= 0) {
  292. return SIMPLE_TOKEN(END_OF_FILE);
  293. }
  294. s.push_back(c);
  295. if (c != '"') {
  296. continue;
  297. }
  298. c = ReadChar(yyscanner, context);
  299. if (c <= 0) {
  300. return SIMPLE_TOKEN(END_OF_FILE);
  301. }
  302. s.push_back(c);
  303. if (c == '"') {
  304. break;
  305. }
  306. }
  307. Carbon::ErrorOr<std::string> block_string =
  308. Carbon::ParseBlockStringLiteral(s);
  309. if (!block_string.ok()) {
  310. return context.RecordSyntaxError(llvm::formatv(
  311. "Invalid block string: {0}", block_string.error().message()));
  312. }
  313. return ARG_TOKEN(string_literal, *block_string);
  314. }
  315. {one_line_comment} {
  316. // Advance end by 1 line, resetting the column to zero.
  317. context.current_token_position.lines(1);
  318. // Make the span empty by setting start to end.
  319. context.current_token_position.step();
  320. }
  321. {horizontal_whitespace}+ {
  322. // Make the span empty by setting start to end.
  323. context.current_token_position.step();
  324. BEGIN(AFTER_WHITESPACE);
  325. }
  326. \n+ {
  327. // Advance end by yyleng lines, resetting the column to zero.
  328. context.current_token_position.lines(yyleng);
  329. // Make the span empty by setting start to end.
  330. context.current_token_position.step();
  331. BEGIN(AFTER_WHITESPACE);
  332. }
  333. . {
  334. return context.RecordSyntaxError(
  335. llvm::formatv("invalid character '\\x{0}' in source file.",
  336. llvm::toHex(llvm::StringRef(yytext, 1))));
  337. }
  338. %%
  339. auto ReadChar(yyscan_t yyscanner, Carbon::ParseAndLexContext& context) -> int {
  340. const int c = yyinput(yyscanner);
  341. if (c <= 0) {
  342. context.RecordSyntaxError("Unexpected end of file");
  343. }
  344. return c;
  345. }