parser.ypp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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/field_list.h"
  41. #include "executable_semantics/ast/function_definition.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::FieldList*> field
  73. %type <Carbon::FieldList*> field_list
  74. %type <std::pair<std::string, Carbon::Expression*>*> alternative
  75. %type <std::list<std::pair<std::string, Carbon::Expression*>>*> alternative_list
  76. %type <std::pair<Carbon::Expression*, Carbon::Statement*>*> clause
  77. %type <std::list<std::pair<Carbon::Expression*, Carbon::Statement*>>*> clause_list
  78. %token END_OF_FILE 0
  79. %token AND
  80. %token OR
  81. %token NOT
  82. %token INT
  83. %token BOOL
  84. %token TYPE
  85. %token FN
  86. %token FNTY
  87. %token ARROW
  88. %token VAR
  89. %token EQUAL_EQUAL
  90. %token IF
  91. %token ELSE
  92. %token WHILE
  93. %token CONTINUATION_TYPE
  94. %token CONTINUATION
  95. %token RUN
  96. %token AWAIT
  97. %token BREAK
  98. %token CONTINUE
  99. %token RETURN
  100. %token TRUE
  101. %token FALSE
  102. %token STRUCT
  103. %token CHOICE
  104. %token MATCH
  105. %token CASE
  106. %token DBLARROW
  107. %token DEFAULT
  108. %token AUTO
  109. %token
  110. EQUAL "="
  111. MINUS "-"
  112. PLUS "+"
  113. STAR "*"
  114. SLASH "/"
  115. LEFT_PARENTHESIS "("
  116. RIGHT_PARENTHESIS ")"
  117. LEFT_CURLY_BRACE "{"
  118. RIGHT_CURLY_BRACE "}"
  119. LEFT_SQUARE_BRACKET "["
  120. RIGHT_SQUARE_BRACKET "]"
  121. PERIOD "."
  122. COMMA ","
  123. SEMICOLON ";"
  124. COLON ":"
  125. ;
  126. %nonassoc "{" "}"
  127. %nonassoc ":" "," DBLARROW
  128. %left OR AND
  129. %nonassoc EQUAL_EQUAL NOT
  130. %left "+" "-"
  131. %left "." ARROW
  132. %nonassoc "(" ")" "[" "]"
  133. %start input
  134. %locations
  135. %%
  136. input: declaration_list
  137. { parsed_program = $1; }
  138. ;
  139. pattern:
  140. expression
  141. { $$ = $1; }
  142. ;
  143. expression:
  144. identifier
  145. { $$ = Carbon::MakeVar(yylineno, $1); }
  146. | expression designator
  147. { $$ = Carbon::MakeGetField(yylineno, $1, $2); }
  148. | expression "[" expression "]"
  149. { $$ = Carbon::MakeIndex(yylineno, $1, $3); }
  150. | expression ":" identifier
  151. { $$ = Carbon::MakeVarPat(yylineno, $3, $1); }
  152. | integer_literal
  153. { $$ = Carbon::MakeInt(yylineno, $1); }
  154. | TRUE
  155. { $$ = Carbon::MakeBool(yylineno, true); }
  156. | FALSE
  157. { $$ = Carbon::MakeBool(yylineno, false); }
  158. | INT
  159. { $$ = Carbon::MakeIntType(yylineno); }
  160. | BOOL
  161. { $$ = Carbon::MakeBoolType(yylineno); }
  162. | TYPE
  163. { $$ = Carbon::MakeTypeType(yylineno); }
  164. | AUTO
  165. { $$ = Carbon::MakeAutoType(yylineno); }
  166. | CONTINUATION_TYPE
  167. { $$ = Carbon::MakeContinuationType(yylineno); }
  168. | paren_expression { $$ = $1; }
  169. | expression EQUAL_EQUAL expression
  170. { $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Eq, $1, $3); }
  171. | expression "+" expression
  172. { $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Add, $1, $3); }
  173. | expression "-" expression
  174. { $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Sub, $1, $3); }
  175. | expression AND expression
  176. { $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::And, $1, $3); }
  177. | expression OR expression
  178. { $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Or, $1, $3); }
  179. | NOT expression
  180. { $$ = Carbon::MakeUnOp(yylineno, Carbon::Operator::Not, $2); }
  181. | "-" expression
  182. { $$ = Carbon::MakeUnOp(yylineno, Carbon::Operator::Neg, $2); }
  183. | expression tuple
  184. { $$ = Carbon::MakeCall(yylineno, $1, $2); }
  185. | FNTY tuple return_type
  186. { $$ = Carbon::MakeFunType(yylineno, $2, $3); }
  187. ;
  188. designator: "." identifier { $$ = $2; }
  189. ;
  190. paren_expression: "(" field_list ")"
  191. {
  192. if ($2->fields->size() == 1 &&
  193. $2->fields->front().first == "" &&
  194. !$2->has_explicit_comma) {
  195. $$ = $2->fields->front().second;
  196. } else {
  197. auto vec = new std::vector<std::pair<std::string,Carbon::Expression*>>(
  198. $2->fields->begin(), $2->fields->end());
  199. $$ = Carbon::MakeTuple(yylineno, vec);
  200. }
  201. }
  202. ;
  203. tuple: "(" field_list ")"
  204. {
  205. auto vec = new std::vector<std::pair<std::string,Carbon::Expression*>>(
  206. $2->fields->begin(), $2->fields->end());
  207. $$ = Carbon::MakeTuple(yylineno, vec);
  208. }
  209. field:
  210. pattern
  211. {
  212. auto fields =
  213. new std::list<std::pair<std::string, Carbon::Expression*>>();
  214. fields->push_back(std::make_pair("", $1));
  215. $$ = Carbon::MakeFieldList(fields);
  216. }
  217. | designator "=" pattern
  218. {
  219. auto fields =
  220. new std::list<std::pair<std::string, Carbon::Expression*>>();
  221. fields->push_back(std::make_pair($1, $3));
  222. $$ = Carbon::MakeFieldList(fields);
  223. }
  224. ;
  225. field_list:
  226. // Empty
  227. {
  228. $$ = Carbon::MakeFieldList(
  229. new std::list<std::pair<std::string, Carbon::Expression*>>());
  230. }
  231. | field
  232. { $$ = $1; }
  233. | field "," field_list
  234. { $$ = Carbon::MakeConsField($1, $3); }
  235. ;
  236. clause:
  237. CASE pattern DBLARROW statement
  238. { $$ = new std::pair<Carbon::Expression*, Carbon::Statement*>($2, $4); }
  239. | DEFAULT DBLARROW statement
  240. {
  241. auto vp = Carbon::MakeVarPat(yylineno, "_",
  242. Carbon::MakeAutoType(yylineno));
  243. $$ = new std::pair<Carbon::Expression*, Carbon::Statement*>(vp, $3);
  244. }
  245. ;
  246. clause_list:
  247. // Empty
  248. {
  249. $$ = new std::list<std::pair<Carbon::Expression*, Carbon::Statement*>>();
  250. }
  251. | clause clause_list
  252. { $$ = $2; $$->push_front(*$1); }
  253. ;
  254. statement:
  255. expression "=" expression ";"
  256. { $$ = Carbon::MakeAssign(yylineno, $1, $3); }
  257. | VAR pattern "=" expression ";"
  258. { $$ = Carbon::MakeVarDef(yylineno, $2, $4); }
  259. | expression ";"
  260. { $$ = Carbon::MakeExpStmt(yylineno, $1); }
  261. | IF "(" expression ")" statement optional_else
  262. { $$ = Carbon::MakeIf(yylineno, $3, $5, $6); }
  263. | WHILE "(" expression ")" statement
  264. { $$ = Carbon::MakeWhile(yylineno, $3, $5); }
  265. | BREAK ";"
  266. { $$ = Carbon::MakeBreak(yylineno); }
  267. | CONTINUE ";"
  268. { $$ = Carbon::MakeContinue(yylineno); }
  269. | RETURN expression ";"
  270. { $$ = Carbon::MakeReturn(yylineno, $2); }
  271. | "{" statement_list "}"
  272. { $$ = Carbon::MakeBlock(yylineno, $2); }
  273. | MATCH "(" expression ")" "{" clause_list "}"
  274. { $$ = Carbon::MakeMatch(yylineno, $3, $6); }
  275. | CONTINUATION identifier statement
  276. { $$ = Carbon::MakeContinuationStatement(yylineno, $2, $3); }
  277. | RUN expression ";"
  278. { $$ = Carbon::MakeRun(yylineno, $2); }
  279. | AWAIT ";"
  280. { $$ = Carbon::MakeAwait(yylineno); }
  281. ;
  282. optional_else:
  283. // Empty
  284. { $$ = 0; }
  285. | ELSE statement { $$ = $2; }
  286. ;
  287. statement_list:
  288. // Empty
  289. { $$ = 0; }
  290. | statement statement_list
  291. { $$ = Carbon::MakeSeq(yylineno, $1, $2); }
  292. ;
  293. return_type:
  294. // Empty
  295. {
  296. $$ = Carbon::MakeTuple(
  297. yylineno,
  298. new std::vector<std::pair<std::string, Carbon::Expression*>>());
  299. }
  300. | ARROW expression
  301. { $$ = $2; }
  302. ;
  303. function_definition:
  304. FN identifier tuple return_type "{" statement_list "}"
  305. { $$ = MakeFunDef(yylineno, $2, $4, $3, $6); }
  306. | FN identifier tuple DBLARROW expression ";"
  307. {
  308. $$ = Carbon::MakeFunDef(yylineno, $2, Carbon::MakeAutoType(yylineno), $3,
  309. Carbon::MakeReturn(yylineno, $5));
  310. }
  311. ;
  312. function_declaration:
  313. FN identifier tuple return_type ";"
  314. { $$ = MakeFunDef(yylineno, $2, $4, $3, 0); }
  315. ;
  316. variable_declaration: expression ":" identifier
  317. { $$ = MakeField(yylineno, $3, $1); }
  318. ;
  319. member: VAR variable_declaration ";"
  320. { $$ = $2; }
  321. ;
  322. member_list:
  323. // Empty
  324. { $$ = new std::list<Carbon::Member*>(); }
  325. | member member_list
  326. { $$ = $2; $$->push_front($1); }
  327. ;
  328. alternative:
  329. identifier tuple
  330. { $$ = new std::pair<std::string, Carbon::Expression*>($1, $2); }
  331. | identifier
  332. {
  333. $$ = new std::pair<std::string, Carbon::Expression*>(
  334. $1, Carbon::MakeTuple(
  335. yylineno,
  336. new std::vector<std::pair<std::string, Carbon::Expression*>>()));
  337. }
  338. ;
  339. alternative_list:
  340. // Empty
  341. { $$ = new std::list<std::pair<std::string, Carbon::Expression*>>(); }
  342. | alternative
  343. {
  344. $$ = new std::list<std::pair<std::string, Carbon::Expression*>>();
  345. $$->push_front(*$1);
  346. }
  347. | alternative "," alternative_list
  348. { $$ = $3; $$->push_front(*$1); }
  349. ;
  350. declaration:
  351. function_definition
  352. { $$ = new Carbon::Declaration(Carbon::FunctionDeclaration{$1}); }
  353. | function_declaration
  354. { $$ = new Carbon::Declaration(Carbon::FunctionDeclaration{$1}); }
  355. | STRUCT identifier "{" member_list "}"
  356. {
  357. $$ = new Carbon::Declaration(
  358. Carbon::StructDeclaration{yylineno, $2, $4});
  359. }
  360. | CHOICE identifier "{" alternative_list "}"
  361. {
  362. $$ = new Carbon::Declaration(
  363. Carbon::ChoiceDeclaration{yylineno, $2, std::list(*$4)});
  364. }
  365. | VAR variable_declaration "=" expression ";"
  366. {
  367. $$ = new Carbon::Declaration(
  368. Carbon::VariableDeclaration(yylineno, *$2->u.field.name, $2->u.field.type, $4));
  369. }
  370. ;
  371. declaration_list:
  372. // Empty
  373. { $$ = new std::list<Carbon::Declaration>(); }
  374. | declaration declaration_list
  375. {
  376. $$ = $2;
  377. $$->push_front(*$1);
  378. }
  379. ;
  380. %%