parser.ypp 18 KB

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