parser.ypp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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. // See README.md#precedence-and-associativity for a description of how
  37. // operator precedence is expressed.
  38. %expect 0
  39. // -----------------------------------------------------------------------------
  40. %code top {
  41. #include <algorithm>
  42. #include <cstdarg>
  43. #include <cstdio>
  44. #include <cstdlib>
  45. #include <vector>
  46. #include "common/check.h"
  47. #include "executable_semantics/syntax/parse_and_lex_context.h"
  48. #include "llvm/ADT/StringExtras.h"
  49. } // %code top
  50. %code requires {
  51. #include <optional>
  52. #include "executable_semantics/ast/ast.h"
  53. #include "executable_semantics/ast/declaration.h"
  54. #include "executable_semantics/ast/expression.h"
  55. #include "executable_semantics/ast/paren_contents.h"
  56. #include "executable_semantics/ast/pattern.h"
  57. #include "executable_semantics/common/arena.h"
  58. #include "executable_semantics/common/nonnull.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> intrinsic_identifier
  73. %token <std::string> sized_type_literal
  74. %token <std::string> string_literal
  75. %type <std::string> designator
  76. %type <ImplKind> impl_kind
  77. %type <std::pair<LibraryName, bool>> package_directive
  78. %type <LibraryName> import_directive
  79. %type <std::vector<LibraryName>> import_directives
  80. %type <std::string> optional_library_path
  81. %type <bool> api_or_impl
  82. %type <Nonnull<Declaration*>> declaration
  83. %type <Nonnull<FunctionDeclaration*>> function_declaration
  84. %type <std::vector<Nonnull<Declaration*>>> declaration_list
  85. %type <Nonnull<Statement*>> statement
  86. %type <Nonnull<If*>> if_statement
  87. %type <std::optional<Nonnull<Block*>>> optional_else
  88. %type <std::pair<Nonnull<Expression*>, bool>> return_expression
  89. %type <Nonnull<Block*>> nonempty_block
  90. %type <Nonnull<Block*>> block
  91. %type <std::vector<Nonnull<Statement*>>> statement_list
  92. %type <Nonnull<Expression*>> primary_expression
  93. %type <Nonnull<Expression*>> postfix_expression
  94. %type <Nonnull<Expression*>> ref_deref_expression
  95. %type <Nonnull<Expression*>> type_expression
  96. %type <Nonnull<Expression*>> fn_type_expression
  97. %type <Nonnull<Expression*>> minus_expression
  98. %type <Nonnull<Expression*>> multiplicative_operand
  99. %type <Nonnull<Expression*>> multiplicative_lhs
  100. %type <Nonnull<Expression*>> multiplicative_expression
  101. %type <Nonnull<Expression*>> additive_operand
  102. %type <Nonnull<Expression*>> additive_lhs
  103. %type <Nonnull<Expression*>> additive_expression
  104. %type <Nonnull<Expression*>> unimpl_expression
  105. %type <Nonnull<Expression*>> value_expression
  106. %type <Nonnull<Expression*>> comparison_operand
  107. %type <Nonnull<Expression*>> comparison_expression
  108. %type <Nonnull<Expression*>> not_expression
  109. %type <Nonnull<Expression*>> predicate_expression
  110. %type <Nonnull<Expression*>> and_or_operand
  111. %type <Nonnull<Expression*>> and_lhs
  112. %type <Nonnull<Expression*>> and_expression
  113. %type <Nonnull<Expression*>> or_lhs
  114. %type <Nonnull<Expression*>> or_expression
  115. %type <Nonnull<Expression*>> statement_expression
  116. %type <Nonnull<Expression*>> if_expression
  117. %type <Nonnull<Expression*>> expression
  118. %type <Nonnull<GenericBinding*>> generic_binding
  119. %type <std::vector<Nonnull<AstNode*>>> deduced_params
  120. %type <std::vector<Nonnull<AstNode*>>> deduced_param_list
  121. %type <Nonnull<Pattern*>> pattern
  122. %type <Nonnull<Pattern*>> non_expression_pattern
  123. %type <BisonWrap<ReturnTerm>> return_term
  124. %type <Nonnull<Expression*>> paren_expression
  125. %type <Nonnull<StructLiteral*>> struct_literal
  126. %type <std::vector<FieldInitializer>> struct_literal_contents
  127. %type <Nonnull<StructTypeLiteral*>> struct_type_literal
  128. %type <std::vector<FieldInitializer>> struct_type_literal_contents
  129. %type <Nonnull<TupleLiteral*>> tuple
  130. %type <std::string> binding_lhs
  131. %type <std::optional<Nonnull<BindingPattern*>>> receiver
  132. %type <Nonnull<BindingPattern*>> variable_declaration
  133. %type <ParenContents<Expression>> paren_expression_base
  134. %type <ParenContents<Expression>> paren_expression_contents
  135. %type <Nonnull<Pattern*>> paren_pattern
  136. %type <Nonnull<TuplePattern*>> tuple_pattern
  137. %type <Nonnull<TuplePattern*>> maybe_empty_tuple_pattern
  138. %type <ParenContents<Pattern>> paren_pattern_base
  139. %type <ParenContents<Pattern>> paren_pattern_contents
  140. %type <Nonnull<AlternativeSignature*>> alternative
  141. %type <std::vector<Nonnull<AlternativeSignature*>>> alternative_list
  142. %type <std::vector<Nonnull<AlternativeSignature*>>> alternative_list_contents
  143. %type <BisonWrap<Match::Clause>> clause
  144. %type <std::vector<Match::Clause>> clause_list
  145. %token
  146. // Most tokens have their spelling defined in lexer.lpp.
  147. // table-begin
  148. AMPERSAND
  149. AND
  150. API
  151. ARROW
  152. AS
  153. AUTO
  154. AWAIT
  155. BOOL
  156. BREAK
  157. CASE
  158. CHOICE
  159. CLASS
  160. COLON
  161. COLON_BANG
  162. COMMA
  163. CONTINUATION
  164. CONTINUATION_TYPE
  165. CONTINUE
  166. DEFAULT
  167. DOUBLE_ARROW
  168. ELSE
  169. EQUAL
  170. EQUAL_EQUAL
  171. EXTERNAL
  172. FALSE
  173. FN
  174. FN_TYPE
  175. IF
  176. IMPL
  177. IMPORT
  178. INTERFACE
  179. LEFT_CURLY_BRACE
  180. LEFT_PARENTHESIS
  181. LEFT_SQUARE_BRACKET
  182. LIBRARY
  183. MATCH
  184. MINUS
  185. NOT
  186. OR
  187. PACKAGE
  188. PERIOD
  189. PLUS
  190. RETURN
  191. RIGHT_CURLY_BRACE
  192. RIGHT_PARENTHESIS
  193. RIGHT_SQUARE_BRACKET
  194. RUN
  195. SEMICOLON
  196. SLASH
  197. STRING
  198. THEN
  199. TRUE
  200. TYPE
  201. UNDERSCORE
  202. UNIMPL_EXAMPLE
  203. VAR
  204. WHILE
  205. // table-end
  206. // Used to track EOF.
  207. END_OF_FILE 0
  208. // Only used for precedence.
  209. FNARROW "-> in return type"
  210. // The lexer determines the arity and fixity of each `*` based on whitespace
  211. // and adjacent tokens. UNARY_STAR indicates that the operator is unary but
  212. // could be either prefix or postfix.
  213. UNARY_STAR "unary *"
  214. PREFIX_STAR "prefix *"
  215. POSTFIX_STAR "postfix *"
  216. BINARY_STAR "binary *"
  217. ;
  218. %start input
  219. %%
  220. input: package_directive import_directives declaration_list
  221. {
  222. *ast = AST({.package = $1.first,
  223. .is_api = $1.second,
  224. .imports = std::move($2),
  225. .declarations = std::move($3)});
  226. }
  227. ;
  228. package_directive:
  229. PACKAGE identifier optional_library_path api_or_impl SEMICOLON
  230. { $$ = {LibraryName({.package = $2, .path = $3}), $4}; }
  231. ;
  232. import_directive:
  233. IMPORT identifier optional_library_path SEMICOLON
  234. { $$ = LibraryName({.package = $2, .path = $3}); }
  235. ;
  236. import_directives:
  237. // Empty
  238. { $$ = std::vector<LibraryName>(); }
  239. | import_directives import_directive
  240. {
  241. $$ = std::move($1);
  242. $$.push_back($2);
  243. }
  244. ;
  245. optional_library_path:
  246. // Empty
  247. { $$ = ""; }
  248. | LIBRARY string_literal
  249. { $$ = $2; }
  250. ;
  251. api_or_impl:
  252. API
  253. { $$ = true; }
  254. | IMPL
  255. { $$ = false; }
  256. ;
  257. primary_expression:
  258. identifier
  259. { $$ = arena->New<IdentifierExpression>(context.source_loc(), $1); }
  260. | integer_literal
  261. { $$ = arena->New<IntLiteral>(context.source_loc(), $1); }
  262. | string_literal
  263. { $$ = arena->New<StringLiteral>(context.source_loc(), $1); }
  264. | TRUE
  265. { $$ = arena->New<BoolLiteral>(context.source_loc(), true); }
  266. | FALSE
  267. { $$ = arena->New<BoolLiteral>(context.source_loc(), false); }
  268. | sized_type_literal
  269. {
  270. int val;
  271. CHECK(llvm::to_integer(llvm::StringRef($1).substr(1), val));
  272. CHECK($1[0] == 'i' && val == 32)
  273. << "Only i32 is supported for now: " << $1;
  274. $$ = arena->New<IntTypeLiteral>(context.source_loc());
  275. }
  276. | STRING
  277. { $$ = arena->New<StringTypeLiteral>(context.source_loc()); }
  278. | BOOL
  279. { $$ = arena->New<BoolTypeLiteral>(context.source_loc()); }
  280. | TYPE
  281. { $$ = arena->New<TypeTypeLiteral>(context.source_loc()); }
  282. | CONTINUATION_TYPE
  283. { $$ = arena->New<ContinuationTypeLiteral>(context.source_loc()); }
  284. | paren_expression { $$ = $1; }
  285. | struct_literal { $$ = $1; }
  286. | struct_type_literal { $$ = $1; }
  287. ;
  288. postfix_expression:
  289. primary_expression
  290. | postfix_expression designator
  291. { $$ = arena->New<FieldAccessExpression>(context.source_loc(), $1, $2); }
  292. | postfix_expression LEFT_SQUARE_BRACKET expression RIGHT_SQUARE_BRACKET
  293. { $$ = arena->New<IndexExpression>(context.source_loc(), $1, $3); }
  294. | intrinsic_identifier tuple
  295. { $$ = arena->New<IntrinsicExpression>($1, $2, context.source_loc()); }
  296. | postfix_expression tuple
  297. { $$ = arena->New<CallExpression>(context.source_loc(), $1, $2); }
  298. | postfix_expression POSTFIX_STAR
  299. {
  300. $$ = arena->New<PrimitiveOperatorExpression>(
  301. context.source_loc(), Operator::Ptr,
  302. std::vector<Nonnull<Expression*>>({$1}));
  303. }
  304. | postfix_expression UNARY_STAR
  305. {
  306. $$ = arena->New<PrimitiveOperatorExpression>(
  307. context.source_loc(), Operator::Ptr,
  308. std::vector<Nonnull<Expression*>>({$1}));
  309. }
  310. ;
  311. ref_deref_expression:
  312. postfix_expression
  313. | PREFIX_STAR ref_deref_expression
  314. {
  315. $$ = arena->New<PrimitiveOperatorExpression>(
  316. context.source_loc(), Operator::Deref,
  317. std::vector<Nonnull<Expression*>>({$2}));
  318. }
  319. | UNARY_STAR ref_deref_expression
  320. {
  321. $$ = arena->New<PrimitiveOperatorExpression>(
  322. context.source_loc(), Operator::Deref,
  323. std::vector<Nonnull<Expression*>>({$2}));
  324. }
  325. | AMPERSAND ref_deref_expression
  326. {
  327. $$ = arena->New<PrimitiveOperatorExpression>(
  328. context.source_loc(), Operator::AddressOf,
  329. std::vector<Nonnull<Expression*>>({$2}));
  330. }
  331. ;
  332. fn_type_expression:
  333. FN_TYPE tuple ARROW type_expression
  334. { $$ = arena->New<FunctionTypeLiteral>(context.source_loc(), $2, $4); }
  335. ;
  336. type_expression:
  337. ref_deref_expression
  338. | fn_type_expression
  339. ;
  340. minus_expression:
  341. // ref_deref_expression excluded due to precedence diamond.
  342. MINUS ref_deref_expression
  343. {
  344. $$ = arena->New<PrimitiveOperatorExpression>(
  345. context.source_loc(), Operator::Neg,
  346. std::vector<Nonnull<Expression*>>({$2}));
  347. }
  348. ;
  349. multiplicative_operand:
  350. ref_deref_expression
  351. | minus_expression
  352. ;
  353. multiplicative_lhs:
  354. ref_deref_expression
  355. | multiplicative_expression
  356. ;
  357. multiplicative_expression:
  358. minus_expression
  359. | multiplicative_lhs BINARY_STAR multiplicative_operand
  360. {
  361. $$ = arena->New<PrimitiveOperatorExpression>(
  362. context.source_loc(), Operator::Mul,
  363. std::vector<Nonnull<Expression*>>({$1, $3}));
  364. }
  365. ;
  366. additive_operand:
  367. ref_deref_expression
  368. | multiplicative_expression
  369. ;
  370. additive_lhs:
  371. ref_deref_expression
  372. | additive_expression
  373. ;
  374. additive_expression:
  375. multiplicative_expression
  376. | additive_lhs PLUS additive_operand
  377. {
  378. $$ = arena->New<PrimitiveOperatorExpression>(
  379. context.source_loc(), Operator::Add,
  380. std::vector<Nonnull<Expression*>>({$1, $3}));
  381. }
  382. | additive_lhs MINUS additive_operand
  383. {
  384. $$ = arena->New<PrimitiveOperatorExpression>(
  385. context.source_loc(), Operator::Sub,
  386. std::vector<Nonnull<Expression*>>({$1, $3}));
  387. }
  388. ;
  389. unimpl_expression:
  390. // ref_deref_expression excluded due to precedence diamond.
  391. ref_deref_expression UNIMPL_EXAMPLE ref_deref_expression
  392. {
  393. $$ = arena->New<UnimplementedExpression>(context.source_loc(),
  394. "ExampleInfix", $1, $3);
  395. }
  396. ;
  397. value_expression:
  398. // ref_deref_expression excluded due to precedence diamond.
  399. additive_expression
  400. | fn_type_expression
  401. | unimpl_expression
  402. ;
  403. comparison_operand:
  404. ref_deref_expression
  405. | value_expression
  406. ;
  407. comparison_expression:
  408. value_expression
  409. | comparison_operand EQUAL_EQUAL comparison_operand
  410. {
  411. $$ = arena->New<PrimitiveOperatorExpression>(
  412. context.source_loc(), Operator::Eq,
  413. std::vector<Nonnull<Expression*>>({$1, $3}));
  414. }
  415. ;
  416. not_expression:
  417. NOT ref_deref_expression
  418. {
  419. $$ = arena->New<PrimitiveOperatorExpression>(
  420. context.source_loc(), Operator::Not,
  421. std::vector<Nonnull<Expression*>>({$2}));
  422. }
  423. ;
  424. predicate_expression:
  425. // ref_deref_expression excluded due to precedence diamond.
  426. not_expression
  427. | comparison_expression
  428. ;
  429. and_or_operand:
  430. ref_deref_expression
  431. | predicate_expression
  432. ;
  433. and_lhs:
  434. and_or_operand
  435. | and_expression
  436. ;
  437. and_expression:
  438. // predicate_expression excluded due to precedence diamond.
  439. and_lhs AND and_or_operand
  440. {
  441. $$ = arena->New<PrimitiveOperatorExpression>(
  442. context.source_loc(), Operator::And,
  443. std::vector<Nonnull<Expression*>>({$1, $3}));
  444. }
  445. ;
  446. or_lhs:
  447. and_or_operand
  448. | or_expression
  449. ;
  450. or_expression:
  451. // predicate_expression excluded due to precedence diamond.
  452. or_lhs OR and_or_operand
  453. {
  454. $$ = arena->New<PrimitiveOperatorExpression>(
  455. context.source_loc(), Operator::Or,
  456. std::vector<Nonnull<Expression*>>({$1, $3}));
  457. }
  458. ;
  459. statement_expression:
  460. ref_deref_expression
  461. | predicate_expression
  462. | and_expression
  463. | or_expression
  464. ;
  465. if_expression:
  466. statement_expression
  467. | IF expression THEN if_expression ELSE if_expression
  468. { $$ = arena->New<IfExpression>(context.source_loc(), $2, $4, $6); }
  469. ;
  470. expression:
  471. if_expression
  472. ;
  473. designator: PERIOD identifier { $$ = $2; }
  474. ;
  475. paren_expression: paren_expression_base
  476. { $$ = ExpressionFromParenContents(arena, context.source_loc(), $1); }
  477. ;
  478. tuple: paren_expression_base
  479. { $$ = TupleExpressionFromParenContents(arena, context.source_loc(), $1); }
  480. ;
  481. paren_expression_base:
  482. LEFT_PARENTHESIS RIGHT_PARENTHESIS
  483. { $$ = {.elements = {}, .has_trailing_comma = false}; }
  484. | LEFT_PARENTHESIS paren_expression_contents RIGHT_PARENTHESIS
  485. { $$ = $2; }
  486. | LEFT_PARENTHESIS paren_expression_contents COMMA RIGHT_PARENTHESIS
  487. {
  488. $$ = $2;
  489. $$.has_trailing_comma = true;
  490. }
  491. ;
  492. paren_expression_contents:
  493. expression
  494. { $$ = {.elements = {$1}, .has_trailing_comma = false}; }
  495. | paren_expression_contents COMMA expression
  496. {
  497. $$ = $1;
  498. $$.elements.push_back($3);
  499. }
  500. ;
  501. struct_literal:
  502. LEFT_CURLY_BRACE struct_literal_contents RIGHT_CURLY_BRACE
  503. { $$ = arena->New<StructLiteral>(context.source_loc(), $2); }
  504. | LEFT_CURLY_BRACE struct_literal_contents COMMA RIGHT_CURLY_BRACE
  505. { $$ = arena->New<StructLiteral>(context.source_loc(), $2); }
  506. ;
  507. struct_literal_contents:
  508. designator EQUAL expression
  509. { $$ = {FieldInitializer($1, $3)}; }
  510. | struct_literal_contents COMMA designator EQUAL expression
  511. {
  512. $$ = $1;
  513. $$.push_back(FieldInitializer($3, $5));
  514. }
  515. ;
  516. struct_type_literal:
  517. LEFT_CURLY_BRACE RIGHT_CURLY_BRACE
  518. { $$ = arena->New<StructTypeLiteral>(context.source_loc()); }
  519. | LEFT_CURLY_BRACE struct_type_literal_contents RIGHT_CURLY_BRACE
  520. { $$ = arena->New<StructTypeLiteral>(context.source_loc(), $2); }
  521. | LEFT_CURLY_BRACE struct_type_literal_contents COMMA RIGHT_CURLY_BRACE
  522. { $$ = arena->New<StructTypeLiteral>(context.source_loc(), $2); }
  523. ;
  524. struct_type_literal_contents:
  525. designator COLON expression
  526. { $$ = {FieldInitializer($1, $3)}; }
  527. | struct_type_literal_contents COMMA designator COLON expression
  528. {
  529. $$ = $1;
  530. $$.push_back(FieldInitializer($3, $5));
  531. }
  532. ;
  533. // In many cases, using `pattern` recursively will result in ambiguities.
  534. // When that happens, it's necessary to factor out two separate productions,
  535. // one for when the sub-pattern is an expression, and one for when it is not.
  536. // To facilitate this, non-terminals besides `pattern` whose names contain
  537. // `pattern` are structured to be disjoint from `expression`, unless otherwise
  538. // specified.
  539. pattern:
  540. non_expression_pattern
  541. { $$ = $1; }
  542. | expression
  543. { $$ = arena->New<ExpressionPattern>($1); }
  544. ;
  545. non_expression_pattern:
  546. AUTO
  547. { $$ = arena->New<AutoPattern>(context.source_loc()); }
  548. | binding_lhs COLON pattern
  549. { $$ = arena->New<BindingPattern>(context.source_loc(), $1, $3); }
  550. | paren_pattern
  551. { $$ = $1; }
  552. | postfix_expression tuple_pattern
  553. { $$ = arena->New<AlternativePattern>(context.source_loc(), $1, $2); }
  554. ;
  555. binding_lhs:
  556. identifier { $$ = $1; }
  557. | UNDERSCORE { $$ = AnonymousName; }
  558. ;
  559. paren_pattern: paren_pattern_base
  560. { $$ = PatternFromParenContents(arena, context.source_loc(), $1); }
  561. ;
  562. paren_pattern_base:
  563. LEFT_PARENTHESIS paren_pattern_contents RIGHT_PARENTHESIS
  564. { $$ = $2; }
  565. | LEFT_PARENTHESIS paren_pattern_contents COMMA RIGHT_PARENTHESIS
  566. {
  567. $$ = $2;
  568. $$.has_trailing_comma = true;
  569. }
  570. ;
  571. // paren_pattern is analogous to paren_expression, but in order to avoid
  572. // ambiguities, it must be disjoint from paren_expression, meaning it must
  573. // contain at least one non_expression_pattern. The structure of this rule
  574. // is very different from the corresponding expression rule because is has to
  575. // enforce that requirement.
  576. paren_pattern_contents:
  577. non_expression_pattern
  578. { $$ = {.elements = {$1}, .has_trailing_comma = false}; }
  579. | paren_expression_contents COMMA non_expression_pattern
  580. {
  581. $$ = ParenExpressionToParenPattern(arena, $1);
  582. $$.elements.push_back($3);
  583. }
  584. | paren_pattern_contents COMMA expression
  585. {
  586. $$ = $1;
  587. $$.elements.push_back(arena->New<ExpressionPattern>($3));
  588. }
  589. | paren_pattern_contents COMMA non_expression_pattern
  590. {
  591. $$ = $1;
  592. $$.elements.push_back($3);
  593. }
  594. ;
  595. tuple_pattern: paren_pattern_base
  596. { $$ = TuplePatternFromParenContents(arena, context.source_loc(), $1); }
  597. ;
  598. // Unlike most `pattern` nonterminals, this one overlaps with `expression`,
  599. // so it should be used only when prior context (such as an introducer)
  600. // rules out the possibility of an `expression` at this point.
  601. maybe_empty_tuple_pattern:
  602. LEFT_PARENTHESIS RIGHT_PARENTHESIS
  603. {
  604. $$ = arena->New<TuplePattern>(context.source_loc(),
  605. std::vector<Nonnull<Pattern*>>());
  606. }
  607. | tuple_pattern
  608. { $$ = $1; }
  609. ;
  610. clause:
  611. CASE pattern DOUBLE_ARROW statement
  612. { $$ = Match::Clause($2, $4); }
  613. | DEFAULT DOUBLE_ARROW statement
  614. {
  615. $$ = Match::Clause(arena->New<BindingPattern>(
  616. context.source_loc(), std::string(AnonymousName),
  617. arena->New<AutoPattern>(context.source_loc())),
  618. $3);
  619. }
  620. ;
  621. clause_list:
  622. // Empty
  623. { $$ = {}; }
  624. | clause_list clause
  625. {
  626. $$ = $1;
  627. $$.push_back($2);
  628. }
  629. ;
  630. statement:
  631. statement_expression EQUAL expression SEMICOLON
  632. { $$ = arena->New<Assign>(context.source_loc(), $1, $3); }
  633. | VAR pattern EQUAL expression SEMICOLON
  634. { $$ = arena->New<VariableDefinition>(context.source_loc(), $2, $4); }
  635. | statement_expression SEMICOLON
  636. { $$ = arena->New<ExpressionStatement>(context.source_loc(), $1); }
  637. | if_statement
  638. { $$ = $1; }
  639. | WHILE LEFT_PARENTHESIS expression RIGHT_PARENTHESIS block
  640. { $$ = arena->New<While>(context.source_loc(), $3, $5); }
  641. | BREAK SEMICOLON
  642. { $$ = arena->New<Break>(context.source_loc()); }
  643. | CONTINUE SEMICOLON
  644. { $$ = arena->New<Continue>(context.source_loc()); }
  645. | RETURN return_expression SEMICOLON
  646. {
  647. auto [return_exp, is_omitted_exp] = $2;
  648. $$ = arena->New<Return>(context.source_loc(), return_exp, is_omitted_exp);
  649. }
  650. // We disallow empty blocks in places where an arbitrary statement can occur
  651. // in order to avoid ambiguity with the empty struct literal `{}`. We can
  652. // allow non-empty blocks because a non-empty struct literal always starts with
  653. // a designator, and a block never does, so one token of lookahead suffices
  654. // to disambiguate. As of this writing, the "official" resolution of this
  655. // ambiguity is an open question (see
  656. // https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/classes.md#literals)
  657. | nonempty_block
  658. { $$ = $1; }
  659. | MATCH LEFT_PARENTHESIS expression RIGHT_PARENTHESIS LEFT_CURLY_BRACE
  660. clause_list RIGHT_CURLY_BRACE
  661. { $$ = arena->New<Match>(context.source_loc(), $3, $6); }
  662. | CONTINUATION identifier block
  663. { $$ = arena->New<Continuation>(context.source_loc(), $2, $3); }
  664. | RUN expression SEMICOLON
  665. { $$ = arena->New<Run>(context.source_loc(), $2); }
  666. | AWAIT SEMICOLON
  667. { $$ = arena->New<Await>(context.source_loc()); }
  668. ;
  669. if_statement:
  670. IF LEFT_PARENTHESIS expression RIGHT_PARENTHESIS block optional_else
  671. { $$ = arena->New<If>(context.source_loc(), $3, $5, $6); }
  672. ;
  673. optional_else:
  674. // Empty
  675. { $$ = std::nullopt; }
  676. | ELSE if_statement
  677. {
  678. $$ = arena->New<Block>(context.source_loc(),
  679. std::vector<Nonnull<Statement*>>({$2}));
  680. }
  681. | ELSE block
  682. { $$ = $2; }
  683. ;
  684. return_expression:
  685. // Empty
  686. { $$ = {arena->New<TupleLiteral>(context.source_loc()), true}; }
  687. | expression
  688. { $$ = {$1, false}; }
  689. ;
  690. statement_list:
  691. // Empty
  692. { $$ = {}; }
  693. | statement_list statement
  694. {
  695. $$ = std::move($1);
  696. $$.push_back($2);
  697. }
  698. ;
  699. block:
  700. LEFT_CURLY_BRACE statement_list RIGHT_CURLY_BRACE
  701. { $$ = arena->New<Block>(context.source_loc(), std::move($2)); }
  702. ;
  703. nonempty_block:
  704. LEFT_CURLY_BRACE statement_list statement RIGHT_CURLY_BRACE
  705. {
  706. $2.push_back($3);
  707. $$ = arena->New<Block>(context.source_loc(), std::move($2));
  708. }
  709. ;
  710. return_term:
  711. // Empty
  712. { $$ = ReturnTerm::Omitted(context.source_loc()); }
  713. | ARROW AUTO
  714. { $$ = ReturnTerm::Auto(context.source_loc()); }
  715. | ARROW expression
  716. { $$ = ReturnTerm::Explicit($2); }
  717. ;
  718. generic_binding:
  719. identifier COLON_BANG expression
  720. {
  721. $$ = arena->New<GenericBinding>(context.source_loc(), std::move($1), $3);
  722. }
  723. ;
  724. deduced_param_list:
  725. // Empty
  726. { $$ = std::vector<Nonnull<AstNode*>>(); }
  727. | generic_binding
  728. {
  729. $$ = std::vector<Nonnull<AstNode*>>();
  730. $$.push_back($1);
  731. }
  732. | generic_binding COMMA deduced_param_list
  733. {
  734. $$ = $3;
  735. $$.push_back($1);
  736. }
  737. | variable_declaration
  738. {
  739. $$ = std::vector<Nonnull<AstNode*>>();
  740. $$.push_back($1);
  741. }
  742. | variable_declaration COMMA deduced_param_list
  743. {
  744. $$ = $3;
  745. $$.push_back($1);
  746. }
  747. ;
  748. deduced_params:
  749. // Empty
  750. { $$ = std::vector<Nonnull<AstNode*>>(); }
  751. | LEFT_SQUARE_BRACKET deduced_param_list RIGHT_SQUARE_BRACKET
  752. { $$ = $2; }
  753. ;
  754. receiver:
  755. // Empty
  756. { $$ = std::nullopt; }
  757. | LEFT_CURLY_BRACE variable_declaration RIGHT_CURLY_BRACE
  758. { $$ = $2; }
  759. ;
  760. function_declaration:
  761. FN identifier deduced_params receiver maybe_empty_tuple_pattern return_term block
  762. {
  763. $$ = arena->New<FunctionDeclaration>(context.source_loc(), $2, $3, $4, $5,
  764. $6, $7);
  765. }
  766. | FN identifier deduced_params receiver maybe_empty_tuple_pattern return_term SEMICOLON
  767. {
  768. $$ = arena->New<FunctionDeclaration>(context.source_loc(), $2, $3, $4, $5,
  769. $6, std::nullopt);
  770. }
  771. ;
  772. variable_declaration: identifier COLON pattern
  773. { $$ = arena->New<BindingPattern>(context.source_loc(), $1, $3); }
  774. ;
  775. alternative:
  776. identifier tuple
  777. { $$ = arena->New<AlternativeSignature>(context.source_loc(), $1, $2); }
  778. | identifier
  779. {
  780. $$ = arena->New<AlternativeSignature>(
  781. context.source_loc(), $1,
  782. arena->New<TupleLiteral>(context.source_loc()));
  783. }
  784. ;
  785. alternative_list:
  786. // Empty
  787. { $$ = {}; }
  788. | alternative_list_contents
  789. { $$ = $1; }
  790. | alternative_list_contents COMMA
  791. { $$ = $1; }
  792. ;
  793. alternative_list_contents:
  794. alternative
  795. { $$ = {std::move($1)}; }
  796. | alternative_list_contents COMMA alternative
  797. {
  798. $$ = $1;
  799. $$.push_back(std::move($3));
  800. }
  801. ;
  802. declaration:
  803. function_declaration
  804. { $$ = $1; }
  805. | CLASS identifier LEFT_CURLY_BRACE declaration_list RIGHT_CURLY_BRACE
  806. { $$ = arena->New<ClassDeclaration>(context.source_loc(), $2, $4); }
  807. | CHOICE identifier LEFT_CURLY_BRACE alternative_list RIGHT_CURLY_BRACE
  808. { $$ = arena->New<ChoiceDeclaration>(context.source_loc(), $2, $4); }
  809. | VAR variable_declaration SEMICOLON
  810. {
  811. $$ = arena->New<VariableDeclaration>(context.source_loc(), $2,
  812. std::nullopt);
  813. }
  814. | VAR variable_declaration EQUAL expression SEMICOLON
  815. { $$ = arena->New<VariableDeclaration>(context.source_loc(), $2, $4); }
  816. | INTERFACE identifier LEFT_CURLY_BRACE declaration_list RIGHT_CURLY_BRACE
  817. {
  818. auto ty_ty = arena -> New<TypeTypeLiteral>(context.source_loc());
  819. auto self =
  820. arena -> New<GenericBinding>(context.source_loc(), "Self", ty_ty);
  821. $$ = arena->New<InterfaceDeclaration>(context.source_loc(), $2, self, $4);
  822. }
  823. | impl_kind IMPL expression AS expression LEFT_CURLY_BRACE declaration_list RIGHT_CURLY_BRACE
  824. { $$ = arena->New<ImplDeclaration>(context.source_loc(), $1, $3, $5, $7); }
  825. ;
  826. impl_kind:
  827. // Internal
  828. { $$ = Carbon::ImplKind::InternalImpl; }
  829. | EXTERNAL
  830. { $$ = Carbon::ImplKind::ExternalImpl; }
  831. ;
  832. declaration_list:
  833. // Empty
  834. { $$ = {}; }
  835. | declaration_list declaration
  836. {
  837. $$ = $1;
  838. $$.push_back(Nonnull<Declaration*>($2));
  839. }
  840. ;
  841. %%