parser.ypp 23 KB

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