parser.ypp 23 KB

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