lexer.lpp 14 KB

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