lexer.lpp 15 KB

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