parser.ypp 30 KB

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