parser.ypp 35 KB

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