parser.ypp 23 KB

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