parser.ypp 41 KB

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