parser.ypp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. // -----------------------------------------------------------------------------
  5. // Bison Configuration
  6. // -----------------------------------------------------------------------------
  7. %require "3.2"
  8. %language "c++"
  9. // We don't need a separate header for Bison locations.
  10. %define api.location.file none
  11. // Use a type-safe C++ variant for semantic values
  12. %define api.value.type variant
  13. // Have Bison generate the functions ‘make_TEXT’ and ‘make_NUMBER’, but also
  14. // ‘make_YYEOF’, for the end of input.
  15. %define api.token.constructor
  16. // Generate the parser as `::Carbon::Parser`.
  17. %define api.namespace { Carbon }
  18. %define api.parser.class { Parser }
  19. // Make parse error messages more detailed
  20. %define parse.error verbose
  21. // Enable support for parser debugging
  22. %define parse.trace true
  23. // Generate location structs.
  24. %locations
  25. // Parameters to the parser and lexer.
  26. //
  27. // Parameters to the parser are stored therein as protected data members, and
  28. // thus available to its methods.
  29. // "inout" parameters passed to both the parser and the lexer.
  30. %param {Nonnull<Arena*> arena}
  31. %param {yyscan_t yyscanner}
  32. %param {ParseAndLexContext& context}
  33. // "out" parameter passed to the parser, where the AST is written.
  34. %parse-param {std::optional<AST>* ast}
  35. // No shift-reduce conflicts are expected.
  36. %expect 0
  37. // -----------------------------------------------------------------------------
  38. %code top {
  39. #include <algorithm>
  40. #include <cstdarg>
  41. #include <cstdio>
  42. #include <cstdlib>
  43. #include <vector>
  44. #include "common/check.h"
  45. #include "executable_semantics/syntax/parse_and_lex_context.h"
  46. #include "llvm/ADT/StringExtras.h"
  47. } // %code top
  48. %code requires {
  49. #include <optional>
  50. #include "executable_semantics/ast/ast.h"
  51. #include "executable_semantics/ast/declaration.h"
  52. #include "executable_semantics/ast/expression.h"
  53. #include "executable_semantics/ast/paren_contents.h"
  54. #include "executable_semantics/ast/pattern.h"
  55. #include "executable_semantics/common/arena.h"
  56. #include "executable_semantics/common/nonnull.h"
  57. #include "executable_semantics/syntax/bison_wrap.h"
  58. namespace Carbon {
  59. class ParseAndLexContext;
  60. } // namespace Carbon
  61. typedef void* yyscan_t;
  62. } // %code requires
  63. %code {
  64. void Carbon::Parser::error(const location_type&, const std::string& message) {
  65. context.PrintDiagnostic(message);
  66. }
  67. } // %code
  68. %token <int> integer_literal
  69. %token <std::string> identifier
  70. %token <std::string> sized_type_literal
  71. %token <std::string> string_literal
  72. %type <std::string> designator
  73. %type <std::pair<LibraryName, bool>> package_directive
  74. %type <LibraryName> import_directive
  75. %type <std::vector<LibraryName>> import_directives
  76. %type <std::string> optional_library_path
  77. %type <bool> api_or_impl
  78. %type <Nonnull<Declaration*>> declaration
  79. %type <Nonnull<FunctionDeclaration*>> function_declaration
  80. %type <std::vector<Nonnull<Declaration*>>> declaration_list
  81. %type <Nonnull<Statement*>> statement
  82. %type <Nonnull<If*>> if_statement
  83. %type <std::optional<Nonnull<Block*>>> optional_else
  84. %type <std::pair<Nonnull<Expression*>, bool>> return_expression
  85. %type <Nonnull<Block*>> nonempty_block
  86. %type <Nonnull<Block*>> block
  87. %type <std::vector<Nonnull<Statement*>>> statement_list
  88. %type <Nonnull<Expression*>> expression
  89. %type <Nonnull<GenericBinding*>> generic_binding
  90. %type <std::vector<Nonnull<GenericBinding*>>> deduced_params
  91. %type <std::vector<Nonnull<GenericBinding*>>> deduced_param_list
  92. %type <Nonnull<Pattern*>> pattern
  93. %type <Nonnull<Pattern*>> non_expression_pattern
  94. %type <BisonWrap<ReturnTerm>> return_term
  95. %type <Nonnull<Expression*>> paren_expression
  96. %type <Nonnull<StructLiteral*>> struct_literal
  97. %type <std::vector<FieldInitializer>> struct_literal_contents
  98. %type <Nonnull<StructTypeLiteral*>> struct_type_literal
  99. %type <std::vector<FieldInitializer>> struct_type_literal_contents
  100. %type <Nonnull<TupleLiteral*>> tuple
  101. %type <std::optional<std::string>> binding_lhs
  102. %type <Nonnull<BindingPattern*>> variable_declaration
  103. %type <Nonnull<Member*>> member
  104. %type <std::vector<Nonnull<Member*>>> member_list
  105. %type <ParenContents<Expression>> paren_expression_base
  106. %type <ParenContents<Expression>> paren_expression_contents
  107. %type <Nonnull<Pattern*>> paren_pattern
  108. %type <Nonnull<TuplePattern*>> tuple_pattern
  109. %type <Nonnull<TuplePattern*>> maybe_empty_tuple_pattern
  110. %type <ParenContents<Pattern>> paren_pattern_base
  111. %type <ParenContents<Pattern>> paren_pattern_contents
  112. %type <Nonnull<AlternativeSignature*>> alternative
  113. %type <std::vector<Nonnull<AlternativeSignature*>>> alternative_list
  114. %type <std::vector<Nonnull<AlternativeSignature*>>> alternative_list_contents
  115. %type <BisonWrap<Match::Clause>> clause
  116. %type <std::vector<Match::Clause>> clause_list
  117. %token
  118. // Most tokens have their spelling defined in lexer.lpp.
  119. // table-begin
  120. AND
  121. API
  122. ARROW
  123. AUTO
  124. AWAIT
  125. BOOL
  126. BREAK
  127. CASE
  128. CHOICE
  129. CLASS
  130. COLON
  131. COLON_BANG
  132. COMMA
  133. CONTINUATION
  134. CONTINUATION_TYPE
  135. CONTINUE
  136. DEFAULT
  137. DOUBLE_ARROW
  138. ELSE
  139. EQUAL
  140. EQUAL_EQUAL
  141. FALSE
  142. FN
  143. FN_TYPE
  144. IF
  145. IMPL
  146. IMPORT
  147. LEFT_CURLY_BRACE
  148. LEFT_PARENTHESIS
  149. LEFT_SQUARE_BRACKET
  150. LIBRARY
  151. MATCH
  152. MINUS
  153. NOT
  154. OR
  155. PACKAGE
  156. PERIOD
  157. PLUS
  158. RETURN
  159. RIGHT_CURLY_BRACE
  160. RIGHT_PARENTHESIS
  161. RIGHT_SQUARE_BRACKET
  162. RUN
  163. SEMICOLON
  164. SLASH
  165. STRING
  166. TRUE
  167. TYPE
  168. UNDERSCORE
  169. VAR
  170. WHILE
  171. // table-end
  172. // Used to track EOF.
  173. END_OF_FILE 0
  174. // Only used for precedence.
  175. FNARROW "-> in return type"
  176. // The lexer determines the arity and fixity of each `*` based on whitespace
  177. // and adjacent tokens. UNARY_STAR indicates that the operator is unary but
  178. // could be either prefix or postfix.
  179. UNARY_STAR "unary *"
  180. PREFIX_STAR "prefix *"
  181. POSTFIX_STAR "postfix *"
  182. BINARY_STAR "binary *"
  183. ;
  184. %precedence FNARROW
  185. %precedence LEFT_CURLY_BRACE RIGHT_CURLY_BRACE
  186. %precedence COLON_BANG COLON COMMA DOUBLE_ARROW
  187. %left OR AND
  188. %nonassoc EQUAL_EQUAL
  189. %left PLUS MINUS
  190. %left BINARY_STAR
  191. %precedence NOT UNARY_MINUS PREFIX_STAR
  192. // We need to give the `UNARY_STAR` token a precedence, rather than overriding
  193. // the precedence of the `expression UNARY_STAR` rule below, because bison
  194. // compares the precedence of the final token (for a shift) to the precedence
  195. // of the other rule (for a reduce) when attempting to resolve a shift-reduce
  196. // conflict. See https://stackoverflow.com/a/26188429/1041090. When UNARY_STAR
  197. // is the final token of a rule, it must be a postfix usage, so we give it the
  198. // same precedence as POSTFIX_STAR.
  199. %precedence POSTFIX_STAR UNARY_STAR
  200. %left PERIOD ARROW
  201. %precedence
  202. LEFT_PARENTHESIS
  203. RIGHT_PARENTHESIS
  204. LEFT_SQUARE_BRACKET
  205. RIGHT_SQUARE_BRACKET
  206. ;
  207. %start input
  208. %%
  209. input: package_directive import_directives declaration_list
  210. {
  211. *ast = AST({.package = $1.first,
  212. .is_api = $1.second,
  213. .imports = std::move($2),
  214. .declarations = std::move($3)});
  215. }
  216. ;
  217. package_directive:
  218. PACKAGE identifier optional_library_path api_or_impl SEMICOLON
  219. { $$ = {LibraryName({.package = $2, .path = $3}), $4}; }
  220. ;
  221. import_directive:
  222. IMPORT identifier optional_library_path SEMICOLON
  223. { $$ = LibraryName({.package = $2, .path = $3}); }
  224. ;
  225. import_directives:
  226. // Empty
  227. { $$ = std::vector<LibraryName>(); }
  228. | import_directives import_directive
  229. {
  230. $$ = std::move($1);
  231. $$.push_back($2);
  232. }
  233. ;
  234. optional_library_path:
  235. // Empty
  236. { $$ = ""; }
  237. | LIBRARY string_literal
  238. { $$ = $2; }
  239. ;
  240. api_or_impl:
  241. API
  242. { $$ = true; }
  243. | IMPL
  244. { $$ = false; }
  245. ;
  246. expression:
  247. identifier
  248. { $$ = arena->New<IdentifierExpression>(context.source_loc(), $1); }
  249. | expression designator
  250. { $$ = arena->New<FieldAccessExpression>(context.source_loc(), $1, $2); }
  251. | expression LEFT_SQUARE_BRACKET expression RIGHT_SQUARE_BRACKET
  252. { $$ = arena->New<IndexExpression>(context.source_loc(), $1, $3); }
  253. | integer_literal
  254. { $$ = arena->New<IntLiteral>(context.source_loc(), $1); }
  255. | string_literal
  256. { $$ = arena->New<StringLiteral>(context.source_loc(), $1); }
  257. | TRUE
  258. { $$ = arena->New<BoolLiteral>(context.source_loc(), true); }
  259. | FALSE
  260. { $$ = arena->New<BoolLiteral>(context.source_loc(), false); }
  261. | sized_type_literal
  262. {
  263. int val;
  264. CHECK(llvm::to_integer(llvm::StringRef($1).substr(1), val));
  265. CHECK($1[0] == 'i' && val == 32)
  266. << "Only i32 is supported for now: " << $1;
  267. $$ = arena->New<IntTypeLiteral>(context.source_loc());
  268. }
  269. | STRING
  270. { $$ = arena->New<StringTypeLiteral>(context.source_loc()); }
  271. | BOOL
  272. { $$ = arena->New<BoolTypeLiteral>(context.source_loc()); }
  273. | TYPE
  274. { $$ = arena->New<TypeTypeLiteral>(context.source_loc()); }
  275. | CONTINUATION_TYPE
  276. { $$ = arena->New<ContinuationTypeLiteral>(context.source_loc()); }
  277. | paren_expression { $$ = $1; }
  278. | struct_literal { $$ = $1; }
  279. | struct_type_literal { $$ = $1; }
  280. | expression EQUAL_EQUAL expression
  281. {
  282. $$ = arena->New<PrimitiveOperatorExpression>(
  283. context.source_loc(), Operator::Eq,
  284. std::vector<Nonnull<Expression*>>({$1, $3}));
  285. }
  286. | expression PLUS expression
  287. {
  288. $$ = arena->New<PrimitiveOperatorExpression>(
  289. context.source_loc(), Operator::Add,
  290. std::vector<Nonnull<Expression*>>({$1, $3}));
  291. }
  292. | expression MINUS expression
  293. {
  294. $$ = arena->New<PrimitiveOperatorExpression>(
  295. context.source_loc(), Operator::Sub,
  296. std::vector<Nonnull<Expression*>>({$1, $3}));
  297. }
  298. | expression BINARY_STAR expression
  299. {
  300. $$ = arena->New<PrimitiveOperatorExpression>(
  301. context.source_loc(), Operator::Mul,
  302. std::vector<Nonnull<Expression*>>({$1, $3}));
  303. }
  304. | expression AND expression
  305. {
  306. $$ = arena->New<PrimitiveOperatorExpression>(
  307. context.source_loc(), Operator::And,
  308. std::vector<Nonnull<Expression*>>({$1, $3}));
  309. }
  310. | expression OR expression
  311. {
  312. $$ = arena->New<PrimitiveOperatorExpression>(
  313. context.source_loc(), Operator::Or,
  314. std::vector<Nonnull<Expression*>>({$1, $3}));
  315. }
  316. | NOT expression
  317. {
  318. $$ = arena->New<PrimitiveOperatorExpression>(
  319. context.source_loc(), Operator::Not,
  320. std::vector<Nonnull<Expression*>>({$2}));
  321. }
  322. | MINUS expression %prec UNARY_MINUS
  323. {
  324. $$ = arena->New<PrimitiveOperatorExpression>(
  325. context.source_loc(), Operator::Neg,
  326. std::vector<Nonnull<Expression*>>({$2}));
  327. }
  328. | PREFIX_STAR expression
  329. {
  330. $$ = arena->New<PrimitiveOperatorExpression>(
  331. context.source_loc(), Operator::Deref,
  332. std::vector<Nonnull<Expression*>>({$2}));
  333. }
  334. | UNARY_STAR expression %prec PREFIX_STAR
  335. {
  336. $$ = arena->New<PrimitiveOperatorExpression>(
  337. context.source_loc(), Operator::Deref,
  338. std::vector<Nonnull<Expression*>>({$2}));
  339. }
  340. | expression tuple
  341. { $$ = arena->New<CallExpression>(context.source_loc(), $1, $2); }
  342. | expression POSTFIX_STAR
  343. {
  344. $$ = arena->New<PrimitiveOperatorExpression>(
  345. context.source_loc(), Operator::Ptr,
  346. std::vector<Nonnull<Expression*>>({$1}));
  347. }
  348. | expression UNARY_STAR
  349. {
  350. $$ = arena->New<PrimitiveOperatorExpression>(
  351. context.source_loc(), Operator::Ptr,
  352. std::vector<Nonnull<Expression*>>({$1}));
  353. }
  354. | FN_TYPE tuple ARROW expression
  355. { $$ = arena->New<FunctionTypeLiteral>(context.source_loc(), $2, $4); }
  356. ;
  357. designator: PERIOD identifier { $$ = $2; }
  358. ;
  359. paren_expression: paren_expression_base
  360. { $$ = ExpressionFromParenContents(arena, context.source_loc(), $1); }
  361. ;
  362. tuple: paren_expression_base
  363. { $$ = TupleExpressionFromParenContents(arena, context.source_loc(), $1); }
  364. ;
  365. paren_expression_base:
  366. LEFT_PARENTHESIS RIGHT_PARENTHESIS
  367. { $$ = {.elements = {}, .has_trailing_comma = false}; }
  368. | LEFT_PARENTHESIS paren_expression_contents RIGHT_PARENTHESIS
  369. { $$ = $2; }
  370. | LEFT_PARENTHESIS paren_expression_contents COMMA RIGHT_PARENTHESIS
  371. {
  372. $$ = $2;
  373. $$.has_trailing_comma = true;
  374. }
  375. ;
  376. paren_expression_contents:
  377. expression
  378. { $$ = {.elements = {$1}, .has_trailing_comma = false}; }
  379. | paren_expression_contents COMMA expression
  380. {
  381. $$ = $1;
  382. $$.elements.push_back($3);
  383. }
  384. ;
  385. struct_literal:
  386. LEFT_CURLY_BRACE struct_literal_contents RIGHT_CURLY_BRACE
  387. { $$ = arena->New<StructLiteral>(context.source_loc(), $2); }
  388. | LEFT_CURLY_BRACE struct_literal_contents COMMA RIGHT_CURLY_BRACE
  389. { $$ = arena->New<StructLiteral>(context.source_loc(), $2); }
  390. ;
  391. struct_literal_contents:
  392. designator EQUAL expression
  393. { $$ = {FieldInitializer($1, $3)}; }
  394. | struct_literal_contents COMMA designator EQUAL expression
  395. {
  396. $$ = $1;
  397. $$.push_back(FieldInitializer($3, $5));
  398. }
  399. ;
  400. struct_type_literal:
  401. LEFT_CURLY_BRACE RIGHT_CURLY_BRACE
  402. { $$ = arena->New<StructTypeLiteral>(context.source_loc()); }
  403. | LEFT_CURLY_BRACE struct_type_literal_contents RIGHT_CURLY_BRACE
  404. { $$ = arena->New<StructTypeLiteral>(context.source_loc(), $2); }
  405. | LEFT_CURLY_BRACE struct_type_literal_contents COMMA RIGHT_CURLY_BRACE
  406. { $$ = arena->New<StructTypeLiteral>(context.source_loc(), $2); }
  407. ;
  408. struct_type_literal_contents:
  409. designator COLON expression
  410. { $$ = {FieldInitializer($1, $3)}; }
  411. | struct_type_literal_contents COMMA designator COLON expression
  412. {
  413. $$ = $1;
  414. $$.push_back(FieldInitializer($3, $5));
  415. }
  416. ;
  417. // In many cases, using `pattern` recursively will result in ambiguities.
  418. // When that happens, it's necessary to factor out two separate productions,
  419. // one for when the sub-pattern is an expression, and one for when it is not.
  420. // To facilitate this, non-terminals besides `pattern` whose names contain
  421. // `pattern` are structured to be disjoint from `expression`, unless otherwise
  422. // specified.
  423. pattern:
  424. non_expression_pattern
  425. { $$ = $1; }
  426. | expression
  427. { $$ = arena->New<ExpressionPattern>($1); }
  428. ;
  429. non_expression_pattern:
  430. AUTO
  431. { $$ = arena->New<AutoPattern>(context.source_loc()); }
  432. | binding_lhs COLON pattern
  433. { $$ = arena->New<BindingPattern>(context.source_loc(), $1, $3); }
  434. | paren_pattern
  435. { $$ = $1; }
  436. | expression tuple_pattern
  437. { $$ = arena->New<AlternativePattern>(context.source_loc(), $1, $2); }
  438. ;
  439. binding_lhs:
  440. identifier { $$ = $1; }
  441. | UNDERSCORE { $$ = std::nullopt; }
  442. ;
  443. paren_pattern: paren_pattern_base
  444. { $$ = PatternFromParenContents(arena, context.source_loc(), $1); }
  445. ;
  446. paren_pattern_base:
  447. LEFT_PARENTHESIS paren_pattern_contents RIGHT_PARENTHESIS
  448. { $$ = $2; }
  449. | LEFT_PARENTHESIS paren_pattern_contents COMMA RIGHT_PARENTHESIS
  450. {
  451. $$ = $2;
  452. $$.has_trailing_comma = true;
  453. }
  454. ;
  455. // paren_pattern is analogous to paren_expression, but in order to avoid
  456. // ambiguities, it must be disjoint from paren_expression, meaning it must
  457. // contain at least one non_expression_pattern. The structure of this rule
  458. // is very different from the corresponding expression rule because is has to
  459. // enforce that requirement.
  460. paren_pattern_contents:
  461. non_expression_pattern
  462. { $$ = {.elements = {$1}, .has_trailing_comma = false}; }
  463. | paren_expression_contents COMMA non_expression_pattern
  464. {
  465. $$ = ParenExpressionToParenPattern(arena, $1);
  466. $$.elements.push_back($3);
  467. }
  468. | paren_pattern_contents COMMA expression
  469. {
  470. $$ = $1;
  471. $$.elements.push_back(arena->New<ExpressionPattern>($3));
  472. }
  473. | paren_pattern_contents COMMA non_expression_pattern
  474. {
  475. $$ = $1;
  476. $$.elements.push_back($3);
  477. }
  478. ;
  479. tuple_pattern: paren_pattern_base
  480. { $$ = TuplePatternFromParenContents(arena, context.source_loc(), $1); }
  481. ;
  482. // Unlike most `pattern` nonterminals, this one overlaps with `expression`,
  483. // so it should be used only when prior context (such as an introducer)
  484. // rules out the possibility of an `expression` at this point.
  485. maybe_empty_tuple_pattern:
  486. LEFT_PARENTHESIS RIGHT_PARENTHESIS
  487. {
  488. $$ = arena->New<TuplePattern>(context.source_loc(),
  489. std::vector<Nonnull<Pattern*>>());
  490. }
  491. | tuple_pattern
  492. { $$ = $1; }
  493. ;
  494. clause:
  495. CASE pattern DOUBLE_ARROW statement
  496. { $$ = Match::Clause($2, $4); }
  497. | DEFAULT DOUBLE_ARROW statement
  498. {
  499. $$ = Match::Clause(arena->New<BindingPattern>(
  500. context.source_loc(), std::nullopt,
  501. arena->New<AutoPattern>(context.source_loc())),
  502. $3);
  503. }
  504. ;
  505. clause_list:
  506. // Empty
  507. { $$ = {}; }
  508. | clause_list clause
  509. {
  510. $$ = $1;
  511. $$.push_back($2);
  512. }
  513. ;
  514. statement:
  515. expression EQUAL expression SEMICOLON
  516. { $$ = arena->New<Assign>(context.source_loc(), $1, $3); }
  517. | VAR pattern EQUAL expression SEMICOLON
  518. { $$ = arena->New<VariableDefinition>(context.source_loc(), $2, $4); }
  519. | expression SEMICOLON
  520. { $$ = arena->New<ExpressionStatement>(context.source_loc(), $1); }
  521. | if_statement
  522. { $$ = $1; }
  523. | WHILE LEFT_PARENTHESIS expression RIGHT_PARENTHESIS block
  524. { $$ = arena->New<While>(context.source_loc(), $3, $5); }
  525. | BREAK SEMICOLON
  526. { $$ = arena->New<Break>(context.source_loc()); }
  527. | CONTINUE SEMICOLON
  528. { $$ = arena->New<Continue>(context.source_loc()); }
  529. | RETURN return_expression SEMICOLON
  530. {
  531. auto [return_exp, is_omitted_exp] = $2;
  532. $$ = arena->New<Return>(context.source_loc(), return_exp, is_omitted_exp);
  533. }
  534. // We disallow empty blocks in places where an arbitrary statement can occur
  535. // in order to avoid ambiguity with the empty struct literal `{}`. We can
  536. // allow non-empty blocks because a non-empty struct literal always starts with
  537. // a designator, and a block never does, so one token of lookahead suffices
  538. // to disambiguate. As of this writing, the "official" resolution of this
  539. // ambiguity is an open question (see
  540. // https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/classes.md#literals)
  541. | nonempty_block
  542. { $$ = $1; }
  543. | MATCH LEFT_PARENTHESIS expression RIGHT_PARENTHESIS LEFT_CURLY_BRACE
  544. clause_list RIGHT_CURLY_BRACE
  545. { $$ = arena->New<Match>(context.source_loc(), $3, $6); }
  546. | CONTINUATION identifier block
  547. { $$ = arena->New<Continuation>(context.source_loc(), $2, $3); }
  548. | RUN expression SEMICOLON
  549. { $$ = arena->New<Run>(context.source_loc(), $2); }
  550. | AWAIT SEMICOLON
  551. { $$ = arena->New<Await>(context.source_loc()); }
  552. ;
  553. if_statement:
  554. IF LEFT_PARENTHESIS expression RIGHT_PARENTHESIS block optional_else
  555. { $$ = arena->New<If>(context.source_loc(), $3, $5, $6); }
  556. ;
  557. optional_else:
  558. // Empty
  559. { $$ = std::nullopt; }
  560. | ELSE if_statement
  561. {
  562. $$ = arena->New<Block>(context.source_loc(),
  563. std::vector<Nonnull<Statement*>>({$2}));
  564. }
  565. | ELSE block
  566. { $$ = $2; }
  567. ;
  568. return_expression:
  569. // Empty
  570. { $$ = {arena->New<TupleLiteral>(context.source_loc()), true}; }
  571. | expression
  572. { $$ = {$1, false}; }
  573. ;
  574. statement_list:
  575. // Empty
  576. { $$ = {}; }
  577. | statement_list statement
  578. {
  579. $$ = std::move($1);
  580. $$.push_back($2);
  581. }
  582. ;
  583. block:
  584. LEFT_CURLY_BRACE statement_list RIGHT_CURLY_BRACE
  585. { $$ = arena->New<Block>(context.source_loc(), std::move($2)); }
  586. ;
  587. nonempty_block:
  588. LEFT_CURLY_BRACE statement_list statement RIGHT_CURLY_BRACE
  589. {
  590. $2.push_back($3);
  591. $$ = arena->New<Block>(context.source_loc(), std::move($2));
  592. }
  593. ;
  594. return_term:
  595. // Empty
  596. { $$ = ReturnTerm::Omitted(context.source_loc()); }
  597. | ARROW AUTO %prec FNARROW
  598. { $$ = ReturnTerm::Auto(context.source_loc()); }
  599. | ARROW expression %prec FNARROW
  600. { $$ = ReturnTerm::Explicit($2); }
  601. ;
  602. generic_binding:
  603. identifier COLON_BANG expression
  604. {
  605. $$ = arena->New<GenericBinding>(context.source_loc(), std::move($1), $3);
  606. }
  607. ;
  608. deduced_param_list:
  609. // Empty
  610. { $$ = std::vector<Nonnull<GenericBinding*>>(); }
  611. | generic_binding
  612. {
  613. $$ = std::vector<Nonnull<GenericBinding*>>();
  614. $$.push_back($1);
  615. }
  616. | generic_binding COMMA deduced_param_list
  617. {
  618. $$ = $3;
  619. $$.push_back($1);
  620. }
  621. ;
  622. deduced_params:
  623. // Empty
  624. { $$ = std::vector<Nonnull<GenericBinding*>>(); }
  625. | LEFT_SQUARE_BRACKET deduced_param_list RIGHT_SQUARE_BRACKET
  626. { $$ = $2; }
  627. ;
  628. function_declaration:
  629. FN identifier deduced_params maybe_empty_tuple_pattern return_term block
  630. {
  631. $$ = arena->New<FunctionDeclaration>(context.source_loc(), $2, $3, $4, $5,
  632. $6);
  633. }
  634. | FN identifier deduced_params maybe_empty_tuple_pattern return_term SEMICOLON
  635. {
  636. $$ = arena->New<FunctionDeclaration>(context.source_loc(), $2, $3, $4, $5,
  637. std::nullopt);
  638. }
  639. ;
  640. variable_declaration: identifier COLON pattern
  641. { $$ = arena->New<BindingPattern>(context.source_loc(), $1, $3); }
  642. ;
  643. member: VAR variable_declaration SEMICOLON
  644. { $$ = arena->New<FieldMember>(context.source_loc(), $2); }
  645. ;
  646. member_list:
  647. // Empty
  648. { $$ = {}; }
  649. | member_list member
  650. {
  651. $$ = $1;
  652. $$.push_back($2);
  653. }
  654. ;
  655. alternative:
  656. identifier tuple
  657. { $$ = arena->New<AlternativeSignature>(context.source_loc(), $1, $2); }
  658. | identifier
  659. {
  660. $$ = arena->New<AlternativeSignature>(
  661. context.source_loc(), $1,
  662. arena->New<TupleLiteral>(context.source_loc()));
  663. }
  664. ;
  665. alternative_list:
  666. // Empty
  667. { $$ = {}; }
  668. | alternative_list_contents
  669. { $$ = $1; }
  670. | alternative_list_contents COMMA
  671. { $$ = $1; }
  672. ;
  673. alternative_list_contents:
  674. alternative
  675. { $$ = {std::move($1)}; }
  676. | alternative_list_contents COMMA alternative
  677. {
  678. $$ = $1;
  679. $$.push_back(std::move($3));
  680. }
  681. ;
  682. declaration:
  683. function_declaration
  684. { $$ = $1; }
  685. | CLASS identifier LEFT_CURLY_BRACE member_list RIGHT_CURLY_BRACE
  686. { $$ = arena->New<ClassDeclaration>(context.source_loc(), $2, $4); }
  687. | CHOICE identifier LEFT_CURLY_BRACE alternative_list RIGHT_CURLY_BRACE
  688. { $$ = arena->New<ChoiceDeclaration>(context.source_loc(), $2, $4); }
  689. | VAR variable_declaration EQUAL expression SEMICOLON
  690. { $$ = arena->New<VariableDeclaration>(context.source_loc(), $2, $4); }
  691. ;
  692. declaration_list:
  693. // Empty
  694. { $$ = {}; }
  695. | declaration_list declaration
  696. {
  697. $$ = $1;
  698. $$.push_back(Nonnull<Declaration*>($2));
  699. }
  700. ;
  701. %%