parser.ypp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 "executable_semantics/syntax/syntax_helpers.h"
  40. #include "executable_semantics/syntax/parse_and_lex_context.h"
  41. } // %code top
  42. %code requires {
  43. #include <optional>
  44. #include "executable_semantics/ast/abstract_syntax_tree.h"
  45. #include "executable_semantics/ast/declaration.h"
  46. #include "executable_semantics/ast/function_definition.h"
  47. #include "executable_semantics/syntax/paren_contents.h"
  48. namespace Carbon {
  49. class ParseAndLexContext;
  50. } // namespace Carbon
  51. } // %code requires
  52. %code {
  53. extern int yylineno;
  54. void yy::parser::error(const location_type&, const std::string& message) {
  55. context.PrintDiagnostic(message, yylineno);
  56. }
  57. } // %code
  58. %token <int> integer_literal
  59. %token <char*> identifier
  60. %type <char*> designator
  61. %type <Carbon::Declaration> declaration
  62. %type <Carbon::FunctionDefinition> function_declaration
  63. %type <Carbon::FunctionDefinition> function_definition
  64. %type <std::list<Carbon::Declaration>> declaration_list
  65. %type <const Carbon::Statement*> statement
  66. %type <const Carbon::Statement*> if_statement
  67. %type <const Carbon::Statement*> optional_else
  68. %type <const Carbon::Statement*> block
  69. %type <const Carbon::Statement*> statement_list
  70. %type <const Carbon::Expression*> expression
  71. %type <Carbon::GenericBinding> generic_binding
  72. %type <std::vector<Carbon::GenericBinding>> deduced_params
  73. %type <std::vector<Carbon::GenericBinding>> deduced_param_list
  74. %type <const Carbon::Expression*> pattern
  75. %type <const Carbon::Expression*> return_type
  76. %type <const Carbon::Expression*> paren_expression
  77. %type <const Carbon::Expression*> tuple
  78. %type <std::optional<std::string>> binding_lhs
  79. %type <Carbon::Member*> variable_declaration
  80. %type <Carbon::Member*> member
  81. %type <std::list<Carbon::Member*>> member_list
  82. %type <Carbon::FieldInitializer> field_initializer
  83. %type <Carbon::ParenContents> paren_contents
  84. %type <std::vector<Carbon::FieldInitializer>> paren_contents_without_trailing_comma
  85. %type <std::pair<std::string, const Carbon::Expression*>> alternative
  86. %type <std::list<std::pair<std::string, const Carbon::Expression*>>> alternative_list
  87. %type <std::pair<const Carbon::Expression*, const Carbon::Statement*>*> clause
  88. %type <std::list<std::pair<const Carbon::Expression*, const Carbon::Statement*>>*> clause_list
  89. %token END_OF_FILE 0
  90. %token AND
  91. %token OR
  92. %token NOT
  93. %token INT
  94. %token BOOL
  95. %token TYPE
  96. %token FN
  97. %token FNTY
  98. %token ARROW "->"
  99. %token FNARROW "-> in return type"
  100. %token VAR
  101. %token EQUAL_EQUAL
  102. %token IF
  103. %token ELSE
  104. %token WHILE
  105. %token CONTINUATION_TYPE
  106. %token CONTINUATION
  107. %token RUN
  108. %token AWAIT
  109. %token BREAK
  110. %token CONTINUE
  111. %token RETURN
  112. %token TRUE
  113. %token FALSE
  114. %token STRUCT
  115. %token CHOICE
  116. %token MATCH
  117. %token CASE
  118. %token DBLARROW "=>"
  119. %token DEFAULT
  120. %token AUTO
  121. %token UNDERSCORE
  122. %token
  123. EQUAL "="
  124. MINUS "-"
  125. PLUS "+"
  126. // The lexer determines the arity and fixity of each `*` based on whitespace
  127. // and adjacent tokens. UNARY_STAR indicates that the operator is unary but
  128. // could be either prefix or postfix.
  129. UNARY_STAR "unary *"
  130. PREFIX_STAR "prefix *"
  131. POSTFIX_STAR "postfix *"
  132. BINARY_STAR "binary *"
  133. SLASH "/"
  134. LEFT_PARENTHESIS "("
  135. RIGHT_PARENTHESIS ")"
  136. LEFT_CURLY_BRACE "{"
  137. RIGHT_CURLY_BRACE "}"
  138. LEFT_SQUARE_BRACKET "["
  139. RIGHT_SQUARE_BRACKET "]"
  140. PERIOD "."
  141. COMMA ","
  142. SEMICOLON ";"
  143. COLON_BANG ":!"
  144. COLON ":"
  145. ;
  146. %precedence FNARROW
  147. %precedence "{" "}"
  148. %precedence ":!" ":" "," DBLARROW
  149. %left OR AND
  150. %nonassoc EQUAL_EQUAL
  151. %left "+" "-"
  152. %left BINARY_STAR
  153. %precedence NOT UNARY_MINUS PREFIX_STAR
  154. // We need to give the `UNARY_STAR` token a precedence, rather than overriding
  155. // the precedence of the `expression UNARY_STAR` rule below, because bison
  156. // compares the precedence of the final token (for a shift) to the precedence
  157. // of the other rule (for a reduce) when attempting to resolve a shift-reduce
  158. // conflict. See https://stackoverflow.com/a/26188429/1041090. When UNARY_STAR
  159. // is the final token of a rule, it must be a postfix usage, so we give it the
  160. // same precedence as POSTFIX_STAR.
  161. %precedence POSTFIX_STAR UNARY_STAR
  162. %left "." ARROW
  163. %precedence "(" ")" "[" "]"
  164. %start input
  165. %locations
  166. %%
  167. input: declaration_list
  168. { parsed_program = $1; }
  169. ;
  170. pattern:
  171. expression
  172. { $$ = $1; }
  173. ;
  174. binding_lhs:
  175. identifier { $$ = $1; }
  176. | UNDERSCORE { $$ = std::nullopt; }
  177. ;
  178. expression:
  179. identifier
  180. { $$ = Carbon::Expression::MakeIdentifierExpression(yylineno, $1); }
  181. | expression designator
  182. { $$ = Carbon::Expression::MakeFieldAccessExpression(yylineno, $1, $2); }
  183. | expression "[" expression "]"
  184. { $$ = Carbon::Expression::MakeIndexExpression(yylineno, $1, $3); }
  185. | binding_lhs ":" expression
  186. {
  187. $$ = Carbon::Expression::MakeBindingExpression(yylineno, $1, $3);
  188. }
  189. | integer_literal
  190. { $$ = Carbon::Expression::MakeIntLiteral(yylineno, $1); }
  191. | TRUE
  192. { $$ = Carbon::Expression::MakeBoolLiteral(yylineno, true); }
  193. | FALSE
  194. { $$ = Carbon::Expression::MakeBoolLiteral(yylineno, false); }
  195. | INT
  196. { $$ = Carbon::Expression::MakeIntTypeLiteral(yylineno); }
  197. | BOOL
  198. { $$ = Carbon::Expression::MakeBoolTypeLiteral(yylineno); }
  199. | TYPE
  200. { $$ = Carbon::Expression::MakeTypeTypeLiteral(yylineno); }
  201. | AUTO
  202. { $$ = Carbon::Expression::MakeAutoTypeLiteral(yylineno); }
  203. | CONTINUATION_TYPE
  204. { $$ = Carbon::Expression::MakeContinuationTypeLiteral(yylineno); }
  205. | paren_expression { $$ = $1; }
  206. | expression EQUAL_EQUAL expression
  207. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  208. yylineno, Carbon::Operator::Eq, {$1, $3}); }
  209. | expression "+" expression
  210. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  211. yylineno, Carbon::Operator::Add, {$1, $3}); }
  212. | expression "-" expression
  213. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  214. yylineno, Carbon::Operator::Sub, {$1, $3}); }
  215. | expression BINARY_STAR expression
  216. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  217. yylineno, Carbon::Operator::Mul, {$1, $3}); }
  218. | expression AND expression
  219. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  220. yylineno, Carbon::Operator::And, {$1, $3}); }
  221. | expression OR expression
  222. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  223. yylineno, Carbon::Operator::Or, {$1, $3}); }
  224. | NOT expression
  225. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  226. yylineno, Carbon::Operator::Not, {$2}); }
  227. | "-" expression %prec UNARY_MINUS
  228. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  229. yylineno, Carbon::Operator::Neg, {$2}); }
  230. | PREFIX_STAR expression
  231. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  232. yylineno, Carbon::Operator::Deref, {$2}); }
  233. | UNARY_STAR expression %prec PREFIX_STAR
  234. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  235. yylineno, Carbon::Operator::Deref, {$2}); }
  236. | expression tuple
  237. { $$ = Carbon::Expression::MakeCallExpression(yylineno, $1, $2); }
  238. | expression POSTFIX_STAR
  239. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  240. yylineno, Carbon::Operator::Ptr, {$1}); }
  241. | expression UNARY_STAR
  242. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  243. yylineno, Carbon::Operator::Ptr, {$1}); }
  244. | FNTY tuple return_type
  245. { $$ = Carbon::Expression::MakeFunctionTypeLiteral(yylineno, $2, $3); }
  246. ;
  247. designator: "." identifier { $$ = $2; }
  248. ;
  249. paren_expression: "(" paren_contents ")"
  250. { $$ = $2.AsExpression(yylineno); }
  251. ;
  252. tuple: "(" paren_contents ")"
  253. { $$ = $2.AsTuple(yylineno); }
  254. ;
  255. field_initializer:
  256. pattern
  257. { $$ = Carbon::FieldInitializer({"", $1}); }
  258. | designator "=" pattern
  259. { $$ = Carbon::FieldInitializer({$1, $3}); }
  260. ;
  261. paren_contents:
  262. // Empty
  263. { $$ = Carbon::ParenContents(); }
  264. | paren_contents_without_trailing_comma
  265. {
  266. $$ = Carbon::ParenContents($1,
  267. Carbon::ParenContents::HasTrailingComma::No);
  268. }
  269. | paren_contents_without_trailing_comma ","
  270. {
  271. $$ = Carbon::ParenContents($1,
  272. Carbon::ParenContents::HasTrailingComma::Yes);
  273. }
  274. ;
  275. paren_contents_without_trailing_comma:
  276. field_initializer
  277. { $$ = {$1}; }
  278. | paren_contents_without_trailing_comma "," field_initializer
  279. {
  280. $$ = $1;
  281. $$.push_back($3);
  282. }
  283. ;
  284. clause:
  285. CASE pattern DBLARROW statement
  286. { $$ = new std::pair<const Carbon::Expression*, const Carbon::Statement*>($2, $4); }
  287. | DEFAULT DBLARROW statement
  288. {
  289. auto vp = Carbon::Expression::MakeBindingExpression(
  290. yylineno, "_", Carbon::Expression::MakeAutoTypeLiteral(yylineno));
  291. $$ = new std::pair<const Carbon::Expression*, const Carbon::Statement*>(vp, $3);
  292. }
  293. ;
  294. clause_list:
  295. // Empty
  296. {
  297. $$ = new std::list<std::pair<const Carbon::Expression*, const Carbon::Statement*>>();
  298. }
  299. | clause clause_list
  300. { $$ = $2; $$->push_front(*$1); }
  301. ;
  302. statement:
  303. expression "=" expression ";"
  304. { $$ = Carbon::Statement::MakeAssign(yylineno, $1, $3); }
  305. | VAR pattern "=" expression ";"
  306. { $$ = Carbon::Statement::MakeVariableDefinition(yylineno, $2, $4); }
  307. | expression ";"
  308. { $$ = Carbon::Statement::MakeExpressionStatement(yylineno, $1); }
  309. | if_statement
  310. { $$ = $1; }
  311. | WHILE "(" expression ")" block
  312. { $$ = Carbon::Statement::MakeWhile(yylineno, $3, $5); }
  313. | BREAK ";"
  314. { $$ = Carbon::Statement::MakeBreak(yylineno); }
  315. | CONTINUE ";"
  316. { $$ = Carbon::Statement::MakeContinue(yylineno); }
  317. | RETURN expression ";"
  318. { $$ = Carbon::Statement::MakeReturn(yylineno, $2); }
  319. | block
  320. { $$ = $1; }
  321. | MATCH "(" expression ")" "{" clause_list "}"
  322. { $$ = Carbon::Statement::MakeMatch(yylineno, $3, $6); }
  323. | CONTINUATION identifier statement
  324. { $$ = Carbon::Statement::MakeContinuation(yylineno, $2, $3); }
  325. | RUN expression ";"
  326. { $$ = Carbon::Statement::MakeRun(yylineno, $2); }
  327. | AWAIT ";"
  328. { $$ = Carbon::Statement::MakeAwait(yylineno); }
  329. ;
  330. if_statement:
  331. IF "(" expression ")" block optional_else
  332. { $$ = Carbon::Statement::MakeIf(yylineno, $3, $5, $6); }
  333. ;
  334. optional_else:
  335. // Empty
  336. { $$ = 0; }
  337. | ELSE if_statement
  338. { $$ = $2; }
  339. | ELSE block
  340. { $$ = $2; }
  341. ;
  342. statement_list:
  343. // Empty
  344. { $$ = 0; }
  345. | statement statement_list
  346. { $$ = Carbon::Statement::MakeSequence(yylineno, $1, $2); }
  347. ;
  348. block:
  349. "{" statement_list "}"
  350. { $$ = Carbon::Statement::MakeBlock(yylineno, $2); }
  351. ;
  352. return_type:
  353. // Empty
  354. { $$ = Carbon::Expression::MakeTupleLiteral(yylineno, {}); }
  355. | ARROW expression %prec FNARROW
  356. { $$ = $2; }
  357. ;
  358. generic_binding:
  359. identifier ":!" expression
  360. {
  361. $$ = Carbon::GenericBinding({.name = std::move($1), .type = $3});
  362. }
  363. ;
  364. deduced_param_list:
  365. // Empty
  366. { $$ = std::vector<Carbon::GenericBinding>(); }
  367. | generic_binding
  368. {
  369. $$ = std::vector<Carbon::GenericBinding>();
  370. $$.push_back($1);
  371. }
  372. | generic_binding "," deduced_param_list
  373. {
  374. $$ = $3;
  375. $$.push_back($1);
  376. }
  377. ;
  378. deduced_params:
  379. // Empty
  380. { $$ = std::vector<Carbon::GenericBinding>(); }
  381. | "[" deduced_param_list "]"
  382. { $$ = $2; }
  383. ;
  384. function_definition:
  385. FN identifier deduced_params tuple return_type block
  386. { $$ = Carbon::FunctionDefinition(yylineno, $2, $3, $4, $5, $6); }
  387. | FN identifier deduced_params tuple DBLARROW expression ";"
  388. {
  389. $$ = Carbon::FunctionDefinition(
  390. yylineno, $2, $3, $4,
  391. Carbon::Expression::MakeAutoTypeLiteral(yylineno),
  392. Carbon::Statement::MakeReturn(yylineno, $6));
  393. }
  394. ;
  395. function_declaration:
  396. FN identifier deduced_params tuple return_type ";"
  397. { $$ = Carbon::FunctionDefinition(yylineno, $2, $3, $4, $5, 0); }
  398. ;
  399. variable_declaration: identifier ":" expression
  400. { $$ = Carbon::Member::MakeFieldMember(yylineno, $1, $3); }
  401. ;
  402. member: VAR variable_declaration ";"
  403. { $$ = $2; }
  404. ;
  405. member_list:
  406. // Empty
  407. { $$ = std::list<Carbon::Member*>(); }
  408. | member member_list
  409. { $$ = $2; $$.push_front($1); }
  410. ;
  411. alternative:
  412. identifier tuple
  413. { $$ = std::pair<std::string, const Carbon::Expression*>($1, $2); }
  414. | identifier
  415. {
  416. $$ = std::pair<std::string, const Carbon::Expression*>(
  417. $1, Carbon::Expression::MakeTupleLiteral(yylineno, {}));
  418. }
  419. ;
  420. alternative_list:
  421. // Empty
  422. { $$ = std::list<std::pair<std::string, const Carbon::Expression*>>(); }
  423. | alternative
  424. {
  425. $$ = std::list<std::pair<std::string, const Carbon::Expression*>>();
  426. $$.push_front($1);
  427. }
  428. | alternative "," alternative_list
  429. { $$ = std::move($3); $$.push_front($1); }
  430. ;
  431. declaration:
  432. function_definition
  433. { $$ = Carbon::Declaration::MakeFunctionDeclaration(std::move($1)); }
  434. | function_declaration
  435. { $$ = Carbon::Declaration::MakeFunctionDeclaration(std::move($1)); }
  436. | STRUCT identifier "{" member_list "}"
  437. {
  438. $$ = Carbon::Declaration::MakeStructDeclaration(yylineno, $2, $4);
  439. }
  440. | CHOICE identifier "{" alternative_list "}"
  441. {
  442. $$ = Carbon::Declaration::MakeChoiceDeclaration(yylineno, $2, $4);
  443. }
  444. | VAR variable_declaration "=" expression ";"
  445. {
  446. $$ = Carbon::Declaration::MakeVariableDeclaration(
  447. yylineno, $2->GetFieldMember().name, $2->GetFieldMember().type, $4);
  448. }
  449. ;
  450. declaration_list:
  451. // Empty
  452. { $$ = std::list<Carbon::Declaration>(); }
  453. | declaration declaration_list
  454. {
  455. $$ = $2;
  456. $$.push_front($1);
  457. }
  458. ;
  459. %%