parser.ypp 20 KB

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