PageRenderTime 63ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/csharp/csharp.y

http://yaxx.googlecode.com/
Happy | 1166 lines | 1137 code | 29 blank | 0 comment | 0 complexity | 65aed62b3a81ff988ff226cecfabf8b5 MD5 | raw file
Possible License(s): AGPL-1.0
  1. /***
  2. *** C# parser/scanner
  3. *** Copyright 2002 James Power, NUI Maynooth, Ireland <james.power@may.ie>
  4. *** This version: 19 Feb 2002
  5. ***
  6. *** This program is free software; you can redistribute it and/or modify
  7. *** it under the terms of the GNU General Public License as published by
  8. *** the Free Software Foundation; either version 2 of the License, or
  9. *** (at your option) any later version.
  10. ***
  11. *** This program is distributed in the hope that it will be useful,
  12. *** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. *** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. *** GNU General Public License for more details.
  15. ***
  16. *** You should have received a copy of the GNU General Public License
  17. *** along with this program; if not, write to the Free Software
  18. *** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. ***/
  20. /* Based on Appendix C of the C# Language Specification,
  21. * version 0.28 of 5/7/2001
  22. */
  23. %{
  24. #define YYERROR_VERBOSE
  25. extern int yylineno;
  26. #include "lex.yy.h"
  27. int yyerror(char *);
  28. %}
  29. /* Special tokens to help disambiguate rank_specifiers */
  30. %token RANK_SPECIFIER
  31. /* C.1.4 Tokens */
  32. %token IDENTIFIER
  33. %token INTEGER_LITERAL REAL_LITERAL CHARACTER_LITERAL STRING_LITERAL
  34. /* C.1.7 KEYWORDS */
  35. %token ABSTRACT AS BASE BOOL BREAK
  36. %token BYTE CASE CATCH CHAR CHECKED
  37. %token CLASS CONST CONTINUE DECIMAL DEFAULT
  38. %token DELEGATE DO DOUBLE ELSE ENUM
  39. %token EVENT EXPLICIT EXTERN FALSE FINALLY
  40. %token FIXED FLOAT FOR FOREACH GOTO
  41. %token IF IMPLICIT IN INT INTERFACE
  42. %token INTERNAL IS LOCK LONG NAMESPACE
  43. %token NEW NULL_LITERAL OBJECT OPERATOR OUT
  44. %token OVERRIDE PARAMS PRIVATE PROTECTED PUBLIC
  45. %token READONLY REF RETURN SBYTE SEALED
  46. %token SHORT SIZEOF STACKALLOC STATIC STRING
  47. %token STRUCT SWITCH THIS THROW TRUE
  48. %token TRY TYPEOF UINT ULONG UNCHECKED
  49. %token UNSAFE USHORT USING VIRTUAL VOID
  50. %token VOLATILE WHILE
  51. /* The ones that seem to be context sensitive */
  52. /* Attribute Targets */
  53. %token ASSEMBLY FIELD METHOD MODULE PARAM PROPERTY TYPE
  54. /* Accessor types */
  55. %token GET SET
  56. /* Event accessor declarations */
  57. %token ADD REMOVE
  58. /*** PUNCTUATION AND SINGLE CHARACTER OPERATORS ***/
  59. %token COMMA
  60. %token LEFT_BRACKET
  61. %token RIGHT_BRACKET
  62. /*** MULTI-CHARACTER OPERATORS ***/
  63. %token PLUSEQ MINUSEQ STAREQ DIVEQ MODEQ
  64. %token XOREQ ANDEQ OREQ LTLT GTGT GTGTEQ LTLTEQ EQEQ NOTEQ
  65. %token LEQ GEQ ANDAND OROR PLUSPLUS MINUSMINUS ARROW
  66. %start compilation_unit /* I think */
  67. %%
  68. /***** C.1.8 Literals *****/
  69. literal
  70. : boolean_literal
  71. | INTEGER_LITERAL
  72. | REAL_LITERAL
  73. | CHARACTER_LITERAL
  74. | STRING_LITERAL
  75. | NULL_LITERAL
  76. ;
  77. boolean_literal
  78. : TRUE
  79. | FALSE
  80. ;
  81. /********** C.2 Syntactic grammar **********/
  82. /***** C.2.1 Basic concepts *****/
  83. namespace_name
  84. : qualified_identifier
  85. ;
  86. type_name
  87. : qualified_identifier
  88. ;
  89. /***** C.2.2 Types *****/
  90. type
  91. : non_array_type
  92. | array_type
  93. ;
  94. non_array_type
  95. : simple_type
  96. | type_name
  97. ;
  98. simple_type
  99. : primitive_type
  100. | class_type
  101. | pointer_type
  102. ;
  103. primitive_type
  104. : numeric_type
  105. | BOOL
  106. ;
  107. numeric_type
  108. : integral_type
  109. | floating_point_type
  110. | DECIMAL
  111. ;
  112. integral_type
  113. : SBYTE | BYTE | SHORT | USHORT | INT | UINT | LONG | ULONG | CHAR
  114. ;
  115. floating_point_type
  116. : FLOAT | DOUBLE
  117. ;
  118. class_type
  119. : OBJECT | STRING
  120. ;
  121. pointer_type
  122. : type '*'
  123. | VOID '*'
  124. ;
  125. array_type
  126. : array_type rank_specifier
  127. | simple_type rank_specifier
  128. | qualified_identifier rank_specifier
  129. ;
  130. rank_specifiers_opt
  131. : /* Nothing */
  132. | rank_specifier rank_specifiers_opt
  133. ;
  134. rank_specifier
  135. : RANK_SPECIFIER
  136. ;
  137. /***** C.2.3 Variables *****/
  138. variable_reference
  139. : expression
  140. ;
  141. /***** C.2.4 Expressions *****/
  142. argument_list
  143. : argument
  144. | argument_list COMMA argument
  145. ;
  146. argument
  147. : expression
  148. | REF variable_reference
  149. | OUT variable_reference
  150. ;
  151. primary_expression
  152. : parenthesized_expression
  153. | primary_expression_no_parenthesis
  154. ;
  155. primary_expression_no_parenthesis
  156. : literal
  157. | array_creation_expression
  158. | member_access
  159. | invocation_expression
  160. | element_access
  161. | this_access
  162. | base_access
  163. | new_expression
  164. | typeof_expression
  165. | sizeof_expression
  166. | checked_expression
  167. | unchecked_expression
  168. ;
  169. parenthesized_expression
  170. : '(' expression ')'
  171. ;
  172. member_access
  173. : primary_expression '.' IDENTIFIER
  174. | primitive_type '.' IDENTIFIER
  175. | class_type '.' IDENTIFIER
  176. ;
  177. invocation_expression
  178. : primary_expression_no_parenthesis '(' argument_list_opt ')'
  179. | qualified_identifier '(' argument_list_opt ')'
  180. ;
  181. argument_list_opt
  182. : /* Nothing */
  183. | argument_list
  184. ;
  185. element_access
  186. : primary_expression LEFT_BRACKET expression_list RIGHT_BRACKET
  187. | qualified_identifier LEFT_BRACKET expression_list RIGHT_BRACKET
  188. ;
  189. expression_list_opt
  190. : /* Nothing */
  191. | expression_list
  192. ;
  193. expression_list
  194. : expression
  195. | expression_list COMMA expression
  196. ;
  197. this_access
  198. : THIS
  199. ;
  200. base_access
  201. : BASE '.' IDENTIFIER
  202. | BASE LEFT_BRACKET expression_list RIGHT_BRACKET
  203. ;
  204. post_increment_expression
  205. : postfix_expression PLUSPLUS
  206. ;
  207. post_decrement_expression
  208. : postfix_expression MINUSMINUS
  209. ;
  210. new_expression
  211. : object_creation_expression
  212. ;
  213. object_creation_expression
  214. : NEW type '(' argument_list_opt ')'
  215. ;
  216. array_creation_expression
  217. : NEW non_array_type LEFT_BRACKET expression_list RIGHT_BRACKET rank_specifiers_opt array_initializer_opt
  218. | NEW array_type array_initializer
  219. ;
  220. array_initializer_opt
  221. : /* Nothing */
  222. | array_initializer
  223. ;
  224. typeof_expression
  225. : TYPEOF '(' type ')'
  226. | TYPEOF '(' VOID ')'
  227. ;
  228. checked_expression
  229. : CHECKED '(' expression ')'
  230. ;
  231. unchecked_expression
  232. : UNCHECKED '(' expression ')'
  233. ;
  234. pointer_member_access
  235. : postfix_expression ARROW IDENTIFIER
  236. ;
  237. addressof_expression
  238. : '&' unary_expression
  239. ;
  240. sizeof_expression
  241. : SIZEOF '(' type ')'
  242. ;
  243. postfix_expression
  244. : primary_expression
  245. | qualified_identifier
  246. | post_increment_expression
  247. | post_decrement_expression
  248. | pointer_member_access
  249. ;
  250. unary_expression_not_plusminus
  251. : postfix_expression
  252. | '!' unary_expression
  253. | '~' unary_expression
  254. | cast_expression
  255. ;
  256. pre_increment_expression
  257. : PLUSPLUS unary_expression
  258. ;
  259. pre_decrement_expression
  260. : MINUSMINUS unary_expression
  261. ;
  262. unary_expression
  263. : unary_expression_not_plusminus
  264. | '+' unary_expression
  265. | '-' unary_expression
  266. | '*' unary_expression
  267. | pre_increment_expression
  268. | pre_decrement_expression
  269. | addressof_expression
  270. ;
  271. /* For cast_expression we really just want a (type) in the brackets,
  272. * but have to do some factoring to get rid of conflict with expressions.
  273. * The paremtnesised expression in the first three cases below should be
  274. * semantically restricted to an identifier, optionally follwed by qualifiers
  275. */
  276. cast_expression
  277. : '(' expression ')' unary_expression_not_plusminus
  278. | '(' multiplicative_expression '*' ')' unary_expression
  279. | '(' qualified_identifier rank_specifier type_quals_opt ')' unary_expression
  280. | '(' primitive_type type_quals_opt ')' unary_expression
  281. | '(' class_type type_quals_opt ')' unary_expression
  282. | '(' VOID type_quals_opt ')' unary_expression
  283. ;
  284. type_quals_opt
  285. : /* Nothing */
  286. | type_quals
  287. ;
  288. type_quals
  289. : type_qual
  290. | type_quals type_qual
  291. ;
  292. type_qual
  293. : rank_specifier
  294. | '*'
  295. ;
  296. multiplicative_expression
  297. : unary_expression
  298. | multiplicative_expression '*' unary_expression
  299. | multiplicative_expression '/' unary_expression
  300. | multiplicative_expression '%' unary_expression
  301. ;
  302. additive_expression
  303. : multiplicative_expression
  304. | additive_expression '+' multiplicative_expression
  305. | additive_expression '-' multiplicative_expression
  306. ;
  307. shift_expression
  308. : additive_expression
  309. | shift_expression LTLT additive_expression
  310. | shift_expression GTGT additive_expression
  311. ;
  312. relational_expression
  313. : shift_expression
  314. | relational_expression '<' shift_expression
  315. | relational_expression '>' shift_expression
  316. | relational_expression LEQ shift_expression
  317. | relational_expression GEQ shift_expression
  318. | relational_expression IS type
  319. | relational_expression AS type
  320. ;
  321. equality_expression
  322. : relational_expression
  323. | equality_expression EQEQ relational_expression
  324. | equality_expression NOTEQ relational_expression
  325. ;
  326. and_expression
  327. : equality_expression
  328. | and_expression '&' equality_expression
  329. ;
  330. exclusive_or_expression
  331. : and_expression
  332. | exclusive_or_expression '^' and_expression
  333. ;
  334. inclusive_or_expression
  335. : exclusive_or_expression
  336. | inclusive_or_expression '|' exclusive_or_expression
  337. ;
  338. conditional_and_expression
  339. : inclusive_or_expression
  340. | conditional_and_expression ANDAND inclusive_or_expression
  341. ;
  342. conditional_or_expression
  343. : conditional_and_expression
  344. | conditional_or_expression OROR conditional_and_expression
  345. ;
  346. conditional_expression
  347. : conditional_or_expression
  348. | conditional_or_expression '?' expression ':' expression
  349. ;
  350. assignment
  351. : unary_expression assignment_operator expression
  352. ;
  353. assignment_operator
  354. : '=' | PLUSEQ | MINUSEQ | STAREQ | DIVEQ | MODEQ
  355. | XOREQ | ANDEQ | OREQ | GTGTEQ | LTLTEQ
  356. ;
  357. expression
  358. : conditional_expression
  359. | assignment
  360. ;
  361. constant_expression
  362. : expression
  363. ;
  364. boolean_expression
  365. : expression
  366. ;
  367. /***** C.2.5 Statements *****/
  368. statement
  369. : labeled_statement
  370. | declaration_statement
  371. | embedded_statement
  372. ;
  373. embedded_statement
  374. : block
  375. | empty_statement
  376. | expression_statement
  377. | selection_statement
  378. | iteration_statement
  379. | jump_statement
  380. | try_statement
  381. | checked_statement
  382. | unchecked_statement
  383. | lock_statement
  384. | using_statement
  385. | unsafe_statement
  386. | fixed_statement
  387. ;
  388. block
  389. : '{' statement_list_opt '}'
  390. ;
  391. statement_list_opt
  392. : /* Nothing */
  393. | statement_list
  394. ;
  395. statement_list
  396. : statement
  397. | statement_list statement
  398. ;
  399. empty_statement
  400. : ';'
  401. ;
  402. labeled_statement
  403. : IDENTIFIER ':' statement
  404. ;
  405. declaration_statement
  406. : local_variable_declaration ';'
  407. | local_constant_declaration ';'
  408. ;
  409. local_variable_declaration
  410. : type variable_declarators
  411. ;
  412. variable_declarators
  413. : variable_declarator
  414. | variable_declarators COMMA variable_declarator
  415. ;
  416. variable_declarator
  417. : IDENTIFIER
  418. | IDENTIFIER '=' variable_initializer
  419. ;
  420. variable_initializer
  421. : expression
  422. | array_initializer
  423. | stackalloc_initializer
  424. ;
  425. stackalloc_initializer
  426. : STACKALLOC type LEFT_BRACKET expression RIGHT_BRACKET
  427. ;
  428. local_constant_declaration
  429. : CONST type constant_declarators
  430. ;
  431. constant_declarators
  432. : constant_declarator
  433. | constant_declarators COMMA constant_declarator
  434. ;
  435. constant_declarator
  436. : IDENTIFIER '=' constant_expression
  437. ;
  438. expression_statement
  439. : statement_expression ';'
  440. ;
  441. statement_expression
  442. : invocation_expression
  443. | object_creation_expression
  444. | assignment
  445. | post_increment_expression
  446. | post_decrement_expression
  447. | pre_increment_expression
  448. | pre_decrement_expression
  449. ;
  450. selection_statement
  451. : if_statement
  452. | switch_statement
  453. ;
  454. if_statement
  455. : IF '(' boolean_expression ')' embedded_statement
  456. | IF '(' boolean_expression ')' embedded_statement ELSE embedded_statement
  457. ;
  458. switch_statement
  459. : SWITCH '(' expression ')' switch_block
  460. ;
  461. switch_block
  462. : '{' switch_sections_opt '}'
  463. ;
  464. switch_sections_opt
  465. : /* Nothing */
  466. | switch_sections
  467. ;
  468. switch_sections
  469. : switch_section
  470. | switch_sections switch_section
  471. ;
  472. switch_section
  473. : switch_labels statement_list
  474. ;
  475. switch_labels
  476. : switch_label
  477. | switch_labels switch_label
  478. ;
  479. switch_label
  480. : CASE constant_expression ':'
  481. | DEFAULT ':'
  482. ;
  483. iteration_statement
  484. : while_statement
  485. | do_statement
  486. | for_statement
  487. | foreach_statement
  488. ;
  489. unsafe_statement
  490. : UNSAFE block
  491. ;
  492. while_statement
  493. : WHILE '(' boolean_expression ')' embedded_statement
  494. ;
  495. do_statement
  496. : DO embedded_statement WHILE '(' boolean_expression ')' ';'
  497. ;
  498. for_statement
  499. : FOR '(' for_initializer_opt ';' for_condition_opt ';' for_iterator_opt ')' embedded_statement
  500. ;
  501. for_initializer_opt
  502. : /* Nothing */
  503. | for_initializer
  504. ;
  505. for_condition_opt
  506. : /* Nothing */
  507. | for_condition
  508. ;
  509. for_iterator_opt
  510. : /* Nothing */
  511. | for_iterator
  512. ;
  513. for_initializer
  514. : local_variable_declaration
  515. | statement_expression_list
  516. ;
  517. for_condition
  518. : boolean_expression
  519. ;
  520. for_iterator
  521. : statement_expression_list
  522. ;
  523. statement_expression_list
  524. : statement_expression
  525. | statement_expression_list COMMA statement_expression
  526. ;
  527. foreach_statement
  528. : FOREACH '(' type IDENTIFIER IN expression ')' embedded_statement
  529. ;
  530. jump_statement
  531. : break_statement
  532. | continue_statement
  533. | goto_statement
  534. | return_statement
  535. | throw_statement
  536. ;
  537. break_statement
  538. : BREAK ';'
  539. ;
  540. continue_statement
  541. : CONTINUE ';'
  542. ;
  543. goto_statement
  544. : GOTO IDENTIFIER ';'
  545. | GOTO CASE constant_expression ';'
  546. | GOTO DEFAULT ';'
  547. ;
  548. return_statement
  549. : RETURN expression_opt ';'
  550. ;
  551. expression_opt
  552. : /* Nothing */
  553. | expression
  554. ;
  555. throw_statement
  556. : THROW expression_opt ';'
  557. ;
  558. try_statement
  559. : TRY block catch_clauses
  560. | TRY block finally_clause
  561. | TRY block catch_clauses finally_clause
  562. ;
  563. catch_clauses
  564. : catch_clause
  565. | catch_clauses catch_clause
  566. ;
  567. catch_clause
  568. : CATCH '(' class_type identifier_opt ')' block
  569. | CATCH '(' type_name identifier_opt ')' block
  570. | CATCH block
  571. ;
  572. identifier_opt
  573. : /* Nothing */
  574. | IDENTIFIER
  575. ;
  576. finally_clause
  577. : FINALLY block
  578. ;
  579. checked_statement
  580. : CHECKED block
  581. ;
  582. unchecked_statement
  583. : UNCHECKED block
  584. ;
  585. lock_statement
  586. : LOCK '(' expression ')' embedded_statement
  587. ;
  588. using_statement
  589. : USING '(' resource_acquisition ')' embedded_statement
  590. ;
  591. resource_acquisition
  592. : local_variable_declaration
  593. | expression
  594. ;
  595. fixed_statement
  596. /*! : FIXED '(' pointer_type fixed_pointer_declarators ')' embedded_statement */
  597. : FIXED '(' type fixed_pointer_declarators ')' embedded_statement
  598. ;
  599. fixed_pointer_declarators
  600. : fixed_pointer_declarator
  601. | fixed_pointer_declarators COMMA fixed_pointer_declarator
  602. ;
  603. fixed_pointer_declarator
  604. : IDENTIFIER '=' expression
  605. ;
  606. compilation_unit
  607. : using_directives_opt attributes_opt
  608. | using_directives_opt namespace_member_declarations
  609. ;
  610. using_directives_opt
  611. : /* Nothing */
  612. | using_directives
  613. ;
  614. attributes_opt
  615. : /* Nothing */
  616. | attributes
  617. ;
  618. namespace_member_declarations_opt
  619. : /* Nothing */
  620. | namespace_member_declarations
  621. ;
  622. namespace_declaration
  623. : attributes_opt NAMESPACE qualified_identifier namespace_body comma_opt
  624. ;
  625. comma_opt
  626. : /* Nothing */
  627. | ';'
  628. ;
  629. /*
  630. qualified_identifier
  631. : IDENTIFIER
  632. | qualified_identifier '.' IDENTIFIER
  633. ;
  634. */
  635. qualified_identifier
  636. : IDENTIFIER
  637. | qualifier IDENTIFIER
  638. ;
  639. qualifier
  640. : IDENTIFIER '.'
  641. | qualifier IDENTIFIER '.'
  642. ;
  643. namespace_body
  644. : '{' using_directives_opt namespace_member_declarations_opt '}'
  645. ;
  646. using_directives
  647. : using_directive
  648. | using_directives using_directive
  649. ;
  650. using_directive
  651. : using_alias_directive
  652. | using_namespace_directive
  653. ;
  654. using_alias_directive
  655. : USING IDENTIFIER '=' qualified_identifier ';'
  656. ;
  657. using_namespace_directive
  658. : USING namespace_name ';'
  659. ;
  660. namespace_member_declarations
  661. : namespace_member_declaration
  662. | namespace_member_declarations namespace_member_declaration
  663. ;
  664. namespace_member_declaration
  665. : namespace_declaration
  666. | type_declaration
  667. ;
  668. type_declaration
  669. : class_declaration
  670. | struct_declaration
  671. | interface_declaration
  672. | enum_declaration
  673. | delegate_declaration
  674. ;
  675. /***** Modifiers *****/
  676. /* This now replaces:
  677. * class_modifier, constant_modifier, field_modifier, method_modifier,
  678. * property_modifier, event_modifier, indexer_modifier, operator_modifier,
  679. * constructor_modifier, struct_modifier, interface_modifier,
  680. * enum_modifier, delegate_modifier
  681. */
  682. modifiers_opt
  683. : /* Nothing */
  684. | modifiers
  685. ;
  686. modifiers
  687. : modifier
  688. | modifiers modifier
  689. ;
  690. modifier
  691. : ABSTRACT
  692. | EXTERN
  693. | INTERNAL
  694. | NEW
  695. | OVERRIDE
  696. | PRIVATE
  697. | PROTECTED
  698. | PUBLIC
  699. | READONLY
  700. | SEALED
  701. | STATIC
  702. | UNSAFE
  703. | VIRTUAL
  704. | VOLATILE
  705. ;
  706. /***** C.2.6 Classes *****/
  707. class_declaration
  708. : attributes_opt modifiers_opt CLASS IDENTIFIER class_base_opt class_body comma_opt
  709. ;
  710. class_base_opt
  711. : /* Nothing */
  712. | class_base
  713. ;
  714. class_base
  715. : ':' class_type
  716. | ':' interface_type_list
  717. | ':' class_type COMMA interface_type_list
  718. ;
  719. interface_type_list
  720. : type_name
  721. | interface_type_list COMMA type_name
  722. ;
  723. class_body
  724. : '{' class_member_declarations_opt '}'
  725. ;
  726. class_member_declarations_opt
  727. : /* Nothing */
  728. | class_member_declarations
  729. ;
  730. class_member_declarations
  731. : class_member_declaration
  732. | class_member_declarations class_member_declaration
  733. ;
  734. class_member_declaration
  735. : constant_declaration
  736. | field_declaration
  737. | method_declaration
  738. | property_declaration
  739. | event_declaration
  740. | indexer_declaration
  741. | operator_declaration
  742. | constructor_declaration
  743. | destructor_declaration
  744. /* | static_constructor_declaration */
  745. | type_declaration
  746. ;
  747. constant_declaration
  748. : attributes_opt modifiers_opt CONST type constant_declarators ';'
  749. ;
  750. field_declaration
  751. : attributes_opt modifiers_opt type variable_declarators ';'
  752. ;
  753. method_declaration
  754. : method_header method_body
  755. ;
  756. /* Inline return_type to avoid conflict with field_declaration */
  757. method_header
  758. : attributes_opt modifiers_opt type qualified_identifier '(' formal_parameter_list_opt ')'
  759. | attributes_opt modifiers_opt VOID qualified_identifier '(' formal_parameter_list_opt ')'
  760. ;
  761. formal_parameter_list_opt
  762. : /* Nothing */
  763. | formal_parameter_list
  764. ;
  765. return_type
  766. : type
  767. | VOID
  768. ;
  769. method_body
  770. : block
  771. | ';'
  772. ;
  773. formal_parameter_list
  774. : formal_parameter
  775. | formal_parameter_list COMMA formal_parameter
  776. ;
  777. formal_parameter
  778. : fixed_parameter
  779. | parameter_array
  780. ;
  781. fixed_parameter
  782. : attributes_opt parameter_modifier_opt type IDENTIFIER
  783. ;
  784. parameter_modifier_opt
  785. : /* Nothing */
  786. | REF
  787. | OUT
  788. ;
  789. parameter_array
  790. /*! : attributes_opt PARAMS array_type IDENTIFIER */
  791. : attributes_opt PARAMS type IDENTIFIER
  792. ;
  793. property_declaration
  794. : attributes_opt modifiers_opt type qualified_identifier
  795. ENTER_getset
  796. '{' accessor_declarations '}'
  797. EXIT_getset
  798. ;
  799. accessor_declarations
  800. : get_accessor_declaration set_accessor_declaration_opt
  801. | set_accessor_declaration get_accessor_declaration_opt
  802. ;
  803. set_accessor_declaration_opt
  804. : /* Nothing */
  805. | set_accessor_declaration
  806. ;
  807. get_accessor_declaration_opt
  808. : /* Nothing */
  809. | get_accessor_declaration
  810. ;
  811. get_accessor_declaration
  812. : attributes_opt GET
  813. EXIT_getset
  814. accessor_body
  815. ENTER_getset
  816. ;
  817. set_accessor_declaration
  818. : attributes_opt SET
  819. EXIT_getset
  820. accessor_body
  821. ENTER_getset
  822. ;
  823. accessor_body
  824. : block
  825. | ';'
  826. ;
  827. event_declaration
  828. : attributes_opt modifiers_opt EVENT type variable_declarators ';'
  829. | attributes_opt modifiers_opt EVENT type qualified_identifier
  830. ENTER_accessor_decl
  831. '{' event_accessor_declarations '}'
  832. EXIT_accessor_decl
  833. ;
  834. event_accessor_declarations
  835. : add_accessor_declaration remove_accessor_declaration
  836. | remove_accessor_declaration add_accessor_declaration
  837. ;
  838. add_accessor_declaration
  839. : attributes_opt ADD
  840. EXIT_accessor_decl
  841. block
  842. ENTER_accessor_decl
  843. ;
  844. remove_accessor_declaration
  845. : attributes_opt REMOVE
  846. EXIT_accessor_decl
  847. block
  848. ENTER_accessor_decl
  849. ;
  850. indexer_declaration
  851. : attributes_opt modifiers_opt indexer_declarator
  852. ENTER_getset
  853. '{' accessor_declarations '}'
  854. EXIT_getset
  855. ;
  856. indexer_declarator
  857. : type THIS LEFT_BRACKET formal_parameter_list RIGHT_BRACKET
  858. /* | type type_name '.' THIS LEFT_BRACKET formal_parameter_list RIGHT_BRACKET */
  859. | type qualified_this LEFT_BRACKET formal_parameter_list RIGHT_BRACKET
  860. ;
  861. qualified_this
  862. : qualifier THIS
  863. ;
  864. /* Widen operator_declaration to make modifiers optional */
  865. operator_declaration
  866. : attributes_opt modifiers_opt operator_declarator operator_body
  867. ;
  868. operator_declarator
  869. : overloadable_operator_declarator
  870. | conversion_operator_declarator
  871. ;
  872. overloadable_operator_declarator
  873. : type OPERATOR overloadable_operator '(' type IDENTIFIER ')'
  874. | type OPERATOR overloadable_operator '(' type IDENTIFIER COMMA type IDENTIFIER ')'
  875. ;
  876. overloadable_operator
  877. : '+' | '-'
  878. | '!' | '~' | PLUSPLUS | MINUSMINUS | TRUE | FALSE
  879. | '*' | '/' | '%' | '&' | '|' | '^'
  880. | LTLT | GTGT | EQEQ | NOTEQ | '>' | '<' | GEQ | LEQ
  881. ;
  882. conversion_operator_declarator
  883. : IMPLICIT OPERATOR type '(' type IDENTIFIER ')'
  884. | EXPLICIT OPERATOR type '(' type IDENTIFIER ')'
  885. ;
  886. constructor_declaration
  887. : attributes_opt modifiers_opt constructor_declarator constructor_body
  888. ;
  889. constructor_declarator
  890. : IDENTIFIER '(' formal_parameter_list_opt ')' constructor_initializer_opt
  891. ;
  892. constructor_initializer_opt
  893. : /* Nothing */
  894. | constructor_initializer
  895. ;
  896. constructor_initializer
  897. : ':' BASE '(' argument_list_opt ')'
  898. | ':' THIS '(' argument_list_opt ')'
  899. ;
  900. /* Widen from unsafe_opt STATIC to modifiers_opt */
  901. /* This is now subsumed by constructor_declaration - delete
  902. * static_constructor_declaration
  903. * : attributes_opt modifiers_opt IDENTIFIER '(' ')' block
  904. * ;
  905. */
  906. /* No longer needed after modification of static_constructor_declaration
  907. * unsafe_opt
  908. * :
  909. * | UNSAFE
  910. * ;
  911. */
  912. /* Widen from unsafe_opt to modifiers_opt */
  913. destructor_declaration
  914. : attributes_opt modifiers_opt '~' IDENTIFIER '(' ')' block
  915. ;
  916. operator_body
  917. : block
  918. | ';'
  919. ;
  920. constructor_body /*** Added by JP - same as method_body ***/
  921. : block
  922. | ';'
  923. ;
  924. /***** C.2.7 Structs *****/
  925. struct_declaration
  926. : attributes_opt modifiers_opt STRUCT IDENTIFIER struct_interfaces_opt struct_body comma_opt
  927. ;
  928. struct_interfaces_opt
  929. : /* Nothing */
  930. | struct_interfaces
  931. ;
  932. struct_interfaces
  933. : ':' interface_type_list
  934. ;
  935. struct_body
  936. : '{' struct_member_declarations_opt '}'
  937. ;
  938. struct_member_declarations_opt
  939. : /* Nothing */
  940. | struct_member_declarations
  941. ;
  942. struct_member_declarations
  943. : struct_member_declaration
  944. | struct_member_declarations struct_member_declaration
  945. ;
  946. struct_member_declaration
  947. : constant_declaration
  948. | field_declaration
  949. | method_declaration
  950. | property_declaration
  951. | event_declaration
  952. | indexer_declaration
  953. | operator_declaration
  954. | constructor_declaration
  955. /* | static_constructor_declaration */
  956. | type_declaration
  957. ;
  958. /***** C.2.8 Arrays *****/
  959. array_initializer
  960. : '{' variable_initializer_list_opt '}'
  961. | '{' variable_initializer_list COMMA '}'
  962. ;
  963. variable_initializer_list_opt
  964. : /* Nothing */
  965. | variable_initializer_list
  966. ;
  967. variable_initializer_list
  968. : variable_initializer
  969. | variable_initializer_list COMMA variable_initializer
  970. ;
  971. /***** C.2.9 Interfaces *****/
  972. interface_declaration
  973. : attributes_opt modifiers_opt INTERFACE IDENTIFIER interface_base_opt interface_body comma_opt
  974. ;
  975. interface_base_opt
  976. : /* Nothing */
  977. | interface_base
  978. ;
  979. interface_base
  980. : ':' interface_type_list
  981. ;
  982. interface_body
  983. : '{' interface_member_declarations_opt '}'
  984. ;
  985. interface_member_declarations_opt
  986. : /* Nothing */
  987. | interface_member_declarations
  988. ;
  989. interface_member_declarations
  990. : interface_member_declaration
  991. | interface_member_declarations interface_member_declaration
  992. ;
  993. interface_member_declaration
  994. : interface_method_declaration
  995. | interface_property_declaration
  996. | interface_event_declaration
  997. | interface_indexer_declaration
  998. ;
  999. /* inline return_type to avoid conflict with interface_property_declaration */
  1000. interface_method_declaration
  1001. : attributes_opt new_opt type IDENTIFIER '(' formal_parameter_list_opt ')' interface_empty_body
  1002. | attributes_opt new_opt VOID IDENTIFIER '(' formal_parameter_list_opt ')' interface_empty_body
  1003. ;
  1004. new_opt
  1005. : /* Nothing */
  1006. | NEW
  1007. ;
  1008. interface_property_declaration
  1009. : attributes_opt new_opt type IDENTIFIER
  1010. ENTER_getset
  1011. '{' interface_accessors '}'
  1012. EXIT_getset
  1013. ;
  1014. interface_indexer_declaration
  1015. : attributes_opt new_opt type THIS
  1016. LEFT_BRACKET formal_parameter_list RIGHT_BRACKET
  1017. ENTER_getset
  1018. '{' interface_accessors '}'
  1019. EXIT_getset
  1020. ;
  1021. interface_accessors
  1022. : attributes_opt GET interface_empty_body
  1023. | attributes_opt SET interface_empty_body
  1024. | attributes_opt GET interface_empty_body attributes_opt SET interface_empty_body
  1025. | attributes_opt SET interface_empty_body attributes_opt GET interface_empty_body
  1026. ;
  1027. interface_event_declaration
  1028. : attributes_opt new_opt EVENT type IDENTIFIER interface_empty_body
  1029. ;
  1030. /* mono seems to allow this */
  1031. interface_empty_body
  1032. : ';'
  1033. | '{' '}'
  1034. ;
  1035. /***** C.2.10 Enums *****/
  1036. enum_declaration
  1037. : attributes_opt modifiers_opt ENUM IDENTIFIER enum_base_opt enum_body comma_opt
  1038. ;
  1039. enum_base_opt
  1040. : /* Nothing */
  1041. | enum_base
  1042. ;
  1043. enum_base
  1044. : ':' integral_type
  1045. ;
  1046. enum_body
  1047. : '{' enum_member_declarations_opt '}'
  1048. | '{' enum_member_declarations COMMA '}'
  1049. ;
  1050. enum_member_declarations_opt
  1051. : /* Nothing */
  1052. | enum_member_declarations
  1053. ;
  1054. enum_member_declarations
  1055. : enum_member_declaration
  1056. | enum_member_declarations COMMA enum_member_declaration
  1057. ;
  1058. enum_member_declaration
  1059. : attributes_opt IDENTIFIER
  1060. | attributes_opt IDENTIFIER '=' constant_expression
  1061. ;
  1062. /***** C.2.11 Delegates *****/
  1063. delegate_declaration
  1064. : attributes_opt modifiers_opt DELEGATE return_type IDENTIFIER '(' formal_parameter_list_opt ')' ';'
  1065. ;
  1066. /***** C.2.12 Attributes *****/
  1067. attributes
  1068. : attribute_sections
  1069. ;
  1070. attribute_sections
  1071. : attribute_section
  1072. | attribute_sections attribute_section
  1073. ;
  1074. attribute_section
  1075. : ENTER_attrib LEFT_BRACKET attribute_target_specifier_opt attribute_list RIGHT_BRACKET EXIT_attrib
  1076. | ENTER_attrib LEFT_BRACKET attribute_target_specifier_opt attribute_list COMMA RIGHT_BRACKET EXIT_attrib
  1077. ;
  1078. attribute_target_specifier_opt
  1079. : /* Nothing */
  1080. | attribute_target_specifier
  1081. ;
  1082. attribute_target_specifier
  1083. : attribute_target ':'
  1084. ;
  1085. attribute_target
  1086. : ASSEMBLY
  1087. | FIELD
  1088. | EVENT
  1089. | METHOD
  1090. | MODULE
  1091. | PARAM
  1092. | PROPERTY
  1093. | RETURN
  1094. | TYPE
  1095. ;
  1096. attribute_list
  1097. : attribute
  1098. | attribute_list COMMA attribute
  1099. ;
  1100. attribute
  1101. : attribute_name attribute_arguments_opt
  1102. ;
  1103. attribute_arguments_opt
  1104. : /* Nothing */
  1105. | attribute_arguments
  1106. ;
  1107. attribute_name
  1108. : type_name
  1109. ;
  1110. attribute_arguments
  1111. : '(' expression_list_opt ')'
  1112. ;
  1113. /** Dummy rules for those context-sensitive "keywords" **/
  1114. ENTER_attrib
  1115. : { lex_enter_attrib(); }
  1116. ;
  1117. EXIT_attrib
  1118. : { lex_exit_attrib(); }
  1119. ;
  1120. ENTER_accessor_decl
  1121. : { lex_enter_accessor(); }
  1122. ;
  1123. EXIT_accessor_decl
  1124. : { lex_exit_accessor(); }
  1125. ;
  1126. ENTER_getset
  1127. : { lex_enter_getset(); }
  1128. ;
  1129. EXIT_getset
  1130. : { lex_exit_getset(); }
  1131. ;
  1132. %%
  1133. int yyerror(char *s)
  1134. {
  1135. fprintf(stderr,"%d:%s LA=[%s]\n",yylineno,s,yytname[YYTRANSLATE(yychar)]);
  1136. return 1;
  1137. }