parser.ypp 21 KB

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