parser.ypp 51 KB

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