parser.ypp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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 <GenericBinding> generic_binding
  90. %type <std::vector<GenericBinding>> deduced_params
  91. %type <std::vector<GenericBinding>> deduced_param_list
  92. %type <Nonnull<Pattern*>> pattern
  93. %type <Nonnull<Pattern*>> non_expression_pattern
  94. %type <std::pair<Nonnull<Expression*>, bool>> return_type
  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 <BisonWrap<ChoiceDeclaration::Alternative>> alternative
  113. %type <std::vector<ChoiceDeclaration::Alternative>> alternative_list
  114. %type <std::vector<ChoiceDeclaration::Alternative>> 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 return_type
  355. {
  356. auto [return_exp, is_omitted_exp] = $3;
  357. $$ = arena->New<FunctionTypeLiteral>(context.source_loc(), $2, return_exp,
  358. is_omitted_exp);
  359. }
  360. ;
  361. designator: PERIOD identifier { $$ = $2; }
  362. ;
  363. paren_expression: paren_expression_base
  364. { $$ = ExpressionFromParenContents(arena, context.source_loc(), $1); }
  365. ;
  366. tuple: paren_expression_base
  367. { $$ = TupleExpressionFromParenContents(arena, context.source_loc(), $1); }
  368. ;
  369. paren_expression_base:
  370. LEFT_PARENTHESIS RIGHT_PARENTHESIS
  371. { $$ = {.elements = {}, .has_trailing_comma = false}; }
  372. | LEFT_PARENTHESIS paren_expression_contents RIGHT_PARENTHESIS
  373. { $$ = $2; }
  374. | LEFT_PARENTHESIS paren_expression_contents COMMA RIGHT_PARENTHESIS
  375. {
  376. $$ = $2;
  377. $$.has_trailing_comma = true;
  378. }
  379. ;
  380. paren_expression_contents:
  381. expression
  382. { $$ = {.elements = {$1}, .has_trailing_comma = false}; }
  383. | paren_expression_contents COMMA expression
  384. {
  385. $$ = $1;
  386. $$.elements.push_back($3);
  387. }
  388. ;
  389. struct_literal:
  390. LEFT_CURLY_BRACE struct_literal_contents RIGHT_CURLY_BRACE
  391. { $$ = arena->New<StructLiteral>(context.source_loc(), $2); }
  392. | LEFT_CURLY_BRACE struct_literal_contents COMMA RIGHT_CURLY_BRACE
  393. { $$ = arena->New<StructLiteral>(context.source_loc(), $2); }
  394. ;
  395. struct_literal_contents:
  396. designator EQUAL expression
  397. { $$ = {FieldInitializer($1, $3)}; }
  398. | struct_literal_contents COMMA designator EQUAL expression
  399. {
  400. $$ = $1;
  401. $$.push_back(FieldInitializer($3, $5));
  402. }
  403. ;
  404. struct_type_literal:
  405. LEFT_CURLY_BRACE RIGHT_CURLY_BRACE
  406. { $$ = arena->New<StructTypeLiteral>(context.source_loc()); }
  407. | LEFT_CURLY_BRACE struct_type_literal_contents RIGHT_CURLY_BRACE
  408. { $$ = arena->New<StructTypeLiteral>(context.source_loc(), $2); }
  409. | LEFT_CURLY_BRACE struct_type_literal_contents COMMA RIGHT_CURLY_BRACE
  410. { $$ = arena->New<StructTypeLiteral>(context.source_loc(), $2); }
  411. ;
  412. struct_type_literal_contents:
  413. designator COLON expression
  414. { $$ = {FieldInitializer($1, $3)}; }
  415. | struct_type_literal_contents COMMA designator COLON expression
  416. {
  417. $$ = $1;
  418. $$.push_back(FieldInitializer($3, $5));
  419. }
  420. ;
  421. // In many cases, using `pattern` recursively will result in ambiguities.
  422. // When that happens, it's necessary to factor out two separate productions,
  423. // one for when the sub-pattern is an expression, and one for when it is not.
  424. // To facilitate this, non-terminals besides `pattern` whose names contain
  425. // `pattern` are structured to be disjoint from `expression`, unless otherwise
  426. // specified.
  427. pattern:
  428. non_expression_pattern
  429. { $$ = $1; }
  430. | expression
  431. { $$ = arena->New<ExpressionPattern>($1); }
  432. ;
  433. non_expression_pattern:
  434. AUTO
  435. { $$ = arena->New<AutoPattern>(context.source_loc()); }
  436. | binding_lhs COLON pattern
  437. { $$ = arena->New<BindingPattern>(context.source_loc(), $1, $3); }
  438. | paren_pattern
  439. { $$ = $1; }
  440. | expression tuple_pattern
  441. { $$ = arena->New<AlternativePattern>(context.source_loc(), $1, $2); }
  442. ;
  443. binding_lhs:
  444. identifier { $$ = $1; }
  445. | UNDERSCORE { $$ = std::nullopt; }
  446. ;
  447. paren_pattern: paren_pattern_base
  448. { $$ = PatternFromParenContents(arena, context.source_loc(), $1); }
  449. ;
  450. paren_pattern_base:
  451. LEFT_PARENTHESIS paren_pattern_contents RIGHT_PARENTHESIS
  452. { $$ = $2; }
  453. | LEFT_PARENTHESIS paren_pattern_contents COMMA RIGHT_PARENTHESIS
  454. {
  455. $$ = $2;
  456. $$.has_trailing_comma = true;
  457. }
  458. ;
  459. // paren_pattern is analogous to paren_expression, but in order to avoid
  460. // ambiguities, it must be disjoint from paren_expression, meaning it must
  461. // contain at least one non_expression_pattern. The structure of this rule
  462. // is very different from the corresponding expression rule because is has to
  463. // enforce that requirement.
  464. paren_pattern_contents:
  465. non_expression_pattern
  466. { $$ = {.elements = {$1}, .has_trailing_comma = false}; }
  467. | paren_expression_contents COMMA non_expression_pattern
  468. {
  469. $$ = ParenExpressionToParenPattern(arena, $1);
  470. $$.elements.push_back($3);
  471. }
  472. | paren_pattern_contents COMMA expression
  473. {
  474. $$ = $1;
  475. $$.elements.push_back(arena->New<ExpressionPattern>($3));
  476. }
  477. | paren_pattern_contents COMMA non_expression_pattern
  478. {
  479. $$ = $1;
  480. $$.elements.push_back($3);
  481. }
  482. ;
  483. tuple_pattern: paren_pattern_base
  484. { $$ = TuplePatternFromParenContents(arena, context.source_loc(), $1); }
  485. ;
  486. // Unlike most `pattern` nonterminals, this one overlaps with `expression`,
  487. // so it should be used only when prior context (such as an introducer)
  488. // rules out the possibility of an `expression` at this point.
  489. maybe_empty_tuple_pattern:
  490. LEFT_PARENTHESIS RIGHT_PARENTHESIS
  491. {
  492. $$ = arena->New<TuplePattern>(context.source_loc(),
  493. std::vector<Nonnull<Pattern*>>());
  494. }
  495. | tuple_pattern
  496. { $$ = $1; }
  497. ;
  498. clause:
  499. CASE pattern DOUBLE_ARROW statement
  500. { $$ = Match::Clause($2, $4); }
  501. | DEFAULT DOUBLE_ARROW statement
  502. {
  503. $$ = Match::Clause(arena->New<BindingPattern>(
  504. context.source_loc(), std::nullopt,
  505. arena->New<AutoPattern>(context.source_loc())),
  506. $3);
  507. }
  508. ;
  509. clause_list:
  510. // Empty
  511. { $$ = {}; }
  512. | clause_list clause
  513. {
  514. $$ = $1;
  515. $$.push_back($2);
  516. }
  517. ;
  518. statement:
  519. expression EQUAL expression SEMICOLON
  520. { $$ = arena->New<Assign>(context.source_loc(), $1, $3); }
  521. | VAR pattern EQUAL expression SEMICOLON
  522. { $$ = arena->New<VariableDefinition>(context.source_loc(), $2, $4); }
  523. | expression SEMICOLON
  524. { $$ = arena->New<ExpressionStatement>(context.source_loc(), $1); }
  525. | if_statement
  526. { $$ = $1; }
  527. | WHILE LEFT_PARENTHESIS expression RIGHT_PARENTHESIS block
  528. { $$ = arena->New<While>(context.source_loc(), $3, $5); }
  529. | BREAK SEMICOLON
  530. { $$ = arena->New<Break>(context.source_loc()); }
  531. | CONTINUE SEMICOLON
  532. { $$ = arena->New<Continue>(context.source_loc()); }
  533. | RETURN return_expression SEMICOLON
  534. {
  535. auto [return_exp, is_omitted_exp] = $2;
  536. $$ = arena->New<Return>(context.source_loc(), return_exp, is_omitted_exp);
  537. }
  538. // We disallow empty blocks in places where an arbitrary statement can occur
  539. // in order to avoid ambiguity with the empty struct literal `{}`. We can
  540. // allow non-empty blocks because a non-empty struct literal always starts with
  541. // a designator, and a block never does, so one token of lookahead suffices
  542. // to disambiguate. As of this writing, the "official" resolution of this
  543. // ambiguity is an open question (see
  544. // https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/classes.md#literals)
  545. | nonempty_block
  546. { $$ = $1; }
  547. | MATCH LEFT_PARENTHESIS expression RIGHT_PARENTHESIS LEFT_CURLY_BRACE
  548. clause_list RIGHT_CURLY_BRACE
  549. { $$ = arena->New<Match>(context.source_loc(), $3, $6); }
  550. | CONTINUATION identifier block
  551. { $$ = arena->New<Continuation>(context.source_loc(), $2, $3); }
  552. | RUN expression SEMICOLON
  553. { $$ = arena->New<Run>(context.source_loc(), $2); }
  554. | AWAIT SEMICOLON
  555. { $$ = arena->New<Await>(context.source_loc()); }
  556. ;
  557. if_statement:
  558. IF LEFT_PARENTHESIS expression RIGHT_PARENTHESIS block optional_else
  559. { $$ = arena->New<If>(context.source_loc(), $3, $5, $6); }
  560. ;
  561. optional_else:
  562. // Empty
  563. { $$ = std::nullopt; }
  564. | ELSE if_statement
  565. {
  566. $$ = arena->New<Block>(context.source_loc(),
  567. std::vector<Nonnull<Statement*>>({$2}));
  568. }
  569. | ELSE block
  570. { $$ = $2; }
  571. ;
  572. return_expression:
  573. // Empty
  574. { $$ = {arena->New<TupleLiteral>(context.source_loc()), true}; }
  575. | expression
  576. { $$ = {$1, false}; }
  577. ;
  578. statement_list:
  579. // Empty
  580. { $$ = {}; }
  581. | statement_list statement
  582. {
  583. $$ = std::move($1);
  584. $$.push_back($2);
  585. }
  586. ;
  587. block:
  588. LEFT_CURLY_BRACE statement_list RIGHT_CURLY_BRACE
  589. { $$ = arena->New<Block>(context.source_loc(), std::move($2)); }
  590. ;
  591. nonempty_block:
  592. LEFT_CURLY_BRACE statement_list statement RIGHT_CURLY_BRACE
  593. {
  594. $2.push_back($3);
  595. $$ = arena->New<Block>(context.source_loc(), std::move($2));
  596. }
  597. ;
  598. return_type:
  599. // Empty
  600. { $$ = {arena->New<TupleLiteral>(context.source_loc()), true}; }
  601. | ARROW expression %prec FNARROW
  602. { $$ = {$2, false}; }
  603. ;
  604. generic_binding:
  605. identifier COLON_BANG expression
  606. { $$ = GenericBinding({.name = std::move($1), .type = $3}); }
  607. ;
  608. deduced_param_list:
  609. // Empty
  610. { $$ = std::vector<GenericBinding>(); }
  611. | generic_binding
  612. {
  613. $$ = std::vector<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<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_type block
  630. {
  631. auto [return_exp, is_omitted_exp] = $5;
  632. $$ = arena->New<FunctionDeclaration>(
  633. context.source_loc(), $2, $3, $4,
  634. arena->New<ExpressionPattern>(return_exp), is_omitted_exp, $6);
  635. }
  636. | FN identifier deduced_params maybe_empty_tuple_pattern ARROW AUTO block
  637. {
  638. // The return type is not considered "omitted" because it's `auto`.
  639. $$ = arena->New<FunctionDeclaration>(
  640. context.source_loc(), $2, $3, $4,
  641. arena->New<AutoPattern>(context.source_loc()),
  642. /*is_omitted_exp=*/false, $7);
  643. }
  644. | FN identifier deduced_params maybe_empty_tuple_pattern return_type SEMICOLON
  645. {
  646. auto [return_exp, is_omitted_exp] = $5;
  647. $$ = arena->New<FunctionDeclaration>(
  648. context.source_loc(), $2, $3, $4,
  649. arena->New<ExpressionPattern>(return_exp), is_omitted_exp,
  650. std::nullopt);
  651. }
  652. ;
  653. variable_declaration: identifier COLON pattern
  654. { $$ = arena->New<BindingPattern>(context.source_loc(), $1, $3); }
  655. ;
  656. member: VAR variable_declaration SEMICOLON
  657. { $$ = arena->New<FieldMember>(context.source_loc(), $2); }
  658. ;
  659. member_list:
  660. // Empty
  661. { $$ = {}; }
  662. | member_list member
  663. {
  664. $$ = $1;
  665. $$.push_back($2);
  666. }
  667. ;
  668. alternative:
  669. identifier tuple
  670. { $$ = ChoiceDeclaration::Alternative($1, $2); }
  671. | identifier
  672. {
  673. $$ = ChoiceDeclaration::Alternative(
  674. $1, arena->New<TupleLiteral>(context.source_loc()));
  675. }
  676. ;
  677. alternative_list:
  678. // Empty
  679. { $$ = {}; }
  680. | alternative_list_contents
  681. { $$ = $1; }
  682. | alternative_list_contents COMMA
  683. { $$ = $1; }
  684. ;
  685. alternative_list_contents:
  686. alternative
  687. { $$ = {std::move($1)}; }
  688. | alternative_list_contents COMMA alternative
  689. {
  690. $$ = $1;
  691. $$.push_back(std::move($3));
  692. }
  693. ;
  694. declaration:
  695. function_declaration
  696. { $$ = $1; }
  697. | CLASS identifier LEFT_CURLY_BRACE member_list RIGHT_CURLY_BRACE
  698. { $$ = arena->New<ClassDeclaration>(context.source_loc(), $2, $4); }
  699. | CHOICE identifier LEFT_CURLY_BRACE alternative_list RIGHT_CURLY_BRACE
  700. { $$ = arena->New<ChoiceDeclaration>(context.source_loc(), $2, $4); }
  701. | VAR variable_declaration EQUAL expression SEMICOLON
  702. { $$ = arena->New<VariableDeclaration>(context.source_loc(), $2, $4); }
  703. ;
  704. declaration_list:
  705. // Empty
  706. { $$ = {}; }
  707. | declaration_list declaration
  708. {
  709. $$ = $1;
  710. $$.push_back(Nonnull<Declaration*>($2));
  711. }
  712. ;
  713. %%