parser.ypp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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 <vector>
  43. #include "common/check.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/ast.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/paren_contents.h"
  54. #include "executable_semantics/ast/pattern.h"
  55. #include "executable_semantics/common/arena.h"
  56. #include "executable_semantics/common/ptr.h"
  57. #include "executable_semantics/syntax/bison_wrap.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 <BisonWrap<Ptr<const Declaration>>> declaration
  79. %type <BisonWrap<Ptr<const FunctionDefinition>>> function_declaration
  80. %type <BisonWrap<Ptr<const FunctionDefinition>>> function_definition
  81. %type <std::vector<Ptr<const Declaration>>> declaration_list
  82. %type <BisonWrap<Ptr<const Statement>>> statement
  83. %type <BisonWrap<Ptr<const Statement>>> if_statement
  84. %type <std::optional<Ptr<const Statement>>> optional_else
  85. %type <BisonWrap<std::pair<Ptr<const Expression>, bool>>> return_expression
  86. %type <BisonWrap<Ptr<const Statement>>> block
  87. %type <std::optional<Ptr<const Statement>>> statement_list
  88. %type <BisonWrap<Ptr<const Expression>>> expression
  89. %type <BisonWrap<GenericBinding>> generic_binding
  90. %type <std::vector<GenericBinding>> deduced_params
  91. %type <std::vector<GenericBinding>> deduced_param_list
  92. %type <BisonWrap<Ptr<const Pattern>>> pattern
  93. %type <BisonWrap<Ptr<const Pattern>>> non_expression_pattern
  94. %type <BisonWrap<std::pair<Ptr<const Expression>, bool>>> return_type
  95. %type <BisonWrap<Ptr<const Expression>>> paren_expression
  96. %type <BisonWrap<Ptr<const Expression>>> tuple
  97. %type <std::optional<std::string>> binding_lhs
  98. %type <BisonWrap<Ptr<const BindingPattern>>> variable_declaration
  99. %type <BisonWrap<Ptr<Member>>> member
  100. %type <std::vector<Ptr<Member>>> member_list
  101. %type <BisonWrap<ParenContents<Expression>::Element>> paren_expression_element
  102. %type <ParenContents<Expression>> paren_expression_base
  103. %type <ParenContents<Expression>> paren_expression_contents
  104. %type <BisonWrap<Ptr<const Pattern>>> paren_pattern
  105. %type <BisonWrap<Ptr<const TuplePattern>>> tuple_pattern
  106. %type <BisonWrap<Ptr<const TuplePattern>>> maybe_empty_tuple_pattern
  107. %type <ParenContents<Pattern>> paren_pattern_base
  108. %type <BisonWrap<ParenContents<Pattern>::Element>> paren_pattern_element
  109. %type <ParenContents<Pattern>> paren_pattern_contents
  110. %type <BisonWrap<std::pair<std::string, Ptr<const Expression>>>> alternative
  111. %type <std::vector<std::pair<std::string, Ptr<const Expression>>>> alternative_list
  112. %type <std::vector<std::pair<std::string, Ptr<const Expression>>>> alternative_list_contents
  113. %type <BisonWrap<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>> clause
  114. %type <std::vector<std::pair<Ptr<const Pattern>, Ptr<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. parsed_program = 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. { $$ = global_arena->New<IdentifierExpression>(context.SourceLoc(), $1); }
  247. | expression designator
  248. {
  249. $$ =
  250. global_arena->New<FieldAccessExpression>(context.SourceLoc(), $1, $2);
  251. }
  252. | expression LEFT_SQUARE_BRACKET expression RIGHT_SQUARE_BRACKET
  253. { $$ = global_arena->New<IndexExpression>(context.SourceLoc(), $1, $3); }
  254. | integer_literal
  255. { $$ = global_arena->New<IntLiteral>(context.SourceLoc(), $1); }
  256. | string_literal
  257. { $$ = global_arena->New<StringLiteral>(context.SourceLoc(), $1); }
  258. | TRUE
  259. { $$ = global_arena->New<BoolLiteral>(context.SourceLoc(), true); }
  260. | FALSE
  261. { $$ = global_arena->New<BoolLiteral>(context.SourceLoc(), false); }
  262. | sized_type_literal
  263. {
  264. int val;
  265. CHECK(llvm::to_integer(llvm::StringRef($1).substr(1), val));
  266. CHECK($1[0] == 'i' && val == 32)
  267. << "Only i32 is supported for now: " << $1;
  268. $$ = global_arena->New<IntTypeLiteral>(context.SourceLoc());
  269. }
  270. | STRING
  271. { $$ = global_arena->New<StringTypeLiteral>(context.SourceLoc()); }
  272. | BOOL
  273. { $$ = global_arena->New<BoolTypeLiteral>(context.SourceLoc()); }
  274. | TYPE
  275. { $$ = global_arena->New<TypeTypeLiteral>(context.SourceLoc()); }
  276. | CONTINUATION_TYPE
  277. { $$ = global_arena->New<ContinuationTypeLiteral>(context.SourceLoc()); }
  278. | paren_expression { $$ = $1; }
  279. | expression EQUAL_EQUAL expression
  280. {
  281. $$ = global_arena->New<PrimitiveOperatorExpression>(
  282. context.SourceLoc(), Operator::Eq,
  283. std::vector<Ptr<const Expression>>({$1, $3}));
  284. }
  285. | expression PLUS expression
  286. {
  287. $$ = global_arena->New<PrimitiveOperatorExpression>(
  288. context.SourceLoc(), Operator::Add,
  289. std::vector<Ptr<const Expression>>({$1, $3}));
  290. }
  291. | expression MINUS expression
  292. {
  293. $$ = global_arena->New<PrimitiveOperatorExpression>(
  294. context.SourceLoc(), Operator::Sub,
  295. std::vector<Ptr<const Expression>>({$1, $3}));
  296. }
  297. | expression BINARY_STAR expression
  298. {
  299. $$ = global_arena->New<PrimitiveOperatorExpression>(
  300. context.SourceLoc(), Operator::Mul,
  301. std::vector<Ptr<const Expression>>({$1, $3}));
  302. }
  303. | expression AND expression
  304. {
  305. $$ = global_arena->New<PrimitiveOperatorExpression>(
  306. context.SourceLoc(), Operator::And,
  307. std::vector<Ptr<const Expression>>({$1, $3}));
  308. }
  309. | expression OR expression
  310. {
  311. $$ = global_arena->New<PrimitiveOperatorExpression>(
  312. context.SourceLoc(), Operator::Or,
  313. std::vector<Ptr<const Expression>>({$1, $3}));
  314. }
  315. | NOT expression
  316. {
  317. $$ = global_arena->New<PrimitiveOperatorExpression>(
  318. context.SourceLoc(), Operator::Not,
  319. std::vector<Ptr<const Expression>>({$2}));
  320. }
  321. | MINUS expression %prec UNARY_MINUS
  322. {
  323. $$ = global_arena->New<PrimitiveOperatorExpression>(
  324. context.SourceLoc(), Operator::Neg,
  325. std::vector<Ptr<const Expression>>({$2}));
  326. }
  327. | PREFIX_STAR expression
  328. {
  329. $$ = global_arena->New<PrimitiveOperatorExpression>(
  330. context.SourceLoc(), Operator::Deref,
  331. std::vector<Ptr<const Expression>>({$2}));
  332. }
  333. | UNARY_STAR expression %prec PREFIX_STAR
  334. {
  335. $$ = global_arena->New<PrimitiveOperatorExpression>(
  336. context.SourceLoc(), Operator::Deref,
  337. std::vector<Ptr<const Expression>>({$2}));
  338. }
  339. | expression tuple
  340. { $$ = global_arena->New<CallExpression>(context.SourceLoc(), $1, $2); }
  341. | expression POSTFIX_STAR
  342. {
  343. $$ = global_arena->New<PrimitiveOperatorExpression>(
  344. context.SourceLoc(), Operator::Ptr,
  345. std::vector<Ptr<const Expression>>({$1}));
  346. }
  347. | expression UNARY_STAR
  348. {
  349. $$ = global_arena->New<PrimitiveOperatorExpression>(
  350. context.SourceLoc(), Operator::Ptr,
  351. std::vector<Ptr<const Expression>>({$1}));
  352. }
  353. | FNTY tuple return_type
  354. {
  355. auto [return_exp, is_omitted_exp] = $3.Release();
  356. $$ = global_arena->New<FunctionTypeLiteral>(context.SourceLoc(), $2,
  357. return_exp, is_omitted_exp);
  358. }
  359. ;
  360. designator: PERIOD identifier { $$ = $2; }
  361. ;
  362. paren_expression: paren_expression_base
  363. { $$ = ExpressionFromParenContents(context.SourceLoc(), $1); }
  364. ;
  365. tuple: paren_expression_base
  366. { $$ = TupleExpressionFromParenContents(context.SourceLoc(), $1); }
  367. ;
  368. paren_expression_element:
  369. expression
  370. { $$ = {.name = std::nullopt, .term = $1}; }
  371. | designator EQUAL expression
  372. { $$ = {.name = $1, .term = $3}; }
  373. ;
  374. paren_expression_base:
  375. LEFT_PARENTHESIS RIGHT_PARENTHESIS
  376. { $$ = {.elements = {}, .has_trailing_comma = false}; }
  377. | LEFT_PARENTHESIS paren_expression_contents RIGHT_PARENTHESIS
  378. { $$ = $2; }
  379. | LEFT_PARENTHESIS paren_expression_contents COMMA RIGHT_PARENTHESIS
  380. {
  381. $$ = $2;
  382. $$.has_trailing_comma = true;
  383. }
  384. ;
  385. paren_expression_contents:
  386. paren_expression_element
  387. { $$ = {.elements = {$1}, .has_trailing_comma = false}; }
  388. | paren_expression_contents COMMA paren_expression_element
  389. {
  390. $$ = $1;
  391. $$.elements.push_back($3);
  392. }
  393. ;
  394. // In many cases, using `pattern` recursively will result in ambiguities.
  395. // When that happens, it's necessary to factor out two separate productions,
  396. // one for when the sub-pattern is an expression, and one for when it is not.
  397. // To facilitate this, non-terminals besides `pattern` whose names contain
  398. // `pattern` are structured to be disjoint from `expression`, unless otherwise
  399. // specified.
  400. pattern:
  401. non_expression_pattern
  402. { $$ = $1; }
  403. | expression
  404. { $$ = global_arena->New<ExpressionPattern>($1); }
  405. ;
  406. non_expression_pattern:
  407. AUTO
  408. { $$ = global_arena->New<AutoPattern>(context.SourceLoc()); }
  409. | binding_lhs COLON pattern
  410. { $$ = global_arena->New<BindingPattern>(context.SourceLoc(), $1, $3); }
  411. | paren_pattern
  412. { $$ = $1; }
  413. | expression tuple_pattern
  414. { $$ = global_arena->New<AlternativePattern>(context.SourceLoc(), $1, $2); }
  415. ;
  416. binding_lhs:
  417. identifier { $$ = $1; }
  418. | UNDERSCORE { $$ = std::nullopt; }
  419. ;
  420. paren_pattern: paren_pattern_base
  421. { $$ = PatternFromParenContents(context.SourceLoc(), $1); }
  422. ;
  423. paren_pattern_base:
  424. LEFT_PARENTHESIS paren_pattern_contents RIGHT_PARENTHESIS
  425. { $$ = $2; }
  426. | LEFT_PARENTHESIS paren_pattern_contents COMMA RIGHT_PARENTHESIS
  427. {
  428. $$ = $2;
  429. $$.has_trailing_comma = true;
  430. }
  431. ;
  432. // paren_pattern is analogous to paren_expression, but in order to avoid
  433. // ambiguities, it must be disjoint from paren_expression, meaning it must
  434. // contain at least one non_expression_pattern. The structure of this rule
  435. // is very different from the corresponding expression rule because is has to
  436. // enforce that requirement.
  437. paren_pattern_contents:
  438. paren_pattern_element
  439. { $$ = {.elements = {$1}, .has_trailing_comma = false}; }
  440. | paren_expression_contents COMMA paren_pattern_element
  441. {
  442. $$ = ParenExpressionToParenPattern($1);
  443. $$.elements.push_back($3);
  444. }
  445. | paren_pattern_contents COMMA paren_expression_element
  446. {
  447. $$ = $1;
  448. auto el = $3.Release();
  449. $$.elements.push_back(
  450. {.name = el.name,
  451. .term = global_arena->New<ExpressionPattern>(el.term)});
  452. }
  453. | paren_pattern_contents COMMA paren_pattern_element
  454. {
  455. $$ = $1;
  456. $$.elements.push_back($3);
  457. }
  458. ;
  459. paren_pattern_element:
  460. non_expression_pattern
  461. { $$ = {.name = std::nullopt, .term = $1}; }
  462. | designator EQUAL non_expression_pattern
  463. { $$ = {.name = $1, .term = $3}; }
  464. ;
  465. tuple_pattern: paren_pattern_base
  466. { $$ = TuplePatternFromParenContents(context.SourceLoc(), $1); }
  467. ;
  468. // Unlike most `pattern` nonterminals, this one overlaps with `expression`,
  469. // so it should be used only when prior context (such as an introducer)
  470. // rules out the possibility of an `expression` at this point.
  471. maybe_empty_tuple_pattern:
  472. LEFT_PARENTHESIS RIGHT_PARENTHESIS
  473. {
  474. $$ = global_arena->New<TuplePattern>(context.SourceLoc(),
  475. std::vector<TuplePattern::Field>());
  476. }
  477. | tuple_pattern
  478. { $$ = $1; }
  479. ;
  480. clause:
  481. CASE pattern DOUBLE_ARROW statement
  482. { $$ = std::pair<Ptr<const Pattern>, Ptr<const Statement>>($2, $4); }
  483. | DEFAULT DOUBLE_ARROW statement
  484. {
  485. auto vp = global_arena -> New<BindingPattern>(
  486. context.SourceLoc(), std::nullopt,
  487. global_arena->New<AutoPattern>(context.SourceLoc()));
  488. $$ = std::pair<Ptr<const Pattern>, Ptr<const Statement>>(vp, $3);
  489. }
  490. ;
  491. clause_list:
  492. // Empty
  493. { $$ = {}; }
  494. | clause_list clause
  495. {
  496. $$ = $1;
  497. $$.push_back($2);
  498. }
  499. ;
  500. statement:
  501. expression EQUAL expression SEMICOLON
  502. { $$ = global_arena->New<Assign>(context.SourceLoc(), $1, $3); }
  503. | VAR pattern EQUAL expression SEMICOLON
  504. { $$ = global_arena->New<VariableDefinition>(context.SourceLoc(), $2, $4); }
  505. | expression SEMICOLON
  506. { $$ = global_arena->New<ExpressionStatement>(context.SourceLoc(), $1); }
  507. | if_statement
  508. { $$ = $1; }
  509. | WHILE LEFT_PARENTHESIS expression RIGHT_PARENTHESIS block
  510. { $$ = global_arena->New<While>(context.SourceLoc(), $3, $5); }
  511. | BREAK SEMICOLON
  512. { $$ = global_arena->New<Break>(context.SourceLoc()); }
  513. | CONTINUE SEMICOLON
  514. { $$ = global_arena->New<Continue>(context.SourceLoc()); }
  515. | RETURN return_expression SEMICOLON
  516. {
  517. auto [return_exp, is_omitted_exp] = $2.Release();
  518. $$ = global_arena->New<Return>(context.SourceLoc(), return_exp,
  519. is_omitted_exp);
  520. }
  521. | block
  522. { $$ = $1; }
  523. | MATCH LEFT_PARENTHESIS expression RIGHT_PARENTHESIS LEFT_CURLY_BRACE
  524. clause_list RIGHT_CURLY_BRACE
  525. { $$ = global_arena->New<Match>(context.SourceLoc(), $3, $6); }
  526. | CONTINUATION identifier statement
  527. { $$ = global_arena->New<Continuation>(context.SourceLoc(), $2, $3); }
  528. | RUN expression SEMICOLON
  529. { $$ = global_arena->New<Run>(context.SourceLoc(), $2); }
  530. | AWAIT SEMICOLON
  531. { $$ = global_arena->New<Await>(context.SourceLoc()); }
  532. ;
  533. if_statement:
  534. IF LEFT_PARENTHESIS expression RIGHT_PARENTHESIS block optional_else
  535. { $$ = global_arena->New<If>(context.SourceLoc(), $3, $5, $6); }
  536. ;
  537. optional_else:
  538. // Empty
  539. { $$ = std::nullopt; }
  540. | ELSE if_statement
  541. { $$ = $2; }
  542. | ELSE block
  543. { $$ = $2; }
  544. ;
  545. return_expression:
  546. // Empty
  547. { $$ = {global_arena->New<TupleLiteral>(context.SourceLoc()), true}; }
  548. | expression
  549. { $$ = {$1, false}; }
  550. ;
  551. statement_list:
  552. // Empty
  553. { $$ = std::nullopt; }
  554. | statement statement_list
  555. { $$ = global_arena->New<Sequence>(context.SourceLoc(), $1, $2); }
  556. ;
  557. block:
  558. LEFT_CURLY_BRACE statement_list RIGHT_CURLY_BRACE
  559. { $$ = global_arena->New<Block>(context.SourceLoc(), $2); }
  560. ;
  561. return_type:
  562. // Empty
  563. { $$ = {global_arena->New<TupleLiteral>(context.SourceLoc()), true}; }
  564. | ARROW expression %prec FNARROW
  565. { $$ = {$2, false}; }
  566. ;
  567. generic_binding:
  568. identifier COLON_BANG expression
  569. { $$ = GenericBinding({.name = std::move($1), .type = $3}); }
  570. ;
  571. deduced_param_list:
  572. // Empty
  573. { $$ = std::vector<GenericBinding>(); }
  574. | generic_binding
  575. {
  576. $$ = std::vector<GenericBinding>();
  577. $$.push_back($1);
  578. }
  579. | generic_binding COMMA deduced_param_list
  580. {
  581. $$ = $3;
  582. $$.push_back($1);
  583. }
  584. ;
  585. deduced_params:
  586. // Empty
  587. { $$ = std::vector<GenericBinding>(); }
  588. | LEFT_SQUARE_BRACKET deduced_param_list RIGHT_SQUARE_BRACKET
  589. { $$ = $2; }
  590. ;
  591. function_definition:
  592. FN identifier deduced_params maybe_empty_tuple_pattern return_type block
  593. {
  594. auto [return_exp, is_omitted_exp] = $5.Release();
  595. $$ = global_arena->New<FunctionDefinition>(
  596. context.SourceLoc(), $2, $3, $4,
  597. global_arena->New<ExpressionPattern>(return_exp), is_omitted_exp, $6);
  598. }
  599. | FN identifier deduced_params maybe_empty_tuple_pattern DOUBLE_ARROW expression
  600. SEMICOLON
  601. {
  602. // The return type is not considered "omitted" because it's automatic from
  603. // the expression.
  604. $$ = global_arena->New<FunctionDefinition>(
  605. context.SourceLoc(), $2, $3, $4,
  606. global_arena->New<AutoPattern>(context.SourceLoc()), true,
  607. global_arena->New<Return>(context.SourceLoc(), $6, true));
  608. }
  609. ;
  610. function_declaration:
  611. FN identifier deduced_params maybe_empty_tuple_pattern return_type SEMICOLON
  612. {
  613. auto [return_exp, is_omitted_exp] = $5.Release();
  614. $$ = global_arena->New<FunctionDefinition>(
  615. context.SourceLoc(), $2, $3, $4,
  616. global_arena->New<ExpressionPattern>(return_exp), is_omitted_exp,
  617. std::nullopt);
  618. }
  619. ;
  620. variable_declaration: identifier COLON pattern
  621. { $$ = global_arena->New<BindingPattern>(context.SourceLoc(), $1, $3); }
  622. ;
  623. member: VAR variable_declaration SEMICOLON
  624. { $$ = global_arena->New<FieldMember>(context.SourceLoc(), $2); }
  625. ;
  626. member_list:
  627. // Empty
  628. { $$ = {}; }
  629. | member_list member
  630. {
  631. $$ = $1;
  632. $$.push_back($2);
  633. }
  634. ;
  635. alternative:
  636. identifier tuple
  637. { $$ = std::pair<std::string, Ptr<const Expression>>($1, $2); }
  638. | identifier
  639. {
  640. $$ = std::pair<std::string, Ptr<const Expression>>(
  641. $1, global_arena->New<TupleLiteral>(context.SourceLoc()));
  642. }
  643. ;
  644. alternative_list:
  645. // Empty
  646. { $$ = {}; }
  647. | alternative_list_contents
  648. { $$ = $1; }
  649. | alternative_list_contents COMMA
  650. { $$ = $1; }
  651. ;
  652. alternative_list_contents:
  653. alternative
  654. { $$ = {$1}; }
  655. | alternative_list_contents COMMA alternative
  656. {
  657. $$ = $1;
  658. $$.push_back($3);
  659. }
  660. ;
  661. declaration:
  662. function_definition
  663. { $$ = global_arena->New<FunctionDeclaration>($1); }
  664. | function_declaration
  665. { $$ = global_arena->New<FunctionDeclaration>($1); }
  666. | CLASS identifier LEFT_CURLY_BRACE member_list RIGHT_CURLY_BRACE
  667. { $$ = global_arena->New<ClassDeclaration>(context.SourceLoc(), $2, $4); }
  668. | CHOICE identifier LEFT_CURLY_BRACE alternative_list RIGHT_CURLY_BRACE
  669. { $$ = global_arena->New<ChoiceDeclaration>(context.SourceLoc(), $2, $4); }
  670. | VAR variable_declaration EQUAL expression SEMICOLON
  671. {
  672. $$ = global_arena->New<VariableDeclaration>(context.SourceLoc(), $2, $4);
  673. }
  674. ;
  675. declaration_list:
  676. // Empty
  677. { $$ = {}; }
  678. | declaration_list declaration
  679. {
  680. $$ = $1;
  681. $$.push_back(Ptr<const Declaration>($2));
  682. }
  683. ;
  684. %%