lexer.lpp 16 KB

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