parser.ypp 50 KB

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