lexer.lpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 "explorer/syntax/lex_helper.h"
  11. #include "explorer/syntax/lex_scan_helper.h"
  12. #include "explorer/syntax/parse_and_lex_context.h"
  13. #include "explorer/syntax/parser.h"
  14. #include "llvm/ADT/StringExtras.h"
  15. #include "llvm/Support/FormatVariadic.h"
  16. %}
  17. /* Turn off legacy bits we don't need. */
  18. %option noyywrap nounput nodefault
  19. %option reentrant
  20. /* Lexing a token immediately after consuming some whitespace. */
  21. %s AFTER_WHITESPACE
  22. /*
  23. * Lexing a token immediately after consuming an operand-ending token:
  24. * a closing bracket, identifier, or literal.
  25. */
  26. %s AFTER_OPERAND
  27. /* table-begin */
  28. ABSTRACT "abstract"
  29. ADDR "addr"
  30. ALIAS "alias"
  31. AMPERSAND "&"
  32. AMPERSAND_EQUAL "&="
  33. AND "and"
  34. API "api"
  35. ARROW "->"
  36. AS "as"
  37. AUTO "auto"
  38. AWAIT "__await"
  39. BASE "base"
  40. BOOL "bool"
  41. BREAK "break"
  42. CARET "^"
  43. CARET_EQUAL "^="
  44. CASE "case"
  45. CHOICE "choice"
  46. CLASS "class"
  47. COLON ":"
  48. COLON_BANG ":!"
  49. COMMA ","
  50. CONSTRAINT "constraint"
  51. CONTINUATION "__continuation"
  52. CONTINUATION_TYPE "__Continuation"
  53. CONTINUE "continue"
  54. DEFAULT "default"
  55. DESTRUCTOR "destructor"
  56. DOUBLE_ARROW "=>"
  57. ELSE "else"
  58. EQUAL "="
  59. EQUAL_EQUAL "=="
  60. EXTENDS "extends"
  61. EXTERNAL "external"
  62. FALSE "false"
  63. FN "fn"
  64. FN_TYPE "__Fn"
  65. FOR "for"
  66. FORALL "forall"
  67. GREATER ">"
  68. GREATER_EQUAL ">="
  69. GREATER_GREATER ">>"
  70. GREATER_GREATER_EQUAL ">>="
  71. IF "if"
  72. IMPL "impl"
  73. IMPORT "import"
  74. IN "in"
  75. INTERFACE "interface"
  76. IS "is"
  77. LEFT_CURLY_BRACE "{"
  78. LEFT_PARENTHESIS "("
  79. LEFT_SQUARE_BRACKET "["
  80. LESS "<"
  81. LESS_EQUAL "<="
  82. LESS_LESS "<<"
  83. LESS_LESS_EQUAL "<<="
  84. LET "let"
  85. LIBRARY "library"
  86. MATCH "match"
  87. MATCH_FIRST "__match_first"
  88. MINUS "-"
  89. MINUS_EQUAL "-="
  90. MINUS_MINUS "--"
  91. MIX "__mix"
  92. MIXIN "__mixin"
  93. NAMESPACE "namespace"
  94. NOT "not"
  95. NOT_EQUAL "!="
  96. OR "or"
  97. PACKAGE "package"
  98. PERCENT "%"
  99. PERCENT_EQUAL "%="
  100. PERIOD "."
  101. PIPE "|"
  102. PIPE_EQUAL "|="
  103. PLUS "+"
  104. PLUS_EQUAL "+="
  105. PLUS_PLUS "++"
  106. RETURN "return"
  107. RETURNED "returned"
  108. RIGHT_CURLY_BRACE "}"
  109. RIGHT_PARENTHESIS ")"
  110. RIGHT_SQUARE_BRACKET "]"
  111. RUN "__run"
  112. SELF "Self"
  113. SEMICOLON ";"
  114. SLASH "/"
  115. SLASH_EQUAL "/="
  116. STAR_EQUAL "*="
  117. STRING "String"
  118. THEN "then"
  119. TRUE "true"
  120. TYPE "type"
  121. UNDERSCORE "_"
  122. UNIMPL_EXAMPLE "__unimplemented_example_infix"
  123. VAR "var"
  124. VIRTUAL "virtual"
  125. WHERE "where"
  126. WHILE "while"
  127. /* table-end */
  128. /* This should be kept table-like, but isn't automatic due to spaces. */
  129. identifier [A-Za-z_][A-Za-z0-9_]*
  130. /* TODO: Remove Print special casing once we have variadics or overloads. */
  131. intrinsic_identifier (Print|__intrinsic_[A-Za-z0-9_]*)
  132. sized_type_literal [iuf][1-9][0-9]*
  133. integer_literal [0-9]+
  134. horizontal_whitespace [ \t\r]
  135. whitespace [ \t\r\n]
  136. one_line_comment \/\/[^\n]*\n
  137. operand_start [(A-Za-z0-9_\"]
  138. %%
  139. %{
  140. // Code run each time yylex is called.
  141. // Begin with an empty token span starting where its previous end was.
  142. context.current_token_position.step();
  143. %}
  144. /* table-begin */
  145. {ABSTRACT} { return CARBON_SIMPLE_TOKEN(ABSTRACT); }
  146. {ADDR} { return CARBON_SIMPLE_TOKEN(ADDR); }
  147. {ALIAS} { return CARBON_SIMPLE_TOKEN(ALIAS); }
  148. {AMPERSAND_EQUAL} { return CARBON_SIMPLE_TOKEN(AMPERSAND_EQUAL); }
  149. {AMPERSAND} { return CARBON_SIMPLE_TOKEN(AMPERSAND); }
  150. {AND} { return CARBON_SIMPLE_TOKEN(AND); }
  151. {API} { return CARBON_SIMPLE_TOKEN(API); }
  152. {ARROW} { return CARBON_SIMPLE_TOKEN(ARROW); }
  153. {AS} { return CARBON_SIMPLE_TOKEN(AS); }
  154. {AUTO} { return CARBON_SIMPLE_TOKEN(AUTO); }
  155. {AWAIT} { return CARBON_SIMPLE_TOKEN(AWAIT); }
  156. {BASE} { return CARBON_SIMPLE_TOKEN(BASE); }
  157. {BOOL} { return CARBON_SIMPLE_TOKEN(BOOL); }
  158. {BREAK} { return CARBON_SIMPLE_TOKEN(BREAK); }
  159. {CARET_EQUAL} { return CARBON_SIMPLE_TOKEN(CARET_EQUAL); }
  160. {CARET} { return CARBON_SIMPLE_TOKEN(CARET); }
  161. {CASE} { return CARBON_SIMPLE_TOKEN(CASE); }
  162. {CHOICE} { return CARBON_SIMPLE_TOKEN(CHOICE); }
  163. {CLASS} { return CARBON_SIMPLE_TOKEN(CLASS); }
  164. {COLON_BANG} { return CARBON_SIMPLE_TOKEN(COLON_BANG); }
  165. {COLON} { return CARBON_SIMPLE_TOKEN(COLON); }
  166. {COMMA} { return CARBON_SIMPLE_TOKEN(COMMA); }
  167. {CONSTRAINT} { return CARBON_SIMPLE_TOKEN(CONSTRAINT); }
  168. {CONTINUATION_TYPE} { return CARBON_SIMPLE_TOKEN(CONTINUATION_TYPE); }
  169. {CONTINUATION} { return CARBON_SIMPLE_TOKEN(CONTINUATION); }
  170. {CONTINUE} { return CARBON_SIMPLE_TOKEN(CONTINUE); }
  171. {DEFAULT} { return CARBON_SIMPLE_TOKEN(DEFAULT); }
  172. {DESTRUCTOR} { return CARBON_SIMPLE_TOKEN(DESTRUCTOR); }
  173. {DOUBLE_ARROW} { return CARBON_SIMPLE_TOKEN(DOUBLE_ARROW); }
  174. {ELSE} { return CARBON_SIMPLE_TOKEN(ELSE); }
  175. {EQUAL_EQUAL} { return CARBON_SIMPLE_TOKEN(EQUAL_EQUAL); }
  176. {EQUAL} { return CARBON_SIMPLE_TOKEN(EQUAL); }
  177. {EXTENDS} { return CARBON_SIMPLE_TOKEN(EXTENDS); }
  178. {EXTERNAL} { return CARBON_SIMPLE_TOKEN(EXTERNAL); }
  179. {FALSE} { return CARBON_SIMPLE_TOKEN(FALSE); }
  180. {FN_TYPE} { return CARBON_SIMPLE_TOKEN(FN_TYPE); }
  181. {FN} { return CARBON_SIMPLE_TOKEN(FN); }
  182. {FORALL} { return CARBON_SIMPLE_TOKEN(FORALL); }
  183. {FOR} { return CARBON_SIMPLE_TOKEN(FOR); }
  184. {GREATER_EQUAL} { return CARBON_SIMPLE_TOKEN(GREATER_EQUAL); }
  185. {GREATER_GREATER_EQUAL} { return CARBON_SIMPLE_TOKEN(GREATER_GREATER_EQUAL); }
  186. {GREATER_GREATER} { return CARBON_SIMPLE_TOKEN(GREATER_GREATER); }
  187. {GREATER} { return CARBON_SIMPLE_TOKEN(GREATER); }
  188. {IF} { return CARBON_SIMPLE_TOKEN(IF); }
  189. {IMPL} { return CARBON_SIMPLE_TOKEN(IMPL); }
  190. {IMPORT} { return CARBON_SIMPLE_TOKEN(IMPORT); }
  191. {INTERFACE} { return CARBON_SIMPLE_TOKEN(INTERFACE); }
  192. {IN} { return CARBON_SIMPLE_TOKEN(IN); }
  193. {IS} { return CARBON_SIMPLE_TOKEN(IS); }
  194. {LEFT_CURLY_BRACE} { return CARBON_SIMPLE_TOKEN(LEFT_CURLY_BRACE); }
  195. {LEFT_PARENTHESIS} { return CARBON_SIMPLE_TOKEN(LEFT_PARENTHESIS); }
  196. {LEFT_SQUARE_BRACKET} { return CARBON_SIMPLE_TOKEN(LEFT_SQUARE_BRACKET); }
  197. {LESS_EQUAL} { return CARBON_SIMPLE_TOKEN(LESS_EQUAL); }
  198. {LESS_LESS_EQUAL} { return CARBON_SIMPLE_TOKEN(LESS_LESS_EQUAL); }
  199. {LESS_LESS} { return CARBON_SIMPLE_TOKEN(LESS_LESS); }
  200. {LESS} { return CARBON_SIMPLE_TOKEN(LESS); }
  201. {LET} { return CARBON_SIMPLE_TOKEN(LET); }
  202. {LIBRARY} { return CARBON_SIMPLE_TOKEN(LIBRARY); }
  203. {MATCH_FIRST} { return CARBON_SIMPLE_TOKEN(MATCH_FIRST); }
  204. {MATCH} { return CARBON_SIMPLE_TOKEN(MATCH); }
  205. {MINUS_EQUAL} { return CARBON_SIMPLE_TOKEN(MINUS_EQUAL); }
  206. {MINUS_MINUS} { return CARBON_SIMPLE_TOKEN(MINUS_MINUS); }
  207. {MINUS} { return CARBON_SIMPLE_TOKEN(MINUS); }
  208. {MIXIN} { return CARBON_SIMPLE_TOKEN(MIXIN); }
  209. {MIX} { return CARBON_SIMPLE_TOKEN(MIX); }
  210. {NAMESPACE} { return CARBON_SIMPLE_TOKEN(NAMESPACE); }
  211. {NOT_EQUAL} { return CARBON_SIMPLE_TOKEN(NOT_EQUAL); }
  212. {NOT} { return CARBON_SIMPLE_TOKEN(NOT); }
  213. {OR} { return CARBON_SIMPLE_TOKEN(OR); }
  214. {PACKAGE} { return CARBON_SIMPLE_TOKEN(PACKAGE); }
  215. {PERCENT_EQUAL} { return CARBON_SIMPLE_TOKEN(PERCENT_EQUAL); }
  216. {PERCENT} { return CARBON_SIMPLE_TOKEN(PERCENT); }
  217. {PERIOD} { return CARBON_SIMPLE_TOKEN(PERIOD); }
  218. {PIPE_EQUAL} { return CARBON_SIMPLE_TOKEN(PIPE_EQUAL); }
  219. {PIPE} { return CARBON_SIMPLE_TOKEN(PIPE); }
  220. {PLUS_EQUAL} { return CARBON_SIMPLE_TOKEN(PLUS_EQUAL); }
  221. {PLUS_PLUS} { return CARBON_SIMPLE_TOKEN(PLUS_PLUS); }
  222. {PLUS} { return CARBON_SIMPLE_TOKEN(PLUS); }
  223. {RETURNED} { return CARBON_SIMPLE_TOKEN(RETURNED); }
  224. {RETURN} { return CARBON_SIMPLE_TOKEN(RETURN); }
  225. {RUN} { return CARBON_SIMPLE_TOKEN(RUN); }
  226. {SELF} { return CARBON_SIMPLE_TOKEN(SELF); }
  227. {SEMICOLON} { return CARBON_SIMPLE_TOKEN(SEMICOLON); }
  228. {SLASH_EQUAL} { return CARBON_SIMPLE_TOKEN(SLASH_EQUAL); }
  229. {SLASH} { return CARBON_SIMPLE_TOKEN(SLASH); }
  230. {STAR_EQUAL} { return CARBON_SIMPLE_TOKEN(STAR_EQUAL); }
  231. {STRING} { return CARBON_SIMPLE_TOKEN(STRING); }
  232. {THEN} { return CARBON_SIMPLE_TOKEN(THEN); }
  233. {TRUE} { return CARBON_SIMPLE_TOKEN(TRUE); }
  234. {TYPE} { return CARBON_SIMPLE_TOKEN(TYPE); }
  235. {UNDERSCORE} { return CARBON_SIMPLE_TOKEN(UNDERSCORE); }
  236. {UNIMPL_EXAMPLE} { return CARBON_SIMPLE_TOKEN(UNIMPL_EXAMPLE); }
  237. {VAR} { return CARBON_SIMPLE_TOKEN(VAR); }
  238. {VIRTUAL} { return CARBON_SIMPLE_TOKEN(VIRTUAL); }
  239. {WHERE} { return CARBON_SIMPLE_TOKEN(WHERE); }
  240. {WHILE} { return CARBON_SIMPLE_TOKEN(WHILE); }
  241. /* table-end */
  242. /* More modern Bisons provide make_EOF. */
  243. <<EOF>> { return CARBON_SIMPLE_TOKEN(END_OF_FILE); }
  244. {RIGHT_PARENTHESIS} {
  245. BEGIN(AFTER_OPERAND);
  246. return CARBON_SIMPLE_TOKEN(RIGHT_PARENTHESIS);
  247. }
  248. {RIGHT_CURLY_BRACE} {
  249. BEGIN(AFTER_OPERAND);
  250. return CARBON_SIMPLE_TOKEN(RIGHT_CURLY_BRACE);
  251. }
  252. {RIGHT_SQUARE_BRACKET} {
  253. BEGIN(AFTER_OPERAND);
  254. return CARBON_SIMPLE_TOKEN(RIGHT_SQUARE_BRACKET);
  255. }
  256. /*
  257. * For a `*` operator, we look at whitespace and local context to determine the
  258. * arity and fixity. There are two ways to write a binary operator:
  259. *
  260. * 1) Whitespace on both sides.
  261. * 2) Whitespace on neither side, and the previous token is considered to be
  262. * the end of an operand, and the next token is considered to be the start
  263. * of an operand.
  264. *
  265. * Otherwise, the operator is unary, but we also check for whitespace to help
  266. * the parser enforce the rule that whitespace is not permitted between the
  267. * operator and its operand, leading to three more cases:
  268. *
  269. * 3) Whitespace before (but implicitly not after, because that would give a
  270. * longer match and hit case 1): this can only be a prefix operator.
  271. * 4) Whitespace after and not before: this can only be a postfix operator.
  272. * 5) No whitespace on either side (otherwise the longest match would take us
  273. * to case 4): this is a unary operator and could be either prefix or
  274. * postfix.
  275. */
  276. /* `*` operator case 1: */
  277. <AFTER_WHITESPACE>"*"{whitespace}+ {
  278. BEGIN(AFTER_WHITESPACE);
  279. return CARBON_SIMPLE_TOKEN(BINARY_STAR);
  280. }
  281. /* `*` operator case 2: */
  282. <AFTER_OPERAND>"*"/{operand_start} { return CARBON_SIMPLE_TOKEN(BINARY_STAR); }
  283. /* `*` operator case 3: */
  284. <AFTER_WHITESPACE>"*" { return CARBON_SIMPLE_TOKEN(PREFIX_STAR); }
  285. /* `*` operator case 4: */
  286. <INITIAL,AFTER_OPERAND>"*"{whitespace}+ {
  287. BEGIN(AFTER_WHITESPACE);
  288. return CARBON_SIMPLE_TOKEN(POSTFIX_STAR);
  289. }
  290. /* `*` operator case 5: */
  291. <INITIAL,AFTER_OPERAND>"*" { return CARBON_SIMPLE_TOKEN(UNARY_STAR); }
  292. {sized_type_literal} {
  293. BEGIN(AFTER_OPERAND);
  294. return CARBON_ARG_TOKEN(sized_type_literal, yytext);
  295. }
  296. {intrinsic_identifier} {
  297. BEGIN(AFTER_OPERAND);
  298. Carbon::ErrorOr<Carbon::IntrinsicExpression::Intrinsic> intrinsic =
  299. Carbon::IntrinsicExpression::FindIntrinsic(yytext, context.source_loc());
  300. if (intrinsic.ok()) {
  301. return CARBON_ARG_TOKEN(intrinsic_identifier, *intrinsic);
  302. } else {
  303. return context.RecordSyntaxError(std::move(intrinsic).error());
  304. }
  305. }
  306. {identifier} {
  307. BEGIN(AFTER_OPERAND);
  308. return CARBON_ARG_TOKEN(identifier, yytext);
  309. }
  310. {integer_literal} {
  311. BEGIN(AFTER_OPERAND);
  312. int val = 0;
  313. if (!llvm::to_integer(yytext, val)) {
  314. return context.RecordSyntaxError(
  315. llvm::formatv("Invalid integer literal: {0}", yytext));
  316. }
  317. return CARBON_ARG_TOKEN(integer_literal, val);
  318. }
  319. #*\" {
  320. // Found a double quote character.
  321. Carbon::StringLexHelper str_lex_helper(yytext, yyscanner, context);
  322. const std::string& s = str_lex_helper.str();
  323. const int hashtag_num = s.find_first_of('"');
  324. if (!str_lex_helper.Advance()) {
  325. return CARBON_SIMPLE_TOKEN(END_OF_FILE);
  326. }
  327. while (!str_lex_helper.is_eof()) {
  328. switch (str_lex_helper.last_char()) {
  329. case '\n': // Fall through.
  330. case '\v': // Fall through.
  331. case '\f': // Fall through.
  332. case '\r':
  333. return context.RecordSyntaxError(
  334. llvm::formatv("missing closing quote in single-line string: {0}",
  335. str_lex_helper.str()));
  336. break;
  337. case '"':
  338. if (Carbon::ReadHashTags(str_lex_helper, hashtag_num)) {
  339. // Reach closing quotes, break out of the loop.
  340. return Carbon::ProcessSingleLineString(str_lex_helper.str(), context,
  341. hashtag_num);
  342. }
  343. break;
  344. case '\\':
  345. if (Carbon::ReadHashTags(str_lex_helper, hashtag_num)) {
  346. // Read the escaped char.
  347. if (!str_lex_helper.Advance()) {
  348. continue;
  349. }
  350. // Read the next char.
  351. str_lex_helper.Advance();
  352. }
  353. break;
  354. default:
  355. str_lex_helper.Advance();
  356. }
  357. }
  358. return CARBON_SIMPLE_TOKEN(END_OF_FILE);
  359. }
  360. #*\'\'\' {
  361. // Multi-line string literal.
  362. Carbon::StringLexHelper str_lex_helper(yytext, yyscanner, context);
  363. const std::string& s = str_lex_helper.str();
  364. const int hashtag_num = s.find_first_of('\'');
  365. if (!str_lex_helper.Advance()) {
  366. return CARBON_SIMPLE_TOKEN(END_OF_FILE);
  367. }
  368. while (!str_lex_helper.is_eof()) {
  369. switch (str_lex_helper.last_char()) {
  370. case '\'':
  371. // Check for 2 more '\''s on block string.
  372. if (!(str_lex_helper.Advance() && str_lex_helper.last_char() == '\'')) {
  373. continue;
  374. }
  375. if (!(str_lex_helper.Advance() && str_lex_helper.last_char() == '\'')) {
  376. continue;
  377. }
  378. // Now we are at the last ' of '''.
  379. if (Carbon::ReadHashTags(str_lex_helper, hashtag_num)) {
  380. // Reach closing quotes, break out of the loop.
  381. return Carbon::ProcessMultiLineString(str_lex_helper.str(), context,
  382. hashtag_num);
  383. }
  384. break;
  385. case '\\':
  386. if (Carbon::ReadHashTags(str_lex_helper, hashtag_num)) {
  387. // Read the escaped char.
  388. if (!str_lex_helper.Advance()) {
  389. continue;
  390. }
  391. // Read the next char.
  392. str_lex_helper.Advance();
  393. }
  394. break;
  395. default:
  396. str_lex_helper.Advance();
  397. }
  398. }
  399. return CARBON_SIMPLE_TOKEN(END_OF_FILE);
  400. }
  401. {one_line_comment} {
  402. // Advance end by 1 line, resetting the column to zero.
  403. context.current_token_position.lines(1);
  404. // Make the span empty by setting start to end.
  405. context.current_token_position.step();
  406. }
  407. {horizontal_whitespace}+ {
  408. // Make the span empty by setting start to end.
  409. context.current_token_position.step();
  410. BEGIN(AFTER_WHITESPACE);
  411. }
  412. \n+ {
  413. // Advance end by yyleng lines, resetting the column to zero.
  414. context.current_token_position.lines(yyleng);
  415. // Make the span empty by setting start to end.
  416. context.current_token_position.step();
  417. BEGIN(AFTER_WHITESPACE);
  418. }
  419. . {
  420. return context.RecordSyntaxError(
  421. llvm::formatv("invalid character '\\x{0}' in source file.",
  422. llvm::toHex(llvm::StringRef(yytext, 1))));
  423. }
  424. %%
  425. auto YyinputWrapper(yyscan_t yyscanner) -> int { return yyinput(yyscanner); }