parser.ypp 39 KB

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