lexer.lpp 16 KB

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