parser.ypp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  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. ;
  467. additive_operand:
  468. simple_binary_operand
  469. | multiplicative_expression
  470. ;
  471. additive_lhs:
  472. simple_binary_operand
  473. | additive_expression
  474. ;
  475. additive_expression:
  476. multiplicative_expression
  477. | additive_lhs PLUS additive_operand
  478. {
  479. $$ = arena->New<OperatorExpression>(
  480. context.source_loc(), Operator::Add,
  481. std::vector<Nonnull<Expression*>>({$1, $3}));
  482. }
  483. | additive_lhs MINUS additive_operand
  484. {
  485. $$ = arena->New<OperatorExpression>(
  486. context.source_loc(), Operator::Sub,
  487. std::vector<Nonnull<Expression*>>({$1, $3}));
  488. }
  489. ;
  490. modulo_expression:
  491. simple_binary_operand PERCENT simple_binary_operand
  492. {
  493. $$ = arena->New<OperatorExpression>(
  494. context.source_loc(), Operator::Mod,
  495. std::vector<Nonnull<Expression*>>({$1, $3}));
  496. }
  497. ;
  498. bitwise_and_lhs:
  499. simple_binary_operand
  500. | bitwise_and_expression
  501. ;
  502. bitwise_and_expression:
  503. bitwise_and_lhs AMPERSAND simple_binary_operand
  504. {
  505. $$ = arena->New<OperatorExpression>(
  506. context.source_loc(), Operator::BitwiseAnd,
  507. std::vector<Nonnull<Expression*>>({$1, $3}));
  508. }
  509. ;
  510. bitwise_or_lhs:
  511. simple_binary_operand
  512. | bitwise_or_expression
  513. ;
  514. bitwise_or_expression:
  515. bitwise_or_lhs PIPE simple_binary_operand
  516. {
  517. $$ = arena->New<OperatorExpression>(
  518. context.source_loc(), Operator::BitwiseOr,
  519. std::vector<Nonnull<Expression*>>({$1, $3}));
  520. }
  521. ;
  522. bitwise_xor_lhs:
  523. simple_binary_operand
  524. | bitwise_xor_expression
  525. ;
  526. bitwise_xor_expression:
  527. bitwise_xor_lhs CARET simple_binary_operand
  528. {
  529. $$ = arena->New<OperatorExpression>(
  530. context.source_loc(), Operator::BitwiseXor,
  531. std::vector<Nonnull<Expression*>>({$1, $3}));
  532. }
  533. ;
  534. bitwise_expression:
  535. bitwise_and_expression
  536. | bitwise_or_expression
  537. | bitwise_xor_expression
  538. ;
  539. bit_shift_expression:
  540. simple_binary_operand LESS_LESS simple_binary_operand
  541. {
  542. $$ = arena->New<OperatorExpression>(
  543. context.source_loc(), Operator::BitShiftLeft,
  544. std::vector<Nonnull<Expression*>>({$1, $3}));
  545. }
  546. | simple_binary_operand GREATER_GREATER simple_binary_operand
  547. {
  548. $$ = arena->New<OperatorExpression>(
  549. context.source_loc(), Operator::BitShiftRight,
  550. std::vector<Nonnull<Expression*>>({$1, $3}));
  551. }
  552. ;
  553. as_expression:
  554. simple_binary_operand AS simple_binary_operand
  555. {
  556. $$ = arena->New<OperatorExpression>(
  557. context.source_loc(), Operator::As,
  558. std::vector<Nonnull<Expression*>>{$1, $3});
  559. }
  560. ;
  561. unimpl_expression:
  562. // ref_deref_expression excluded due to precedence diamond.
  563. ref_deref_expression UNIMPL_EXAMPLE ref_deref_expression
  564. {
  565. $$ = arena->New<UnimplementedExpression>(context.source_loc(),
  566. "ExampleInfix", $1, $3);
  567. }
  568. ;
  569. value_expression:
  570. // ref_deref_expression excluded due to precedence diamond.
  571. additive_expression
  572. | as_expression
  573. | bitwise_expression
  574. | bit_shift_expression
  575. | fn_type_expression
  576. | modulo_expression
  577. | unary_expression
  578. | unimpl_expression
  579. ;
  580. comparison_operand:
  581. ref_deref_expression
  582. | value_expression
  583. ;
  584. comparison_operator:
  585. EQUAL_EQUAL
  586. { $$ = Operator::Eq; }
  587. | LESS
  588. { $$ = Operator::Less; }
  589. | LESS_EQUAL
  590. { $$ = Operator::LessEq; }
  591. | GREATER
  592. { $$ = Operator::Greater; }
  593. | GREATER_EQUAL
  594. { $$ = Operator::GreaterEq; }
  595. | NOT_EQUAL
  596. { $$ = Operator::NotEq; }
  597. ;
  598. comparison_expression:
  599. value_expression
  600. | comparison_operand comparison_operator comparison_operand
  601. {
  602. $$ = arena->New<OperatorExpression>(
  603. context.source_loc(), $2,
  604. std::vector<Nonnull<Expression*>>({$1, $3}));
  605. }
  606. ;
  607. not_expression:
  608. NOT ref_deref_expression
  609. {
  610. $$ = arena->New<OperatorExpression>(
  611. context.source_loc(), Operator::Not,
  612. std::vector<Nonnull<Expression*>>({$2}));
  613. }
  614. ;
  615. predicate_expression:
  616. // ref_deref_expression excluded due to precedence diamond.
  617. not_expression
  618. | comparison_expression
  619. ;
  620. and_or_operand:
  621. ref_deref_expression
  622. | predicate_expression
  623. ;
  624. and_lhs:
  625. and_or_operand
  626. | and_expression
  627. ;
  628. and_expression:
  629. // predicate_expression excluded due to precedence diamond.
  630. and_lhs AND and_or_operand
  631. {
  632. $$ = arena->New<OperatorExpression>(
  633. context.source_loc(), Operator::And,
  634. std::vector<Nonnull<Expression*>>({$1, $3}));
  635. }
  636. ;
  637. or_lhs:
  638. and_or_operand
  639. | or_expression
  640. ;
  641. or_expression:
  642. // predicate_expression excluded due to precedence diamond.
  643. or_lhs OR and_or_operand
  644. {
  645. $$ = arena->New<OperatorExpression>(
  646. context.source_loc(), Operator::Or,
  647. std::vector<Nonnull<Expression*>>({$1, $3}));
  648. }
  649. ;
  650. where_clause:
  651. comparison_operand IS comparison_operand
  652. { $$ = arena->New<IsWhereClause>(context.source_loc(), $1, $3); }
  653. | comparison_operand EQUAL_EQUAL comparison_operand
  654. { $$ = arena->New<EqualsWhereClause>(context.source_loc(), $1, $3); }
  655. ;
  656. where_clause_list:
  657. where_clause
  658. { $$ = {$1}; }
  659. | where_clause_list AND where_clause
  660. {
  661. $$ = std::move($1);
  662. $$.push_back($3);
  663. }
  664. ;
  665. where_expression:
  666. type_expression WHERE where_clause_list
  667. {
  668. auto* self =
  669. arena -> New<GenericBinding>(context.source_loc(), ".Self", $1);
  670. $$ = arena->New<WhereExpression>(context.source_loc(), self, $3);
  671. }
  672. ;
  673. type_or_where_expression:
  674. type_expression
  675. | where_expression
  676. ;
  677. statement_expression:
  678. ref_deref_expression
  679. | predicate_expression
  680. | and_expression
  681. | or_expression
  682. | where_expression
  683. ;
  684. if_expression:
  685. statement_expression
  686. | IF expression THEN if_expression ELSE if_expression
  687. { $$ = arena->New<IfExpression>(context.source_loc(), $2, $4, $6); }
  688. ;
  689. expression:
  690. if_expression
  691. ;
  692. designator: PERIOD identifier { $$ = $2; }
  693. ;
  694. paren_expression: paren_expression_base
  695. { $$ = ExpressionFromParenContents(arena, context.source_loc(), $1); }
  696. ;
  697. tuple: paren_expression_base
  698. { $$ = TupleExpressionFromParenContents(arena, context.source_loc(), $1); }
  699. ;
  700. paren_expression_base:
  701. LEFT_PARENTHESIS RIGHT_PARENTHESIS
  702. { $$ = {.elements = {}, .has_trailing_comma = false}; }
  703. | LEFT_PARENTHESIS paren_expression_contents RIGHT_PARENTHESIS
  704. { $$ = $2; }
  705. | LEFT_PARENTHESIS paren_expression_contents COMMA RIGHT_PARENTHESIS
  706. {
  707. $$ = $2;
  708. $$.has_trailing_comma = true;
  709. }
  710. ;
  711. paren_expression_contents:
  712. expression
  713. { $$ = {.elements = {$1}, .has_trailing_comma = false}; }
  714. | paren_expression_contents COMMA expression
  715. {
  716. $$ = $1;
  717. $$.elements.push_back($3);
  718. }
  719. ;
  720. struct_literal:
  721. LEFT_CURLY_BRACE struct_literal_contents RIGHT_CURLY_BRACE
  722. { $$ = arena->New<StructLiteral>(context.source_loc(), $2); }
  723. | LEFT_CURLY_BRACE struct_literal_contents COMMA RIGHT_CURLY_BRACE
  724. { $$ = arena->New<StructLiteral>(context.source_loc(), $2); }
  725. ;
  726. struct_literal_contents:
  727. designator EQUAL expression
  728. { $$ = {FieldInitializer($1, $3)}; }
  729. | struct_literal_contents COMMA designator EQUAL expression
  730. {
  731. $$ = $1;
  732. $$.push_back(FieldInitializer($3, $5));
  733. }
  734. ;
  735. struct_type_literal:
  736. LEFT_CURLY_BRACE RIGHT_CURLY_BRACE
  737. { $$ = arena->New<StructTypeLiteral>(context.source_loc()); }
  738. | LEFT_CURLY_BRACE struct_type_literal_contents RIGHT_CURLY_BRACE
  739. { $$ = arena->New<StructTypeLiteral>(context.source_loc(), $2); }
  740. | LEFT_CURLY_BRACE struct_type_literal_contents COMMA RIGHT_CURLY_BRACE
  741. { $$ = arena->New<StructTypeLiteral>(context.source_loc(), $2); }
  742. ;
  743. struct_type_literal_contents:
  744. designator COLON expression
  745. { $$ = {FieldInitializer($1, $3)}; }
  746. | struct_type_literal_contents COMMA designator COLON expression
  747. {
  748. $$ = $1;
  749. $$.push_back(FieldInitializer($3, $5));
  750. }
  751. ;
  752. // In many cases, using `pattern` recursively will result in ambiguities.
  753. // When that happens, it's necessary to factor out two separate productions,
  754. // one for when the sub-pattern is an expression, and one for when it is not.
  755. // To facilitate this, non-terminals besides `pattern` whose names contain
  756. // `pattern` are structured to be disjoint from `expression`, unless otherwise
  757. // specified.
  758. pattern:
  759. non_expression_pattern
  760. { $$ = $1; }
  761. | expression
  762. { $$ = arena->New<ExpressionPattern>($1); }
  763. ;
  764. non_expression_pattern:
  765. AUTO
  766. { $$ = arena->New<AutoPattern>(context.source_loc()); }
  767. | binding_lhs COLON pattern
  768. {
  769. $$ = arena->New<BindingPattern>(context.source_loc(), $1, $3,
  770. std::nullopt);
  771. }
  772. | binding_lhs COLON_BANG expression
  773. { $$ = arena->New<GenericBinding>(context.source_loc(), $1, $3); }
  774. | paren_pattern
  775. { $$ = $1; }
  776. | postfix_expression tuple_pattern
  777. {
  778. ErrorOr<Nonnull<AlternativePattern*>> alternative_pattern =
  779. AlternativePattern::Create(arena, context.source_loc(), $1, $2);
  780. if (alternative_pattern.ok()) {
  781. $$ = *alternative_pattern;
  782. } else {
  783. context.RecordSyntaxError(std::move(alternative_pattern).error());
  784. YYERROR;
  785. }
  786. }
  787. | VAR non_expression_pattern
  788. { $$ = arena->New<VarPattern>(context.source_loc(), $2); }
  789. ;
  790. binding_lhs:
  791. identifier { $$ = $1; }
  792. | UNDERSCORE { $$ = AnonymousName; }
  793. ;
  794. paren_pattern: paren_pattern_base
  795. { $$ = PatternFromParenContents(arena, context.source_loc(), $1); }
  796. ;
  797. paren_pattern_base:
  798. LEFT_PARENTHESIS paren_pattern_contents RIGHT_PARENTHESIS
  799. { $$ = $2; }
  800. | LEFT_PARENTHESIS paren_pattern_contents COMMA RIGHT_PARENTHESIS
  801. {
  802. $$ = $2;
  803. $$.has_trailing_comma = true;
  804. }
  805. ;
  806. // paren_pattern is analogous to paren_expression, but in order to avoid
  807. // ambiguities, it must be disjoint from paren_expression, meaning it must
  808. // contain at least one non_expression_pattern. The structure of this rule
  809. // is very different from the corresponding expression rule because is has to
  810. // enforce that requirement.
  811. paren_pattern_contents:
  812. non_expression_pattern
  813. { $$ = {.elements = {$1}, .has_trailing_comma = false}; }
  814. | paren_expression_contents COMMA non_expression_pattern
  815. {
  816. $$ = ParenExpressionToParenPattern(arena, $1);
  817. $$.elements.push_back($3);
  818. }
  819. | paren_pattern_contents COMMA expression
  820. {
  821. $$ = $1;
  822. $$.elements.push_back(arena->New<ExpressionPattern>($3));
  823. }
  824. | paren_pattern_contents COMMA non_expression_pattern
  825. {
  826. $$ = $1;
  827. $$.elements.push_back($3);
  828. }
  829. ;
  830. tuple_pattern: paren_pattern_base
  831. { $$ = TuplePatternFromParenContents(arena, context.source_loc(), $1); }
  832. ;
  833. // Unlike most `pattern` nonterminals, this one overlaps with `expression`,
  834. // so it should be used only when prior context (such as an introducer)
  835. // rules out the possibility of an `expression` at this point.
  836. maybe_empty_tuple_pattern:
  837. LEFT_PARENTHESIS RIGHT_PARENTHESIS
  838. {
  839. $$ = arena->New<TuplePattern>(context.source_loc(),
  840. std::vector<Nonnull<Pattern*>>());
  841. }
  842. | tuple_pattern
  843. { $$ = $1; }
  844. ;
  845. clause:
  846. CASE pattern DOUBLE_ARROW block
  847. { $$ = Match::Clause($2, $4); }
  848. | DEFAULT DOUBLE_ARROW block
  849. {
  850. $$ = Match::Clause(arena->New<BindingPattern>(
  851. context.source_loc(), std::string(AnonymousName),
  852. arena->New<AutoPattern>(context.source_loc()),
  853. ValueCategory::Let),
  854. $3);
  855. }
  856. ;
  857. clause_list:
  858. // Empty
  859. { $$ = {}; }
  860. | clause_list clause
  861. {
  862. $$ = $1;
  863. $$.push_back($2);
  864. }
  865. ;
  866. statement:
  867. statement_expression EQUAL expression SEMICOLON
  868. { $$ = arena->New<Assign>(context.source_loc(), $1, $3); }
  869. | VAR pattern SEMICOLON
  870. {
  871. $$ = arena->New<VariableDefinition>(
  872. context.source_loc(), $2, std::nullopt, ValueCategory::Var,
  873. VariableDefinition::DefinitionType::Var);
  874. }
  875. | VAR pattern EQUAL expression SEMICOLON
  876. {
  877. $$ = arena->New<VariableDefinition>(
  878. context.source_loc(), $2, $4, ValueCategory::Var,
  879. VariableDefinition::DefinitionType::Var);
  880. }
  881. | RETURNED VAR variable_declaration SEMICOLON
  882. {
  883. $$ = arena->New<VariableDefinition>(
  884. context.source_loc(), $3, std::nullopt, ValueCategory::Var,
  885. VariableDefinition::DefinitionType::Returned);
  886. }
  887. | RETURNED VAR variable_declaration EQUAL expression SEMICOLON
  888. {
  889. $$ = arena->New<VariableDefinition>(
  890. context.source_loc(), $3, $5, ValueCategory::Var,
  891. VariableDefinition::DefinitionType::Returned);
  892. }
  893. | LET pattern EQUAL expression SEMICOLON
  894. {
  895. $$ = arena->New<VariableDefinition>(
  896. context.source_loc(), $2, $4, ValueCategory::Let,
  897. VariableDefinition::DefinitionType::Var);
  898. }
  899. | statement_expression SEMICOLON
  900. { $$ = arena->New<ExpressionStatement>(context.source_loc(), $1); }
  901. | if_statement
  902. { $$ = $1; }
  903. | WHILE LEFT_PARENTHESIS expression RIGHT_PARENTHESIS block
  904. { $$ = arena->New<While>(context.source_loc(), $3, $5); }
  905. | BREAK SEMICOLON
  906. { $$ = arena->New<Break>(context.source_loc()); }
  907. | CONTINUE SEMICOLON
  908. { $$ = arena->New<Continue>(context.source_loc()); }
  909. | RETURN return_expression SEMICOLON
  910. {
  911. auto [return_exp, is_omitted_exp] = $2;
  912. $$ = arena->New<ReturnExpression>(context.source_loc(), return_exp,
  913. is_omitted_exp);
  914. }
  915. | RETURN VAR SEMICOLON
  916. { $$ = arena->New<ReturnVar>(context.source_loc()); }
  917. | MATCH LEFT_PARENTHESIS expression RIGHT_PARENTHESIS LEFT_CURLY_BRACE
  918. clause_list RIGHT_CURLY_BRACE
  919. { $$ = arena->New<Match>(context.source_loc(), $3, $6); }
  920. | CONTINUATION identifier block
  921. { $$ = arena->New<Continuation>(context.source_loc(), $2, $3); }
  922. | RUN expression SEMICOLON
  923. { $$ = arena->New<Run>(context.source_loc(), $2); }
  924. | AWAIT SEMICOLON
  925. { $$ = arena->New<Await>(context.source_loc()); }
  926. | FOR LEFT_PARENTHESIS variable_declaration IN type_expression RIGHT_PARENTHESIS block
  927. { $$ = arena->New<For>(context.source_loc(), $3, $5, $7); }
  928. ;
  929. if_statement:
  930. IF LEFT_PARENTHESIS expression RIGHT_PARENTHESIS block optional_else
  931. { $$ = arena->New<If>(context.source_loc(), $3, $5, $6); }
  932. ;
  933. optional_else:
  934. // Empty
  935. { $$ = std::nullopt; }
  936. | ELSE if_statement
  937. {
  938. $$ = arena->New<Block>(context.source_loc(),
  939. std::vector<Nonnull<Statement*>>({$2}));
  940. }
  941. | ELSE block
  942. { $$ = $2; }
  943. ;
  944. return_expression:
  945. // Empty
  946. { $$ = {arena->New<TupleLiteral>(context.source_loc()), true}; }
  947. | expression
  948. { $$ = {$1, false}; }
  949. ;
  950. statement_list:
  951. // Empty
  952. { $$ = {}; }
  953. | statement_list statement
  954. {
  955. $$ = std::move($1);
  956. $$.push_back($2);
  957. }
  958. ;
  959. block:
  960. LEFT_CURLY_BRACE statement_list RIGHT_CURLY_BRACE
  961. { $$ = arena->New<Block>(context.source_loc(), std::move($2)); }
  962. ;
  963. return_term:
  964. // Empty
  965. { $$ = ReturnTerm::Omitted(context.source_loc()); }
  966. | ARROW AUTO
  967. { $$ = ReturnTerm::Auto(context.source_loc()); }
  968. | ARROW expression
  969. { $$ = ReturnTerm::Explicit($2); }
  970. ;
  971. generic_binding:
  972. identifier COLON_BANG expression
  973. {
  974. $$ = arena->New<GenericBinding>(context.source_loc(), std::move($1), $3);
  975. }
  976. ;
  977. deduced_param:
  978. generic_binding
  979. { $$ = $1; }
  980. | variable_declaration
  981. { $$ = $1; }
  982. | ADDR variable_declaration
  983. { $$ = arena->New<AddrPattern>(context.source_loc(), $2); }
  984. ;
  985. deduced_param_list:
  986. // Empty
  987. { $$ = {}; }
  988. | deduced_param
  989. { $$ = {$1}; }
  990. | deduced_param_list COMMA deduced_param
  991. {
  992. $$ = $1; // TODO: can we std::move here?
  993. $$.push_back($3);
  994. }
  995. ;
  996. deduced_params:
  997. // Empty
  998. { $$ = std::vector<Nonnull<AstNode*>>(); }
  999. | LEFT_SQUARE_BRACKET deduced_param_list RIGHT_SQUARE_BRACKET
  1000. { $$ = $2; }
  1001. ;
  1002. impl_deduced_params:
  1003. // Empty
  1004. { $$ = std::vector<Nonnull<AstNode*>>(); }
  1005. | FORALL LEFT_SQUARE_BRACKET deduced_param_list RIGHT_SQUARE_BRACKET
  1006. { $$ = $3; }
  1007. ;
  1008. receiver:
  1009. // Empty
  1010. { $$ = std::nullopt; }
  1011. | LEFT_CURLY_BRACE variable_declaration RIGHT_CURLY_BRACE
  1012. { $$ = $2; }
  1013. | LEFT_CURLY_BRACE ADDR variable_declaration RIGHT_CURLY_BRACE
  1014. { $$ = arena->New<AddrPattern>(context.source_loc(), $3); }
  1015. ;
  1016. function_declaration:
  1017. FN identifier deduced_params receiver maybe_empty_tuple_pattern return_term block
  1018. {
  1019. ErrorOr<FunctionDeclaration*> fn = FunctionDeclaration::Create(
  1020. arena, context.source_loc(), $2, $3, $4, $5, $6, $7);
  1021. if (fn.ok()) {
  1022. $$ = *fn;
  1023. } else {
  1024. context.RecordSyntaxError(std::move(fn).error());
  1025. YYERROR;
  1026. }
  1027. }
  1028. | FN identifier deduced_params receiver maybe_empty_tuple_pattern return_term SEMICOLON
  1029. {
  1030. ErrorOr<FunctionDeclaration*> fn = FunctionDeclaration::Create(
  1031. arena, context.source_loc(), $2, $3, $4, $5, $6, std::nullopt);
  1032. if (fn.ok()) {
  1033. $$ = *fn;
  1034. } else {
  1035. context.RecordSyntaxError(std::move(fn).error());
  1036. YYERROR;
  1037. }
  1038. }
  1039. ;
  1040. variable_declaration: identifier COLON pattern
  1041. {
  1042. $$ = arena->New<BindingPattern>(context.source_loc(), $1, $3,
  1043. std::nullopt);
  1044. }
  1045. ;
  1046. alias_declaration: ALIAS identifier EQUAL expression SEMICOLON
  1047. { $$ = arena->New<AliasDeclaration>(context.source_loc(), $2, $4); }
  1048. ;
  1049. // EXPERIMENTAL MIXIN FEATURE
  1050. mix_declaration: MIX expression SEMICOLON
  1051. { $$ = arena->New<MixDeclaration>(context.source_loc(), $2); }
  1052. ;
  1053. alternative:
  1054. identifier tuple
  1055. { $$ = arena->New<AlternativeSignature>(context.source_loc(), $1, $2); }
  1056. | identifier
  1057. {
  1058. $$ = arena->New<AlternativeSignature>(
  1059. context.source_loc(), $1,
  1060. arena->New<TupleLiteral>(context.source_loc()));
  1061. }
  1062. ;
  1063. alternative_list:
  1064. // Empty
  1065. { $$ = {}; }
  1066. | alternative_list_contents
  1067. { $$ = $1; }
  1068. | alternative_list_contents COMMA
  1069. { $$ = $1; }
  1070. ;
  1071. alternative_list_contents:
  1072. alternative
  1073. { $$ = {std::move($1)}; }
  1074. | alternative_list_contents COMMA alternative
  1075. {
  1076. $$ = $1;
  1077. $$.push_back(std::move($3));
  1078. }
  1079. ;
  1080. type_params:
  1081. // Empty
  1082. { $$ = std::nullopt; }
  1083. | tuple_pattern
  1084. { $$ = $1; }
  1085. ;
  1086. // EXPERIMENTAL MIXIN FEATURE
  1087. mixin_import:
  1088. // Empty
  1089. { $$ = arena->New<TypeTypeLiteral>(context.source_loc()); }
  1090. | FOR expression
  1091. {
  1092. context.RecordSyntaxError("'for' not supported currently");
  1093. YYERROR;
  1094. // $$ = $2;
  1095. }
  1096. ;
  1097. class_declaration_extensibility:
  1098. // Empty
  1099. { $$ = Carbon::ClassExtensibility::None; }
  1100. | ABSTRACT
  1101. { $$ = Carbon::ClassExtensibility::Abstract; }
  1102. | BASE
  1103. { $$ = Carbon::ClassExtensibility::Base; }
  1104. ;
  1105. class_declaration_extends:
  1106. // Empty
  1107. { $$ = std::nullopt; }
  1108. | EXTENDS expression
  1109. { $$ = $2; }
  1110. ;
  1111. declaration:
  1112. function_declaration
  1113. { $$ = $1; }
  1114. | class_declaration_extensibility CLASS identifier type_params class_declaration_extends LEFT_CURLY_BRACE class_body RIGHT_CURLY_BRACE
  1115. {
  1116. $$ = arena->New<ClassDeclaration>(
  1117. context.source_loc(), $3,
  1118. arena->New<SelfDeclaration>(context.source_loc()), $1, $4, $5, $7);
  1119. }
  1120. | MIXIN identifier type_params mixin_import LEFT_CURLY_BRACE mixin_body RIGHT_CURLY_BRACE
  1121. {
  1122. // EXPERIMENTAL MIXN FEATURE
  1123. auto self =
  1124. arena -> New<GenericBinding>(context.source_loc(), "Self", $4);
  1125. $$ = arena->New<MixinDeclaration>(context.source_loc(), $2, $3, self, $6);
  1126. }
  1127. | CHOICE identifier type_params LEFT_CURLY_BRACE alternative_list RIGHT_CURLY_BRACE
  1128. { $$ = arena->New<ChoiceDeclaration>(context.source_loc(), $2, $3, $5); }
  1129. | VAR variable_declaration SEMICOLON
  1130. {
  1131. $$ = arena->New<VariableDeclaration>(context.source_loc(), $2,
  1132. std::nullopt, ValueCategory::Var);
  1133. }
  1134. | VAR variable_declaration EQUAL expression SEMICOLON
  1135. {
  1136. $$ = arena->New<VariableDeclaration>(context.source_loc(), $2, $4,
  1137. ValueCategory::Var);
  1138. }
  1139. | LET variable_declaration EQUAL expression SEMICOLON
  1140. {
  1141. $$ = arena->New<VariableDeclaration>(context.source_loc(), $2, $4,
  1142. ValueCategory::Let);
  1143. }
  1144. | INTERFACE identifier type_params LEFT_CURLY_BRACE interface_body RIGHT_CURLY_BRACE
  1145. {
  1146. // TODO: Type of `Self` should be the interface being declared, not
  1147. // `Type`.
  1148. auto ty_ty = arena -> New<TypeTypeLiteral>(context.source_loc());
  1149. // TODO: Should this be switched to use a `SelfDeclaration` instead?
  1150. auto self =
  1151. arena -> New<GenericBinding>(context.source_loc(), "Self", ty_ty);
  1152. $$ = arena->New<InterfaceDeclaration>(context.source_loc(), $2, $3, self,
  1153. $5);
  1154. }
  1155. | impl_kind IMPL impl_deduced_params impl_type AS type_or_where_expression LEFT_CURLY_BRACE impl_body RIGHT_CURLY_BRACE
  1156. {
  1157. ErrorOr<ImplDeclaration*> impl = ImplDeclaration::Create(
  1158. arena, context.source_loc(), $1, $4, $6, $3, $8);
  1159. if (impl.ok()) {
  1160. $$ = *impl;
  1161. } else {
  1162. context.RecordSyntaxError(std::move(impl).error());
  1163. YYERROR;
  1164. }
  1165. }
  1166. | alias_declaration
  1167. { $$ = $1; }
  1168. ;
  1169. impl_kind:
  1170. // Internal
  1171. { $$ = Carbon::ImplKind::InternalImpl; }
  1172. | EXTERNAL
  1173. { $$ = Carbon::ImplKind::ExternalImpl; }
  1174. ;
  1175. impl_type:
  1176. // Self
  1177. { $$ = arena->New<IdentifierExpression>(context.source_loc(), "Self"); }
  1178. | type_expression
  1179. ;
  1180. declaration_list:
  1181. // Empty
  1182. { $$ = {}; }
  1183. | declaration_list declaration
  1184. {
  1185. $$ = $1;
  1186. $$.push_back(Nonnull<Declaration*>($2));
  1187. }
  1188. ;
  1189. class_body:
  1190. // Empty
  1191. { $$ = {}; }
  1192. | class_body declaration
  1193. {
  1194. $$ = $1;
  1195. $$.push_back(Nonnull<Declaration*>($2));
  1196. }
  1197. | class_body mix_declaration
  1198. {
  1199. $$ = $1;
  1200. $$.push_back(Nonnull<Declaration*>($2));
  1201. }
  1202. ;
  1203. // EXPERIMENTAL MIXIN FEATURE
  1204. mixin_body:
  1205. // Empty
  1206. { $$ = {}; }
  1207. | mixin_body function_declaration
  1208. {
  1209. $$ = $1;
  1210. $$.push_back(Nonnull<Declaration*>($2));
  1211. }
  1212. | mixin_body mix_declaration
  1213. {
  1214. $$ = $1;
  1215. $$.push_back(Nonnull<Declaration*>($2));
  1216. }
  1217. ;
  1218. interface_body:
  1219. // Empty
  1220. { $$ = {}; }
  1221. | interface_body function_declaration
  1222. {
  1223. $$ = $1;
  1224. $$.push_back($2);
  1225. }
  1226. | interface_body LET generic_binding SEMICOLON
  1227. {
  1228. $$ = $1;
  1229. $$.push_back(
  1230. arena->New<AssociatedConstantDeclaration>(context.source_loc(), $3));
  1231. }
  1232. ;
  1233. impl_body:
  1234. // Empty
  1235. { $$ = {}; }
  1236. | impl_body function_declaration
  1237. {
  1238. $$ = $1;
  1239. $$.push_back($2);
  1240. }
  1241. | impl_body alias_declaration
  1242. {
  1243. $$ = $1;
  1244. $$.push_back($2);
  1245. }
  1246. ;
  1247. %%