parser.ypp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  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 "explorer/syntax/parse_and_lex_context.h"
  47. #include "llvm/ADT/StringExtras.h"
  48. #include "llvm/Support/FormatVariadic.h"
  49. #include "llvm/Support/raw_ostream.h"
  50. } // %code top
  51. %code requires {
  52. #include <optional>
  53. #include "explorer/ast/ast.h"
  54. #include "explorer/ast/declaration.h"
  55. #include "explorer/ast/expression.h"
  56. #include "explorer/ast/paren_contents.h"
  57. #include "explorer/ast/pattern.h"
  58. #include "explorer/ast/value_category.h"
  59. #include "explorer/common/arena.h"
  60. #include "explorer/common/nonnull.h"
  61. #include "explorer/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 <Nonnull<Expression*>> impl_type
  80. %type <std::pair<LibraryName, bool>> package_directive
  81. %type <LibraryName> import_directive
  82. %type <std::vector<LibraryName>> import_directives
  83. %type <std::string> optional_library_path
  84. %type <bool> api_or_impl
  85. %type <ClassExtensibility> class_declaration_extensibility
  86. %type <std::optional<Nonnull<Expression*>>> class_declaration_extends
  87. %type <Nonnull<Declaration*>> declaration
  88. %type <Nonnull<FunctionDeclaration*>> function_declaration
  89. %type <Nonnull<DestructorDeclaration*>> destructor_declaration;
  90. %type <Nonnull<MixDeclaration*>> mix_declaration
  91. %type <Nonnull<AliasDeclaration*>> alias_declaration
  92. %type <std::vector<Nonnull<Declaration*>>> declaration_list
  93. %type <std::vector<Nonnull<Declaration*>>> class_body
  94. %type <std::vector<Nonnull<Declaration*>>> mixin_body
  95. %type <std::vector<Nonnull<Declaration*>>> interface_body
  96. %type <std::vector<Nonnull<Declaration*>>> impl_body
  97. %type <Nonnull<Statement*>> statement
  98. %type <Nonnull<If*>> if_statement
  99. %type <std::optional<Nonnull<Block*>>> optional_else
  100. %type <std::pair<Nonnull<Expression*>, bool>> return_expression
  101. %type <Nonnull<Block*>> block
  102. %type <std::vector<Nonnull<Statement*>>> statement_list
  103. %type <Nonnull<Expression*>> primary_expression
  104. %type <Nonnull<Expression*>> postfix_expression
  105. %type <Nonnull<Expression*>> ref_deref_expression
  106. %type <Nonnull<Expression*>> type_expression
  107. %type <Nonnull<Expression*>> fn_type_expression
  108. %type <Nonnull<Expression*>> minus_expression
  109. %type <Nonnull<Expression*>> complement_expression
  110. %type <Nonnull<Expression*>> unary_expression
  111. %type <Nonnull<Expression*>> simple_binary_operand
  112. %type <Nonnull<Expression*>> multiplicative_lhs
  113. %type <Nonnull<Expression*>> multiplicative_expression
  114. %type <Nonnull<Expression*>> additive_operand
  115. %type <Nonnull<Expression*>> additive_lhs
  116. %type <Nonnull<Expression*>> additive_expression
  117. %type <Nonnull<Expression*>> modulo_expression
  118. %type <Nonnull<Expression*>> bitwise_and_lhs
  119. %type <Nonnull<Expression*>> bitwise_and_expression
  120. %type <Nonnull<Expression*>> bitwise_or_lhs
  121. %type <Nonnull<Expression*>> bitwise_or_expression
  122. %type <Nonnull<Expression*>> bitwise_xor_lhs
  123. %type <Nonnull<Expression*>> bitwise_xor_expression
  124. %type <Nonnull<Expression*>> bitwise_expression
  125. %type <Nonnull<Expression*>> bit_shift_expression
  126. %type <Nonnull<Expression*>> as_expression
  127. %type <Nonnull<Expression*>> unimpl_expression
  128. %type <Nonnull<Expression*>> value_expression
  129. %type <Nonnull<Expression*>> comparison_operand
  130. %type <Nonnull<Expression*>> comparison_expression
  131. %type <Nonnull<Expression*>> not_expression
  132. %type <Nonnull<Expression*>> predicate_expression
  133. %type <Nonnull<Expression*>> and_or_operand
  134. %type <Nonnull<Expression*>> and_lhs
  135. %type <Nonnull<Expression*>> and_expression
  136. %type <Nonnull<Expression*>> or_lhs
  137. %type <Nonnull<Expression*>> or_expression
  138. %type <Nonnull<WhereClause*>> where_clause
  139. %type <std::vector<Nonnull<WhereClause*>>> where_clause_list
  140. %type <Nonnull<Expression*>> where_expression
  141. %type <Nonnull<Expression*>> type_or_where_expression
  142. %type <Nonnull<Expression*>> statement_expression
  143. %type <Nonnull<Expression*>> if_expression
  144. %type <Nonnull<Expression*>> expression
  145. %type <Nonnull<Expression*>> mixin_import
  146. %type <Nonnull<GenericBinding*>> generic_binding
  147. %type <Nonnull<Pattern*>> deduced_param
  148. %type <std::vector<Nonnull<AstNode*>>> deduced_params
  149. %type <std::vector<Nonnull<AstNode*>>> impl_deduced_params
  150. %type <std::vector<Nonnull<AstNode*>>> deduced_param_list
  151. %type <Nonnull<Pattern*>> pattern
  152. %type <Nonnull<Pattern*>> non_expression_pattern
  153. %type <BisonWrap<ReturnTerm>> return_term
  154. %type <Nonnull<Expression*>> paren_expression
  155. %type <Nonnull<StructLiteral*>> struct_literal
  156. %type <std::vector<FieldInitializer>> struct_literal_contents
  157. %type <Nonnull<StructTypeLiteral*>> struct_type_literal
  158. %type <std::vector<FieldInitializer>> struct_type_literal_contents
  159. %type <Nonnull<TupleLiteral*>> tuple
  160. %type <std::string> binding_lhs
  161. %type <std::optional<Nonnull<Pattern*>>> receiver
  162. %type <Nonnull<BindingPattern*>> variable_declaration
  163. %type <ParenContents<Expression>> paren_expression_base
  164. %type <ParenContents<Expression>> paren_expression_contents
  165. %type <Nonnull<Pattern*>> paren_pattern
  166. %type <Nonnull<TuplePattern*>> tuple_pattern
  167. %type <Nonnull<TuplePattern*>> maybe_empty_tuple_pattern
  168. %type <std::optional<Nonnull<TuplePattern*>>> type_params
  169. %type <ParenContents<Pattern>> paren_pattern_base
  170. %type <ParenContents<Pattern>> paren_pattern_contents
  171. %type <Nonnull<AlternativeSignature*>> alternative
  172. %type <std::vector<Nonnull<AlternativeSignature*>>> alternative_list
  173. %type <std::vector<Nonnull<AlternativeSignature*>>> alternative_list_contents
  174. %type <BisonWrap<Match::Clause>> clause
  175. %type <std::vector<Match::Clause>> clause_list
  176. %type <Operator> comparison_operator;
  177. %token
  178. // Most tokens have their spelling defined in lexer.lpp.
  179. // table-begin
  180. ABSTRACT
  181. ADDR
  182. ALIAS
  183. AMPERSAND
  184. AND
  185. API
  186. ARROW
  187. AS
  188. AUTO
  189. AWAIT
  190. BASE
  191. BOOL
  192. BREAK
  193. CARET
  194. CASE
  195. CHOICE
  196. CLASS
  197. COLON
  198. COLON_BANG
  199. COMMA
  200. CONTINUATION
  201. CONTINUATION_TYPE
  202. CONTINUE
  203. DEFAULT
  204. DESTRUCTOR
  205. DOUBLE_ARROW
  206. ELSE
  207. EQUAL
  208. EQUAL_EQUAL
  209. EXTENDS
  210. EXTERNAL
  211. FALSE
  212. FN
  213. FN_TYPE
  214. FOR
  215. FORALL
  216. GREATER
  217. GREATER_EQUAL
  218. GREATER_GREATER
  219. IF
  220. IMPL
  221. IMPORT
  222. IN
  223. INTERFACE
  224. IS
  225. LEFT_CURLY_BRACE
  226. LEFT_PARENTHESIS
  227. LEFT_SQUARE_BRACKET
  228. LESS
  229. LESS_EQUAL
  230. LESS_LESS
  231. LET
  232. LIBRARY
  233. MATCH
  234. MINUS
  235. MIX
  236. MIXIN
  237. NOT
  238. NOT_EQUAL
  239. OR
  240. PACKAGE
  241. PERCENT
  242. PERIOD
  243. PIPE
  244. PLUS
  245. RETURN
  246. RETURNED
  247. RIGHT_CURLY_BRACE
  248. RIGHT_PARENTHESIS
  249. RIGHT_SQUARE_BRACKET
  250. RUN
  251. SELF
  252. SEMICOLON
  253. SLASH
  254. STRING
  255. THEN
  256. TRUE
  257. TYPE
  258. UNDERSCORE
  259. UNIMPL_EXAMPLE
  260. VAR
  261. WHERE
  262. WHILE
  263. // table-end
  264. // Used to track EOF.
  265. END_OF_FILE 0
  266. // Only used for precedence.
  267. FNARROW "-> in return type"
  268. // The lexer determines the arity and fixity of each `*` based on whitespace
  269. // and adjacent tokens. UNARY_STAR indicates that the operator is unary but
  270. // could be either prefix or postfix.
  271. UNARY_STAR "unary *"
  272. PREFIX_STAR "prefix *"
  273. POSTFIX_STAR "postfix *"
  274. BINARY_STAR "binary *"
  275. ;
  276. %start input
  277. %%
  278. input: package_directive import_directives declaration_list
  279. {
  280. *ast = AST({.package = $1.first,
  281. .is_api = $1.second,
  282. .imports = std::move($2),
  283. .declarations = std::move($3)});
  284. }
  285. ;
  286. package_directive:
  287. PACKAGE identifier optional_library_path api_or_impl SEMICOLON
  288. { $$ = {LibraryName({.package = $2, .path = $3}), $4}; }
  289. ;
  290. import_directive:
  291. IMPORT identifier optional_library_path SEMICOLON
  292. { $$ = LibraryName({.package = $2, .path = $3}); }
  293. ;
  294. import_directives:
  295. // Empty
  296. { $$ = std::vector<LibraryName>(); }
  297. | import_directives import_directive
  298. {
  299. $$ = std::move($1);
  300. $$.push_back($2);
  301. }
  302. ;
  303. optional_library_path:
  304. // Empty
  305. { $$ = ""; }
  306. | LIBRARY string_literal
  307. { $$ = $2; }
  308. ;
  309. api_or_impl:
  310. API
  311. { $$ = true; }
  312. | IMPL
  313. { $$ = false; }
  314. ;
  315. primary_expression:
  316. identifier
  317. { $$ = arena->New<IdentifierExpression>(context.source_loc(), $1); }
  318. | designator
  319. {
  320. // `.Foo` is rewritten to `.Self.Foo`.
  321. $$ = arena->New<SimpleMemberAccessExpression>(
  322. context.source_loc(),
  323. arena->New<DotSelfExpression>(context.source_loc()), $1);
  324. }
  325. | PERIOD SELF
  326. { $$ = arena->New<DotSelfExpression>(context.source_loc()); }
  327. | integer_literal
  328. { $$ = arena->New<IntLiteral>(context.source_loc(), $1); }
  329. | string_literal
  330. { $$ = arena->New<StringLiteral>(context.source_loc(), $1); }
  331. | TRUE
  332. { $$ = arena->New<BoolLiteral>(context.source_loc(), true); }
  333. | FALSE
  334. { $$ = arena->New<BoolLiteral>(context.source_loc(), false); }
  335. | sized_type_literal
  336. {
  337. int val = 0;
  338. if (!llvm::to_integer(llvm::StringRef($1).substr(1), val)) {
  339. context.RecordSyntaxError(
  340. llvm::formatv("Invalid type literal: {0}", $1));
  341. YYERROR;
  342. } else if ($1[0] != 'i' || val != 32) {
  343. context.RecordSyntaxError(
  344. llvm::formatv("Only i32 is supported for now: {0}", $1));
  345. YYERROR;
  346. } else {
  347. $$ = arena->New<IntTypeLiteral>(context.source_loc());
  348. }
  349. }
  350. | SELF
  351. // TODO: Should we create a new TypeLiteral for `Self`?
  352. { $$ = arena->New<IdentifierExpression>(context.source_loc(), "Self"); }
  353. | STRING
  354. { $$ = arena->New<StringTypeLiteral>(context.source_loc()); }
  355. | BOOL
  356. { $$ = arena->New<BoolTypeLiteral>(context.source_loc()); }
  357. | TYPE
  358. { $$ = arena->New<TypeTypeLiteral>(context.source_loc()); }
  359. | CONTINUATION_TYPE
  360. { $$ = arena->New<ContinuationTypeLiteral>(context.source_loc()); }
  361. | paren_expression { $$ = $1; }
  362. | struct_literal { $$ = $1; }
  363. | struct_type_literal { $$ = $1; }
  364. | LEFT_SQUARE_BRACKET expression SEMICOLON expression RIGHT_SQUARE_BRACKET
  365. { $$ = arena->New<ArrayTypeLiteral>(context.source_loc(), $2, $4); }
  366. ;
  367. postfix_expression:
  368. primary_expression
  369. | postfix_expression designator
  370. {
  371. $$ = arena->New<SimpleMemberAccessExpression>(context.source_loc(), $1,
  372. $2);
  373. }
  374. | postfix_expression PERIOD LEFT_PARENTHESIS expression RIGHT_PARENTHESIS
  375. {
  376. $$ = arena->New<CompoundMemberAccessExpression>(context.source_loc(), $1,
  377. $4);
  378. }
  379. | postfix_expression LEFT_SQUARE_BRACKET expression RIGHT_SQUARE_BRACKET
  380. { $$ = arena->New<IndexExpression>(context.source_loc(), $1, $3); }
  381. | intrinsic_identifier tuple
  382. { $$ = arena->New<IntrinsicExpression>($1, $2, context.source_loc()); }
  383. | postfix_expression tuple
  384. { $$ = arena->New<CallExpression>(context.source_loc(), $1, $2); }
  385. | postfix_expression POSTFIX_STAR
  386. {
  387. $$ = arena->New<OperatorExpression>(
  388. context.source_loc(), Operator::Ptr,
  389. std::vector<Nonnull<Expression*>>({$1}));
  390. }
  391. | postfix_expression UNARY_STAR
  392. {
  393. $$ = arena->New<OperatorExpression>(
  394. context.source_loc(), Operator::Ptr,
  395. std::vector<Nonnull<Expression*>>({$1}));
  396. }
  397. ;
  398. ref_deref_expression:
  399. postfix_expression
  400. | PREFIX_STAR ref_deref_expression
  401. {
  402. $$ = arena->New<OperatorExpression>(
  403. context.source_loc(), Operator::Deref,
  404. std::vector<Nonnull<Expression*>>({$2}));
  405. }
  406. | UNARY_STAR ref_deref_expression
  407. {
  408. $$ = arena->New<OperatorExpression>(
  409. context.source_loc(), Operator::Deref,
  410. std::vector<Nonnull<Expression*>>({$2}));
  411. }
  412. | AMPERSAND ref_deref_expression
  413. {
  414. $$ = arena->New<OperatorExpression>(
  415. context.source_loc(), Operator::AddressOf,
  416. std::vector<Nonnull<Expression*>>({$2}));
  417. }
  418. ;
  419. fn_type_expression:
  420. FN_TYPE tuple ARROW type_expression
  421. { $$ = arena->New<FunctionTypeLiteral>(context.source_loc(), $2, $4); }
  422. ;
  423. type_expression:
  424. ref_deref_expression
  425. | bitwise_and_expression
  426. | fn_type_expression
  427. ;
  428. minus_expression:
  429. // ref_deref_expression excluded due to precedence diamond.
  430. MINUS ref_deref_expression
  431. {
  432. $$ = arena->New<OperatorExpression>(
  433. context.source_loc(), Operator::Neg,
  434. std::vector<Nonnull<Expression*>>({$2}));
  435. }
  436. ;
  437. complement_expression:
  438. // ref_deref_expression excluded due to precedence diamond.
  439. CARET ref_deref_expression
  440. {
  441. $$ = arena->New<OperatorExpression>(
  442. context.source_loc(), Operator::Complement,
  443. std::vector<Nonnull<Expression*>>({$2}));
  444. }
  445. ;
  446. unary_expression:
  447. // ref_deref_expression excluded due to precedence diamond.
  448. minus_expression
  449. | complement_expression
  450. ;
  451. // A simple_binary_operand is an operand of a binary operator
  452. // that is not itself a binary operator expression.
  453. simple_binary_operand:
  454. ref_deref_expression
  455. | unary_expression
  456. ;
  457. multiplicative_lhs:
  458. simple_binary_operand
  459. | multiplicative_expression
  460. ;
  461. multiplicative_expression:
  462. multiplicative_lhs BINARY_STAR simple_binary_operand
  463. {
  464. $$ = arena->New<OperatorExpression>(
  465. context.source_loc(), Operator::Mul,
  466. std::vector<Nonnull<Expression*>>({$1, $3}));
  467. }
  468. | multiplicative_lhs SLASH simple_binary_operand
  469. {
  470. $$ = arena->New<OperatorExpression>(
  471. context.source_loc(), Operator::Div,
  472. std::vector<Nonnull<Expression*>>({$1, $3}));
  473. }
  474. ;
  475. additive_operand:
  476. simple_binary_operand
  477. | multiplicative_expression
  478. ;
  479. additive_lhs:
  480. simple_binary_operand
  481. | additive_expression
  482. ;
  483. additive_expression:
  484. multiplicative_expression
  485. | additive_lhs PLUS additive_operand
  486. {
  487. $$ = arena->New<OperatorExpression>(
  488. context.source_loc(), Operator::Add,
  489. std::vector<Nonnull<Expression*>>({$1, $3}));
  490. }
  491. | additive_lhs MINUS additive_operand
  492. {
  493. $$ = arena->New<OperatorExpression>(
  494. context.source_loc(), Operator::Sub,
  495. std::vector<Nonnull<Expression*>>({$1, $3}));
  496. }
  497. ;
  498. modulo_expression:
  499. simple_binary_operand PERCENT simple_binary_operand
  500. {
  501. $$ = arena->New<OperatorExpression>(
  502. context.source_loc(), Operator::Mod,
  503. std::vector<Nonnull<Expression*>>({$1, $3}));
  504. }
  505. ;
  506. bitwise_and_lhs:
  507. simple_binary_operand
  508. | bitwise_and_expression
  509. ;
  510. bitwise_and_expression:
  511. bitwise_and_lhs AMPERSAND simple_binary_operand
  512. {
  513. $$ = arena->New<OperatorExpression>(
  514. context.source_loc(), Operator::BitwiseAnd,
  515. std::vector<Nonnull<Expression*>>({$1, $3}));
  516. }
  517. ;
  518. bitwise_or_lhs:
  519. simple_binary_operand
  520. | bitwise_or_expression
  521. ;
  522. bitwise_or_expression:
  523. bitwise_or_lhs PIPE simple_binary_operand
  524. {
  525. $$ = arena->New<OperatorExpression>(
  526. context.source_loc(), Operator::BitwiseOr,
  527. std::vector<Nonnull<Expression*>>({$1, $3}));
  528. }
  529. ;
  530. bitwise_xor_lhs:
  531. simple_binary_operand
  532. | bitwise_xor_expression
  533. ;
  534. bitwise_xor_expression:
  535. bitwise_xor_lhs CARET simple_binary_operand
  536. {
  537. $$ = arena->New<OperatorExpression>(
  538. context.source_loc(), Operator::BitwiseXor,
  539. std::vector<Nonnull<Expression*>>({$1, $3}));
  540. }
  541. ;
  542. bitwise_expression:
  543. bitwise_and_expression
  544. | bitwise_or_expression
  545. | bitwise_xor_expression
  546. ;
  547. bit_shift_expression:
  548. simple_binary_operand LESS_LESS simple_binary_operand
  549. {
  550. $$ = arena->New<OperatorExpression>(
  551. context.source_loc(), Operator::BitShiftLeft,
  552. std::vector<Nonnull<Expression*>>({$1, $3}));
  553. }
  554. | simple_binary_operand GREATER_GREATER simple_binary_operand
  555. {
  556. $$ = arena->New<OperatorExpression>(
  557. context.source_loc(), Operator::BitShiftRight,
  558. std::vector<Nonnull<Expression*>>({$1, $3}));
  559. }
  560. ;
  561. as_expression:
  562. simple_binary_operand AS simple_binary_operand
  563. {
  564. $$ = arena->New<OperatorExpression>(
  565. context.source_loc(), Operator::As,
  566. std::vector<Nonnull<Expression*>>{$1, $3});
  567. }
  568. ;
  569. unimpl_expression:
  570. // ref_deref_expression excluded due to precedence diamond.
  571. ref_deref_expression UNIMPL_EXAMPLE ref_deref_expression
  572. {
  573. $$ = arena->New<UnimplementedExpression>(context.source_loc(),
  574. "ExampleInfix", $1, $3);
  575. }
  576. ;
  577. value_expression:
  578. // ref_deref_expression excluded due to precedence diamond.
  579. additive_expression
  580. | as_expression
  581. | bitwise_expression
  582. | bit_shift_expression
  583. | fn_type_expression
  584. | modulo_expression
  585. | unary_expression
  586. | unimpl_expression
  587. ;
  588. comparison_operand:
  589. ref_deref_expression
  590. | value_expression
  591. ;
  592. comparison_operator:
  593. EQUAL_EQUAL
  594. { $$ = Operator::Eq; }
  595. | LESS
  596. { $$ = Operator::Less; }
  597. | LESS_EQUAL
  598. { $$ = Operator::LessEq; }
  599. | GREATER
  600. { $$ = Operator::Greater; }
  601. | GREATER_EQUAL
  602. { $$ = Operator::GreaterEq; }
  603. | NOT_EQUAL
  604. { $$ = Operator::NotEq; }
  605. ;
  606. comparison_expression:
  607. value_expression
  608. | comparison_operand comparison_operator comparison_operand
  609. {
  610. $$ = arena->New<OperatorExpression>(
  611. context.source_loc(), $2,
  612. std::vector<Nonnull<Expression*>>({$1, $3}));
  613. }
  614. ;
  615. not_expression:
  616. NOT ref_deref_expression
  617. {
  618. $$ = arena->New<OperatorExpression>(
  619. context.source_loc(), Operator::Not,
  620. std::vector<Nonnull<Expression*>>({$2}));
  621. }
  622. ;
  623. predicate_expression:
  624. // ref_deref_expression excluded due to precedence diamond.
  625. not_expression
  626. | comparison_expression
  627. ;
  628. and_or_operand:
  629. ref_deref_expression
  630. | predicate_expression
  631. ;
  632. and_lhs:
  633. and_or_operand
  634. | and_expression
  635. ;
  636. and_expression:
  637. // predicate_expression excluded due to precedence diamond.
  638. and_lhs AND and_or_operand
  639. {
  640. $$ = arena->New<OperatorExpression>(
  641. context.source_loc(), Operator::And,
  642. std::vector<Nonnull<Expression*>>({$1, $3}));
  643. }
  644. ;
  645. or_lhs:
  646. and_or_operand
  647. | or_expression
  648. ;
  649. or_expression:
  650. // predicate_expression excluded due to precedence diamond.
  651. or_lhs OR and_or_operand
  652. {
  653. $$ = arena->New<OperatorExpression>(
  654. context.source_loc(), Operator::Or,
  655. std::vector<Nonnull<Expression*>>({$1, $3}));
  656. }
  657. ;
  658. where_clause:
  659. comparison_operand IS comparison_operand
  660. { $$ = arena->New<IsWhereClause>(context.source_loc(), $1, $3); }
  661. | comparison_operand EQUAL_EQUAL comparison_operand
  662. { $$ = arena->New<EqualsWhereClause>(context.source_loc(), $1, $3); }
  663. ;
  664. where_clause_list:
  665. where_clause
  666. { $$ = {$1}; }
  667. | where_clause_list AND where_clause
  668. {
  669. $$ = std::move($1);
  670. $$.push_back($3);
  671. }
  672. ;
  673. where_expression:
  674. type_expression WHERE where_clause_list
  675. {
  676. auto* self =
  677. arena -> New<GenericBinding>(context.source_loc(), ".Self", $1);
  678. $$ = arena->New<WhereExpression>(context.source_loc(), self, $3);
  679. }
  680. ;
  681. type_or_where_expression:
  682. type_expression
  683. | where_expression
  684. ;
  685. statement_expression:
  686. ref_deref_expression
  687. | predicate_expression
  688. | and_expression
  689. | or_expression
  690. | where_expression
  691. ;
  692. if_expression:
  693. statement_expression
  694. | IF expression THEN if_expression ELSE if_expression
  695. { $$ = arena->New<IfExpression>(context.source_loc(), $2, $4, $6); }
  696. ;
  697. expression:
  698. if_expression
  699. ;
  700. designator: PERIOD identifier { $$ = $2; }
  701. ;
  702. paren_expression: paren_expression_base
  703. { $$ = ExpressionFromParenContents(arena, context.source_loc(), $1); }
  704. ;
  705. tuple: paren_expression_base
  706. { $$ = TupleExpressionFromParenContents(arena, context.source_loc(), $1); }
  707. ;
  708. paren_expression_base:
  709. LEFT_PARENTHESIS RIGHT_PARENTHESIS
  710. { $$ = {.elements = {}, .has_trailing_comma = false}; }
  711. | LEFT_PARENTHESIS paren_expression_contents RIGHT_PARENTHESIS
  712. { $$ = $2; }
  713. | LEFT_PARENTHESIS paren_expression_contents COMMA RIGHT_PARENTHESIS
  714. {
  715. $$ = $2;
  716. $$.has_trailing_comma = true;
  717. }
  718. ;
  719. paren_expression_contents:
  720. expression
  721. { $$ = {.elements = {$1}, .has_trailing_comma = false}; }
  722. | paren_expression_contents COMMA expression
  723. {
  724. $$ = $1;
  725. $$.elements.push_back($3);
  726. }
  727. ;
  728. struct_literal:
  729. LEFT_CURLY_BRACE struct_literal_contents RIGHT_CURLY_BRACE
  730. { $$ = arena->New<StructLiteral>(context.source_loc(), $2); }
  731. | LEFT_CURLY_BRACE struct_literal_contents COMMA RIGHT_CURLY_BRACE
  732. { $$ = arena->New<StructLiteral>(context.source_loc(), $2); }
  733. ;
  734. struct_literal_contents:
  735. designator EQUAL expression
  736. { $$ = {FieldInitializer($1, $3)}; }
  737. | struct_literal_contents COMMA designator EQUAL expression
  738. {
  739. $$ = $1;
  740. $$.push_back(FieldInitializer($3, $5));
  741. }
  742. ;
  743. struct_type_literal:
  744. LEFT_CURLY_BRACE RIGHT_CURLY_BRACE
  745. { $$ = arena->New<StructTypeLiteral>(context.source_loc()); }
  746. | LEFT_CURLY_BRACE struct_type_literal_contents RIGHT_CURLY_BRACE
  747. { $$ = arena->New<StructTypeLiteral>(context.source_loc(), $2); }
  748. | LEFT_CURLY_BRACE struct_type_literal_contents COMMA RIGHT_CURLY_BRACE
  749. { $$ = arena->New<StructTypeLiteral>(context.source_loc(), $2); }
  750. ;
  751. struct_type_literal_contents:
  752. designator COLON expression
  753. { $$ = {FieldInitializer($1, $3)}; }
  754. | struct_type_literal_contents COMMA designator COLON expression
  755. {
  756. $$ = $1;
  757. $$.push_back(FieldInitializer($3, $5));
  758. }
  759. ;
  760. // In many cases, using `pattern` recursively will result in ambiguities.
  761. // When that happens, it's necessary to factor out two separate productions,
  762. // one for when the sub-pattern is an expression, and one for when it is not.
  763. // To facilitate this, non-terminals besides `pattern` whose names contain
  764. // `pattern` are structured to be disjoint from `expression`, unless otherwise
  765. // specified.
  766. pattern:
  767. non_expression_pattern
  768. { $$ = $1; }
  769. | expression
  770. { $$ = arena->New<ExpressionPattern>($1); }
  771. ;
  772. non_expression_pattern:
  773. AUTO
  774. { $$ = arena->New<AutoPattern>(context.source_loc()); }
  775. | binding_lhs COLON pattern
  776. {
  777. $$ = arena->New<BindingPattern>(context.source_loc(), $1, $3,
  778. std::nullopt);
  779. }
  780. | binding_lhs COLON_BANG expression
  781. { $$ = arena->New<GenericBinding>(context.source_loc(), $1, $3); }
  782. | paren_pattern
  783. { $$ = $1; }
  784. | postfix_expression tuple_pattern
  785. {
  786. ErrorOr<Nonnull<AlternativePattern*>> alternative_pattern =
  787. AlternativePattern::Create(arena, context.source_loc(), $1, $2);
  788. if (alternative_pattern.ok()) {
  789. $$ = *alternative_pattern;
  790. } else {
  791. context.RecordSyntaxError(std::move(alternative_pattern).error());
  792. YYERROR;
  793. }
  794. }
  795. | VAR non_expression_pattern
  796. { $$ = arena->New<VarPattern>(context.source_loc(), $2); }
  797. ;
  798. binding_lhs:
  799. identifier { $$ = $1; }
  800. | UNDERSCORE { $$ = AnonymousName; }
  801. ;
  802. paren_pattern: paren_pattern_base
  803. { $$ = PatternFromParenContents(arena, context.source_loc(), $1); }
  804. ;
  805. paren_pattern_base:
  806. LEFT_PARENTHESIS paren_pattern_contents RIGHT_PARENTHESIS
  807. { $$ = $2; }
  808. | LEFT_PARENTHESIS paren_pattern_contents COMMA RIGHT_PARENTHESIS
  809. {
  810. $$ = $2;
  811. $$.has_trailing_comma = true;
  812. }
  813. ;
  814. // paren_pattern is analogous to paren_expression, but in order to avoid
  815. // ambiguities, it must be disjoint from paren_expression, meaning it must
  816. // contain at least one non_expression_pattern. The structure of this rule
  817. // is very different from the corresponding expression rule because is has to
  818. // enforce that requirement.
  819. paren_pattern_contents:
  820. non_expression_pattern
  821. { $$ = {.elements = {$1}, .has_trailing_comma = false}; }
  822. | paren_expression_contents COMMA non_expression_pattern
  823. {
  824. $$ = ParenExpressionToParenPattern(arena, $1);
  825. $$.elements.push_back($3);
  826. }
  827. | paren_pattern_contents COMMA expression
  828. {
  829. $$ = $1;
  830. $$.elements.push_back(arena->New<ExpressionPattern>($3));
  831. }
  832. | paren_pattern_contents COMMA non_expression_pattern
  833. {
  834. $$ = $1;
  835. $$.elements.push_back($3);
  836. }
  837. ;
  838. tuple_pattern: paren_pattern_base
  839. { $$ = TuplePatternFromParenContents(arena, context.source_loc(), $1); }
  840. ;
  841. // Unlike most `pattern` nonterminals, this one overlaps with `expression`,
  842. // so it should be used only when prior context (such as an introducer)
  843. // rules out the possibility of an `expression` at this point.
  844. maybe_empty_tuple_pattern:
  845. LEFT_PARENTHESIS RIGHT_PARENTHESIS
  846. {
  847. $$ = arena->New<TuplePattern>(context.source_loc(),
  848. std::vector<Nonnull<Pattern*>>());
  849. }
  850. | tuple_pattern
  851. { $$ = $1; }
  852. ;
  853. clause:
  854. CASE pattern DOUBLE_ARROW block
  855. { $$ = Match::Clause($2, $4); }
  856. | DEFAULT DOUBLE_ARROW block
  857. {
  858. $$ = Match::Clause(arena->New<BindingPattern>(
  859. context.source_loc(), std::string(AnonymousName),
  860. arena->New<AutoPattern>(context.source_loc()),
  861. ValueCategory::Let),
  862. $3);
  863. }
  864. ;
  865. clause_list:
  866. // Empty
  867. { $$ = {}; }
  868. | clause_list clause
  869. {
  870. $$ = $1;
  871. $$.push_back($2);
  872. }
  873. ;
  874. statement:
  875. statement_expression EQUAL expression SEMICOLON
  876. { $$ = arena->New<Assign>(context.source_loc(), $1, $3); }
  877. | VAR pattern SEMICOLON
  878. {
  879. $$ = arena->New<VariableDefinition>(
  880. context.source_loc(), $2, std::nullopt, ValueCategory::Var,
  881. VariableDefinition::DefinitionType::Var);
  882. }
  883. | VAR pattern EQUAL expression SEMICOLON
  884. {
  885. $$ = arena->New<VariableDefinition>(
  886. context.source_loc(), $2, $4, ValueCategory::Var,
  887. VariableDefinition::DefinitionType::Var);
  888. }
  889. | RETURNED VAR variable_declaration SEMICOLON
  890. {
  891. $$ = arena->New<VariableDefinition>(
  892. context.source_loc(), $3, std::nullopt, ValueCategory::Var,
  893. VariableDefinition::DefinitionType::Returned);
  894. }
  895. | RETURNED VAR variable_declaration EQUAL expression SEMICOLON
  896. {
  897. $$ = arena->New<VariableDefinition>(
  898. context.source_loc(), $3, $5, ValueCategory::Var,
  899. VariableDefinition::DefinitionType::Returned);
  900. }
  901. | LET pattern EQUAL expression SEMICOLON
  902. {
  903. $$ = arena->New<VariableDefinition>(
  904. context.source_loc(), $2, $4, ValueCategory::Let,
  905. VariableDefinition::DefinitionType::Var);
  906. }
  907. | statement_expression SEMICOLON
  908. { $$ = arena->New<ExpressionStatement>(context.source_loc(), $1); }
  909. | if_statement
  910. { $$ = $1; }
  911. | WHILE LEFT_PARENTHESIS expression RIGHT_PARENTHESIS block
  912. { $$ = arena->New<While>(context.source_loc(), $3, $5); }
  913. | BREAK SEMICOLON
  914. { $$ = arena->New<Break>(context.source_loc()); }
  915. | CONTINUE SEMICOLON
  916. { $$ = arena->New<Continue>(context.source_loc()); }
  917. | RETURN return_expression SEMICOLON
  918. {
  919. auto [return_exp, is_omitted_exp] = $2;
  920. $$ = arena->New<ReturnExpression>(context.source_loc(), return_exp,
  921. is_omitted_exp);
  922. }
  923. | RETURN VAR SEMICOLON
  924. { $$ = arena->New<ReturnVar>(context.source_loc()); }
  925. | MATCH LEFT_PARENTHESIS expression RIGHT_PARENTHESIS LEFT_CURLY_BRACE
  926. clause_list RIGHT_CURLY_BRACE
  927. { $$ = arena->New<Match>(context.source_loc(), $3, $6); }
  928. | CONTINUATION identifier block
  929. { $$ = arena->New<Continuation>(context.source_loc(), $2, $3); }
  930. | RUN expression SEMICOLON
  931. { $$ = arena->New<Run>(context.source_loc(), $2); }
  932. | AWAIT SEMICOLON
  933. { $$ = arena->New<Await>(context.source_loc()); }
  934. | FOR LEFT_PARENTHESIS variable_declaration IN type_expression RIGHT_PARENTHESIS block
  935. { $$ = arena->New<For>(context.source_loc(), $3, $5, $7); }
  936. ;
  937. if_statement:
  938. IF LEFT_PARENTHESIS expression RIGHT_PARENTHESIS block optional_else
  939. { $$ = arena->New<If>(context.source_loc(), $3, $5, $6); }
  940. ;
  941. optional_else:
  942. // Empty
  943. { $$ = std::nullopt; }
  944. | ELSE if_statement
  945. {
  946. $$ = arena->New<Block>(context.source_loc(),
  947. std::vector<Nonnull<Statement*>>({$2}));
  948. }
  949. | ELSE block
  950. { $$ = $2; }
  951. ;
  952. return_expression:
  953. // Empty
  954. { $$ = {arena->New<TupleLiteral>(context.source_loc()), true}; }
  955. | expression
  956. { $$ = {$1, false}; }
  957. ;
  958. statement_list:
  959. // Empty
  960. { $$ = {}; }
  961. | statement_list statement
  962. {
  963. $$ = std::move($1);
  964. $$.push_back($2);
  965. }
  966. ;
  967. block:
  968. LEFT_CURLY_BRACE statement_list RIGHT_CURLY_BRACE
  969. { $$ = arena->New<Block>(context.source_loc(), std::move($2)); }
  970. ;
  971. return_term:
  972. // Empty
  973. { $$ = ReturnTerm::Omitted(context.source_loc()); }
  974. | ARROW AUTO
  975. { $$ = ReturnTerm::Auto(context.source_loc()); }
  976. | ARROW expression
  977. { $$ = ReturnTerm::Explicit($2); }
  978. ;
  979. generic_binding:
  980. identifier COLON_BANG expression
  981. {
  982. $$ = arena->New<GenericBinding>(context.source_loc(), std::move($1), $3);
  983. }
  984. ;
  985. deduced_param:
  986. generic_binding
  987. { $$ = $1; }
  988. | variable_declaration
  989. { $$ = $1; }
  990. | ADDR variable_declaration
  991. { $$ = arena->New<AddrPattern>(context.source_loc(), $2); }
  992. ;
  993. deduced_param_list:
  994. // Empty
  995. { $$ = {}; }
  996. | deduced_param
  997. { $$ = {$1}; }
  998. | deduced_param_list COMMA deduced_param
  999. {
  1000. $$ = $1; // TODO: can we std::move here?
  1001. $$.push_back($3);
  1002. }
  1003. ;
  1004. deduced_params:
  1005. // Empty
  1006. { $$ = std::vector<Nonnull<AstNode*>>(); }
  1007. | LEFT_SQUARE_BRACKET deduced_param_list RIGHT_SQUARE_BRACKET
  1008. { $$ = $2; }
  1009. ;
  1010. impl_deduced_params:
  1011. // Empty
  1012. { $$ = std::vector<Nonnull<AstNode*>>(); }
  1013. | FORALL LEFT_SQUARE_BRACKET deduced_param_list RIGHT_SQUARE_BRACKET
  1014. { $$ = $3; }
  1015. ;
  1016. receiver:
  1017. // Empty
  1018. { $$ = std::nullopt; }
  1019. | LEFT_CURLY_BRACE variable_declaration RIGHT_CURLY_BRACE
  1020. { $$ = $2; }
  1021. | LEFT_CURLY_BRACE ADDR variable_declaration RIGHT_CURLY_BRACE
  1022. { $$ = arena->New<AddrPattern>(context.source_loc(), $3); }
  1023. ;
  1024. function_declaration:
  1025. FN identifier deduced_params receiver maybe_empty_tuple_pattern return_term block
  1026. {
  1027. ErrorOr<FunctionDeclaration*> fn = FunctionDeclaration::Create(
  1028. arena, context.source_loc(), $2, $3, $4, $5, $6, $7);
  1029. if (fn.ok()) {
  1030. $$ = *fn;
  1031. } else {
  1032. context.RecordSyntaxError(std::move(fn).error());
  1033. YYERROR;
  1034. }
  1035. }
  1036. | FN identifier deduced_params receiver maybe_empty_tuple_pattern return_term SEMICOLON
  1037. {
  1038. ErrorOr<FunctionDeclaration*> fn = FunctionDeclaration::Create(
  1039. arena, context.source_loc(), $2, $3, $4, $5, $6, std::nullopt);
  1040. if (fn.ok()) {
  1041. $$ = *fn;
  1042. } else {
  1043. context.RecordSyntaxError(std::move(fn).error());
  1044. YYERROR;
  1045. }
  1046. }
  1047. ;
  1048. variable_declaration: identifier COLON pattern
  1049. {
  1050. $$ = arena->New<BindingPattern>(context.source_loc(), $1, $3,
  1051. std::nullopt);
  1052. }
  1053. ;
  1054. alias_declaration: ALIAS identifier EQUAL expression SEMICOLON
  1055. { $$ = arena->New<AliasDeclaration>(context.source_loc(), $2, $4); }
  1056. ;
  1057. // EXPERIMENTAL MIXIN FEATURE
  1058. mix_declaration: MIX expression SEMICOLON
  1059. { $$ = arena->New<MixDeclaration>(context.source_loc(), $2); }
  1060. ;
  1061. alternative:
  1062. identifier tuple
  1063. { $$ = arena->New<AlternativeSignature>(context.source_loc(), $1, $2); }
  1064. | identifier
  1065. {
  1066. $$ = arena->New<AlternativeSignature>(
  1067. context.source_loc(), $1,
  1068. arena->New<TupleLiteral>(context.source_loc()));
  1069. }
  1070. ;
  1071. alternative_list:
  1072. // Empty
  1073. { $$ = {}; }
  1074. | alternative_list_contents
  1075. { $$ = $1; }
  1076. | alternative_list_contents COMMA
  1077. { $$ = $1; }
  1078. ;
  1079. alternative_list_contents:
  1080. alternative
  1081. { $$ = {std::move($1)}; }
  1082. | alternative_list_contents COMMA alternative
  1083. {
  1084. $$ = $1;
  1085. $$.push_back(std::move($3));
  1086. }
  1087. ;
  1088. type_params:
  1089. // Empty
  1090. { $$ = std::nullopt; }
  1091. | tuple_pattern
  1092. { $$ = $1; }
  1093. ;
  1094. // EXPERIMENTAL MIXIN FEATURE
  1095. mixin_import:
  1096. // Empty
  1097. { $$ = arena->New<TypeTypeLiteral>(context.source_loc()); }
  1098. | FOR expression
  1099. {
  1100. context.RecordSyntaxError("'for' not supported currently");
  1101. YYERROR;
  1102. // $$ = $2;
  1103. }
  1104. ;
  1105. class_declaration_extensibility:
  1106. // Empty
  1107. { $$ = Carbon::ClassExtensibility::None; }
  1108. | ABSTRACT
  1109. { $$ = Carbon::ClassExtensibility::Abstract; }
  1110. | BASE
  1111. { $$ = Carbon::ClassExtensibility::Base; }
  1112. ;
  1113. class_declaration_extends:
  1114. // Empty
  1115. { $$ = std::nullopt; }
  1116. | EXTENDS expression
  1117. { $$ = $2; }
  1118. ;
  1119. declaration:
  1120. function_declaration
  1121. { $$ = $1; }
  1122. | destructor_declaration
  1123. { $$ = $1; }
  1124. | class_declaration_extensibility CLASS identifier type_params class_declaration_extends LEFT_CURLY_BRACE class_body RIGHT_CURLY_BRACE
  1125. {
  1126. $$ = arena->New<ClassDeclaration>(
  1127. context.source_loc(), $3,
  1128. arena->New<SelfDeclaration>(context.source_loc()), $1, $4, $5, $7);
  1129. }
  1130. | MIXIN identifier type_params mixin_import LEFT_CURLY_BRACE mixin_body RIGHT_CURLY_BRACE
  1131. {
  1132. // EXPERIMENTAL MIXN FEATURE
  1133. auto self =
  1134. arena -> New<GenericBinding>(context.source_loc(), "Self", $4);
  1135. $$ = arena->New<MixinDeclaration>(context.source_loc(), $2, $3, self, $6);
  1136. }
  1137. | CHOICE identifier type_params LEFT_CURLY_BRACE alternative_list RIGHT_CURLY_BRACE
  1138. { $$ = arena->New<ChoiceDeclaration>(context.source_loc(), $2, $3, $5); }
  1139. | VAR variable_declaration SEMICOLON
  1140. {
  1141. $$ = arena->New<VariableDeclaration>(context.source_loc(), $2,
  1142. std::nullopt, ValueCategory::Var);
  1143. }
  1144. | VAR variable_declaration EQUAL expression SEMICOLON
  1145. {
  1146. $$ = arena->New<VariableDeclaration>(context.source_loc(), $2, $4,
  1147. ValueCategory::Var);
  1148. }
  1149. | LET variable_declaration EQUAL expression SEMICOLON
  1150. {
  1151. $$ = arena->New<VariableDeclaration>(context.source_loc(), $2, $4,
  1152. ValueCategory::Let);
  1153. }
  1154. | INTERFACE identifier type_params LEFT_CURLY_BRACE interface_body RIGHT_CURLY_BRACE
  1155. {
  1156. // TODO: Type of `Self` should be the interface being declared, not
  1157. // `Type`.
  1158. auto ty_ty = arena -> New<TypeTypeLiteral>(context.source_loc());
  1159. // TODO: Should this be switched to use a `SelfDeclaration` instead?
  1160. auto self =
  1161. arena -> New<GenericBinding>(context.source_loc(), "Self", ty_ty);
  1162. $$ = arena->New<InterfaceDeclaration>(context.source_loc(), $2, $3, self,
  1163. $5);
  1164. }
  1165. | impl_kind IMPL impl_deduced_params impl_type AS type_or_where_expression LEFT_CURLY_BRACE impl_body RIGHT_CURLY_BRACE
  1166. {
  1167. ErrorOr<ImplDeclaration*> impl = ImplDeclaration::Create(
  1168. arena, context.source_loc(), $1, $4, $6, $3, $8);
  1169. if (impl.ok()) {
  1170. $$ = *impl;
  1171. } else {
  1172. context.RecordSyntaxError(std::move(impl).error());
  1173. YYERROR;
  1174. }
  1175. }
  1176. | alias_declaration
  1177. { $$ = $1; }
  1178. ;
  1179. impl_kind:
  1180. // Internal
  1181. { $$ = Carbon::ImplKind::InternalImpl; }
  1182. | EXTERNAL
  1183. { $$ = Carbon::ImplKind::ExternalImpl; }
  1184. ;
  1185. impl_type:
  1186. // Self
  1187. { $$ = arena->New<IdentifierExpression>(context.source_loc(), "Self"); }
  1188. | type_expression
  1189. ;
  1190. destructor_declaration:
  1191. DESTRUCTOR deduced_params block
  1192. {
  1193. ErrorOr<DestructorDeclaration*> fn =
  1194. DestructorDeclaration::CreateDestructor(
  1195. arena, context.source_loc(), $2,
  1196. arena->New<TuplePattern>(context.source_loc(),
  1197. std::vector<Nonnull<Pattern*>>()),
  1198. ReturnTerm::Omitted(context.source_loc()), $3);
  1199. if (fn.ok()) {
  1200. $$ = *fn;
  1201. } else {
  1202. context.RecordSyntaxError(std::move(fn).error());
  1203. YYERROR;
  1204. }
  1205. }
  1206. ;
  1207. declaration_list:
  1208. // Empty
  1209. { $$ = {}; }
  1210. | declaration_list declaration
  1211. {
  1212. $$ = $1;
  1213. $$.push_back(Nonnull<Declaration*>($2));
  1214. }
  1215. ;
  1216. class_body:
  1217. // Empty
  1218. { $$ = {}; }
  1219. | class_body declaration
  1220. {
  1221. $$ = $1;
  1222. $$.push_back(Nonnull<Declaration*>($2));
  1223. }
  1224. | class_body mix_declaration
  1225. {
  1226. $$ = $1;
  1227. $$.push_back(Nonnull<Declaration*>($2));
  1228. }
  1229. ;
  1230. // EXPERIMENTAL MIXIN FEATURE
  1231. mixin_body:
  1232. // Empty
  1233. { $$ = {}; }
  1234. | mixin_body function_declaration
  1235. {
  1236. $$ = $1;
  1237. $$.push_back(Nonnull<Declaration*>($2));
  1238. }
  1239. | mixin_body mix_declaration
  1240. {
  1241. $$ = $1;
  1242. $$.push_back(Nonnull<Declaration*>($2));
  1243. }
  1244. ;
  1245. interface_body:
  1246. // Empty
  1247. { $$ = {}; }
  1248. | interface_body function_declaration
  1249. {
  1250. $$ = $1;
  1251. $$.push_back($2);
  1252. }
  1253. | interface_body LET generic_binding SEMICOLON
  1254. {
  1255. $$ = $1;
  1256. $$.push_back(
  1257. arena->New<AssociatedConstantDeclaration>(context.source_loc(), $3));
  1258. }
  1259. ;
  1260. impl_body:
  1261. // Empty
  1262. { $$ = {}; }
  1263. | impl_body function_declaration
  1264. {
  1265. $$ = $1;
  1266. $$.push_back($2);
  1267. }
  1268. | impl_body alias_declaration
  1269. {
  1270. $$ = $1;
  1271. $$.push_back($2);
  1272. }
  1273. ;
  1274. %%