lexer.lpp 14 KB

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