parser.ypp 21 KB

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