parser.ypp 52 KB

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