lexer.lpp 18 KB

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