parser.ypp 37 KB

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