options { OUTPUT_DIRECTORY="generated_cs"; STATIC = false; } PARSER_BEGIN(CSParser) package tmp.generated_cs; import java.io.*; import java.util.*; import cide.gast.*; import cide.gparser.*; public class CSParser{ } PARSER_END(CSParser) SKIP : { " " | "\t" | "\n" | "\r" | <"//" (~["\n","\r"])* ("\n" | "\r" | "\r\n")> | <"/*" (~["*"])* "*" ("*" | ~["*","/"] (~["*"])* "*")* "/"> } TOKEN : { | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | } TOKEN : { | | | | | | | | | | | | | | | | >" > | >=" > | >>" > | >>=" > | | | | | | " > | =" > | | | | | | | | | | " > } TOKEN:{ <#UNICODE_ESCAPE_SEQUENCE : ("\\" "u" | "\\" "U" ) > | ()*> | <#IDENTIFIER_START_CHARACTER: ["a"-"z","A"-"Z","_","$","@"]> | <#IDENTIFIER_PART_CHARACTER: ["a"-"z","A"-"Z","0"-"9","_","$"]> | )+ ()? ()? | ()+ ("." ()+ ()? ()? | () ()? | () | ()? ) > | )+ ()? > | "'"> | )* "\""> | | <#DECIMAL_DIGIT : ["0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9"]> | <#INTEGER_TYPE_SUFFIX: ( "UL" | "LU" | "ul" | "lu" | "UL" | "LU" | "uL" | "lU" | "U" | "L" | "u" | "l")> | <#HEX_DIGIT : ["0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "A" , "B" , "C" , "D" , "E" , "F" , "a" , "b" , "c" , "d" , "e" , "f"]> | <#EXPONENT_PART: ("e" ()* ()+ | "E" ()* ()+)> | <#SIGN : ["+" , "-" ]> | <#REAL_TYPE_SUFFIX : ["F" , "f" , "D" , "d" , "M" , "m"]> | <#CHARACTER: ( | | | )> | <#SINGLE_CHARACTER:~["\\","\'","\r", "\n", "\u2028", "\u2029"]> | <#SIMPLE_ESCAPE_SEQUENCE: ( "\\'" | "\\\"" | "\\\\" | "\\0" | "\\a" | "\\b" | "\\f" | "\\n" | "\\r" | "\\t" | "\\v")> | <#HEXADECIMAL_ESCAPE_SEQUENCE: "\\" "x" ((()?)?)?> | | | | > | <#SINGLE_REGULAR_STRING_LITERAL_CHARACTER: ~[ "\"" , "\\" , "\r" , "\n" , "\u2028" , "\u2029"]> } GRAMMARSTART compilation_unit : (using_directive)* (attributes_either)? ( compilation_unitEnd)? ; identifier : | "module" | "assembly" ; //C.1.8 Literals literal : boolean_literal | | | | | | "null" ; boolean_literal : "true" | "false" ; //***********************************************************************************************************************************/ // ***** C.2.1 Basic concepts ***** //***********************************************************************************************************************************/ type_name : identifier ("." identifier)* ; //***********************************************************************************************************************************/ //C.2.2 Types //***********************************************************************************************************************************/ type : non_array_type ( rank_specifiers )? ("*")? ; rank_specifiers : (rank_specifier)+ ; rank_specifier : "[" ( "," )* "]" ; non_array_type : predefined_type | type_name ; predefined_type : simple_type | "object" | "string" | "void" ; simple_type : numeric_type | "bool" ; numeric_type : integral_type | floating_point_type | "decimal" ; integral_type : "sbyte" | "byte" | "short" | "ushort" | "int" | "uint" | "long" | "ulong" | "char" ; class_type : type_name | "object" | "string" ; floating_point_type : "float" | "double" ; //***********************************************************************************************************************************/ //C.2.4 Expressions //***********************************************************************************************************************************/ expression : conditional_expression (expressionInternal )? ; expressionInternal: assignment_operator expression; assignment : conditional_expression assignment_operator expression ; assignment_operator : "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "&=" | "|=" | "^=" | "<<=" | ">>=" | ">>>=" ; conditional_expression : conditional_or_expression (conditional_expressionInternal )? ; conditional_expressionInternal: "?" expression ":" conditional_expression; conditional_or_expression : conditional_and_expression ( "||" conditional_or_expression )? ; conditional_and_expression : inclusive_or_expression ( "&&" conditional_and_expression )? ; inclusive_or_expression : exclusive_or_expression ( "|" inclusive_or_expression )? ; exclusive_or_expression : and_expression ("^" exclusive_or_expression )? ; and_expression : equality_expression ( "&" and_expression )? ; equality_expression : relational_expression (equality_expressionInternal )? ; equality_expressionInternal: equality_operator equality_expression; equality_operator : "==" | "!=" ; relational_expression : shift_expression ( relational_expressionInternal )? ; relational_expressionInternal: relational_operator relational_expression | relational_operator2I type; relational_operator : "<" | ">" | "<=" | ">=" ; relational_operator2I : "is" | "as" ; shift_expression : additive_expression ( shift_expressionInternal )? ; shift_expressionInternal: shift_operator shift_expression ; shift_operator : "<<" |">>" ; additive_expression : multiplicative_expression ( additive_expressionInternal )? ; additive_expressionInternal: additive_operator additive_expression; additive_operator : "+" | "-" ; multiplicative_expression : unary_expression ( multiplicative_expressionInternal )? ; multiplicative_expressionInternal:multiplicative_operator multiplicative_expression; multiplicative_operator : "*" | "/" |"%" ; unary_expression : unary_operator unary_expression | LOOK_AHEAD("cast_expression()") cast_expression | primary_expression ; unary_operator //While STAR is listed as a unary operator in the grammar, it appears to be undocumented in the unary expression MS C# deffinition section?!?! : "+" | "-" | "!" | "~" | "*"| "++" | "--" ; cast_expression : "(" type ")" unary_expression ; primary_expression : primary_expression_start ( primary_expression_postfix )? | creation_expression ; primary_expression_start : literal | identifier | parenthesized_expression //"(" expression ")" | predefined_type member_access | this_access //"this" | base_access //"base" (("." identifier) | ("[" expression_list "]")) | typeof_expression //TYPEOF "(" (type | VOID) ")" | sizeof_expression //SIZEOF "(" unmanaged_type ")" | checked_expression //CHECKED "(" expression ")" | unchecked_expression //UNCHECKED "(" expression ")" ; primary_expression_postfix : (primary_expression_postfixInternal )+ ; primary_expression_postfixInternal: member_access //"." identifier | invocation_expression //"(" (argument_list)? ")" | element_access //"[" expression_list "]" | post_increment_expression //INC | post_decrement_expression //DEC | pointer_member_access; array_creation_postfix_expression : (array_creation_postfix_expressionInternal )+ ; array_creation_postfix_expressionInternal: member_access //"." identifier | invocation_expression //"(" (argument_list)? ")" | post_increment_expression //INC | post_decrement_expression //DEC | pointer_member_access ; creation_expression : "new" non_array_type creation_expressionPostFix ; creation_expressionPostFix: "(" (argument_list)? ")" ( primary_expression_postfix )? | LOOK_AHEAD(2) "[" expression_list "]" ( rank_specifiers )? ( array_initializer )? ( array_creation_postfix_expression )? | rank_specifiers array_initializer ( array_creation_postfix_expression )? ; parenthesized_expression : "(" expression ")" ; member_access //assumes a predefined type or a primaryexpression came before this rule : "." identifier ; invocation_expression //assumes a predefined type or a primaryexpression came before this rule : "(" ( argument_list )? ")" ; argument_list : argument ("," argument)* ; argument : (argumentPrefix)? expression ; argumentPrefix:"ref" | "out"; element_access : "[" argument_list "]" ; expression_list : expression (expression_listList)* ; expression_listList:"," expression; this_access : "this" ; base_access : LOOK_AHEAD(2) "base" member_access | "base" element_access ; post_increment_expression : "++" ; post_decrement_expression : "--" ; typeof_expression : "typeof" "(" type ")" ; checked_expression : "checked" "(" expression ")" ; unchecked_expression : "unchecked" "(" expression ")" ; //***********************************************************************************************************************************/ //C.2.5 Statements //***********************************************************************************************************************************/ statement : LOOK_AHEAD(2) identifier ":" statement //labeled_statement | LOOK_AHEAD("type() identifier()") local_variable_declaration ";" @! | local_constant_declaration ";" @! | embedded_statement ; local_variable_declaration : type local_variable_declarators ; local_variable_declarators : local_variable_declarator ("," local_variable_declarators)? ; local_variable_declarator : identifier (local_variable_assignment)? ; local_variable_assignment : "=" local_variable_initializer ; local_variable_initializer : array_initializer | expression ; local_constant_declaration : "const" type local_constant_declarators ; local_constant_declarators : local_constant_declarator ("," local_constant_declarators)? ; local_constant_declarator : identifier "=" local_variable_initializer ; embedded_statement : block //LBRACE (statement_list)? "}" | ";" @! //empty_statement | selection_statement //IF "(" || SWITCH "(" | iteration_statement //WHILE || DO || FOR || FOREACH | jump_statement //BREAK || CONTINUE || GOTO || "return" || THROW | try_statement //TRY block catch_clauses finally_clause | checked_statement //CHECKED block | unchecked_statement //UNCHECKED block | lock_statement //LOCK "(" expression ")" embedded_statement | using_statement //USING "(" resource_acquisition ")" embedded_statement | unsafe_statement //UNSAFE block | fixed_statement | expression_statement //statement_expression "," ; block : "{" @+! (statement)* @-! "}" @! ; statement_list : (statement)+ ; expression_statement : expression ";" @! ; selection_statement : if_statement | switch_statement ; if_statement : "if" "(" expression ")" @+! embedded_statement @-! ("else" @+! embedded_statement @-!)? ; switch_statement : "switch" "(" expression ")" @! switch_block ; switch_block : "{" @+! (switch_section)+ @-! "}" @! ; switch_section : (switch_label)+ statement_list ; switch_label : "case" expression ":" | "default" ":" ; iteration_statement : while_statement //WHILE "(" | do_statement //DO embedded_statement | for_statement //FOR "(" | foreach_statement //FOREACH "(" ; while_statement : "while" "(" expression ")" embedded_statement ; do_statement : "do" embedded_statement "while" "(" expression ")" ; for_statement : "for" "(" (for_initializer)? ";" (expression)? ";" (expression_list)? ")" embedded_statement ; for_initializer : LOOK_AHEAD("type() identifier()") local_variable_declaration | expression_list ; foreach_statement : "foreach" "(" type identifier "in" expression ")" embedded_statement ; jump_statement : "break" ";" @! | "continue" ";" @! | goto_statement | return_statement | throw_statement ; goto_statement : "goto" goto_statementEnd ; goto_statementEnd: identifier ";" @! | "case" expression ";" @! | "default" ";" @!; return_statement : "return" (expression)? ";" @! ; throw_statement : "throw" (expression)? ";" @! ; try_statement : "try" block try_statement_clauses ; try_statement_clauses : catch_clauses (finally_clause)? | finally_clause ; catch_clauses : ( catch_clause )+ ; catch_clause : "catch" catch_clauseEnd ; catch_clauseEnd: "(" type ( identifier )? ")" block | block ; /* catch_clause : cat:CATCH^ ( ( "("! class_type ( identifier )? ")"! block ) {#cat.setType(CatchClause); #cat.setText("specific");} | block {#cat.setType(CatchClause); #cat.setText("generic");} ) ; */ finally_clause : "finally" block ; checked_statement : "checked" block ; unchecked_statement : "unchecked" block ; lock_statement : "lock" "(" expression ")" embedded_statement ; using_statement : "using" "(" resource_acquisition ")" embedded_statement ; resource_acquisition : LOOK_AHEAD("type() identifier() (\",\"|\";\"|\"=\")") local_variable_declaration | expression ; //***********************************************************************************************************************************/ //C.2.6 Namespaces //***********************************************************************************************************************************/ compilation_unitEnd:namespace_member_declaration_no_attr (namespace_member_declaration)*; namespace_declaration : "namespace" type_name namespace_body ; namespace_body : "{" @+! (using_directive)* (namespace_member_declaration)* @-! "}" (";")? @! ; using_directive : "using" type_name using_directiveEnd ; using_directiveEnd: "=" type_name ";" @! | ";" @!; namespace_member_declaration_no_attr : namespace_declaration | (type_modifiers)? type_declaration ; namespace_member_declaration : namespace_declaration | (attributes)? (type_modifiers)? type_declaration ; type_declaration //Type declaration that assumes parent rule has already looked for attributes and modifiers : class_declaration | struct_declaration | interface_declaration | enum_declaration | delegate_declaration ; type_modifiers : (type_modifier)+ ; type_modifier : "new" | "public" | "protected" | "internal" | "private" | "abstract" | "sealed" | "unsafe" ; //***********************************************************************************************************************************/ //C.2.7 Classes section //***********************************************************************************************************************************/ class_declaration : "class" identifier (class_base)? class_body (";")? ; class_base : ":" class_type ("," type_name)* ; interface_type_list : type_name ("," type_name)* ; class_body : "{" @+! (class_member_declaration)* @-! "}" @! ; class_member_declaration : (attributes)? (member_modifiers)? class_member_declarationEnd; class_member_declarationEnd: constant_declaration //CONST type constant_declarators ";" | event_declaration //"event" type variable_declarators ";" | destructor_declaration //"~" identifier "(" ")" body | conversion_operator_declaration | type_declaration | type typeEnd ; typeEnd: constructor_declaration | indexer_declaration_no_interface | operator_declaration | LOOK_AHEAD("indexer_base() \"[\"") indexer_declaration_interface | type_name type_nameEnd; type_nameEnd: method_declaration | property_declaration | field_declaration ; member_modifiers : (member_modifier)+ ; member_modifier : "new" | "public" | "protected" | "internal" | "private" | "static" | "virtual" | "sealed" | "override" | "abstract" | "extern" | "readonly" | "unsafe" ; constant_declaration : "const" type constant_declarators ";" @! ; constant_declarators : constant_declarator ("," constant_declarator)* ; constant_declarator : identifier "=" expression ; field_declaration //okay I know this is ugly, but as long as it all works out I have saved us an arbitrary look ahead operation : field_declaration_start ("," field_declarators)? ";" @! ; field_declaration_start : ("=" variable_initializer)? ; field_declarators : field_declarator ("," field_declarator)* ; field_declarator : identifier ("=" variable_initializer)? ; variable_initializer : array_initializer | expression | stackalloc_initializer ; method_declaration : "(" (formal_parameter_list)? ")" body ; body : block | ";" @! ; formal_parameter_list : ( attributes )? formal_parameter_listEnd; formal_parameter_listEnd: fixed_parameter ( fixed_parameterEnd )* | parameter_array ; formal_parameter_listEndInt: fixed_parameter | parameter_array; fixed_parameterEnd: "," ( attributes )? formal_parameter_listEndInt ; fixed_parameter : (parameter_modifier)? type identifier ; parameter_modifier : "ref" | "out" ; parameter_array //: PARAMS non_array_type rank_specifiers identifier : "params" type identifier ; property_declaration : "{" accessor_declarations "}" ; accessor_declarations : accessor_declaration (accessor_declaration)? ; accessor_declaration : (attributes)? body ; event_declaration : "event" type event_declarationInt ; event_declarationInt: LOOK_AHEAD(2) type_name "{" event_accessor_declarations "}" | event_variable_declarator ("," event_variable_declarator )* ";" @! ; event_variable_declarator : identifier ("=" variable_initializer)? ; event_accessor_declarations : accessor_declaration accessor_declaration ; indexer_declaration_no_interface : "this" "[" formal_parameter_list "]" "{" accessor_declarations "}" ; indexer_declaration_interface : indexer_base "[" formal_parameter_list "]" "{" accessor_declarations "}" ; indexer_base : identifier ("." indexer_baseInt)* ; indexer_baseInt:identifier|"this"; operator_declaration : LOOK_AHEAD("\"operator\" overloadable_unary_operator() \"(\" type() identifier() \")\"") "operator" unary_operator_declaration | "operator" binary_operator_declaration ; unary_operator_declaration : overloadable_unary_operator "(" type identifier ")" body ; overloadable_unary_operator : "+" | "-" | "!" | "~" | "++" | "--" | "true" | "false" ; binary_operator_declaration : overloadable_binary_operator "(" type identifier "," type identifier ")" body ; overloadable_binary_operator : "+" | "-" | "*" | "/" | "%" | "&" | "|" | "^" | "<<" | ">>" | "==" | "!=" | ">" | "<" | ">=" | "<=" ; conversion_operator_declaration : conversion_operator "operator" type "(" type identifier ")" body ; conversion_operator : "implicit" |"explicit" ; constructor_declaration : "(" (formal_parameter_list)? ")" (constructor_initializer)? body ; constructor_initializer : ":" constructor_initializerInt "(" (argument_list)? ")" ; constructor_initializerInt: "base" | "this"; destructor_declaration : "~" identifier "(" ")" body ; //***********************************************************************************************************************************/ //C.2.8 Structs //***********************************************************************************************************************************/ struct_declaration : "struct" identifier (base_interfaces)? class_body (";")? ; base_interfaces : ":" interface_type_list ; //***********************************************************************************************************************************/ //C.2.9 Arrays //***********************************************************************************************************************************/ array_initializer : "{" rest_of_array_initializer ; rest_of_array_initializer : "}" | variable_initializer rest_of_array_initializerEnd ; rest_of_array_initializerEnd: "}" | "," rest_of_array_initializer; //***********************************************************************************************************************************/ //C.2.10 Interfaces //***********************************************************************************************************************************/ interface_declaration : "interface" identifier (base_interfaces)? interface_body (";")? ; interface_body : "{" @+! (interface_member_declaration)* @-! "}" @! ; interface_member_declaration : (attributes)? (interface_member_modifier)* interface_member_declarationEnd ; interface_member_declarationEnd: interface_event_declaration //"event" type identifier ";" | type interface_member_declarationEndType; interface_member_declarationEndType: interface_indexer_declaration //type "this" "[" formal_parameter_list "]" "{" interface_accessors "}" | identifier interface_member_declarationEndTypeIdentifier; interface_member_declarationEndTypeIdentifier: interface_method_declaration //type identifier "(" (formal_parameter_list)? ")" ";" | interface_property_declaration //type identifier "{" interface_accessors "}" ; interface_member_modifier : "new" | "unsafe" ; interface_event_declaration : "event" type identifier ";" @! ; interface_indexer_declaration : "this" "[" formal_parameter_list "]" "{" interface_accessors "}" ; interface_method_declaration : "(" (formal_parameter_list)? ")" ";" @! ; interface_property_declaration : "{" interface_accessors "}" ; interface_accessors : interface_accessor (interface_accessor)? ; interface_accessor : (attributes)? ";" @! ; //***********************************************************************************************************************************/ //C.2.11 Enums //***********************************************************************************************************************************/ enum_declaration : "enum" identifier (enum_base)? enum_body (";")? ; enum_base : ":" integral_type ; enum_body : "{" rest_of_enum_body ; rest_of_enum_body : "}" | enum_member_declaration rest_of_enum_bodyEnd ; rest_of_enum_bodyEnd: "}" | "," rest_of_enum_body; enum_member_declaration : (attributes)? identifier (enum_member_assignment)? ; enum_member_assignment : "=" expression ; //***********************************************************************************************************************************/ //C.2.12 Delegates //***********************************************************************************************************************************/ delegate_declaration : "delegate" type identifier "(" (formal_parameter_list)? ")" ";" @! ; //***********************************************************************************************************************************/ //C.2.13 Attributes //***********************************************************************************************************************************/ attributes_either : (attribute_section_start)+ ; attribute_section_start : LOOK_AHEAD("\"[\" (\"assembly\" |\"module\") \":\"") "["global_attribute_target_specifier attribute_section | "[" (attribute_target_specifier)? attribute_section ; global_attribute_target_specifier : global_attribute_target ":" ; global_attribute_target : "assembly"|"module" ; attributes : (local_attribute)+ ; local_attribute : "[" (attribute_target_specifier)? attribute_section ; attribute_section : attribute attribute_sectionEnd; attribute_sectionEnd: "]" | LOOK_AHEAD(2) "," "]" | "," attribute_section ; attribute_target_specifier : attribute_target ":" ; attribute_target : "event" | "return" | ; attribute : type_name (attribute_arguments)? ; attribute_arguments : "(" (expression_list)? ")" ; //***********************************************************************************************************************************/ //C.3 Grammar extensions for unsafe code //***********************************************************************************************************************************/ sizeof_expression : "sizeof" "(" type ")" ; unsafe_statement : "unsafe" block ; pointer_member_access : "->" ; /* addressof_expression : "&" unary_expression ; */ fixed_statement : "fixed" "(" type fixed_pointer_declarators ")" embedded_statement ; fixed_pointer_declarators : fixed_pointer_declarator ( "," fixed_pointer_declarator )* ; fixed_pointer_declarator : identifier "=" fixed_pointer_initializer ; fixed_pointer_initializer : ("&")? expression ; stackalloc_initializer : "stackalloc" non_array_type "[" expression "]" ;