lexer.lpp 16 KB

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