parser.ypp 36 KB

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