lexer.lpp 14 KB

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