parser.ypp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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 "common/check.h"
  47. #include "explorer/syntax/parse_and_lex_context.h"
  48. #include "llvm/ADT/StringExtras.h"
  49. #include "llvm/Support/raw_ostream.h"
  50. } // %code top
  51. %code requires {
  52. #include <optional>
  53. #include "explorer/ast/ast.h"
  54. #include "explorer/ast/declaration.h"
  55. #include "explorer/ast/expression.h"
  56. #include "explorer/ast/paren_contents.h"
  57. #include "explorer/ast/pattern.h"
  58. #include "explorer/ast/value_category.h"
  59. #include "explorer/common/arena.h"
  60. #include "explorer/common/nonnull.h"
  61. #include "explorer/syntax/bison_wrap.h"
  62. namespace Carbon {
  63. class ParseAndLexContext;
  64. } // namespace Carbon
  65. typedef void* yyscan_t;
  66. } // %code requires
  67. %code {
  68. void Carbon::Parser::error(const location_type&, const std::string& message) {
  69. context.RecordSyntaxError(message);
  70. }
  71. } // %code
  72. %token <int> integer_literal
  73. %token <std::string> identifier
  74. %token <IntrinsicExpression::Intrinsic> intrinsic_identifier
  75. %token <std::string> sized_type_literal
  76. %token <std::string> string_literal
  77. %type <std::string> designator
  78. %type <ImplKind> impl_kind
  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 <Nonnull<Declaration*>> declaration
  86. %type <Nonnull<FunctionDeclaration*>> function_declaration
  87. %type <Nonnull<AliasDeclaration*>> alias_declaration
  88. %type <std::vector<Nonnull<Declaration*>>> declaration_list
  89. %type <std::vector<Nonnull<Declaration*>>> interface_body
  90. %type <std::vector<Nonnull<Declaration*>>> impl_body
  91. %type <Nonnull<Statement*>> statement
  92. %type <Nonnull<If*>> if_statement
  93. %type <std::optional<Nonnull<Block*>>> optional_else
  94. %type <std::pair<Nonnull<Expression*>, bool>> return_expression
  95. %type <Nonnull<Block*>> block
  96. %type <std::vector<Nonnull<Statement*>>> statement_list
  97. %type <Nonnull<Expression*>> primary_expression
  98. %type <Nonnull<Expression*>> postfix_expression
  99. %type <Nonnull<Expression*>> ref_deref_expression
  100. %type <Nonnull<Expression*>> combine_lhs
  101. %type <Nonnull<Expression*>> combine_expression
  102. %type <Nonnull<Expression*>> type_expression
  103. %type <Nonnull<Expression*>> fn_type_expression
  104. %type <Nonnull<Expression*>> minus_expression
  105. %type <Nonnull<Expression*>> multiplicative_operand
  106. %type <Nonnull<Expression*>> multiplicative_lhs
  107. %type <Nonnull<Expression*>> multiplicative_expression
  108. %type <Nonnull<Expression*>> additive_operand
  109. %type <Nonnull<Expression*>> additive_lhs
  110. %type <Nonnull<Expression*>> additive_expression
  111. %type <Nonnull<Expression*>> as_operand
  112. %type <Nonnull<Expression*>> as_expression
  113. %type <Nonnull<Expression*>> unimpl_expression
  114. %type <Nonnull<Expression*>> value_expression
  115. %type <Nonnull<Expression*>> comparison_operand
  116. %type <Nonnull<Expression*>> comparison_expression
  117. %type <Nonnull<Expression*>> not_expression
  118. %type <Nonnull<Expression*>> predicate_expression
  119. %type <Nonnull<Expression*>> and_or_operand
  120. %type <Nonnull<Expression*>> and_lhs
  121. %type <Nonnull<Expression*>> and_expression
  122. %type <Nonnull<Expression*>> or_lhs
  123. %type <Nonnull<Expression*>> or_expression
  124. %type <Nonnull<WhereClause*>> where_clause
  125. %type <std::vector<Nonnull<WhereClause*>>> where_clause_list
  126. %type <Nonnull<Expression*>> where_expression
  127. %type <Nonnull<Expression*>> type_or_where_expression
  128. %type <Nonnull<Expression*>> statement_expression
  129. %type <Nonnull<Expression*>> if_expression
  130. %type <Nonnull<Expression*>> expression
  131. %type <Nonnull<GenericBinding*>> generic_binding
  132. %type <Nonnull<Pattern*>> deduced_param
  133. %type <std::vector<Nonnull<AstNode*>>> deduced_params
  134. %type <std::vector<Nonnull<AstNode*>>> impl_deduced_params
  135. %type <std::vector<Nonnull<AstNode*>>> deduced_param_list
  136. %type <Nonnull<Pattern*>> pattern
  137. %type <Nonnull<Pattern*>> non_expression_pattern
  138. %type <BisonWrap<ReturnTerm>> return_term
  139. %type <Nonnull<Expression*>> paren_expression
  140. %type <Nonnull<StructLiteral*>> struct_literal
  141. %type <std::vector<FieldInitializer>> struct_literal_contents
  142. %type <Nonnull<StructTypeLiteral*>> struct_type_literal
  143. %type <std::vector<FieldInitializer>> struct_type_literal_contents
  144. %type <Nonnull<TupleLiteral*>> tuple
  145. %type <std::string> binding_lhs
  146. %type <std::optional<Nonnull<Pattern*>>> receiver
  147. %type <Nonnull<BindingPattern*>> variable_declaration
  148. %type <ParenContents<Expression>> paren_expression_base
  149. %type <ParenContents<Expression>> paren_expression_contents
  150. %type <Nonnull<Pattern*>> paren_pattern
  151. %type <Nonnull<TuplePattern*>> tuple_pattern
  152. %type <Nonnull<TuplePattern*>> maybe_empty_tuple_pattern
  153. %type <std::optional<Nonnull<TuplePattern*>>> type_params
  154. %type <ParenContents<Pattern>> paren_pattern_base
  155. %type <ParenContents<Pattern>> paren_pattern_contents
  156. %type <Nonnull<AlternativeSignature*>> alternative
  157. %type <std::vector<Nonnull<AlternativeSignature*>>> alternative_list
  158. %type <std::vector<Nonnull<AlternativeSignature*>>> alternative_list_contents
  159. %type <BisonWrap<Match::Clause>> clause
  160. %type <std::vector<Match::Clause>> clause_list
  161. %token
  162. // Most tokens have their spelling defined in lexer.lpp.
  163. // table-begin
  164. ADDR
  165. ALIAS
  166. AMPERSAND
  167. AND
  168. API
  169. ARROW
  170. AS
  171. AUTO
  172. AWAIT
  173. BOOL
  174. BREAK
  175. CASE
  176. CHOICE
  177. CLASS
  178. COLON
  179. COLON_BANG
  180. COMMA
  181. CONTINUATION
  182. CONTINUATION_TYPE
  183. CONTINUE
  184. DEFAULT
  185. DOUBLE_ARROW
  186. ELSE
  187. EQUAL
  188. EQUAL_EQUAL
  189. EXTERNAL
  190. FALSE
  191. FN
  192. FN_TYPE
  193. FORALL
  194. IF
  195. IMPL
  196. IMPORT
  197. INTERFACE
  198. IS
  199. LEFT_CURLY_BRACE
  200. LEFT_PARENTHESIS
  201. LEFT_SQUARE_BRACKET
  202. LET
  203. LIBRARY
  204. MATCH
  205. MINUS
  206. NOT
  207. OR
  208. PACKAGE
  209. PERIOD
  210. PLUS
  211. RETURN
  212. RETURNED
  213. RIGHT_CURLY_BRACE
  214. RIGHT_PARENTHESIS
  215. RIGHT_SQUARE_BRACKET
  216. RUN
  217. SELF
  218. SEMICOLON
  219. SLASH
  220. STRING
  221. THEN
  222. TRUE
  223. TYPE
  224. UNDERSCORE
  225. UNIMPL_EXAMPLE
  226. VAR
  227. WHERE
  228. WHILE
  229. // table-end
  230. // Used to track EOF.
  231. END_OF_FILE 0
  232. // Only used for precedence.
  233. FNARROW "-> in return type"
  234. // The lexer determines the arity and fixity of each `*` based on whitespace
  235. // and adjacent tokens. UNARY_STAR indicates that the operator is unary but
  236. // could be either prefix or postfix.
  237. UNARY_STAR "unary *"
  238. PREFIX_STAR "prefix *"
  239. POSTFIX_STAR "postfix *"
  240. BINARY_STAR "binary *"
  241. ;
  242. %start input
  243. %%
  244. input: package_directive import_directives declaration_list
  245. {
  246. *ast = AST({.package = $1.first,
  247. .is_api = $1.second,
  248. .imports = std::move($2),
  249. .declarations = std::move($3)});
  250. }
  251. ;
  252. package_directive:
  253. PACKAGE identifier optional_library_path api_or_impl SEMICOLON
  254. { $$ = {LibraryName({.package = $2, .path = $3}), $4}; }
  255. ;
  256. import_directive:
  257. IMPORT identifier optional_library_path SEMICOLON
  258. { $$ = LibraryName({.package = $2, .path = $3}); }
  259. ;
  260. import_directives:
  261. // Empty
  262. { $$ = std::vector<LibraryName>(); }
  263. | import_directives import_directive
  264. {
  265. $$ = std::move($1);
  266. $$.push_back($2);
  267. }
  268. ;
  269. optional_library_path:
  270. // Empty
  271. { $$ = ""; }
  272. | LIBRARY string_literal
  273. { $$ = $2; }
  274. ;
  275. api_or_impl:
  276. API
  277. { $$ = true; }
  278. | IMPL
  279. { $$ = false; }
  280. ;
  281. primary_expression:
  282. identifier
  283. { $$ = arena->New<IdentifierExpression>(context.source_loc(), $1); }
  284. | designator
  285. {
  286. // `.Foo` is rewritten to `.Self.Foo`.
  287. $$ = arena->New<SimpleMemberAccessExpression>(
  288. context.source_loc(),
  289. arena->New<DotSelfExpression>(context.source_loc()), $1);
  290. }
  291. | PERIOD SELF
  292. { $$ = arena->New<DotSelfExpression>(context.source_loc()); }
  293. | integer_literal
  294. { $$ = arena->New<IntLiteral>(context.source_loc(), $1); }
  295. | string_literal
  296. { $$ = arena->New<StringLiteral>(context.source_loc(), $1); }
  297. | TRUE
  298. { $$ = arena->New<BoolLiteral>(context.source_loc(), true); }
  299. | FALSE
  300. { $$ = arena->New<BoolLiteral>(context.source_loc(), false); }
  301. | sized_type_literal
  302. {
  303. int val;
  304. CARBON_CHECK(llvm::to_integer(llvm::StringRef($1).substr(1), val));
  305. CARBON_CHECK($1[0] == 'i' && val == 32)
  306. << "Only i32 is supported for now: " << $1;
  307. $$ = arena->New<IntTypeLiteral>(context.source_loc());
  308. }
  309. | SELF
  310. // TODO: Should we create a new TypeLiteral for `Self`?
  311. { $$ = arena->New<IdentifierExpression>(context.source_loc(), "Self"); }
  312. | STRING
  313. { $$ = arena->New<StringTypeLiteral>(context.source_loc()); }
  314. | BOOL
  315. { $$ = arena->New<BoolTypeLiteral>(context.source_loc()); }
  316. | TYPE
  317. { $$ = arena->New<TypeTypeLiteral>(context.source_loc()); }
  318. | CONTINUATION_TYPE
  319. { $$ = arena->New<ContinuationTypeLiteral>(context.source_loc()); }
  320. | paren_expression { $$ = $1; }
  321. | struct_literal { $$ = $1; }
  322. | struct_type_literal { $$ = $1; }
  323. | LEFT_SQUARE_BRACKET expression SEMICOLON expression RIGHT_SQUARE_BRACKET
  324. { $$ = arena->New<ArrayTypeLiteral>(context.source_loc(), $2, $4); }
  325. ;
  326. postfix_expression:
  327. primary_expression
  328. | postfix_expression designator
  329. {
  330. $$ = arena->New<SimpleMemberAccessExpression>(context.source_loc(), $1,
  331. $2);
  332. }
  333. | postfix_expression PERIOD LEFT_PARENTHESIS expression RIGHT_PARENTHESIS
  334. {
  335. $$ = arena->New<CompoundMemberAccessExpression>(context.source_loc(), $1,
  336. $4);
  337. }
  338. | postfix_expression LEFT_SQUARE_BRACKET expression RIGHT_SQUARE_BRACKET
  339. { $$ = arena->New<IndexExpression>(context.source_loc(), $1, $3); }
  340. | intrinsic_identifier tuple
  341. { $$ = arena->New<IntrinsicExpression>($1, $2, context.source_loc()); }
  342. | postfix_expression tuple
  343. { $$ = arena->New<CallExpression>(context.source_loc(), $1, $2); }
  344. | postfix_expression POSTFIX_STAR
  345. {
  346. $$ = arena->New<OperatorExpression>(
  347. context.source_loc(), Operator::Ptr,
  348. std::vector<Nonnull<Expression*>>({$1}));
  349. }
  350. | postfix_expression UNARY_STAR
  351. {
  352. $$ = arena->New<OperatorExpression>(
  353. context.source_loc(), Operator::Ptr,
  354. std::vector<Nonnull<Expression*>>({$1}));
  355. }
  356. ;
  357. ref_deref_expression:
  358. postfix_expression
  359. | PREFIX_STAR ref_deref_expression
  360. {
  361. $$ = arena->New<OperatorExpression>(
  362. context.source_loc(), Operator::Deref,
  363. std::vector<Nonnull<Expression*>>({$2}));
  364. }
  365. | UNARY_STAR ref_deref_expression
  366. {
  367. $$ = arena->New<OperatorExpression>(
  368. context.source_loc(), Operator::Deref,
  369. std::vector<Nonnull<Expression*>>({$2}));
  370. }
  371. | AMPERSAND ref_deref_expression
  372. {
  373. $$ = arena->New<OperatorExpression>(
  374. context.source_loc(), Operator::AddressOf,
  375. std::vector<Nonnull<Expression*>>({$2}));
  376. }
  377. ;
  378. fn_type_expression:
  379. FN_TYPE tuple ARROW type_expression
  380. { $$ = arena->New<FunctionTypeLiteral>(context.source_loc(), $2, $4); }
  381. ;
  382. combine_lhs:
  383. ref_deref_expression
  384. | combine_expression
  385. ;
  386. combine_expression:
  387. combine_lhs AMPERSAND ref_deref_expression
  388. {
  389. $$ = arena->New<OperatorExpression>(
  390. context.source_loc(), Operator::Combine,
  391. std::vector<Nonnull<Expression*>>({$1, $3}));
  392. }
  393. ;
  394. type_expression:
  395. ref_deref_expression
  396. | combine_expression
  397. | fn_type_expression
  398. ;
  399. minus_expression:
  400. // ref_deref_expression excluded due to precedence diamond.
  401. MINUS ref_deref_expression
  402. {
  403. $$ = arena->New<OperatorExpression>(
  404. context.source_loc(), Operator::Neg,
  405. std::vector<Nonnull<Expression*>>({$2}));
  406. }
  407. ;
  408. multiplicative_operand:
  409. ref_deref_expression
  410. | minus_expression
  411. ;
  412. multiplicative_lhs:
  413. ref_deref_expression
  414. | multiplicative_expression
  415. ;
  416. multiplicative_expression:
  417. minus_expression
  418. | multiplicative_lhs BINARY_STAR multiplicative_operand
  419. {
  420. $$ = arena->New<OperatorExpression>(
  421. context.source_loc(), Operator::Mul,
  422. std::vector<Nonnull<Expression*>>({$1, $3}));
  423. }
  424. ;
  425. additive_operand:
  426. ref_deref_expression
  427. | multiplicative_expression
  428. ;
  429. additive_lhs:
  430. ref_deref_expression
  431. | additive_expression
  432. ;
  433. additive_expression:
  434. multiplicative_expression
  435. | additive_lhs PLUS additive_operand
  436. {
  437. $$ = arena->New<OperatorExpression>(
  438. context.source_loc(), Operator::Add,
  439. std::vector<Nonnull<Expression*>>({$1, $3}));
  440. }
  441. | additive_lhs MINUS additive_operand
  442. {
  443. $$ = arena->New<OperatorExpression>(
  444. context.source_loc(), Operator::Sub,
  445. std::vector<Nonnull<Expression*>>({$1, $3}));
  446. }
  447. ;
  448. as_operand:
  449. minus_expression
  450. | ref_deref_expression
  451. ;
  452. as_expression:
  453. as_operand AS as_operand
  454. {
  455. $$ = arena->New<OperatorExpression>(
  456. context.source_loc(), Operator::As,
  457. std::vector<Nonnull<Expression*>>{$1, $3});
  458. }
  459. ;
  460. unimpl_expression:
  461. // ref_deref_expression excluded due to precedence diamond.
  462. ref_deref_expression UNIMPL_EXAMPLE ref_deref_expression
  463. {
  464. $$ = arena->New<UnimplementedExpression>(context.source_loc(),
  465. "ExampleInfix", $1, $3);
  466. }
  467. ;
  468. value_expression:
  469. // ref_deref_expression excluded due to precedence diamond.
  470. additive_expression
  471. | as_expression
  472. | combine_expression
  473. | fn_type_expression
  474. | unimpl_expression
  475. ;
  476. comparison_operand:
  477. ref_deref_expression
  478. | value_expression
  479. ;
  480. comparison_expression:
  481. value_expression
  482. | comparison_operand EQUAL_EQUAL comparison_operand
  483. {
  484. $$ = arena->New<OperatorExpression>(
  485. context.source_loc(), Operator::Eq,
  486. std::vector<Nonnull<Expression*>>({$1, $3}));
  487. }
  488. ;
  489. not_expression:
  490. NOT ref_deref_expression
  491. {
  492. $$ = arena->New<OperatorExpression>(
  493. context.source_loc(), Operator::Not,
  494. std::vector<Nonnull<Expression*>>({$2}));
  495. }
  496. ;
  497. predicate_expression:
  498. // ref_deref_expression excluded due to precedence diamond.
  499. not_expression
  500. | comparison_expression
  501. ;
  502. and_or_operand:
  503. ref_deref_expression
  504. | predicate_expression
  505. ;
  506. and_lhs:
  507. and_or_operand
  508. | and_expression
  509. ;
  510. and_expression:
  511. // predicate_expression excluded due to precedence diamond.
  512. and_lhs AND and_or_operand
  513. {
  514. $$ = arena->New<OperatorExpression>(
  515. context.source_loc(), Operator::And,
  516. std::vector<Nonnull<Expression*>>({$1, $3}));
  517. }
  518. ;
  519. or_lhs:
  520. and_or_operand
  521. | or_expression
  522. ;
  523. or_expression:
  524. // predicate_expression excluded due to precedence diamond.
  525. or_lhs OR and_or_operand
  526. {
  527. $$ = arena->New<OperatorExpression>(
  528. context.source_loc(), Operator::Or,
  529. std::vector<Nonnull<Expression*>>({$1, $3}));
  530. }
  531. ;
  532. where_clause:
  533. comparison_operand IS comparison_operand
  534. { $$ = arena->New<IsWhereClause>(context.source_loc(), $1, $3); }
  535. | comparison_operand EQUAL_EQUAL comparison_operand
  536. { $$ = arena->New<EqualsWhereClause>(context.source_loc(), $1, $3); }
  537. ;
  538. where_clause_list:
  539. where_clause
  540. { $$ = {$1}; }
  541. | where_clause_list AND where_clause
  542. {
  543. $$ = std::move($1);
  544. $$.push_back($3);
  545. }
  546. ;
  547. where_expression:
  548. type_expression WHERE where_clause_list
  549. {
  550. auto* self =
  551. arena -> New<GenericBinding>(context.source_loc(), ".Self", $1);
  552. $$ = arena->New<WhereExpression>(context.source_loc(), self, $3);
  553. }
  554. ;
  555. type_or_where_expression:
  556. type_expression
  557. | where_expression
  558. ;
  559. statement_expression:
  560. ref_deref_expression
  561. | predicate_expression
  562. | and_expression
  563. | or_expression
  564. | where_expression
  565. ;
  566. if_expression:
  567. statement_expression
  568. | IF expression THEN if_expression ELSE if_expression
  569. { $$ = arena->New<IfExpression>(context.source_loc(), $2, $4, $6); }
  570. ;
  571. expression:
  572. if_expression
  573. ;
  574. designator: PERIOD identifier { $$ = $2; }
  575. ;
  576. paren_expression: paren_expression_base
  577. { $$ = ExpressionFromParenContents(arena, context.source_loc(), $1); }
  578. ;
  579. tuple: paren_expression_base
  580. { $$ = TupleExpressionFromParenContents(arena, context.source_loc(), $1); }
  581. ;
  582. paren_expression_base:
  583. LEFT_PARENTHESIS RIGHT_PARENTHESIS
  584. { $$ = {.elements = {}, .has_trailing_comma = false}; }
  585. | LEFT_PARENTHESIS paren_expression_contents RIGHT_PARENTHESIS
  586. { $$ = $2; }
  587. | LEFT_PARENTHESIS paren_expression_contents COMMA RIGHT_PARENTHESIS
  588. {
  589. $$ = $2;
  590. $$.has_trailing_comma = true;
  591. }
  592. ;
  593. paren_expression_contents:
  594. expression
  595. { $$ = {.elements = {$1}, .has_trailing_comma = false}; }
  596. | paren_expression_contents COMMA expression
  597. {
  598. $$ = $1;
  599. $$.elements.push_back($3);
  600. }
  601. ;
  602. struct_literal:
  603. LEFT_CURLY_BRACE struct_literal_contents RIGHT_CURLY_BRACE
  604. { $$ = arena->New<StructLiteral>(context.source_loc(), $2); }
  605. | LEFT_CURLY_BRACE struct_literal_contents COMMA RIGHT_CURLY_BRACE
  606. { $$ = arena->New<StructLiteral>(context.source_loc(), $2); }
  607. ;
  608. struct_literal_contents:
  609. designator EQUAL expression
  610. { $$ = {FieldInitializer($1, $3)}; }
  611. | struct_literal_contents COMMA designator EQUAL expression
  612. {
  613. $$ = $1;
  614. $$.push_back(FieldInitializer($3, $5));
  615. }
  616. ;
  617. struct_type_literal:
  618. LEFT_CURLY_BRACE RIGHT_CURLY_BRACE
  619. { $$ = arena->New<StructTypeLiteral>(context.source_loc()); }
  620. | LEFT_CURLY_BRACE struct_type_literal_contents RIGHT_CURLY_BRACE
  621. { $$ = arena->New<StructTypeLiteral>(context.source_loc(), $2); }
  622. | LEFT_CURLY_BRACE struct_type_literal_contents COMMA RIGHT_CURLY_BRACE
  623. { $$ = arena->New<StructTypeLiteral>(context.source_loc(), $2); }
  624. ;
  625. struct_type_literal_contents:
  626. designator COLON expression
  627. { $$ = {FieldInitializer($1, $3)}; }
  628. | struct_type_literal_contents COMMA designator COLON expression
  629. {
  630. $$ = $1;
  631. $$.push_back(FieldInitializer($3, $5));
  632. }
  633. ;
  634. // In many cases, using `pattern` recursively will result in ambiguities.
  635. // When that happens, it's necessary to factor out two separate productions,
  636. // one for when the sub-pattern is an expression, and one for when it is not.
  637. // To facilitate this, non-terminals besides `pattern` whose names contain
  638. // `pattern` are structured to be disjoint from `expression`, unless otherwise
  639. // specified.
  640. pattern:
  641. non_expression_pattern
  642. { $$ = $1; }
  643. | expression
  644. { $$ = arena->New<ExpressionPattern>($1); }
  645. ;
  646. non_expression_pattern:
  647. AUTO
  648. { $$ = arena->New<AutoPattern>(context.source_loc()); }
  649. | binding_lhs COLON pattern
  650. {
  651. $$ = arena->New<BindingPattern>(context.source_loc(), $1, $3,
  652. std::nullopt);
  653. }
  654. | binding_lhs COLON_BANG expression
  655. { $$ = arena->New<GenericBinding>(context.source_loc(), $1, $3); }
  656. | paren_pattern
  657. { $$ = $1; }
  658. | postfix_expression tuple_pattern
  659. {
  660. ErrorOr<Nonnull<AlternativePattern*>> alternative_pattern =
  661. AlternativePattern::Create(arena, context.source_loc(), $1, $2);
  662. if (alternative_pattern.ok()) {
  663. $$ = *alternative_pattern;
  664. } else {
  665. context.RecordSyntaxError(std::move(alternative_pattern).error());
  666. YYERROR;
  667. }
  668. }
  669. | VAR non_expression_pattern
  670. { $$ = arena->New<VarPattern>(context.source_loc(), $2); }
  671. ;
  672. binding_lhs:
  673. identifier { $$ = $1; }
  674. | UNDERSCORE { $$ = AnonymousName; }
  675. ;
  676. paren_pattern: paren_pattern_base
  677. { $$ = PatternFromParenContents(arena, context.source_loc(), $1); }
  678. ;
  679. paren_pattern_base:
  680. LEFT_PARENTHESIS paren_pattern_contents RIGHT_PARENTHESIS
  681. { $$ = $2; }
  682. | LEFT_PARENTHESIS paren_pattern_contents COMMA RIGHT_PARENTHESIS
  683. {
  684. $$ = $2;
  685. $$.has_trailing_comma = true;
  686. }
  687. ;
  688. // paren_pattern is analogous to paren_expression, but in order to avoid
  689. // ambiguities, it must be disjoint from paren_expression, meaning it must
  690. // contain at least one non_expression_pattern. The structure of this rule
  691. // is very different from the corresponding expression rule because is has to
  692. // enforce that requirement.
  693. paren_pattern_contents:
  694. non_expression_pattern
  695. { $$ = {.elements = {$1}, .has_trailing_comma = false}; }
  696. | paren_expression_contents COMMA non_expression_pattern
  697. {
  698. $$ = ParenExpressionToParenPattern(arena, $1);
  699. $$.elements.push_back($3);
  700. }
  701. | paren_pattern_contents COMMA expression
  702. {
  703. $$ = $1;
  704. $$.elements.push_back(arena->New<ExpressionPattern>($3));
  705. }
  706. | paren_pattern_contents COMMA non_expression_pattern
  707. {
  708. $$ = $1;
  709. $$.elements.push_back($3);
  710. }
  711. ;
  712. tuple_pattern: paren_pattern_base
  713. { $$ = TuplePatternFromParenContents(arena, context.source_loc(), $1); }
  714. ;
  715. // Unlike most `pattern` nonterminals, this one overlaps with `expression`,
  716. // so it should be used only when prior context (such as an introducer)
  717. // rules out the possibility of an `expression` at this point.
  718. maybe_empty_tuple_pattern:
  719. LEFT_PARENTHESIS RIGHT_PARENTHESIS
  720. {
  721. $$ = arena->New<TuplePattern>(context.source_loc(),
  722. std::vector<Nonnull<Pattern*>>());
  723. }
  724. | tuple_pattern
  725. { $$ = $1; }
  726. ;
  727. clause:
  728. CASE pattern DOUBLE_ARROW block
  729. { $$ = Match::Clause($2, $4); }
  730. | DEFAULT DOUBLE_ARROW block
  731. {
  732. $$ = Match::Clause(arena->New<BindingPattern>(
  733. context.source_loc(), std::string(AnonymousName),
  734. arena->New<AutoPattern>(context.source_loc()),
  735. ValueCategory::Let),
  736. $3);
  737. }
  738. ;
  739. clause_list:
  740. // Empty
  741. { $$ = {}; }
  742. | clause_list clause
  743. {
  744. $$ = $1;
  745. $$.push_back($2);
  746. }
  747. ;
  748. statement:
  749. statement_expression EQUAL expression SEMICOLON
  750. { $$ = arena->New<Assign>(context.source_loc(), $1, $3); }
  751. | VAR pattern SEMICOLON
  752. {
  753. $$ = arena->New<VariableDefinition>(
  754. context.source_loc(), $2, std::nullopt, ValueCategory::Var,
  755. VariableDefinition::DefinitionType::Var);
  756. }
  757. | VAR pattern EQUAL expression SEMICOLON
  758. {
  759. $$ = arena->New<VariableDefinition>(
  760. context.source_loc(), $2, $4, ValueCategory::Var,
  761. VariableDefinition::DefinitionType::Var);
  762. }
  763. | RETURNED VAR variable_declaration EQUAL expression SEMICOLON
  764. {
  765. $$ = arena->New<VariableDefinition>(
  766. context.source_loc(), $3, $5, ValueCategory::Var,
  767. VariableDefinition::DefinitionType::Returned);
  768. }
  769. | LET pattern EQUAL expression SEMICOLON
  770. {
  771. $$ = arena->New<VariableDefinition>(
  772. context.source_loc(), $2, $4, ValueCategory::Let,
  773. VariableDefinition::DefinitionType::Var);
  774. }
  775. | statement_expression SEMICOLON
  776. { $$ = arena->New<ExpressionStatement>(context.source_loc(), $1); }
  777. | if_statement
  778. { $$ = $1; }
  779. | WHILE LEFT_PARENTHESIS expression RIGHT_PARENTHESIS block
  780. { $$ = arena->New<While>(context.source_loc(), $3, $5); }
  781. | BREAK SEMICOLON
  782. { $$ = arena->New<Break>(context.source_loc()); }
  783. | CONTINUE SEMICOLON
  784. { $$ = arena->New<Continue>(context.source_loc()); }
  785. | RETURN return_expression SEMICOLON
  786. {
  787. auto [return_exp, is_omitted_exp] = $2;
  788. $$ = arena->New<ReturnExpression>(context.source_loc(), return_exp,
  789. is_omitted_exp);
  790. }
  791. | RETURN VAR SEMICOLON
  792. { $$ = arena->New<ReturnVar>(context.source_loc()); }
  793. | MATCH LEFT_PARENTHESIS expression RIGHT_PARENTHESIS LEFT_CURLY_BRACE
  794. clause_list RIGHT_CURLY_BRACE
  795. { $$ = arena->New<Match>(context.source_loc(), $3, $6); }
  796. | CONTINUATION identifier block
  797. { $$ = arena->New<Continuation>(context.source_loc(), $2, $3); }
  798. | RUN expression SEMICOLON
  799. { $$ = arena->New<Run>(context.source_loc(), $2); }
  800. | AWAIT SEMICOLON
  801. { $$ = arena->New<Await>(context.source_loc()); }
  802. ;
  803. if_statement:
  804. IF LEFT_PARENTHESIS expression RIGHT_PARENTHESIS block optional_else
  805. { $$ = arena->New<If>(context.source_loc(), $3, $5, $6); }
  806. ;
  807. optional_else:
  808. // Empty
  809. { $$ = std::nullopt; }
  810. | ELSE if_statement
  811. {
  812. $$ = arena->New<Block>(context.source_loc(),
  813. std::vector<Nonnull<Statement*>>({$2}));
  814. }
  815. | ELSE block
  816. { $$ = $2; }
  817. ;
  818. return_expression:
  819. // Empty
  820. { $$ = {arena->New<TupleLiteral>(context.source_loc()), true}; }
  821. | expression
  822. { $$ = {$1, false}; }
  823. ;
  824. statement_list:
  825. // Empty
  826. { $$ = {}; }
  827. | statement_list statement
  828. {
  829. $$ = std::move($1);
  830. $$.push_back($2);
  831. }
  832. ;
  833. block:
  834. LEFT_CURLY_BRACE statement_list RIGHT_CURLY_BRACE
  835. { $$ = arena->New<Block>(context.source_loc(), std::move($2)); }
  836. ;
  837. return_term:
  838. // Empty
  839. { $$ = ReturnTerm::Omitted(context.source_loc()); }
  840. | ARROW AUTO
  841. { $$ = ReturnTerm::Auto(context.source_loc()); }
  842. | ARROW expression
  843. { $$ = ReturnTerm::Explicit($2); }
  844. ;
  845. generic_binding:
  846. identifier COLON_BANG expression
  847. {
  848. $$ = arena->New<GenericBinding>(context.source_loc(), std::move($1), $3);
  849. }
  850. ;
  851. deduced_param:
  852. generic_binding
  853. { $$ = $1; }
  854. | variable_declaration
  855. { $$ = $1; }
  856. | ADDR variable_declaration
  857. { $$ = arena->New<AddrPattern>(context.source_loc(), $2); }
  858. ;
  859. deduced_param_list:
  860. // Empty
  861. { $$ = {}; }
  862. | deduced_param
  863. { $$ = {$1}; }
  864. | deduced_param_list COMMA deduced_param
  865. {
  866. $$ = $1; // TODO: can we std::move here?
  867. $$.push_back($3);
  868. }
  869. ;
  870. deduced_params:
  871. // Empty
  872. { $$ = std::vector<Nonnull<AstNode*>>(); }
  873. | LEFT_SQUARE_BRACKET deduced_param_list RIGHT_SQUARE_BRACKET
  874. { $$ = $2; }
  875. ;
  876. impl_deduced_params:
  877. // Empty
  878. { $$ = std::vector<Nonnull<AstNode*>>(); }
  879. | FORALL LEFT_SQUARE_BRACKET deduced_param_list RIGHT_SQUARE_BRACKET
  880. { $$ = $3; }
  881. ;
  882. receiver:
  883. // Empty
  884. { $$ = std::nullopt; }
  885. | LEFT_CURLY_BRACE variable_declaration RIGHT_CURLY_BRACE
  886. { $$ = $2; }
  887. | LEFT_CURLY_BRACE ADDR variable_declaration RIGHT_CURLY_BRACE
  888. { $$ = arena->New<AddrPattern>(context.source_loc(), $3); }
  889. ;
  890. function_declaration:
  891. FN identifier deduced_params receiver maybe_empty_tuple_pattern return_term block
  892. {
  893. ErrorOr<FunctionDeclaration*> fn = FunctionDeclaration::Create(
  894. arena, context.source_loc(), $2, $3, $4, $5, $6, $7);
  895. if (fn.ok()) {
  896. $$ = *fn;
  897. } else {
  898. context.RecordSyntaxError(std::move(fn).error());
  899. YYERROR;
  900. }
  901. }
  902. | FN identifier deduced_params receiver maybe_empty_tuple_pattern return_term SEMICOLON
  903. {
  904. ErrorOr<FunctionDeclaration*> fn = FunctionDeclaration::Create(
  905. arena, context.source_loc(), $2, $3, $4, $5, $6, std::nullopt);
  906. if (fn.ok()) {
  907. $$ = *fn;
  908. } else {
  909. context.RecordSyntaxError(std::move(fn).error());
  910. YYERROR;
  911. }
  912. }
  913. ;
  914. variable_declaration: identifier COLON pattern
  915. {
  916. $$ = arena->New<BindingPattern>(context.source_loc(), $1, $3,
  917. std::nullopt);
  918. }
  919. ;
  920. alias_declaration: ALIAS identifier EQUAL expression SEMICOLON
  921. { $$ = arena->New<AliasDeclaration>(context.source_loc(), $2, $4); }
  922. ;
  923. alternative:
  924. identifier tuple
  925. { $$ = arena->New<AlternativeSignature>(context.source_loc(), $1, $2); }
  926. | identifier
  927. {
  928. $$ = arena->New<AlternativeSignature>(
  929. context.source_loc(), $1,
  930. arena->New<TupleLiteral>(context.source_loc()));
  931. }
  932. ;
  933. alternative_list:
  934. // Empty
  935. { $$ = {}; }
  936. | alternative_list_contents
  937. { $$ = $1; }
  938. | alternative_list_contents COMMA
  939. { $$ = $1; }
  940. ;
  941. alternative_list_contents:
  942. alternative
  943. { $$ = {std::move($1)}; }
  944. | alternative_list_contents COMMA alternative
  945. {
  946. $$ = $1;
  947. $$.push_back(std::move($3));
  948. }
  949. ;
  950. type_params:
  951. // Empty
  952. { $$ = std::nullopt; }
  953. | tuple_pattern
  954. { $$ = $1; }
  955. ;
  956. declaration:
  957. function_declaration
  958. { $$ = $1; }
  959. | CLASS identifier type_params LEFT_CURLY_BRACE declaration_list RIGHT_CURLY_BRACE
  960. {
  961. $$ = arena->New<ClassDeclaration>(
  962. context.source_loc(), $2,
  963. arena->New<SelfDeclaration>(context.source_loc()), $3, $5);
  964. }
  965. | CHOICE identifier LEFT_CURLY_BRACE alternative_list RIGHT_CURLY_BRACE
  966. { $$ = arena->New<ChoiceDeclaration>(context.source_loc(), $2, $4); }
  967. | VAR variable_declaration SEMICOLON
  968. {
  969. $$ = arena->New<VariableDeclaration>(context.source_loc(), $2,
  970. std::nullopt, ValueCategory::Var);
  971. }
  972. | VAR variable_declaration EQUAL expression SEMICOLON
  973. {
  974. $$ = arena->New<VariableDeclaration>(context.source_loc(), $2, $4,
  975. ValueCategory::Var);
  976. }
  977. | LET variable_declaration EQUAL expression SEMICOLON
  978. {
  979. $$ = arena->New<VariableDeclaration>(context.source_loc(), $2, $4,
  980. ValueCategory::Let);
  981. }
  982. | INTERFACE identifier type_params LEFT_CURLY_BRACE interface_body RIGHT_CURLY_BRACE
  983. {
  984. // TODO: Type of `Self` should be the interface being declared, not
  985. // `Type`.
  986. auto ty_ty = arena -> New<TypeTypeLiteral>(context.source_loc());
  987. // TODO: Should this be switched to use a `SelfDeclaration` instead?
  988. auto self =
  989. arena -> New<GenericBinding>(context.source_loc(), "Self", ty_ty);
  990. $$ = arena->New<InterfaceDeclaration>(context.source_loc(), $2, $3, self,
  991. $5);
  992. }
  993. | impl_kind IMPL impl_deduced_params impl_type AS type_or_where_expression LEFT_CURLY_BRACE impl_body RIGHT_CURLY_BRACE
  994. {
  995. ErrorOr<ImplDeclaration*> impl = ImplDeclaration::Create(
  996. arena, context.source_loc(), $1, $4, $6, $3, $8);
  997. if (impl.ok()) {
  998. $$ = *impl;
  999. } else {
  1000. context.RecordSyntaxError(std::move(impl).error());
  1001. YYERROR;
  1002. }
  1003. }
  1004. | alias_declaration
  1005. { $$ = $1; }
  1006. ;
  1007. impl_kind:
  1008. // Internal
  1009. { $$ = Carbon::ImplKind::InternalImpl; }
  1010. | EXTERNAL
  1011. { $$ = Carbon::ImplKind::ExternalImpl; }
  1012. ;
  1013. impl_type:
  1014. // Self
  1015. { $$ = arena->New<IdentifierExpression>(context.source_loc(), "Self"); }
  1016. | type_expression
  1017. ;
  1018. declaration_list:
  1019. // Empty
  1020. { $$ = {}; }
  1021. | declaration_list declaration
  1022. {
  1023. $$ = $1;
  1024. $$.push_back(Nonnull<Declaration*>($2));
  1025. }
  1026. ;
  1027. interface_body:
  1028. // Empty
  1029. { $$ = {}; }
  1030. | interface_body function_declaration
  1031. {
  1032. $$ = $1;
  1033. $$.push_back($2);
  1034. }
  1035. | interface_body LET generic_binding SEMICOLON
  1036. {
  1037. $$ = $1;
  1038. $$.push_back(
  1039. arena->New<AssociatedConstantDeclaration>(context.source_loc(), $3));
  1040. }
  1041. ;
  1042. impl_body:
  1043. // Empty
  1044. { $$ = {}; }
  1045. | impl_body function_declaration
  1046. {
  1047. $$ = $1;
  1048. $$.push_back($2);
  1049. }
  1050. | impl_body alias_declaration
  1051. {
  1052. $$ = $1;
  1053. $$.push_back($2);
  1054. }
  1055. ;
  1056. %%