lexer.lpp 16 KB

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