parser.ypp 35 KB

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