parser.ypp 37 KB

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