parser.ypp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. //
  24. // Parameters to the parser and lexer
  25. //
  26. // Parameters to the parser are stored therein as protected data members, and
  27. // thus available to its methods.
  28. // "out" parameter passed to the parser, where the AST is written.
  29. %parse-param {std::optional<AST>& parsed_program}
  30. // "inout" parameter passed to both the parser and the lexer.
  31. %param {ParseAndLexContext& context}
  32. // No shift-reduce conflicts are expected.
  33. %expect 0
  34. // -----------------------------------------------------------------------------
  35. %code top {
  36. #include <algorithm>
  37. #include <cstdarg>
  38. #include <cstdio>
  39. #include <cstdlib>
  40. #include <iostream>
  41. #include <list>
  42. #include <vector>
  43. #include "common/check.h"
  44. #include "executable_semantics/syntax/syntax_helpers.h"
  45. #include "executable_semantics/syntax/parse_and_lex_context.h"
  46. #include "llvm/ADT/StringExtras.h"
  47. } // %code top
  48. %code requires {
  49. #include <optional>
  50. #include "executable_semantics/ast/abstract_syntax_tree.h"
  51. #include "executable_semantics/ast/declaration.h"
  52. #include "executable_semantics/ast/expression.h"
  53. #include "executable_semantics/ast/function_definition.h"
  54. #include "executable_semantics/ast/pattern.h"
  55. #include "executable_semantics/common/arena.h"
  56. #include "executable_semantics/syntax/paren_contents.h"
  57. namespace Carbon {
  58. class ParseAndLexContext;
  59. } // namespace Carbon
  60. } // %code requires
  61. %code {
  62. extern int yylineno;
  63. void Carbon::Parser::error(const location_type&, const std::string& message) {
  64. context.PrintDiagnostic(message, yylineno);
  65. }
  66. } // %code
  67. %token <int> integer_literal
  68. %token <std::string> identifier
  69. %token <std::string> sized_type_literal
  70. %type <std::string> designator
  71. %type <Declaration> declaration
  72. %type <FunctionDefinition> function_declaration
  73. %type <FunctionDefinition> function_definition
  74. %type <std::list<Declaration>> declaration_list
  75. %type <const Statement*> statement
  76. %type <const Statement*> if_statement
  77. %type <const Statement*> optional_else
  78. %type <std::pair<const Expression*, bool>> return_expression
  79. %type <const Statement*> block
  80. %type <const Statement*> statement_list
  81. %type <const Expression*> expression
  82. %type <GenericBinding> generic_binding
  83. %type <std::vector<GenericBinding>> deduced_params
  84. %type <std::vector<GenericBinding>> deduced_param_list
  85. %type <const Pattern*> pattern
  86. %type <const Pattern*> non_expression_pattern
  87. %type <std::pair<const Expression*, bool>> return_type
  88. %type <const Expression*> paren_expression
  89. %type <const Expression*> tuple
  90. %type <std::optional<std::string>> binding_lhs
  91. %type <const BindingPattern*> variable_declaration
  92. %type <Member*> member
  93. %type <std::list<Member*>> member_list
  94. %type <ParenContents<Expression>::Element> paren_expression_element
  95. %type <ParenContents<Expression>> paren_expression_base
  96. %type <ParenContents<Expression>> paren_expression_contents
  97. %type <const Pattern*> paren_pattern
  98. %type <const TuplePattern*> tuple_pattern
  99. %type <const TuplePattern*> maybe_empty_tuple_pattern
  100. %type <ParenContents<Pattern>> paren_pattern_base
  101. %type <ParenContents<Pattern>::Element> paren_pattern_element
  102. %type <ParenContents<Pattern>> paren_pattern_contents
  103. %type <std::pair<std::string, const Expression*>> alternative
  104. %type <std::list<std::pair<std::string, const Expression*>>> alternative_list
  105. %type <std::pair<const Pattern*, const Statement*>*> clause
  106. %type <std::list<std::pair<const Pattern*, const Statement*>>*> clause_list
  107. %token END_OF_FILE 0
  108. %token AND
  109. %token OR
  110. %token NOT
  111. %token BOOL
  112. %token TYPE
  113. %token FN
  114. %token FNTY
  115. %token ARROW "->"
  116. %token FNARROW "-> in return type"
  117. %token VAR
  118. %token EQUAL_EQUAL
  119. %token IF
  120. %token ELSE
  121. %token WHILE
  122. %token CONTINUATION_TYPE
  123. %token CONTINUATION
  124. %token RUN
  125. %token AWAIT
  126. %token BREAK
  127. %token CONTINUE
  128. %token RETURN
  129. %token TRUE
  130. %token FALSE
  131. %token STRUCT
  132. %token CHOICE
  133. %token MATCH
  134. %token CASE
  135. %token DBLARROW "=>"
  136. %token DEFAULT
  137. %token AUTO
  138. %token UNDERSCORE
  139. %token
  140. EQUAL "="
  141. MINUS "-"
  142. PLUS "+"
  143. // The lexer determines the arity and fixity of each `*` based on whitespace
  144. // and adjacent tokens. UNARY_STAR indicates that the operator is unary but
  145. // could be either prefix or postfix.
  146. UNARY_STAR "unary *"
  147. PREFIX_STAR "prefix *"
  148. POSTFIX_STAR "postfix *"
  149. BINARY_STAR "binary *"
  150. SLASH "/"
  151. LEFT_PARENTHESIS "("
  152. RIGHT_PARENTHESIS ")"
  153. LEFT_CURLY_BRACE "{"
  154. RIGHT_CURLY_BRACE "}"
  155. LEFT_SQUARE_BRACKET "["
  156. RIGHT_SQUARE_BRACKET "]"
  157. PERIOD "."
  158. COMMA ","
  159. SEMICOLON ";"
  160. COLON_BANG ":!"
  161. COLON ":"
  162. ;
  163. %precedence FNARROW
  164. %precedence "{" "}"
  165. %precedence ":!" ":" "," DBLARROW
  166. %left OR AND
  167. %nonassoc EQUAL_EQUAL
  168. %left "+" "-"
  169. %left BINARY_STAR
  170. %precedence NOT UNARY_MINUS PREFIX_STAR
  171. // We need to give the `UNARY_STAR` token a precedence, rather than overriding
  172. // the precedence of the `expression UNARY_STAR` rule below, because bison
  173. // compares the precedence of the final token (for a shift) to the precedence
  174. // of the other rule (for a reduce) when attempting to resolve a shift-reduce
  175. // conflict. See https://stackoverflow.com/a/26188429/1041090. When UNARY_STAR
  176. // is the final token of a rule, it must be a postfix usage, so we give it the
  177. // same precedence as POSTFIX_STAR.
  178. %precedence POSTFIX_STAR UNARY_STAR
  179. %left "." ARROW
  180. %precedence "(" ")" "[" "]"
  181. %start input
  182. %locations
  183. %%
  184. input: declaration_list
  185. { parsed_program = $1; }
  186. ;
  187. expression:
  188. identifier
  189. { $$ = Expression::MakeIdentifierExpression(yylineno, $1); }
  190. | expression designator
  191. { $$ = Expression::MakeFieldAccessExpression(yylineno, $1, $2); }
  192. | expression "[" expression "]"
  193. { $$ = Expression::MakeIndexExpression(yylineno, $1, $3); }
  194. | integer_literal
  195. { $$ = Expression::MakeIntLiteral(yylineno, $1); }
  196. | TRUE
  197. { $$ = Expression::MakeBoolLiteral(yylineno, true); }
  198. | FALSE
  199. { $$ = Expression::MakeBoolLiteral(yylineno, false); }
  200. | sized_type_literal
  201. {
  202. int val;
  203. CHECK(llvm::to_integer(llvm::StringRef($1).substr(1), val));
  204. CHECK($1[0] == 'i' && val == 32) << "Only i32 is supported for now: " << $1;
  205. $$ = Expression::MakeIntTypeLiteral(yylineno);
  206. }
  207. | BOOL
  208. { $$ = Expression::MakeBoolTypeLiteral(yylineno); }
  209. | TYPE
  210. { $$ = Expression::MakeTypeTypeLiteral(yylineno); }
  211. | CONTINUATION_TYPE
  212. { $$ = Expression::MakeContinuationTypeLiteral(yylineno); }
  213. | paren_expression { $$ = $1; }
  214. | expression EQUAL_EQUAL expression
  215. { $$ = Expression::MakePrimitiveOperatorExpression(
  216. yylineno, Operator::Eq, {$1, $3}); }
  217. | expression "+" expression
  218. { $$ = Expression::MakePrimitiveOperatorExpression(
  219. yylineno, Operator::Add, {$1, $3}); }
  220. | expression "-" expression
  221. { $$ = Expression::MakePrimitiveOperatorExpression(
  222. yylineno, Operator::Sub, {$1, $3}); }
  223. | expression BINARY_STAR expression
  224. { $$ = Expression::MakePrimitiveOperatorExpression(
  225. yylineno, Operator::Mul, {$1, $3}); }
  226. | expression AND expression
  227. { $$ = Expression::MakePrimitiveOperatorExpression(
  228. yylineno, Operator::And, {$1, $3}); }
  229. | expression OR expression
  230. { $$ = Expression::MakePrimitiveOperatorExpression(
  231. yylineno, Operator::Or, {$1, $3}); }
  232. | NOT expression
  233. { $$ = Expression::MakePrimitiveOperatorExpression(
  234. yylineno, Operator::Not, {$2}); }
  235. | "-" expression %prec UNARY_MINUS
  236. { $$ = Expression::MakePrimitiveOperatorExpression(
  237. yylineno, Operator::Neg, {$2}); }
  238. | PREFIX_STAR expression
  239. { $$ = Expression::MakePrimitiveOperatorExpression(
  240. yylineno, Operator::Deref, {$2}); }
  241. | UNARY_STAR expression %prec PREFIX_STAR
  242. { $$ = Expression::MakePrimitiveOperatorExpression(
  243. yylineno, Operator::Deref, {$2}); }
  244. | expression tuple
  245. { $$ = Expression::MakeCallExpression(yylineno, $1, $2); }
  246. | expression POSTFIX_STAR
  247. { $$ = Expression::MakePrimitiveOperatorExpression(
  248. yylineno, Operator::Ptr, {$1}); }
  249. | expression UNARY_STAR
  250. { $$ = Expression::MakePrimitiveOperatorExpression(
  251. yylineno, Operator::Ptr, {$1}); }
  252. | FNTY tuple return_type
  253. { $$ = Expression::MakeFunctionTypeLiteral(
  254. yylineno, $2, $3.first, $3.second); }
  255. ;
  256. designator: "." identifier { $$ = $2; }
  257. ;
  258. paren_expression: paren_expression_base
  259. { $$ = ExpressionFromParenContents(yylineno, $1); }
  260. ;
  261. tuple: paren_expression_base
  262. { $$ = TupleExpressionFromParenContents(yylineno, $1); }
  263. ;
  264. paren_expression_element:
  265. expression
  266. { $$ = {.name = std::nullopt, .term = $1}; }
  267. | designator "=" expression
  268. { $$ = {.name = $1, .term = $3}; }
  269. ;
  270. paren_expression_base:
  271. "(" ")"
  272. { $$ = {.elements = {}, .has_trailing_comma = false}; }
  273. | "(" paren_expression_contents ")"
  274. { $$ = $2; }
  275. | "(" paren_expression_contents "," ")"
  276. {
  277. $$ = $2;
  278. $$.has_trailing_comma = true;
  279. }
  280. ;
  281. paren_expression_contents:
  282. paren_expression_element
  283. { $$ = {.elements = {$1}, .has_trailing_comma = false}; }
  284. | paren_expression_contents "," paren_expression_element
  285. {
  286. $$ = $1;
  287. $$.elements.push_back($3);
  288. }
  289. ;
  290. // In many cases, using `pattern` recursively will result in ambiguities.
  291. // When that happens, it's necessary to factor out two separate productions,
  292. // one for when the sub-pattern is an expression, and one for when it is not.
  293. // To facilitate this, non-terminals besides `pattern` whose names contain
  294. // `pattern` are structured to be disjoint from `expression`, unless otherwise
  295. // specified.
  296. pattern:
  297. non_expression_pattern
  298. { $$ = $1; }
  299. | expression
  300. { $$ = global_arena->New<ExpressionPattern>($1); }
  301. ;
  302. non_expression_pattern:
  303. AUTO
  304. { $$ = global_arena->New<AutoPattern>(yylineno); }
  305. | binding_lhs ":" pattern
  306. { $$ = global_arena->New<BindingPattern>(yylineno, $1, $3); }
  307. | paren_pattern
  308. { $$ = $1; }
  309. | expression tuple_pattern
  310. { $$ = global_arena->New<AlternativePattern>(yylineno, $1, $2); }
  311. ;
  312. binding_lhs:
  313. identifier { $$ = $1; }
  314. | UNDERSCORE { $$ = std::nullopt; }
  315. ;
  316. paren_pattern: paren_pattern_base
  317. { $$ = PatternFromParenContents(yylineno, $1); }
  318. ;
  319. paren_pattern_base:
  320. "(" paren_pattern_contents ")"
  321. { $$ = $2; }
  322. | "(" paren_pattern_contents "," ")"
  323. {
  324. $$ = $2;
  325. $$.has_trailing_comma = true;
  326. }
  327. ;
  328. // paren_pattern is analogous to paren_expression, but in order to avoid
  329. // ambiguities, it must be disjoint from paren_expression, meaning it must
  330. // contain at least one non_expression_pattern. The structure of this rule
  331. // is very different from the corresponding expression rule because is has to
  332. // enforce that requirement.
  333. paren_pattern_contents:
  334. paren_pattern_element
  335. { $$ = {.elements = {$1}, .has_trailing_comma = false }; }
  336. | paren_expression_contents "," paren_pattern_element
  337. {
  338. $$ = ParenExpressionToParenPattern($1);
  339. $$.elements.push_back($3);
  340. }
  341. | paren_pattern_contents "," paren_expression_element
  342. {
  343. $$ = $1;
  344. $$.elements.push_back({.name = $3.name, .term = global_arena->New<ExpressionPattern>($3.term)});
  345. }
  346. | paren_pattern_contents "," paren_pattern_element
  347. {
  348. $$ = $1;
  349. $$.elements.push_back($3);
  350. }
  351. ;
  352. paren_pattern_element:
  353. non_expression_pattern
  354. { $$ = {.name = std::nullopt, .term = $1}; }
  355. | designator "=" non_expression_pattern
  356. { $$ = {.name = $1, .term = $3}; }
  357. ;
  358. tuple_pattern: paren_pattern_base
  359. { $$ = TuplePatternFromParenContents(yylineno, $1); }
  360. ;
  361. // Unlike most `pattern` nonterminals, this one overlaps with `expression`,
  362. // so it should be used only when prior context (such as an introducer)
  363. // rules out the possibility of an `expression` at this point.
  364. maybe_empty_tuple_pattern:
  365. "(" ")"
  366. { $$ = global_arena->New<TuplePattern>(yylineno, std::vector<TuplePattern::Field>()); }
  367. | tuple_pattern
  368. { $$ = $1; }
  369. ;
  370. clause:
  371. CASE pattern DBLARROW statement
  372. { $$ = global_arena->New<std::pair<const Pattern*, const Statement*>>($2, $4); }
  373. | DEFAULT DBLARROW statement
  374. {
  375. auto vp = global_arena->New<BindingPattern>(
  376. yylineno, std::nullopt, global_arena->New<AutoPattern>(yylineno));
  377. $$ = global_arena->New<std::pair<const Pattern*, const Statement*>>(vp, $3);
  378. }
  379. ;
  380. clause_list:
  381. // Empty
  382. {
  383. $$ = global_arena->New<std::list<
  384. std::pair<const Pattern*, const Statement*>>>();
  385. }
  386. | clause clause_list
  387. { $$ = $2; $$->push_front(*$1); }
  388. ;
  389. statement:
  390. expression "=" expression ";"
  391. { $$ = Statement::MakeAssign(yylineno, $1, $3); }
  392. | VAR pattern "=" expression ";"
  393. { $$ = Statement::MakeVariableDefinition(yylineno, $2, $4); }
  394. | expression ";"
  395. { $$ = Statement::MakeExpressionStatement(yylineno, $1); }
  396. | if_statement
  397. { $$ = $1; }
  398. | WHILE "(" expression ")" block
  399. { $$ = Statement::MakeWhile(yylineno, $3, $5); }
  400. | BREAK ";"
  401. { $$ = Statement::MakeBreak(yylineno); }
  402. | CONTINUE ";"
  403. { $$ = Statement::MakeContinue(yylineno); }
  404. | RETURN return_expression ";"
  405. { $$ = Statement::MakeReturn(yylineno, $2.first, $2.second); }
  406. | block
  407. { $$ = $1; }
  408. | MATCH "(" expression ")" "{" clause_list "}"
  409. { $$ = Statement::MakeMatch(yylineno, $3, $6); }
  410. | CONTINUATION identifier statement
  411. { $$ = Statement::MakeContinuation(yylineno, $2, $3); }
  412. | RUN expression ";"
  413. { $$ = Statement::MakeRun(yylineno, $2); }
  414. | AWAIT ";"
  415. { $$ = Statement::MakeAwait(yylineno); }
  416. ;
  417. if_statement:
  418. IF "(" expression ")" block optional_else
  419. { $$ = Statement::MakeIf(yylineno, $3, $5, $6); }
  420. ;
  421. optional_else:
  422. // Empty
  423. { $$ = 0; }
  424. | ELSE if_statement
  425. { $$ = $2; }
  426. | ELSE block
  427. { $$ = $2; }
  428. ;
  429. return_expression:
  430. // Empty
  431. { $$ = {Expression::MakeTupleLiteral(yylineno, {}), true}; }
  432. | expression
  433. { $$ = {$1, false}; }
  434. ;
  435. statement_list:
  436. // Empty
  437. { $$ = 0; }
  438. | statement statement_list
  439. { $$ = Statement::MakeSequence(yylineno, $1, $2); }
  440. ;
  441. block:
  442. "{" statement_list "}"
  443. { $$ = Statement::MakeBlock(yylineno, $2); }
  444. ;
  445. return_type:
  446. // Empty
  447. { $$ = {Expression::MakeTupleLiteral(yylineno, {}), true}; }
  448. | ARROW expression %prec FNARROW
  449. { $$ = {$2, false}; }
  450. ;
  451. generic_binding:
  452. identifier ":!" expression
  453. {
  454. $$ = GenericBinding({.name = std::move($1), .type = $3});
  455. }
  456. ;
  457. deduced_param_list:
  458. // Empty
  459. { $$ = std::vector<GenericBinding>(); }
  460. | generic_binding
  461. {
  462. $$ = std::vector<GenericBinding>();
  463. $$.push_back($1);
  464. }
  465. | generic_binding "," deduced_param_list
  466. {
  467. $$ = $3;
  468. $$.push_back($1);
  469. }
  470. ;
  471. deduced_params:
  472. // Empty
  473. { $$ = std::vector<GenericBinding>(); }
  474. | "[" deduced_param_list "]"
  475. { $$ = $2; }
  476. ;
  477. function_definition:
  478. FN identifier deduced_params maybe_empty_tuple_pattern return_type block
  479. {
  480. $$ = FunctionDefinition(
  481. yylineno, $2, $3, $4,
  482. global_arena->New<ExpressionPattern>($5.first),
  483. $5.second, $6);
  484. }
  485. | FN identifier deduced_params maybe_empty_tuple_pattern DBLARROW expression ";"
  486. {
  487. $$ = FunctionDefinition(
  488. yylineno, $2, $3, $4,
  489. global_arena->New<AutoPattern>(yylineno), true,
  490. Statement::MakeReturn(yylineno, $6, false));
  491. }
  492. ;
  493. function_declaration:
  494. FN identifier deduced_params maybe_empty_tuple_pattern return_type ";"
  495. {
  496. $$ = FunctionDefinition(
  497. yylineno, $2, $3, $4,
  498. global_arena->New<ExpressionPattern>($5.first),
  499. $5.second, nullptr); }
  500. ;
  501. variable_declaration: identifier ":" pattern
  502. { $$ = global_arena->New<BindingPattern>(yylineno, $1, $3); }
  503. ;
  504. member: VAR variable_declaration ";"
  505. { $$ = Member::MakeFieldMember(yylineno, $2); }
  506. ;
  507. member_list:
  508. // Empty
  509. { $$ = std::list<Member*>(); }
  510. | member member_list
  511. { $$ = $2; $$.push_front($1); }
  512. ;
  513. alternative:
  514. identifier tuple
  515. { $$ = std::pair<std::string, const Expression*>($1, $2); }
  516. | identifier
  517. {
  518. $$ = std::pair<std::string, const Expression*>(
  519. $1, Expression::MakeTupleLiteral(yylineno, {}));
  520. }
  521. ;
  522. alternative_list:
  523. // Empty
  524. { $$ = std::list<std::pair<std::string, const Expression*>>(); }
  525. | alternative
  526. {
  527. $$ = std::list<std::pair<std::string, const Expression*>>();
  528. $$.push_front($1);
  529. }
  530. | alternative "," alternative_list
  531. { $$ = std::move($3); $$.push_front($1); }
  532. ;
  533. declaration:
  534. function_definition
  535. { $$ = Declaration::MakeFunctionDeclaration(std::move($1)); }
  536. | function_declaration
  537. { $$ = Declaration::MakeFunctionDeclaration(std::move($1)); }
  538. | STRUCT identifier "{" member_list "}"
  539. {
  540. $$ = Declaration::MakeStructDeclaration(yylineno, $2, $4);
  541. }
  542. | CHOICE identifier "{" alternative_list "}"
  543. {
  544. $$ = Declaration::MakeChoiceDeclaration(yylineno, $2, $4);
  545. }
  546. | VAR variable_declaration "=" expression ";"
  547. {
  548. $$ = Declaration::MakeVariableDeclaration(yylineno, $2, $4);
  549. }
  550. ;
  551. declaration_list:
  552. // Empty
  553. { $$ = std::list<Declaration>(); }
  554. | declaration declaration_list
  555. {
  556. $$ = $2;
  557. $$.push_front($1);
  558. }
  559. ;
  560. %%