parser.ypp 42 KB

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