lexer.lpp 15 KB

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