parser.ypp 20 KB

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