parser.ypp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. //
  17. // Parameters to the parser and lexer
  18. //
  19. // Parameters to the parser are stored therein as protected data members, and
  20. // thus available to its methods.
  21. // "out" parameter passed to the parser, where the AST is written.
  22. %parse-param {std::optional<Carbon::AST>& parsed_program}
  23. // "inout" parameter passed to both the parser and the lexer.
  24. %param {Carbon::ParseAndLexContext& context}
  25. // -----------------------------------------------------------------------------
  26. %code top {
  27. #include <algorithm>
  28. #include <cstdarg>
  29. #include <cstdio>
  30. #include <cstdlib>
  31. #include <iostream>
  32. #include <list>
  33. #include "executable_semantics/syntax/syntax_helpers.h"
  34. #include "executable_semantics/syntax/parse_and_lex_context.h"
  35. }
  36. %code requires {
  37. #include <optional>
  38. #include "executable_semantics/ast/abstract_syntax_tree.h"
  39. #include "executable_semantics/ast/declaration.h"
  40. #include "executable_semantics/ast/function_definition.h"
  41. #include "executable_semantics/syntax/paren_contents.h"
  42. namespace Carbon {
  43. class ParseAndLexContext;
  44. }
  45. }
  46. %code {
  47. extern int yylineno;
  48. void yy::parser::error(
  49. const location_type&, const std::string& message)
  50. {
  51. context.PrintDiagnostic(message, yylineno);
  52. }
  53. }
  54. %token <int> integer_literal
  55. %token <char*> identifier
  56. %type <char*> designator
  57. %type <Carbon::Declaration*> declaration
  58. %type <Carbon::FunctionDefinition*> function_declaration
  59. %type <Carbon::FunctionDefinition*> function_definition
  60. %type <std::list<Carbon::Declaration>*> declaration_list
  61. %type <Carbon::Statement*> statement
  62. %type <Carbon::Statement*> optional_else
  63. %type <Carbon::Statement*> statement_list
  64. %type <Carbon::Expression*> expression
  65. %type <Carbon::Expression*> pattern
  66. %type <Carbon::Expression*> return_type
  67. %type <Carbon::Expression*> paren_expression
  68. %type <Carbon::Expression*> tuple
  69. %type <Carbon::Member*> variable_declaration
  70. %type <Carbon::Member*> member
  71. %type <std::list<Carbon::Member*>*> member_list
  72. %type <Carbon::FieldInitializer> field_initializer
  73. %type <Carbon::ParenContents> paren_contents
  74. %type <std::vector<Carbon::FieldInitializer>> paren_contents_without_trailing_comma
  75. %type <std::pair<std::string, Carbon::Expression*>*> alternative
  76. %type <std::list<std::pair<std::string, Carbon::Expression*>>*> alternative_list
  77. %type <std::pair<Carbon::Expression*, Carbon::Statement*>*> clause
  78. %type <std::list<std::pair<Carbon::Expression*, Carbon::Statement*>>*> clause_list
  79. %token END_OF_FILE 0
  80. %token AND
  81. %token OR
  82. %token NOT
  83. %token INT
  84. %token BOOL
  85. %token TYPE
  86. %token FN
  87. %token FNTY
  88. %token ARROW
  89. %token VAR
  90. %token EQUAL_EQUAL
  91. %token IF
  92. %token ELSE
  93. %token WHILE
  94. %token CONTINUATION_TYPE
  95. %token CONTINUATION
  96. %token RUN
  97. %token AWAIT
  98. %token BREAK
  99. %token CONTINUE
  100. %token RETURN
  101. %token TRUE
  102. %token FALSE
  103. %token STRUCT
  104. %token CHOICE
  105. %token MATCH
  106. %token CASE
  107. %token DBLARROW
  108. %token DEFAULT
  109. %token AUTO
  110. %token
  111. EQUAL "="
  112. MINUS "-"
  113. PLUS "+"
  114. STAR "*"
  115. SLASH "/"
  116. LEFT_PARENTHESIS "("
  117. RIGHT_PARENTHESIS ")"
  118. LEFT_CURLY_BRACE "{"
  119. RIGHT_CURLY_BRACE "}"
  120. LEFT_SQUARE_BRACKET "["
  121. RIGHT_SQUARE_BRACKET "]"
  122. PERIOD "."
  123. COMMA ","
  124. SEMICOLON ";"
  125. COLON ":"
  126. ;
  127. %nonassoc "{" "}"
  128. %nonassoc ":" "," DBLARROW
  129. %left OR AND
  130. %nonassoc EQUAL_EQUAL NOT
  131. %left "+" "-"
  132. %left "." ARROW
  133. %nonassoc "(" ")" "[" "]"
  134. %start input
  135. %locations
  136. %%
  137. input: declaration_list
  138. { parsed_program = $1; }
  139. ;
  140. pattern:
  141. expression
  142. { $$ = $1; }
  143. ;
  144. expression:
  145. identifier
  146. { $$ = Carbon::MakeVar(yylineno, $1); }
  147. | expression designator
  148. { $$ = Carbon::MakeGetField(yylineno, $1, $2); }
  149. | expression "[" expression "]"
  150. { $$ = Carbon::MakeIndex(yylineno, $1, $3); }
  151. | expression ":" identifier
  152. { $$ = Carbon::MakeVarPat(yylineno, $3, $1); }
  153. | integer_literal
  154. { $$ = Carbon::MakeInt(yylineno, $1); }
  155. | TRUE
  156. { $$ = Carbon::MakeBool(yylineno, true); }
  157. | FALSE
  158. { $$ = Carbon::MakeBool(yylineno, false); }
  159. | INT
  160. { $$ = Carbon::MakeIntType(yylineno); }
  161. | BOOL
  162. { $$ = Carbon::MakeBoolType(yylineno); }
  163. | TYPE
  164. { $$ = Carbon::MakeTypeType(yylineno); }
  165. | AUTO
  166. { $$ = Carbon::MakeAutoType(yylineno); }
  167. | CONTINUATION_TYPE
  168. { $$ = Carbon::MakeContinuationType(yylineno); }
  169. | paren_expression { $$ = $1; }
  170. | expression EQUAL_EQUAL expression
  171. { $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Eq, $1, $3); }
  172. | expression "+" expression
  173. { $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Add, $1, $3); }
  174. | expression "-" expression
  175. { $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Sub, $1, $3); }
  176. | expression AND expression
  177. { $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::And, $1, $3); }
  178. | expression OR expression
  179. { $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Or, $1, $3); }
  180. | NOT expression
  181. { $$ = Carbon::MakeUnOp(yylineno, Carbon::Operator::Not, $2); }
  182. | "-" expression
  183. { $$ = Carbon::MakeUnOp(yylineno, Carbon::Operator::Neg, $2); }
  184. | expression tuple
  185. { $$ = Carbon::MakeCall(yylineno, $1, $2); }
  186. | FNTY tuple return_type
  187. { $$ = Carbon::MakeFunType(yylineno, $2, $3); }
  188. ;
  189. designator: "." identifier { $$ = $2; }
  190. ;
  191. paren_expression: "(" paren_contents ")"
  192. { $$ = $2.AsExpression(yylineno); }
  193. ;
  194. tuple: "(" paren_contents ")"
  195. { $$ = $2.AsTuple(yylineno); }
  196. ;
  197. field_initializer:
  198. pattern
  199. { $$ = Carbon::FieldInitializer({"", $1}); }
  200. | designator "=" pattern
  201. { $$ = Carbon::FieldInitializer({$1, $3}); }
  202. ;
  203. paren_contents:
  204. // Empty
  205. { $$ = Carbon::ParenContents(); }
  206. | paren_contents_without_trailing_comma
  207. {
  208. $$ = Carbon::ParenContents($1,
  209. Carbon::ParenContents::HasTrailingComma::No);
  210. }
  211. | paren_contents_without_trailing_comma ","
  212. {
  213. $$ = Carbon::ParenContents($1,
  214. Carbon::ParenContents::HasTrailingComma::Yes);
  215. }
  216. ;
  217. paren_contents_without_trailing_comma:
  218. field_initializer
  219. { $$ = {$1}; }
  220. | paren_contents_without_trailing_comma "," field_initializer
  221. {
  222. $$ = $1;
  223. $$.push_back($3);
  224. }
  225. ;
  226. clause:
  227. CASE pattern DBLARROW statement
  228. { $$ = new std::pair<Carbon::Expression*, Carbon::Statement*>($2, $4); }
  229. | DEFAULT DBLARROW statement
  230. {
  231. auto vp = Carbon::MakeVarPat(yylineno, "_",
  232. Carbon::MakeAutoType(yylineno));
  233. $$ = new std::pair<Carbon::Expression*, Carbon::Statement*>(vp, $3);
  234. }
  235. ;
  236. clause_list:
  237. // Empty
  238. {
  239. $$ = new std::list<std::pair<Carbon::Expression*, Carbon::Statement*>>();
  240. }
  241. | clause clause_list
  242. { $$ = $2; $$->push_front(*$1); }
  243. ;
  244. statement:
  245. expression "=" expression ";"
  246. { $$ = Carbon::MakeAssign(yylineno, $1, $3); }
  247. | VAR pattern "=" expression ";"
  248. { $$ = Carbon::MakeVarDef(yylineno, $2, $4); }
  249. | expression ";"
  250. { $$ = Carbon::MakeExpStmt(yylineno, $1); }
  251. | IF "(" expression ")" statement optional_else
  252. { $$ = Carbon::MakeIf(yylineno, $3, $5, $6); }
  253. | WHILE "(" expression ")" statement
  254. { $$ = Carbon::MakeWhile(yylineno, $3, $5); }
  255. | BREAK ";"
  256. { $$ = Carbon::MakeBreak(yylineno); }
  257. | CONTINUE ";"
  258. { $$ = Carbon::MakeContinue(yylineno); }
  259. | RETURN expression ";"
  260. { $$ = Carbon::MakeReturn(yylineno, $2); }
  261. | "{" statement_list "}"
  262. { $$ = Carbon::MakeBlock(yylineno, $2); }
  263. | MATCH "(" expression ")" "{" clause_list "}"
  264. { $$ = Carbon::MakeMatch(yylineno, $3, $6); }
  265. | CONTINUATION identifier statement
  266. { $$ = Carbon::MakeContinuationStatement(yylineno, $2, $3); }
  267. | RUN expression ";"
  268. { $$ = Carbon::MakeRun(yylineno, $2); }
  269. | AWAIT ";"
  270. { $$ = Carbon::MakeAwait(yylineno); }
  271. ;
  272. optional_else:
  273. // Empty
  274. { $$ = 0; }
  275. | ELSE statement { $$ = $2; }
  276. ;
  277. statement_list:
  278. // Empty
  279. { $$ = 0; }
  280. | statement statement_list
  281. { $$ = Carbon::MakeSeq(yylineno, $1, $2); }
  282. ;
  283. return_type:
  284. // Empty
  285. {
  286. $$ = Carbon::MakeTuple(
  287. yylineno,
  288. new std::vector<std::pair<std::string, Carbon::Expression*>>());
  289. }
  290. | ARROW expression
  291. { $$ = $2; }
  292. ;
  293. function_definition:
  294. FN identifier tuple return_type "{" statement_list "}"
  295. { $$ = MakeFunDef(yylineno, $2, $4, $3, $6); }
  296. | FN identifier tuple DBLARROW expression ";"
  297. {
  298. $$ = Carbon::MakeFunDef(yylineno, $2, Carbon::MakeAutoType(yylineno), $3,
  299. Carbon::MakeReturn(yylineno, $5));
  300. }
  301. ;
  302. function_declaration:
  303. FN identifier tuple return_type ";"
  304. { $$ = MakeFunDef(yylineno, $2, $4, $3, 0); }
  305. ;
  306. variable_declaration: expression ":" identifier
  307. { $$ = MakeField(yylineno, $3, $1); }
  308. ;
  309. member: VAR variable_declaration ";"
  310. { $$ = $2; }
  311. ;
  312. member_list:
  313. // Empty
  314. { $$ = new std::list<Carbon::Member*>(); }
  315. | member member_list
  316. { $$ = $2; $$->push_front($1); }
  317. ;
  318. alternative:
  319. identifier tuple
  320. { $$ = new std::pair<std::string, Carbon::Expression*>($1, $2); }
  321. | identifier
  322. {
  323. $$ = new std::pair<std::string, Carbon::Expression*>(
  324. $1, Carbon::MakeTuple(
  325. yylineno,
  326. new std::vector<std::pair<std::string, Carbon::Expression*>>()));
  327. }
  328. ;
  329. alternative_list:
  330. // Empty
  331. { $$ = new std::list<std::pair<std::string, Carbon::Expression*>>(); }
  332. | alternative
  333. {
  334. $$ = new std::list<std::pair<std::string, Carbon::Expression*>>();
  335. $$->push_front(*$1);
  336. }
  337. | alternative "," alternative_list
  338. { $$ = $3; $$->push_front(*$1); }
  339. ;
  340. declaration:
  341. function_definition
  342. { $$ = new Carbon::Declaration(Carbon::FunctionDeclaration{$1}); }
  343. | function_declaration
  344. { $$ = new Carbon::Declaration(Carbon::FunctionDeclaration{$1}); }
  345. | STRUCT identifier "{" member_list "}"
  346. {
  347. $$ = new Carbon::Declaration(
  348. Carbon::StructDeclaration{yylineno, $2, $4});
  349. }
  350. | CHOICE identifier "{" alternative_list "}"
  351. {
  352. $$ = new Carbon::Declaration(
  353. Carbon::ChoiceDeclaration{yylineno, $2, std::list(*$4)});
  354. }
  355. | VAR variable_declaration "=" expression ";"
  356. {
  357. $$ = new Carbon::Declaration(
  358. Carbon::VariableDeclaration(yylineno, *$2->u.field.name, $2->u.field.type, $4));
  359. }
  360. ;
  361. declaration_list:
  362. // Empty
  363. { $$ = new std::list<Carbon::Declaration>(); }
  364. | declaration declaration_list
  365. {
  366. $$ = $2;
  367. $$->push_front(*$1);
  368. }
  369. ;
  370. %%