parser.ypp 41 KB

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