parser.ypp 17 KB

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