parser.ypp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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. // TODO: .(expression) = expression
  663. | designator EQUAL comparison_operand
  664. { $$ = arena->New<RewriteWhereClause>(context.source_loc(), $1, $3); }
  665. ;
  666. where_clause_list:
  667. where_clause
  668. { $$ = {$1}; }
  669. | where_clause_list AND where_clause
  670. {
  671. $$ = std::move($1);
  672. $$.push_back($3);
  673. }
  674. ;
  675. where_expression:
  676. type_expression WHERE where_clause_list
  677. {
  678. auto* self =
  679. arena->New<GenericBinding>(context.source_loc(), ".Self", $1);
  680. $$ = arena->New<WhereExpression>(context.source_loc(), self, $3);
  681. }
  682. ;
  683. type_or_where_expression:
  684. type_expression
  685. | where_expression
  686. ;
  687. statement_expression:
  688. ref_deref_expression
  689. | predicate_expression
  690. | and_expression
  691. | or_expression
  692. | where_expression
  693. ;
  694. if_expression:
  695. statement_expression
  696. | IF expression THEN if_expression ELSE if_expression
  697. { $$ = arena->New<IfExpression>(context.source_loc(), $2, $4, $6); }
  698. ;
  699. expression:
  700. if_expression
  701. ;
  702. designator: PERIOD identifier { $$ = $2; }
  703. ;
  704. paren_expression: paren_expression_base
  705. { $$ = ExpressionFromParenContents(arena, context.source_loc(), $1); }
  706. ;
  707. tuple: paren_expression_base
  708. { $$ = TupleExpressionFromParenContents(arena, context.source_loc(), $1); }
  709. ;
  710. paren_expression_base:
  711. LEFT_PARENTHESIS RIGHT_PARENTHESIS
  712. { $$ = {.elements = {}, .has_trailing_comma = false}; }
  713. | LEFT_PARENTHESIS paren_expression_contents RIGHT_PARENTHESIS
  714. { $$ = $2; }
  715. | LEFT_PARENTHESIS paren_expression_contents COMMA RIGHT_PARENTHESIS
  716. {
  717. $$ = $2;
  718. $$.has_trailing_comma = true;
  719. }
  720. ;
  721. paren_expression_contents:
  722. expression
  723. { $$ = {.elements = {$1}, .has_trailing_comma = false}; }
  724. | paren_expression_contents COMMA expression
  725. {
  726. $$ = std::move($1);
  727. $$.elements.push_back($3);
  728. }
  729. ;
  730. struct_literal:
  731. LEFT_CURLY_BRACE struct_literal_contents RIGHT_CURLY_BRACE
  732. { $$ = arena->New<StructLiteral>(context.source_loc(), $2); }
  733. | LEFT_CURLY_BRACE struct_literal_contents COMMA RIGHT_CURLY_BRACE
  734. { $$ = arena->New<StructLiteral>(context.source_loc(), $2); }
  735. ;
  736. struct_literal_contents:
  737. designator EQUAL expression
  738. { $$ = {FieldInitializer($1, $3)}; }
  739. | struct_literal_contents COMMA designator EQUAL expression
  740. {
  741. $$ = std::move($1);
  742. $$.push_back(FieldInitializer($3, $5));
  743. }
  744. ;
  745. struct_type_literal:
  746. LEFT_CURLY_BRACE RIGHT_CURLY_BRACE
  747. { $$ = arena->New<StructTypeLiteral>(context.source_loc()); }
  748. | LEFT_CURLY_BRACE struct_type_literal_contents RIGHT_CURLY_BRACE
  749. { $$ = arena->New<StructTypeLiteral>(context.source_loc(), $2); }
  750. | LEFT_CURLY_BRACE struct_type_literal_contents COMMA RIGHT_CURLY_BRACE
  751. { $$ = arena->New<StructTypeLiteral>(context.source_loc(), $2); }
  752. ;
  753. struct_type_literal_contents:
  754. designator COLON expression
  755. { $$ = {FieldInitializer($1, $3)}; }
  756. | struct_type_literal_contents COMMA designator COLON expression
  757. {
  758. $$ = std::move($1);
  759. $$.push_back(FieldInitializer($3, $5));
  760. }
  761. ;
  762. // In many cases, using `pattern` recursively will result in ambiguities.
  763. // When that happens, it's necessary to factor out two separate productions,
  764. // one for when the sub-pattern is an expression, and one for when it is not.
  765. // To facilitate this, non-terminals besides `pattern` whose names contain
  766. // `pattern` are structured to be disjoint from `expression`, unless otherwise
  767. // specified.
  768. pattern:
  769. non_expression_pattern
  770. { $$ = $1; }
  771. | expression
  772. { $$ = arena->New<ExpressionPattern>($1); }
  773. ;
  774. non_expression_pattern:
  775. AUTO
  776. { $$ = arena->New<AutoPattern>(context.source_loc()); }
  777. | binding_lhs COLON pattern
  778. {
  779. $$ = arena->New<BindingPattern>(context.source_loc(), $1, $3,
  780. std::nullopt);
  781. }
  782. | binding_lhs COLON_BANG expression
  783. { $$ = arena->New<GenericBinding>(context.source_loc(), $1, $3); }
  784. | paren_pattern
  785. { $$ = $1; }
  786. | postfix_expression tuple_pattern
  787. {
  788. ErrorOr<Nonnull<AlternativePattern*>> alternative_pattern =
  789. AlternativePattern::Create(arena, context.source_loc(), $1, $2);
  790. if (alternative_pattern.ok()) {
  791. $$ = *alternative_pattern;
  792. } else {
  793. context.RecordSyntaxError(std::move(alternative_pattern).error());
  794. YYERROR;
  795. }
  796. }
  797. | VAR non_expression_pattern
  798. { $$ = arena->New<VarPattern>(context.source_loc(), $2); }
  799. ;
  800. binding_lhs:
  801. identifier { $$ = $1; }
  802. | UNDERSCORE { $$ = AnonymousName; }
  803. ;
  804. paren_pattern: paren_pattern_base
  805. { $$ = PatternFromParenContents(arena, context.source_loc(), $1); }
  806. ;
  807. paren_pattern_base:
  808. LEFT_PARENTHESIS paren_pattern_contents RIGHT_PARENTHESIS
  809. { $$ = $2; }
  810. | LEFT_PARENTHESIS paren_pattern_contents COMMA RIGHT_PARENTHESIS
  811. {
  812. $$ = $2;
  813. $$.has_trailing_comma = true;
  814. }
  815. ;
  816. // paren_pattern is analogous to paren_expression, but in order to avoid
  817. // ambiguities, it must be disjoint from paren_expression, meaning it must
  818. // contain at least one non_expression_pattern. The structure of this rule
  819. // is very different from the corresponding expression rule because is has to
  820. // enforce that requirement.
  821. paren_pattern_contents:
  822. non_expression_pattern
  823. { $$ = {.elements = {$1}, .has_trailing_comma = false}; }
  824. | paren_expression_contents COMMA non_expression_pattern
  825. {
  826. $$ = ParenExpressionToParenPattern(arena, $1);
  827. $$.elements.push_back($3);
  828. }
  829. | paren_pattern_contents COMMA expression
  830. {
  831. $$ = std::move($1);
  832. $$.elements.push_back(arena->New<ExpressionPattern>($3));
  833. }
  834. | paren_pattern_contents COMMA non_expression_pattern
  835. {
  836. $$ = std::move($1);
  837. $$.elements.push_back($3);
  838. }
  839. ;
  840. tuple_pattern: paren_pattern_base
  841. { $$ = TuplePatternFromParenContents(arena, context.source_loc(), $1); }
  842. ;
  843. // Unlike most `pattern` nonterminals, this one overlaps with `expression`,
  844. // so it should be used only when prior context (such as an introducer)
  845. // rules out the possibility of an `expression` at this point.
  846. maybe_empty_tuple_pattern:
  847. LEFT_PARENTHESIS RIGHT_PARENTHESIS
  848. {
  849. $$ = arena->New<TuplePattern>(context.source_loc(),
  850. std::vector<Nonnull<Pattern*>>());
  851. }
  852. | tuple_pattern
  853. { $$ = $1; }
  854. ;
  855. clause:
  856. CASE pattern DOUBLE_ARROW block
  857. { $$ = Match::Clause($2, $4); }
  858. | DEFAULT DOUBLE_ARROW block
  859. {
  860. $$ = Match::Clause(arena->New<BindingPattern>(
  861. context.source_loc(), std::string(AnonymousName),
  862. arena->New<AutoPattern>(context.source_loc()),
  863. ValueCategory::Let),
  864. $3);
  865. }
  866. ;
  867. clause_list:
  868. // Empty
  869. { $$ = {}; }
  870. | clause_list clause
  871. {
  872. $$ = std::move($1);
  873. $$.push_back($2);
  874. }
  875. ;
  876. statement:
  877. statement_expression EQUAL expression SEMICOLON
  878. { $$ = arena->New<Assign>(context.source_loc(), $1, $3); }
  879. | VAR pattern SEMICOLON
  880. {
  881. $$ = arena->New<VariableDefinition>(
  882. context.source_loc(), $2, std::nullopt, ValueCategory::Var,
  883. VariableDefinition::DefinitionType::Var);
  884. }
  885. | VAR pattern EQUAL expression SEMICOLON
  886. {
  887. $$ = arena->New<VariableDefinition>(
  888. context.source_loc(), $2, $4, ValueCategory::Var,
  889. VariableDefinition::DefinitionType::Var);
  890. }
  891. | RETURNED VAR variable_declaration SEMICOLON
  892. {
  893. $$ = arena->New<VariableDefinition>(
  894. context.source_loc(), $3, std::nullopt, ValueCategory::Var,
  895. VariableDefinition::DefinitionType::Returned);
  896. }
  897. | RETURNED VAR variable_declaration EQUAL expression SEMICOLON
  898. {
  899. $$ = arena->New<VariableDefinition>(
  900. context.source_loc(), $3, $5, ValueCategory::Var,
  901. VariableDefinition::DefinitionType::Returned);
  902. }
  903. | LET pattern EQUAL expression SEMICOLON
  904. {
  905. $$ = arena->New<VariableDefinition>(
  906. context.source_loc(), $2, $4, ValueCategory::Let,
  907. VariableDefinition::DefinitionType::Var);
  908. }
  909. | statement_expression SEMICOLON
  910. { $$ = arena->New<ExpressionStatement>(context.source_loc(), $1); }
  911. | if_statement
  912. { $$ = $1; }
  913. | WHILE LEFT_PARENTHESIS expression RIGHT_PARENTHESIS block
  914. { $$ = arena->New<While>(context.source_loc(), $3, $5); }
  915. | BREAK SEMICOLON
  916. { $$ = arena->New<Break>(context.source_loc()); }
  917. | CONTINUE SEMICOLON
  918. { $$ = arena->New<Continue>(context.source_loc()); }
  919. | RETURN return_expression SEMICOLON
  920. {
  921. auto [return_exp, is_omitted_exp] = $2;
  922. $$ = arena->New<ReturnExpression>(context.source_loc(), return_exp,
  923. is_omitted_exp);
  924. }
  925. | RETURN VAR SEMICOLON
  926. { $$ = arena->New<ReturnVar>(context.source_loc()); }
  927. | MATCH LEFT_PARENTHESIS expression RIGHT_PARENTHESIS LEFT_CURLY_BRACE
  928. clause_list RIGHT_CURLY_BRACE
  929. { $$ = arena->New<Match>(context.source_loc(), $3, $6); }
  930. | CONTINUATION identifier block
  931. { $$ = arena->New<Continuation>(context.source_loc(), $2, $3); }
  932. | RUN expression SEMICOLON
  933. { $$ = arena->New<Run>(context.source_loc(), $2); }
  934. | AWAIT SEMICOLON
  935. { $$ = arena->New<Await>(context.source_loc()); }
  936. | FOR LEFT_PARENTHESIS variable_declaration IN type_expression RIGHT_PARENTHESIS block
  937. { $$ = arena->New<For>(context.source_loc(), $3, $5, $7); }
  938. ;
  939. if_statement:
  940. IF LEFT_PARENTHESIS expression RIGHT_PARENTHESIS block optional_else
  941. { $$ = arena->New<If>(context.source_loc(), $3, $5, $6); }
  942. ;
  943. optional_else:
  944. // Empty
  945. { $$ = std::nullopt; }
  946. | ELSE if_statement
  947. {
  948. $$ = arena->New<Block>(context.source_loc(),
  949. std::vector<Nonnull<Statement*>>({$2}));
  950. }
  951. | ELSE block
  952. { $$ = $2; }
  953. ;
  954. return_expression:
  955. // Empty
  956. { $$ = {arena->New<TupleLiteral>(context.source_loc()), true}; }
  957. | expression
  958. { $$ = {$1, false}; }
  959. ;
  960. statement_list:
  961. // Empty
  962. { $$ = {}; }
  963. | statement_list statement
  964. {
  965. $$ = std::move($1);
  966. $$.push_back($2);
  967. }
  968. ;
  969. block:
  970. LEFT_CURLY_BRACE statement_list RIGHT_CURLY_BRACE
  971. { $$ = arena->New<Block>(context.source_loc(), std::move($2)); }
  972. ;
  973. return_term:
  974. // Empty
  975. { $$ = ReturnTerm::Omitted(context.source_loc()); }
  976. | ARROW AUTO
  977. { $$ = ReturnTerm::Auto(context.source_loc()); }
  978. | ARROW expression
  979. { $$ = ReturnTerm::Explicit($2); }
  980. ;
  981. generic_binding:
  982. identifier COLON_BANG expression
  983. {
  984. $$ = arena->New<GenericBinding>(context.source_loc(), std::move($1), $3);
  985. }
  986. ;
  987. deduced_param:
  988. generic_binding
  989. { $$ = $1; }
  990. | variable_declaration
  991. { $$ = $1; }
  992. | ADDR variable_declaration
  993. { $$ = arena->New<AddrPattern>(context.source_loc(), $2); }
  994. ;
  995. deduced_param_list:
  996. // Empty
  997. { $$ = {}; }
  998. | deduced_param
  999. { $$ = {$1}; }
  1000. | deduced_param_list COMMA deduced_param
  1001. {
  1002. $$ = std::move($1);
  1003. $$.push_back($3);
  1004. }
  1005. ;
  1006. deduced_params:
  1007. // Empty
  1008. { $$ = std::vector<Nonnull<AstNode*>>(); }
  1009. | LEFT_SQUARE_BRACKET deduced_param_list RIGHT_SQUARE_BRACKET
  1010. { $$ = $2; }
  1011. ;
  1012. impl_deduced_params:
  1013. // Empty
  1014. { $$ = std::vector<Nonnull<AstNode*>>(); }
  1015. | FORALL LEFT_SQUARE_BRACKET deduced_param_list RIGHT_SQUARE_BRACKET
  1016. { $$ = $3; }
  1017. ;
  1018. function_declaration:
  1019. FN identifier deduced_params maybe_empty_tuple_pattern return_term block
  1020. {
  1021. ErrorOr<FunctionDeclaration*> fn = FunctionDeclaration::Create(
  1022. arena, context.source_loc(), $2, $3, $4, $5, $6);
  1023. if (fn.ok()) {
  1024. $$ = *fn;
  1025. } else {
  1026. context.RecordSyntaxError(std::move(fn).error());
  1027. YYERROR;
  1028. }
  1029. }
  1030. | FN identifier deduced_params maybe_empty_tuple_pattern return_term SEMICOLON
  1031. {
  1032. ErrorOr<FunctionDeclaration*> fn = FunctionDeclaration::Create(
  1033. arena, context.source_loc(), $2, $3, $4, $5, std::nullopt);
  1034. if (fn.ok()) {
  1035. $$ = *fn;
  1036. } else {
  1037. context.RecordSyntaxError(std::move(fn).error());
  1038. YYERROR;
  1039. }
  1040. }
  1041. ;
  1042. variable_declaration: identifier COLON pattern
  1043. {
  1044. $$ = arena->New<BindingPattern>(context.source_loc(), $1, $3,
  1045. std::nullopt);
  1046. }
  1047. ;
  1048. alias_declaration: ALIAS identifier EQUAL expression SEMICOLON
  1049. { $$ = arena->New<AliasDeclaration>(context.source_loc(), $2, $4); }
  1050. ;
  1051. // EXPERIMENTAL MIXIN FEATURE
  1052. mix_declaration: MIX expression SEMICOLON
  1053. { $$ = arena->New<MixDeclaration>(context.source_loc(), $2); }
  1054. ;
  1055. alternative:
  1056. identifier tuple
  1057. { $$ = arena->New<AlternativeSignature>(context.source_loc(), $1, $2); }
  1058. | identifier
  1059. {
  1060. $$ = arena->New<AlternativeSignature>(
  1061. context.source_loc(), $1,
  1062. arena->New<TupleLiteral>(context.source_loc()));
  1063. }
  1064. ;
  1065. alternative_list:
  1066. // Empty
  1067. { $$ = {}; }
  1068. | alternative_list_contents
  1069. { $$ = std::move($1); }
  1070. | alternative_list_contents COMMA
  1071. { $$ = std::move($1); }
  1072. ;
  1073. alternative_list_contents:
  1074. alternative
  1075. { $$ = {std::move($1)}; }
  1076. | alternative_list_contents COMMA alternative
  1077. {
  1078. $$ = std::move($1);
  1079. $$.push_back(std::move($3));
  1080. }
  1081. ;
  1082. type_params:
  1083. // Empty
  1084. { $$ = std::nullopt; }
  1085. | tuple_pattern
  1086. { $$ = $1; }
  1087. ;
  1088. // EXPERIMENTAL MIXIN FEATURE
  1089. mixin_import:
  1090. // Empty
  1091. { $$ = arena->New<TypeTypeLiteral>(context.source_loc()); }
  1092. | FOR expression
  1093. {
  1094. context.RecordSyntaxError("'for' not supported currently");
  1095. YYERROR;
  1096. // $$ = $2;
  1097. }
  1098. ;
  1099. class_declaration_extensibility:
  1100. // Empty
  1101. { $$ = Carbon::ClassExtensibility::None; }
  1102. | ABSTRACT
  1103. { $$ = Carbon::ClassExtensibility::Abstract; }
  1104. | BASE
  1105. { $$ = Carbon::ClassExtensibility::Base; }
  1106. ;
  1107. class_declaration_extends:
  1108. // Empty
  1109. { $$ = std::nullopt; }
  1110. | EXTENDS expression
  1111. { $$ = $2; }
  1112. ;
  1113. declaration:
  1114. function_declaration
  1115. { $$ = $1; }
  1116. | destructor_declaration
  1117. { $$ = $1; }
  1118. | class_declaration_extensibility CLASS identifier type_params class_declaration_extends LEFT_CURLY_BRACE class_body RIGHT_CURLY_BRACE
  1119. {
  1120. $$ = arena->New<ClassDeclaration>(
  1121. context.source_loc(), $3,
  1122. arena->New<SelfDeclaration>(context.source_loc()), $1, $4, $5, $7);
  1123. }
  1124. | MIXIN identifier type_params mixin_import LEFT_CURLY_BRACE mixin_body RIGHT_CURLY_BRACE
  1125. {
  1126. // EXPERIMENTAL MIXN FEATURE
  1127. auto self = arena->New<GenericBinding>(context.source_loc(), "Self", $4);
  1128. $$ = arena->New<MixinDeclaration>(context.source_loc(), $2, $3, self, $6);
  1129. }
  1130. | CHOICE identifier type_params LEFT_CURLY_BRACE alternative_list RIGHT_CURLY_BRACE
  1131. { $$ = arena->New<ChoiceDeclaration>(context.source_loc(), $2, $3, $5); }
  1132. | VAR variable_declaration SEMICOLON
  1133. {
  1134. $$ = arena->New<VariableDeclaration>(context.source_loc(), $2,
  1135. std::nullopt, ValueCategory::Var);
  1136. }
  1137. | VAR variable_declaration EQUAL expression SEMICOLON
  1138. {
  1139. $$ = arena->New<VariableDeclaration>(context.source_loc(), $2, $4,
  1140. ValueCategory::Var);
  1141. }
  1142. | LET variable_declaration EQUAL expression SEMICOLON
  1143. {
  1144. $$ = arena->New<VariableDeclaration>(context.source_loc(), $2, $4,
  1145. ValueCategory::Let);
  1146. }
  1147. | INTERFACE identifier type_params LEFT_CURLY_BRACE interface_body RIGHT_CURLY_BRACE
  1148. {
  1149. $$ = arena->New<InterfaceDeclaration>(arena, context.source_loc(), $2, $3,
  1150. $5);
  1151. }
  1152. | impl_kind IMPL impl_deduced_params impl_type AS type_or_where_expression LEFT_CURLY_BRACE impl_body RIGHT_CURLY_BRACE
  1153. {
  1154. ErrorOr<ImplDeclaration*> impl = ImplDeclaration::Create(
  1155. arena, context.source_loc(), $1, $4, $6, $3, $8);
  1156. if (impl.ok()) {
  1157. $$ = *impl;
  1158. } else {
  1159. context.RecordSyntaxError(std::move(impl).error());
  1160. YYERROR;
  1161. }
  1162. }
  1163. | alias_declaration
  1164. { $$ = $1; }
  1165. ;
  1166. impl_kind:
  1167. // Internal
  1168. { $$ = Carbon::ImplKind::InternalImpl; }
  1169. | EXTERNAL
  1170. { $$ = Carbon::ImplKind::ExternalImpl; }
  1171. ;
  1172. impl_type:
  1173. // Self
  1174. { $$ = arena->New<IdentifierExpression>(context.source_loc(), "Self"); }
  1175. | type_expression
  1176. ;
  1177. destructor_declaration:
  1178. DESTRUCTOR deduced_params block
  1179. {
  1180. ErrorOr<DestructorDeclaration*> fn =
  1181. DestructorDeclaration::CreateDestructor(
  1182. arena, context.source_loc(), $2,
  1183. arena->New<TuplePattern>(context.source_loc(),
  1184. std::vector<Nonnull<Pattern*>>()),
  1185. ReturnTerm::Omitted(context.source_loc()), $3);
  1186. if (fn.ok()) {
  1187. $$ = *fn;
  1188. } else {
  1189. context.RecordSyntaxError(std::move(fn).error());
  1190. YYERROR;
  1191. }
  1192. }
  1193. ;
  1194. declaration_list:
  1195. // Empty
  1196. { $$ = {}; }
  1197. | declaration_list declaration
  1198. {
  1199. $$ = std::move($1);
  1200. $$.push_back(Nonnull<Declaration*>($2));
  1201. }
  1202. ;
  1203. class_body:
  1204. // Empty
  1205. { $$ = {}; }
  1206. | class_body declaration
  1207. {
  1208. $$ = std::move($1);
  1209. $$.push_back(Nonnull<Declaration*>($2));
  1210. }
  1211. | class_body mix_declaration
  1212. {
  1213. $$ = std::move($1);
  1214. $$.push_back(Nonnull<Declaration*>($2));
  1215. }
  1216. ;
  1217. // EXPERIMENTAL MIXIN FEATURE
  1218. mixin_body:
  1219. // Empty
  1220. { $$ = {}; }
  1221. | mixin_body function_declaration
  1222. {
  1223. $$ = std::move($1);
  1224. $$.push_back(Nonnull<Declaration*>($2));
  1225. }
  1226. | mixin_body mix_declaration
  1227. {
  1228. $$ = std::move($1);
  1229. $$.push_back(Nonnull<Declaration*>($2));
  1230. }
  1231. ;
  1232. interface_body:
  1233. // Empty
  1234. { $$ = {}; }
  1235. | interface_body function_declaration
  1236. {
  1237. $$ = std::move($1);
  1238. $$.push_back($2);
  1239. }
  1240. | interface_body LET generic_binding SEMICOLON
  1241. {
  1242. $$ = std::move($1);
  1243. $$.push_back(
  1244. arena->New<AssociatedConstantDeclaration>(context.source_loc(), $3));
  1245. }
  1246. | interface_body EXTENDS expression SEMICOLON
  1247. {
  1248. $$ = std::move($1);
  1249. $$.push_back(arena->New<InterfaceExtendsDeclaration>(context.source_loc(),
  1250. $3));
  1251. }
  1252. | interface_body IMPL impl_type AS type_or_where_expression SEMICOLON
  1253. {
  1254. $$ = std::move($1);
  1255. $$.push_back(arena->New<InterfaceImplDeclaration>(context.source_loc(),
  1256. $3, $5));
  1257. }
  1258. ;
  1259. impl_body:
  1260. // Empty
  1261. { $$ = {}; }
  1262. | impl_body function_declaration
  1263. {
  1264. $$ = std::move($1);
  1265. $$.push_back($2);
  1266. }
  1267. | impl_body alias_declaration
  1268. {
  1269. $$ = std::move($1);
  1270. $$.push_back($2);
  1271. }
  1272. ;
  1273. %%