parser.ypp 27 KB

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