parser.ypp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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
  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. { $$ = global_arena->New<FieldAccessExpression>(context.SourceLoc(), $1, $2); }
  204. | expression LEFT_SQUARE_BRACKET expression RIGHT_SQUARE_BRACKET
  205. { $$ = global_arena->New<IndexExpression>(context.SourceLoc(), $1, $3); }
  206. | integer_literal
  207. { $$ = global_arena->New<IntLiteral>(context.SourceLoc(), $1); }
  208. | string_literal
  209. { $$ = global_arena->New<StringLiteral>(context.SourceLoc(), $1); }
  210. | TRUE
  211. { $$ = global_arena->New<BoolLiteral>(context.SourceLoc(), true); }
  212. | FALSE
  213. { $$ = global_arena->New<BoolLiteral>(context.SourceLoc(), false); }
  214. | sized_type_literal
  215. {
  216. int val;
  217. CHECK(llvm::to_integer(llvm::StringRef($1).substr(1), val));
  218. CHECK($1[0] == 'i' && val == 32) << "Only i32 is supported for now: " << $1;
  219. $$ = global_arena->New<IntTypeLiteral>(context.SourceLoc());
  220. }
  221. | STRING
  222. { $$ = global_arena->New<StringTypeLiteral>(context.SourceLoc()); }
  223. | BOOL
  224. { $$ = global_arena->New<BoolTypeLiteral>(context.SourceLoc()); }
  225. | TYPE
  226. { $$ = global_arena->New<TypeTypeLiteral>(context.SourceLoc()); }
  227. | CONTINUATION_TYPE
  228. { $$ = global_arena->New<ContinuationTypeLiteral>(context.SourceLoc()); }
  229. | paren_expression { $$ = $1; }
  230. | expression EQUAL_EQUAL expression
  231. { $$ = global_arena->New<PrimitiveOperatorExpression>(
  232. context.SourceLoc(), Operator::Eq, std::vector<Ptr<const Expression>>({$1, $3})); }
  233. | expression PLUS expression
  234. { $$ = global_arena->New<PrimitiveOperatorExpression>(
  235. context.SourceLoc(), Operator::Add, std::vector<Ptr<const Expression>>({$1, $3})); }
  236. | expression MINUS expression
  237. { $$ = global_arena->New<PrimitiveOperatorExpression>(
  238. context.SourceLoc(), Operator::Sub, std::vector<Ptr<const Expression>>({$1, $3})); }
  239. | expression BINARY_STAR expression
  240. { $$ = global_arena->New<PrimitiveOperatorExpression>(
  241. context.SourceLoc(), Operator::Mul, std::vector<Ptr<const Expression>>({$1, $3})); }
  242. | expression AND expression
  243. { $$ = global_arena->New<PrimitiveOperatorExpression>(
  244. context.SourceLoc(), Operator::And, std::vector<Ptr<const Expression>>({$1, $3})); }
  245. | expression OR expression
  246. { $$ = global_arena->New<PrimitiveOperatorExpression>(
  247. context.SourceLoc(), Operator::Or, std::vector<Ptr<const Expression>>({$1, $3})); }
  248. | NOT expression
  249. { $$ = global_arena->New<PrimitiveOperatorExpression>(
  250. context.SourceLoc(), Operator::Not, std::vector<Ptr<const Expression>>({$2})); }
  251. | MINUS expression %prec UNARY_MINUS
  252. { $$ = global_arena->New<PrimitiveOperatorExpression>(
  253. context.SourceLoc(), Operator::Neg, std::vector<Ptr<const Expression>>({$2})); }
  254. | PREFIX_STAR expression
  255. { $$ = global_arena->New<PrimitiveOperatorExpression>(
  256. context.SourceLoc(), Operator::Deref, std::vector<Ptr<const Expression>>({$2})); }
  257. | UNARY_STAR expression %prec PREFIX_STAR
  258. { $$ = global_arena->New<PrimitiveOperatorExpression>(
  259. context.SourceLoc(), Operator::Deref, std::vector<Ptr<const Expression>>({$2})); }
  260. | expression tuple
  261. { $$ = global_arena->New<CallExpression>(context.SourceLoc(), $1, $2); }
  262. | expression POSTFIX_STAR
  263. { $$ = global_arena->New<PrimitiveOperatorExpression>(
  264. context.SourceLoc(), Operator::Ptr, std::vector<Ptr<const Expression>>({$1})); }
  265. | expression UNARY_STAR
  266. { $$ = global_arena->New<PrimitiveOperatorExpression>(
  267. context.SourceLoc(), Operator::Ptr, std::vector<Ptr<const Expression>>({$1})); }
  268. | FNTY tuple return_type
  269. {
  270. auto [return_exp, is_omitted_exp] = $3.Release();
  271. $$ = global_arena->New<FunctionTypeLiteral>(
  272. context.SourceLoc(), $2, return_exp, is_omitted_exp); }
  273. ;
  274. designator: PERIOD identifier { $$ = $2; }
  275. ;
  276. paren_expression: paren_expression_base
  277. { $$ = ExpressionFromParenContents(context.SourceLoc(), $1); }
  278. ;
  279. tuple: paren_expression_base
  280. { $$ = TupleExpressionFromParenContents(context.SourceLoc(), $1); }
  281. ;
  282. paren_expression_element:
  283. expression
  284. { $$ = {.name = std::nullopt, .term = $1}; }
  285. | designator EQUAL expression
  286. { $$ = {.name = $1, .term = $3}; }
  287. ;
  288. paren_expression_base:
  289. LEFT_PARENTHESIS RIGHT_PARENTHESIS
  290. { $$ = {.elements = {}, .has_trailing_comma = false}; }
  291. | LEFT_PARENTHESIS paren_expression_contents RIGHT_PARENTHESIS
  292. { $$ = $2; }
  293. | LEFT_PARENTHESIS paren_expression_contents COMMA RIGHT_PARENTHESIS
  294. {
  295. $$ = $2;
  296. $$.has_trailing_comma = true;
  297. }
  298. ;
  299. paren_expression_contents:
  300. paren_expression_element
  301. { $$ = {.elements = {$1}, .has_trailing_comma = false}; }
  302. | paren_expression_contents COMMA paren_expression_element
  303. {
  304. $$ = $1;
  305. $$.elements.push_back($3);
  306. }
  307. ;
  308. // In many cases, using `pattern` recursively will result in ambiguities.
  309. // When that happens, it's necessary to factor out two separate productions,
  310. // one for when the sub-pattern is an expression, and one for when it is not.
  311. // To facilitate this, non-terminals besides `pattern` whose names contain
  312. // `pattern` are structured to be disjoint from `expression`, unless otherwise
  313. // specified.
  314. pattern:
  315. non_expression_pattern
  316. { $$ = $1; }
  317. | expression
  318. { $$ = global_arena->New<ExpressionPattern>($1); }
  319. ;
  320. non_expression_pattern:
  321. AUTO
  322. { $$ = global_arena->New<AutoPattern>(context.SourceLoc()); }
  323. | binding_lhs COLON pattern
  324. { $$ = global_arena->New<BindingPattern>(context.SourceLoc(), $1, $3); }
  325. | paren_pattern
  326. { $$ = $1; }
  327. | expression tuple_pattern
  328. { $$ = global_arena->New<AlternativePattern>(context.SourceLoc(), $1, $2); }
  329. ;
  330. binding_lhs:
  331. identifier { $$ = $1; }
  332. | UNDERSCORE { $$ = std::nullopt; }
  333. ;
  334. paren_pattern: paren_pattern_base
  335. { $$ = PatternFromParenContents(context.SourceLoc(), $1); }
  336. ;
  337. paren_pattern_base:
  338. LEFT_PARENTHESIS paren_pattern_contents RIGHT_PARENTHESIS
  339. { $$ = $2; }
  340. | LEFT_PARENTHESIS paren_pattern_contents COMMA RIGHT_PARENTHESIS
  341. {
  342. $$ = $2;
  343. $$.has_trailing_comma = true;
  344. }
  345. ;
  346. // paren_pattern is analogous to paren_expression, but in order to avoid
  347. // ambiguities, it must be disjoint from paren_expression, meaning it must
  348. // contain at least one non_expression_pattern. The structure of this rule
  349. // is very different from the corresponding expression rule because is has to
  350. // enforce that requirement.
  351. paren_pattern_contents:
  352. paren_pattern_element
  353. { $$ = {.elements = {$1}, .has_trailing_comma = false }; }
  354. | paren_expression_contents COMMA paren_pattern_element
  355. {
  356. $$ = ParenExpressionToParenPattern($1);
  357. $$.elements.push_back($3);
  358. }
  359. | paren_pattern_contents COMMA paren_expression_element
  360. {
  361. $$ = $1;
  362. auto el = $3.Release();
  363. $$.elements.push_back({.name = el.name, .term = global_arena->New<ExpressionPattern>(el.term)});
  364. }
  365. | paren_pattern_contents COMMA paren_pattern_element
  366. {
  367. $$ = $1;
  368. $$.elements.push_back($3);
  369. }
  370. ;
  371. paren_pattern_element:
  372. non_expression_pattern
  373. { $$ = {.name = std::nullopt, .term = $1}; }
  374. | designator EQUAL non_expression_pattern
  375. { $$ = {.name = $1, .term = $3}; }
  376. ;
  377. tuple_pattern: paren_pattern_base
  378. { $$ = TuplePatternFromParenContents(context.SourceLoc(), $1); }
  379. ;
  380. // Unlike most `pattern` nonterminals, this one overlaps with `expression`,
  381. // so it should be used only when prior context (such as an introducer)
  382. // rules out the possibility of an `expression` at this point.
  383. maybe_empty_tuple_pattern:
  384. LEFT_PARENTHESIS RIGHT_PARENTHESIS
  385. { $$ = global_arena->New<TuplePattern>(context.SourceLoc(), std::vector<TuplePattern::Field>()); }
  386. | tuple_pattern
  387. { $$ = $1; }
  388. ;
  389. clause:
  390. CASE pattern DOUBLE_ARROW statement
  391. { $$ = global_arena->RawNew<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>($2, $4); }
  392. | DEFAULT DOUBLE_ARROW statement
  393. {
  394. auto vp = global_arena->New<BindingPattern>(
  395. context.SourceLoc(), std::nullopt, global_arena->New<AutoPattern>(context.SourceLoc()));
  396. $$ = global_arena->RawNew<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>(vp, $3);
  397. }
  398. ;
  399. clause_list:
  400. // Empty
  401. {
  402. $$ = global_arena->RawNew<std::list<
  403. std::pair<Ptr<const Pattern>, Ptr<const Statement>>>>();
  404. }
  405. | clause clause_list
  406. { $$ = $2; $$->push_front(*$1); }
  407. ;
  408. statement:
  409. expression EQUAL expression SEMICOLON
  410. { $$ = global_arena->New<Assign>(context.SourceLoc(), $1, $3); }
  411. | VAR pattern EQUAL expression SEMICOLON
  412. { $$ = global_arena->New<VariableDefinition>(context.SourceLoc(), $2, $4); }
  413. | expression SEMICOLON
  414. { $$ = global_arena->New<ExpressionStatement>(context.SourceLoc(), $1); }
  415. | if_statement
  416. { $$ = $1; }
  417. | WHILE LEFT_PARENTHESIS expression RIGHT_PARENTHESIS block
  418. { $$ = global_arena->New<While>(context.SourceLoc(), $3, $5); }
  419. | BREAK SEMICOLON
  420. { $$ = global_arena->New<Break>(context.SourceLoc()); }
  421. | CONTINUE SEMICOLON
  422. { $$ = global_arena->New<Continue>(context.SourceLoc()); }
  423. | RETURN return_expression SEMICOLON
  424. {
  425. auto [return_exp, is_omitted_exp] = $2.Release();
  426. $$ = global_arena->New<Return>(context.SourceLoc(), return_exp, is_omitted_exp);
  427. }
  428. | block
  429. { $$ = $1; }
  430. | MATCH LEFT_PARENTHESIS expression RIGHT_PARENTHESIS LEFT_CURLY_BRACE
  431. clause_list RIGHT_CURLY_BRACE
  432. { $$ = global_arena->New<Match>(context.SourceLoc(), $3, $6); }
  433. | CONTINUATION identifier statement
  434. { $$ = global_arena->New<Continuation>(context.SourceLoc(), $2, $3); }
  435. | RUN expression SEMICOLON
  436. { $$ = global_arena->New<Run>(context.SourceLoc(), $2); }
  437. | AWAIT SEMICOLON
  438. { $$ = global_arena->New<Await>(context.SourceLoc()); }
  439. ;
  440. if_statement:
  441. IF LEFT_PARENTHESIS expression RIGHT_PARENTHESIS block optional_else
  442. { $$ = global_arena->New<If>(context.SourceLoc(), $3, $5, $6); }
  443. ;
  444. optional_else:
  445. // Empty
  446. { $$ = std::nullopt; }
  447. | ELSE if_statement
  448. { $$ = $2; }
  449. | ELSE block
  450. { $$ = $2; }
  451. ;
  452. return_expression:
  453. // Empty
  454. { $$ = {global_arena->New<TupleLiteral>(context.SourceLoc()), true}; }
  455. | expression
  456. { $$ = {$1, false}; }
  457. ;
  458. statement_list:
  459. // Empty
  460. { $$ = std::nullopt; }
  461. | statement statement_list
  462. { $$ = global_arena->New<Sequence>(context.SourceLoc(), $1, $2); }
  463. ;
  464. block:
  465. LEFT_CURLY_BRACE statement_list RIGHT_CURLY_BRACE
  466. { $$ = global_arena->New<Block>(context.SourceLoc(), $2); }
  467. ;
  468. return_type:
  469. // Empty
  470. { $$ = {global_arena->New<TupleLiteral>(context.SourceLoc()), true}; }
  471. | ARROW expression %prec FNARROW
  472. { $$ = {$2, false}; }
  473. ;
  474. generic_binding:
  475. identifier COLON_BANG expression
  476. {
  477. $$ = GenericBinding({.name = std::move($1), .type = $3});
  478. }
  479. ;
  480. deduced_param_list:
  481. // Empty
  482. { $$ = std::vector<GenericBinding>(); }
  483. | generic_binding
  484. {
  485. $$ = std::vector<GenericBinding>();
  486. $$.push_back($1);
  487. }
  488. | generic_binding COMMA deduced_param_list
  489. {
  490. $$ = $3;
  491. $$.push_back($1);
  492. }
  493. ;
  494. deduced_params:
  495. // Empty
  496. { $$ = std::vector<GenericBinding>(); }
  497. | LEFT_SQUARE_BRACKET deduced_param_list RIGHT_SQUARE_BRACKET
  498. { $$ = $2; }
  499. ;
  500. function_definition:
  501. FN identifier deduced_params maybe_empty_tuple_pattern return_type block
  502. {
  503. auto [return_exp, is_omitted_exp] = $5.Release();
  504. $$ = global_arena->New<FunctionDefinition>(
  505. context.SourceLoc(), $2, $3, $4,
  506. global_arena->New<ExpressionPattern>(return_exp),
  507. is_omitted_exp, $6);
  508. }
  509. | FN identifier deduced_params maybe_empty_tuple_pattern DOUBLE_ARROW expression
  510. SEMICOLON
  511. {
  512. // The return type is not considered "omitted" because it's automatic from
  513. // the expression.
  514. $$ = global_arena->New<FunctionDefinition>(
  515. context.SourceLoc(), $2, $3, $4,
  516. global_arena->New<AutoPattern>(context.SourceLoc()), true,
  517. global_arena->New<Return>(context.SourceLoc(), $6, true));
  518. }
  519. ;
  520. function_declaration:
  521. FN identifier deduced_params maybe_empty_tuple_pattern return_type SEMICOLON
  522. {
  523. auto [return_exp, is_omitted_exp] = $5.Release();
  524. $$ = global_arena->New<FunctionDefinition>(
  525. context.SourceLoc(), $2, $3, $4,
  526. global_arena->New<ExpressionPattern>(return_exp),
  527. is_omitted_exp, std::nullopt);
  528. }
  529. ;
  530. variable_declaration: identifier COLON pattern
  531. { $$ = global_arena->New<BindingPattern>(context.SourceLoc(), $1, $3); }
  532. ;
  533. member: VAR variable_declaration SEMICOLON
  534. { $$ = global_arena->New<FieldMember>(context.SourceLoc(), $2); }
  535. ;
  536. member_list:
  537. // Empty
  538. { $$ = std::list<Ptr<Member>>(); }
  539. | member member_list
  540. { $$ = $2; $$.push_front($1); }
  541. ;
  542. alternative:
  543. identifier tuple
  544. { $$ = std::pair<std::string, Ptr<const Expression>>($1, $2); }
  545. | identifier
  546. {
  547. $$ = std::pair<std::string, Ptr<const Expression>>(
  548. $1, global_arena->New<TupleLiteral>(context.SourceLoc()));
  549. }
  550. ;
  551. alternative_list:
  552. // Empty
  553. { $$ = std::list<std::pair<std::string, Ptr<const Expression>>>(); }
  554. | alternative
  555. {
  556. $$ = std::list<std::pair<std::string, Ptr<const Expression>>>();
  557. $$.push_front($1);
  558. }
  559. | alternative COMMA alternative_list
  560. { $$ = std::move($3); $$.push_front($1); }
  561. ;
  562. declaration:
  563. function_definition
  564. { $$ = global_arena->New<FunctionDeclaration>($1); }
  565. | function_declaration
  566. { $$ = global_arena->New<FunctionDeclaration>($1); }
  567. | CLASS identifier LEFT_CURLY_BRACE member_list RIGHT_CURLY_BRACE
  568. {
  569. $$ = global_arena->New<ClassDeclaration>(context.SourceLoc(), $2, $4);
  570. }
  571. | CHOICE identifier LEFT_CURLY_BRACE alternative_list RIGHT_CURLY_BRACE
  572. {
  573. $$ = global_arena->New<ChoiceDeclaration>(context.SourceLoc(), $2, $4);
  574. }
  575. | VAR variable_declaration EQUAL expression SEMICOLON
  576. {
  577. $$ = global_arena->New<VariableDeclaration>(context.SourceLoc(), $2, $4);
  578. }
  579. ;
  580. declaration_list:
  581. // Empty
  582. { $$ = std::list<Ptr<const Declaration>>(); }
  583. | declaration declaration_list
  584. {
  585. $$ = $2;
  586. $$.push_front(Ptr<const Declaration>($1));
  587. }
  588. ;
  589. %%