parser.ypp 27 KB

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