parser.ypp 26 KB

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