parser.ypp 21 KB

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