parser.ypp 17 KB

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