parser.ypp 22 KB

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