From d0a695315a779698ed31a04e79f1dfeedb5eb161 Mon Sep 17 00:00:00 2001 From: Kevin Glynn Date: Wed, 1 Dec 2010 17:02:06 +0100 Subject: [PATCH] lots of treeifying and removed ambiguities --- CSharpTranslator/antlr3/mkit | 2 +- .../antlr3/src/cs2j/AntlrUtils/AntlrUtils.cs | 9 +- CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs | 19 +- .../antlr3/src/cs2j/CSharp/JavaMaker.g | 158 +++++++-------- .../antlr3/src/cs2j/CSharp/JavaPrettyPrint.g | 184 +++++++++++------- .../antlr3/src/cs2j/CSharp/NetMaker.g | 155 ++++++++------- CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g | 29 ++- 7 files changed, 341 insertions(+), 215 deletions(-) diff --git a/CSharpTranslator/antlr3/mkit b/CSharpTranslator/antlr3/mkit index 208e1e7..e3990c2 100755 --- a/CSharpTranslator/antlr3/mkit +++ b/CSharpTranslator/antlr3/mkit @@ -14,6 +14,6 @@ java -Xmx512m -jar ../../../jar/antlr-3.3.jar -Xconversiontimeout 5000 -make -ve # echo JavaPrettyPrint.g java -Xmx512m -jar ../../../jar/antlr-3.3.jar -Xconversiontimeout 10000 -make -verbose JavaPrettyPrint.g cd ../../../ -#xbuild +xbuild echo 'All Done' diff --git a/CSharpTranslator/antlr3/src/cs2j/AntlrUtils/AntlrUtils.cs b/CSharpTranslator/antlr3/src/cs2j/AntlrUtils/AntlrUtils.cs index 47b4745..dfdce40 100644 --- a/CSharpTranslator/antlr3/src/cs2j/AntlrUtils/AntlrUtils.cs +++ b/CSharpTranslator/antlr3/src/cs2j/AntlrUtils/AntlrUtils.cs @@ -50,9 +50,14 @@ namespace RusticiSoftware.Translator.AntlrUtils } public static void DumpNodesFlat(CommonTreeNodeStream nodes) + { + DumpNodesFlat(nodes, "Nodes"); + } + + public static void DumpNodesFlat(CommonTreeNodeStream nodes, string title) { Console.ForegroundColor = ConsoleColor.Magenta; - Console.WriteLine("Nodes"); + Console.WriteLine(title); //object o_prev = string.Empty; object o = nodes.NextElement(); while (!nodes.IsEndOfFile(o)) @@ -89,7 +94,7 @@ namespace RusticiSoftware.Translator.AntlrUtils } // if (o_prev.ToString() != "UP" && o_prev.ToString() != "DOWN") // Console.Write(" {0}", o_prev.ToString()); -// Console.WriteLine(); + Console.WriteLine(); Console.ResetColor(); } diff --git a/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs b/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs index 5ec7eaf..2a63c87 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs +++ b/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs @@ -272,8 +272,12 @@ namespace RusticiSoftware.Translator.CSharp long startTime = DateTime.Now.Ticks; if (cfg.DebugLevel > 3) Console.Out.WriteLine("Translating file {0}", fullName); if (cfg.DebugLevel > 5) Console.Out.WriteLine("Parsing file {0}", fullName); - ITreeNodeStream csTree = parseFile(fullName); - if (cfg.DumpCSharp && csTree != null) AntlrUtils.AntlrUtils.DumpNodes((CommonTreeNodeStream)csTree); + CommonTreeNodeStream csTree = parseFile(fullName); + if (cfg.DumpCSharp && csTree != null) + { + AntlrUtils.AntlrUtils.DumpNodesFlat(csTree, "C Sharp Parse Tree"); + csTree.Reset(); + } if (csTree != null) { @@ -327,7 +331,11 @@ namespace RusticiSoftware.Translator.CSharp // Translate calls to .Net to calls to Java libraries CommonTreeNodeStream javaSyntaxNodes = new CommonTreeNodeStream(typeAST); - if (cfg.DumpJavaSyntax && javaSyntaxNodes != null) AntlrUtils.AntlrUtils.DumpNodesFlat(javaSyntaxNodes); + if (cfg.DumpJavaSyntax && javaSyntaxNodes != null) + { + AntlrUtils.AntlrUtils.DumpNodesFlat(javaSyntaxNodes, "Java Syntax Parse Tree for " + claName); + javaSyntaxNodes.Reset(); + } javaSyntaxNodes.TokenStream = csTree.TokenStream; NetMaker netMaker = new NetMaker(javaSyntaxNodes); @@ -342,6 +350,11 @@ namespace RusticiSoftware.Translator.CSharp CommonTreeNodeStream javaCompilationUnitNodes = new CommonTreeNodeStream(javaCompilationUnit.Tree); javaCompilationUnitNodes.TokenStream = csTree.TokenStream; + if (cfg.DumpJava && javaCompilationUnitNodes != null) + { + AntlrUtils.AntlrUtils.DumpNodesFlat(javaCompilationUnitNodes, "Final Java Parse Tree for " + claName); + javaCompilationUnitNodes.Reset(); + } // Pretty print java parse tree as text JavaPrettyPrint outputMaker = new JavaPrettyPrint(javaCompilationUnitNodes); outputMaker.Filename = fullName; diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g index 36b403e..40feed2 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g @@ -164,41 +164,42 @@ modifier: | 'readonly' -> FINAL["final"] | 'volatile' | 'extern' | 'virtual' | 'override'; class_member_declaration: - attributes? + a=attributes? m=modifiers? - ( 'const' type constant_declarators ';' - | event_declaration // 'event' - | p='partial' { Warning($p.line, "[UNSUPPORTED] 'partial' definition"); } (method_declaration - | interface_declaration - | class_declaration - | struct_declaration) - | interface_declaration // 'interface' - | 'void' method_declaration - | type ( (member_name '(') => method_declaration - | (member_name '{') => property_declaration - | (member_name '.' 'this') => type_name '.' indexer_declaration - | indexer_declaration //this - | field_declaration // qid - | operator_declaration + ( c='const' ct=type constant_declarators ';' -> ^(CONST[$c.token, "CONST"] $a? $m? $ct constant_declarators) + | ed=event_declaration -> ^(EVENT[$ed.start.Token, "EVENT"] $a? $m? $ed) + | p='partial' { Warning($p.line, "[UNSUPPORTED] 'partial' definition"); } (v1='void' m3=method_declaration -> ^(METHOD[$v1.token, "METHOD"] $a? $m? ^(TYPE $v1) $m3) + | i1=interface_declaration -> ^(INTERFACE[$i1.start.Token, "INTERFACE"] $a? $m? $i1) + | c1=class_declaration -> ^(CLASS[$c1.start.Token, "CLASS"] $a? $m? $c1) + | s1=struct_declaration) -> ^(CLASS[$s1.start.Token, "CLASS"] $a? $m? $s1) + | i2=interface_declaration -> ^(INTERFACE[$i2.start.Token, "INTERFACE"] $a? $m? $i2) // 'interface' + | v='void' m1=method_declaration -> ^(METHOD[$v.token, "METHOD"] $a? $m? ^(TYPE[$v.token, "TYPE"] $v) $m1) + | t=type ( (member_name '(') => m2=method_declaration -> ^(METHOD[$t.start.Token, "METHOD"] $a? $m? $t $m2) + | (member_name '{') => property_declaration -> ^(PROPERTY[$t.start.Token, "PROPERTY"] $a? $m? $t property_declaration) + | (member_name '.' 'this') => type_name '.' ix1=indexer_declaration -> ^(INDEXER[$t.start.Token, "INDEXER"] $a? $m? $t type_name $ix1) + | ix2=indexer_declaration -> ^(INDEXER[$t.start.Token,"INDEXER"] $a? $m? $t $ix2) //this + | field_declaration -> ^(FIELD[$t.start.Token, "FIELD"] $a? $m? $t field_declaration) // qid + | operator_declaration -> ^(OPERATOR[$t.start.Token, "OPERATOR"] $a? $m? $t operator_declaration) ) // common_modifiers// (method_modifiers | field_modifiers) - | class_declaration // 'class' - | struct_declaration // 'struct' - | enum_declaration // 'enum' - | delegate_declaration // 'delegate' - | conversion_operator_declaration - | constructor_declaration // | static_constructor_declaration - | destructor_declaration + | c3=class_declaration -> ^(CLASS[$c3.start.Token, "CLASS"] $a? $m? $c3) // 'class' + | s3=struct_declaration -> ^(CLASS[$s3.start.Token, "CLASS"] $a? $m? $s3) + | e3=enum_declaration -> ^(ENUM[$e3.start.Token, "ENUM"] $a? $m? $e3) + | d3=delegate_declaration -> ^(DELEGATE[$d3.start.Token, "DELEGATE"] $a? $m? $d3) + | co3=conversion_operator_declaration -> ^(CONVERSION_OPERATOR[$co3.start.Token, "CONVERSION"] $a? $m? $co3) + | con3=constructor_declaration -> ^(CONSTRUCTOR[$con3.start.Token, "CONSTRUCTOR"] $a? $m? $con3) + | de3=destructor_declaration -> ^(DESTRUCTOR[$de3.start.Token, "DESTRUCTOR"] $a? $m? $de3) ) ; primary_expression: - ('this' brackets) => 'this' brackets primary_expression_part* - | ('base' brackets) => 'this' brackets primary_expression_part* - | primary_expression_start primary_expression_part* + ('this' brackets[null]) => (t='this' -> $t) (b1=brackets[$primary_expression.tree] -> $b1) (pp1=primary_expression_part[$primary_expression.tree] -> $pp1) * + | ('base' brackets[null]) => (b='this' -> $b) (b2=brackets[$primary_expression.tree] -> $b2) (pp2=primary_expression_part[$primary_expression.tree] -> $pp2) * + | (primary_expression_start -> primary_expression_start) (pp3=primary_expression_part[$primary_expression.tree] -> $pp3 )* +// keving:TODO fixup | 'new' ( (object_creation_expression ('.'|'->'|'[')) => - object_creation_expression primary_expression_part+ // new Foo(arg, arg).Member + object_creation_expression primary_expression_part[null]+ // new Foo(arg, arg).Member // try the simple one first, this has no argS and no expressions // symantically could be object creation | (delegate_creation_expression) => delegate_creation_expression// new FooDelegate (MyFunction) @@ -222,23 +223,25 @@ primary_expression_start: | literal ; -primary_expression_part: - access_identifier - | brackets_or_arguments - | '++' - | '--' ; -access_identifier: - access_operator type_or_generic ; +primary_expression_part [CommonTree lhs]: + access_identifier[$lhs] + | brackets_or_arguments[$lhs] + | p='++' -> ^(POSTINC[$p.token, "POST++"] { (CommonTree)adaptor.DupTree($lhs) } ) + | m='--' -> ^(POSTDEC[$m.token, "POST--"] { (CommonTree)adaptor.DupTree($lhs) } ) + ; +access_identifier [CommonTree lhs]: + access_operator type_or_generic -> ^(access_operator { (CommonTree)adaptor.DupTree($lhs) } type_or_generic); access_operator: '.' | '->' ; -brackets_or_arguments: - brackets | arguments ; -brackets: - '[' expression_list? ']' ; +brackets_or_arguments [CommonTree lhs]: + brackets[$lhs] | arguments[$lhs] ; +brackets [CommonTree lhs]: + '[' expression_list? ']' -> ^(INDEX { (CommonTree)adaptor.DupTree($lhs) } expression_list?); +// keving: TODO: drop this. paren_expression: - '(' expression ')' ; -arguments: - '(' argument_list? ')' ; + '(' expression ')' -> ^(TEMPPARENS expression); +arguments [CommonTree lhs]: + '(' argument_list? ')' -> ^(APPLY { (CommonTree)adaptor.DupTree($lhs) } argument_list?); argument_list: argument (',' argument)*; // 4.0 @@ -291,12 +294,12 @@ primary_or_array_creation_expression: ; // new Type[2] { } array_creation_expression: - 'new' + 'new'^ (type ('[' expression_list ']' ( rank_specifiers? array_initializer? // new int[4] // | invocation_part* - | ( ((arguments ('['|'.'|'->')) => arguments invocation_part)// new object[2].GetEnumerator() - | invocation_part)* arguments + | ( ((arguments[null] ('['|'.'|'->')) => arguments[ (CommonTree)adaptor.Create(KGHOLE, "KGHOLE") ] invocation_part)// new object[2].GetEnumerator() + | invocation_part)* arguments[ (CommonTree)adaptor.Create(KGHOLE, "KGHOLE") ] ) // new int[4]() | array_initializer ) @@ -311,15 +314,15 @@ variable_initializer_list: variable_initializer: expression | array_initializer ; sizeof_expression: - 'sizeof' '(' unmanaged_type ')'; + 'sizeof'^ '('! unmanaged_type ')'!; checked_expression: - 'checked' '(' expression ')' ; + 'checked'^ '('! expression ')'! ; unchecked_expression: - 'unchecked' '(' expression ')' ; + 'unchecked'^ '('! expression ')'! ; default_value_expression: - 'default' '(' type ')' ; + 'default'^ '('! type ')'! ; anonymous_method_expression: - 'delegate' explicit_anonymous_function_signature? block; + 'delegate'^ explicit_anonymous_function_signature? block; explicit_anonymous_function_signature: '(' explicit_anonymous_function_parameter_list? ')' ; explicit_anonymous_function_parameter_list: @@ -366,9 +369,9 @@ initializer_value: /////////////////////////////////////////////////////// typeof_expression: - 'typeof' '(' ((unbound_type_name) => unbound_type_name + 'typeof'^ '('! ((unbound_type_name) => unbound_type_name | type - | 'void') ')' ; + | 'void') ')'! ; // unbound type examples //foo>> //bar::foo<> @@ -423,7 +426,7 @@ qid_start returns [string name, List tyargs]: qid_part: - access_identifier ; + access_identifier[ (CommonTree)adaptor.Create(KGHOLE, "KGHOLE") ] ; generic_argument_list returns [List tyargs] @after { @@ -442,12 +445,12 @@ type returns [string thetext]: ((predefined_type | type_name) rank_specifiers) => (p1=predefined_type { $thetext = $p1.thetext; } | tn1=type_name { $thetext = $tn1.thetext; }) rs=rank_specifiers { $thetext += $rs.text; } (s1+='*' { $thetext += "*"; })* -> ^(TYPE $p1? $tn1? $rs $s1*) | ((predefined_type | type_name) ('*'+ | '?')) => (p2=predefined_type { $thetext = $p2.thetext; } | tn2=type_name { $thetext = $tn2.thetext; }) ((s2+='*' { $thetext += "*"; })+ | o2='?' { $thetext += "?"; }) -> ^(TYPE $p2? $tn2? $s2* $o2?) | (p3=predefined_type { $thetext = $p3.thetext; } | tn3=type_name { $thetext = $tn3.thetext; }) -> ^(TYPE $p3? $tn3?) - | v='void' { $thetext = "System.Void"; } (s+='*' { $thetext += "*"; })+ -> ^(TYPE $v $s+) + | v='void' { $thetext = "System.Void"; } (s+='*' { $thetext += "*"; })+ -> ^(TYPE[$v.token, "TYPE"] $v $s+) ; non_nullable_type: - type + (p=predefined_type | t=type_name) rs=rank_specifiers? (s+='*')* -> ^(TYPE["TYPE"] $p? $t? $rs? $s*) + | v='void' (s+='*')+ -> ^(TYPE[$v.token,"TYPE"] $v $s+) ; - non_array_type: type; array_type: @@ -482,30 +485,30 @@ assignment: unary_expression assignment_operator expression ; unary_expression: //('(' arguments ')' ('[' | '.' | '(')) => primary_or_array_creation_expression - (cast_expression) => cast_expression - | primary_or_array_creation_expression - | '+' unary_expression - | '-' unary_expression - | '!' unary_expression - | '~' unary_expression - | pre_increment_expression - | pre_decrement_expression - | pointer_indirection_expression - | addressof_expression + (cast_expression) => cast_expression + | primary_or_array_creation_expression -> primary_or_array_creation_expression + | p='+' unary_expression -> ^(MONOPLUS[$p.token,"MONOPLUS"] unary_expression) + | m='-' unary_expression -> ^(MONOMINUS[$m.token, "MONOMINUS"] unary_expression) + | n='!' unary_expression -> ^(MONONOT[$n.token, "MONONOT"] unary_expression) + | t='~' unary_expression -> ^(MONOTWIDDLE[$t.token, "TWIDDLE"] unary_expression) + | pre_increment_expression -> pre_increment_expression + | pre_decrement_expression -> pre_decrement_expression + | pointer_indirection_expression -> pointer_indirection_expression + | addressof_expression -> addressof_expression ; cast_expression: - //'(' type ')' unary_expression ; - '(' type ')' unary_expression -> ^(CAST_EXPR type unary_expression); +// //'(' type ')' unary_expression ; + l='(' type ')' unary_expression -> ^(CAST_EXPR[$l.token, "CAST"] type unary_expression); assignment_operator: '=' | '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>' '>=' ; pre_increment_expression: - '++' unary_expression ; + s='++' unary_expression -> ^(PREINC[$s.token, "PRE++"] unary_expression) ; pre_decrement_expression: - '--' unary_expression ; + s='--' unary_expression -> ^(PREDEC[$s.token, "PRE--"] unary_expression); pointer_indirection_expression: - '*' unary_expression ; + s='*' unary_expression -> ^(MONOSTAR[$s.token, "STAR"] unary_expression); addressof_expression: - '&' unary_expression ; + a='&' unary_expression -> ^(ADDRESSOF[$a.token, "ADDRESSOF"] unary_expression); non_assignment_expression: //'non ASSIGNment' @@ -553,8 +556,9 @@ conditional_or_expression: null_coalescing_expression: conditional_or_expression ('??'^ conditional_or_expression)* ; conditional_expression: - (null_coalescing_expression '?' expression ':') => e1=null_coalescing_expression q='?' e2=expression ':' e3=expression -> ^(COND_EXPR[$q.Token, "?:"] $e1 $e2 $e3) - | null_coalescing_expression ; + (ne=null_coalescing_expression -> $ne) (q='?' te=expression ':' ee=expression -> ^(COND_EXPR[$q.token, "?:"] $conditional_expression $te $ee))? ; +// (null_coalescing_expression '?' expression ':') => e1=null_coalescing_expression q='?' e2=expression ':' e3=expression -> ^(COND_EXPR[$q.token, "?:"] $e1 $e2 $e3) +// | null_coalescing_expression ; /////////////////////////////////////////////////////// // lambda Section @@ -854,7 +858,7 @@ constructor_constraint: 'new' '(' ')' ; return_type: type - | v='void' -> ^(TYPE $v); + | v='void' -> ^(TYPE[$v.token, "TYPE"] $v); formal_parameter_list: formal_parameter (',' formal_parameter)* ; formal_parameter: @@ -1021,8 +1025,8 @@ destructor_body: /////////////////////////////////////////////////////// invocation_expression: - invocation_start (((arguments ('['|'.'|'->')) => arguments invocation_part) - | invocation_part)* arguments ; + invocation_start (((arguments[null] ('['|'.'|'->')) => arguments[ (CommonTree)adaptor.Create(KGHOLE, "KGHOLE") ] invocation_part) + | invocation_part)* arguments[ (CommonTree)adaptor.Create(KGHOLE, "KGHOLE") ] ; invocation_start: predefined_type | (identifier generic_argument_list) => identifier generic_argument_list @@ -1032,8 +1036,8 @@ invocation_start: | typeof_expression // typeof(Foo).Name ; invocation_part: - access_identifier - | brackets ; + access_identifier[ (CommonTree)adaptor.Create(KGHOLE, "KGHOLE") ] + | brackets[ (CommonTree)adaptor.Create(KGHOLE, "KGHOLE") ] ; /////////////////////////////////////////////////////// diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g index 2681d1c..6dc76c4 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g @@ -91,39 +91,61 @@ modifier -> string(payload={$m.text}); class_member_declaration: - attributes? - m=modifiers? - ( 'const' t1=type constant_declarators ';' - | event_declaration // 'event' - | 'partial' (method_declaration - | interface_declaration[$m.st] - | class_declaration[$m.st] - | struct_declaration) - | interface_declaration[$m.st] // 'interface' - | 'void' method_declaration - | t2=type ( (member_name '(') => method_declaration - | (member_name '{') => property_declaration - | (member_name '.' 'this') => type_name '.' indexer_declaration - | indexer_declaration //this - | field_declaration -> field(modifiers={$m.st}, type={$t2.st}, field={$field_declaration.st}) // qid - | operator_declaration - ) -// common_modifiers// (method_modifiers | field_modifiers) - - | c2=class_declaration[$m.st] -> { $c2.st } // 'class' - | s2=struct_declaration -> { $s2.st }// 'struct' - | e2=enum_declaration[$m.st] -> { $e2.st } // 'enum' - | delegate_declaration // 'delegate' - | conversion_operator_declaration - | constructor_declaration // | static_constructor_declaration - | destructor_declaration - ) - ; + ^(CONST attributes? modifiers? type constant_declarators) + | ^(EVENT attributes? modifiers? event_declaration) + | ^(METHOD attributes? modifiers? type method_declaration) + | ^(INTERFACE attributes? modifiers? interface_declaration[$modifiers.st]) + | ^(CLASS attributes? modifiers? class_declaration[$modifiers.st]) + | ^(PROPERTY attributes? modifiers? type property_declaration) + | ^(INDEXER attributes? modifiers? type type_name? indexer_declaration) + | ^(FIELD attributes? modifiers? type field_declaration) -> field(modifiers={$modifiers.st}, type={$type.st}, field={$field_declaration.st}) + | ^(OPERATOR attributes? modifiers? type operator_declaration) + | ^(ENUM attributes? modifiers? enum_declaration[$modifiers.st]) + | ^(DELEGATE attributes? modifiers? delegate_declaration) + | ^(CONSTRUCTOR attributes? modifiers? constructor_declaration) + | ^(DESTRUCTOR attributes? modifiers? destructor_declaration) + ; +// class_member_declaration: +// attributes? +// m=modifiers? +// ( 'const' t1=type constant_declarators ';' +// | event_declaration // 'event' +// | 'partial' (method_declaration +// | interface_declaration[$m.st] +// | class_declaration[$m.st] +// | struct_declaration) +// | interface_declaration[$m.st] // 'interface' +// // | 'void' method_declaration +// | t2=type ( (member_name '(') => method_declaration +// | (member_name '{') => property_declaration +// | (member_name '.' 'this') => type_name '.' indexer_declaration +// | indexer_declaration //this +// | field_declaration -> field(modifiers={$m.st}, type={$t2.st}, field={$field_declaration.st}) // qid +// | operator_declaration +// ) +// // common_modifiers// (method_modifiers | field_modifiers) +// +// | c2=class_declaration[$m.st] -> { $c2.st } // 'class' +// | s2=struct_declaration -> { $s2.st }// 'struct' +// | e2=enum_declaration[$m.st] -> { $e2.st } // 'enum' +// | delegate_declaration // 'delegate' +// | conversion_operator_declaration +// | constructor_declaration // | static_constructor_declaration +// | destructor_declaration +// ) +// ; +// primary_expression: - ('this' brackets) => 'this' brackets primary_expression_part* - | ('base' brackets) => 'this' brackets primary_expression_part* - | primary_expression_start pp+=primary_expression_part* -> primary_expression_start_parts(start={ $primary_expression_start.st }, follows={ $pp }) + ^(INDEX expression expression_list?) + | ^(APPLY expression argument_list?) + | ^(POSTINC expression) + | ^(POSTDEC expression) + | primary_expression_start -> { $primary_expression_start.st } + | ^(access_operator expression type_or_generic) +// ('this' brackets) => 'this' brackets primary_expression_part* +// | ('base' brackets) => 'this' brackets primary_expression_part* +// | primary_expression_start primary_expression_part* | 'new' ( (object_creation_expression ('.'|'->'|'[')) => object_creation_expression primary_expression_part+ // new Foo(arg, arg).Member // try the simple one first, this has no argS and no expressions @@ -137,6 +159,23 @@ primary_expression: | default_value_expression // default | anonymous_method_expression // delegate (int foo) {} ; +// primary_expression: +// ('this' brackets) => 'this' brackets primary_expression_part* +// | ('base' brackets) => 'this' brackets primary_expression_part* +// | primary_expression_start pp+=primary_expression_part* -> primary_expression_start_parts(start={ $primary_expression_start.st }, follows={ $pp }) +// | 'new' ( (object_creation_expression ('.'|'->'|'[')) => +// object_creation_expression primary_expression_part+ // new Foo(arg, arg).Member +// // try the simple one first, this has no argS and no expressions +// // symantically could be object creation +// | (delegate_creation_expression) => delegate_creation_expression// new FooDelegate (MyFunction) +// | object_creation_expression +// | anonymous_object_creation_expression) // new {int X, string Y} +// | sizeof_expression // sizeof (struct) +// | checked_expression // checked (... +// | unchecked_expression // unchecked {...} +// | default_value_expression // default +// | anonymous_method_expression // delegate (int foo) {} +// ; primary_expression_start: predefined_type @@ -144,7 +183,7 @@ primary_expression_start: | identifier ('::' identifier)? | 'this' | 'base' - | paren_expression + | ^(TEMPPARENS expression) -> template(e={$expression.st}) "()" | typeof_expression // typeof(Foo).Name | literal -> { $literal.st } ; @@ -212,7 +251,7 @@ primary_or_array_creation_expression: ; // new Type[2] { } array_creation_expression: - 'new' + ^('new' (type ('[' expression_list ']' ( rank_specifiers? array_initializer? // new int[4] // | invocation_part* @@ -224,7 +263,7 @@ array_creation_expression: | rank_specifier // [,] (array_initializer // var a = new[] { 1, 10, 100, 1000 }; // int[] ) - ) ; + )) ; array_initializer: '{' variable_initializer_list? ','? '}' ; variable_initializer_list: @@ -232,15 +271,15 @@ variable_initializer_list: variable_initializer: expression -> { $expression.st } | array_initializer -> { $array_initializer.st }; sizeof_expression: - 'sizeof' '(' unmanaged_type ')'; + ^('sizeof' unmanaged_type ); checked_expression: - 'checked' '(' expression ')' ; + ^('checked' expression ) ; unchecked_expression: - 'unchecked' '(' expression ')' ; + ^('unchecked' expression ) ; default_value_expression: - 'default' '(' type ')' ; + ^('default' type ) ; anonymous_method_expression: - 'delegate' explicit_anonymous_function_signature? block; + ^('delegate' explicit_anonymous_function_signature? block); explicit_anonymous_function_signature: '(' explicit_anonymous_function_parameter_list? ')' ; explicit_anonymous_function_parameter_list: @@ -287,9 +326,7 @@ initializer_value: /////////////////////////////////////////////////////// typeof_expression: - 'typeof' '(' ((unbound_type_name) => unbound_type_name - | type - | 'void') ')' ; + ^('typeof' (unbound_type_name | type | 'void') ) ; // unbound type examples //foo>> //bar::foo<> @@ -351,7 +388,7 @@ type List stars = new List(); string opt = null; }: - ^(TYPE (tp=predefined_type {nm=$tp.st;} | tn=type_name {nm=$tn.st;}) rank_specifiers? ('*' { stars.Add("*");})* ('?' { opt = "?";} )?) -> type(name={ nm }, stars={ stars }, rs={ $rank_specifiers.st }, opt={ opt }) + ^(TYPE (tp=predefined_type {nm=$tp.st;} | tn=type_name {nm=$tn.st;} | tv='void' {nm=new StringTemplate("void");}) rank_specifiers? ('*' { stars.Add("*");})* ('?' { opt = "?";} )?) -> type(name={ nm }, stars={ stars }, rs={ $rank_specifiers.st }, opt={ opt }) ; non_nullable_type: type -> { $type.st } ; @@ -391,29 +428,41 @@ assignment: unary_expression: //('(' arguments ')' ('[' | '.' | '(')) => primary_or_array_creation_expression // ^(CAST_EXPR type expression) - (cast_expression) => cast_expression + ^(CAST_EXPR type u0=unary_expression) -> cast_expr(type= { $type.st}, exp = { $u0.st}) | primary_or_array_creation_expression -> { $primary_or_array_creation_expression.st } - | '+' u1=unary_expression -> template(e={$u1.st}) "+" - | '-' u2=unary_expression -> template(e={$u2.st}) "-" - | '!' u3=unary_expression -> template(e={$u3.st}) "!" - | '~' u4=unary_expression -> template(e={$u4.st}) "~" - | pre_increment_expression - | pre_decrement_expression - | pointer_indirection_expression - | addressof_expression - ; -cast_expression: - ^(CAST_EXPR type unary_expression ) -> cast_expr(type= { $type.st}, exp = { $unary_expression.st}); -assignment_operator: + | ^(MONOPLUS u1=unary_expression) -> template(e={$u1.st}) "+" + | ^(MONOMINUS u2=unary_expression) -> template(e={$u2.st}) "-" + | ^(MONONOT u3=unary_expression) -> template(e={$u3.st}) "!" + | ^(MONOTWIDDLE u4=unary_expression) -> template(e={$u4.st}) "~" + | ^(PREINC u5=unary_expression) -> template(e={$u5.st}) "++" + | ^(PREDEC u6=unary_expression) -> template(e={$u6.st}) "--" + | ^(MONOSTAR unary_expression) + | ^(ADDRESSOF unary_expression) + ; + +// (cast_expression) => cast_expression +// | primary_or_array_creation_expression -> { $primary_or_array_creation_expression.st } +// | '+' u1=unary_expression -> template(e={$u1.st}) "+" +// | '-' u2=unary_expression -> template(e={$u2.st}) "-" +// | '!' u3=unary_expression -> template(e={$u3.st}) "!" +// | '~' u4=unary_expression -> template(e={$u4.st}) "~" +// | pre_increment_expression +// | pre_decrement_expression +// | pointer_indirection_expression +// | addressof_expression +// ; +// cast_expression: +// ^(CAST_EXPR type unary_expression ) -> cast_expr(type= { $type.st}, exp = { $unary_expression.st}); + assignment_operator: '=' | '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>' '>=' ; -pre_increment_expression: - '++' unary_expression ; -pre_decrement_expression: - '--' unary_expression ; -pointer_indirection_expression: - '*' unary_expression ; -addressof_expression: - '&' unary_expression ; +// pre_increment_expression: +// '++' unary_expression ; +// pre_decrement_expression: +// '--' unary_expression ; +// pointer_indirection_expression: +// '*' unary_expression ; +// addressof_expression: +// '&' unary_expression ; non_assignment_expression: //'non ASSIGNment' @@ -887,9 +936,14 @@ invocation_part: /////////////////////////////////////////////////////// +// keving: split statement into two parts, there seems to be a problem with the state +// machine if we combine statement and statement_plus. statement: (declaration_statement) => declaration_statement - | (identifier ':') => labeled_statement + | statement_plus + ; +statement_plus: + (identifier ':') => labeled_statement | embedded_statement ; embedded_statement: diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g index 973a78b..9e44081 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g @@ -36,39 +36,60 @@ modifier: | 'readonly' | 'volatile' | 'extern' | 'virtual' | 'override' | FINAL ; class_member_declaration: - attributes? - m=modifiers? - ( 'const' type constant_declarators ';' - | event_declaration // 'event' - | 'partial' (method_declaration - | interface_declaration - | class_declaration - | struct_declaration) - | interface_declaration // 'interface' - | 'void' method_declaration - | type ( (member_name '(') => method_declaration - | (member_name '{') => property_declaration - | (member_name '.' 'this') => type_name '.' indexer_declaration - | indexer_declaration //this - | field_declaration // qid - | operator_declaration - ) -// common_modifiers// (method_modifiers | field_modifiers) - - | class_declaration // 'class' - | struct_declaration // 'struct' - | enum_declaration // 'enum' - | delegate_declaration // 'delegate' - | conversion_operator_declaration - | constructor_declaration // | static_constructor_declaration - | destructor_declaration - ) - ; - + ^(CONST attributes? modifiers? type constant_declarators) + | ^(EVENT attributes? modifiers? event_declaration) + | ^(METHOD attributes? modifiers? type method_declaration) + | ^(INTERFACE attributes? modifiers? interface_declaration) + | ^(CLASS attributes? modifiers? class_declaration) + | ^(PROPERTY attributes? modifiers? type property_declaration) + | ^(INDEXER attributes? modifiers? type type_name? indexer_declaration) + | ^(FIELD attributes? modifiers? type field_declaration) + | ^(OPERATOR attributes? modifiers? type operator_declaration) + | ^(ENUM attributes? modifiers? enum_declaration) + | ^(DELEGATE attributes? modifiers? delegate_declaration) + | ^(CONSTRUCTOR attributes? modifiers? constructor_declaration) + | ^(DESTRUCTOR attributes? modifiers? destructor_declaration) + ; +// class_member_declaration: +// attributes? +// m=modifiers? +// ( 'const' type constant_declarators ';' +// | event_declaration // 'event' +// | 'partial' (method_declaration +// | interface_declaration +// | class_declaration +// | struct_declaration) +// | interface_declaration // 'interface' +// // | 'void' method_declaration +// | type ( (member_name '(') => method_declaration +// | (member_name '{') => property_declaration +// | (member_name '.' 'this') => type_name '.' indexer_declaration +// | indexer_declaration //this +// | field_declaration // qid +// | operator_declaration +// ) +// // common_modifiers// (method_modifiers | field_modifiers) +// +// | class_declaration // 'class' +// | struct_declaration // 'struct' +// | enum_declaration // 'enum' +// | delegate_declaration // 'delegate' +// | conversion_operator_declaration +// | constructor_declaration // | static_constructor_declaration +// | destructor_declaration +// ) +// ; +// primary_expression: - ('this' brackets) => 'this' brackets primary_expression_part* - | ('base' brackets) => 'this' brackets primary_expression_part* - | primary_expression_start primary_expression_part* + ^(INDEX expression expression_list?) + | ^(APPLY expression argument_list?) + | ^(POSTINC expression) + | ^(POSTDEC expression) + | primary_expression_start + | ^(access_operator expression type_or_generic) +// ('this' brackets) => 'this' brackets primary_expression_part* +// | ('base' brackets) => 'this' brackets primary_expression_part* +// | primary_expression_start primary_expression_part* | 'new' ( (object_creation_expression ('.'|'->'|'[')) => object_creation_expression primary_expression_part+ // new Foo(arg, arg).Member // try the simple one first, this has no argS and no expressions @@ -89,7 +110,7 @@ primary_expression_start: | identifier ('::' identifier)? | 'this' | 'base' - | paren_expression + | ^(TEMPPARENS expression) | typeof_expression // typeof(Foo).Name | literal ; @@ -157,7 +178,7 @@ primary_or_array_creation_expression: ; // new Type[2] { } array_creation_expression: - 'new' + ^('new' (type ('[' expression_list ']' ( rank_specifiers? array_initializer? // new int[4] // | invocation_part* @@ -169,7 +190,7 @@ array_creation_expression: | rank_specifier // [,] (array_initializer // var a = new[] { 1, 10, 100, 1000 }; // int[] ) - ) ; + )) ; array_initializer: '{' variable_initializer_list? ','? '}' ; variable_initializer_list: @@ -177,15 +198,15 @@ variable_initializer_list: variable_initializer: expression | array_initializer ; sizeof_expression: - 'sizeof' '(' unmanaged_type ')'; + ^('sizeof' unmanaged_type ); checked_expression: - 'checked' '(' expression ')' ; + ^('checked' expression ) ; unchecked_expression: - 'unchecked' '(' expression ')' ; + ^('unchecked' expression ) ; default_value_expression: - 'default' '(' type ')' ; + ^('default' type ) ; anonymous_method_expression: - 'delegate' explicit_anonymous_function_signature? block; + ^('delegate' explicit_anonymous_function_signature? block); explicit_anonymous_function_signature: '(' explicit_anonymous_function_parameter_list? ')' ; explicit_anonymous_function_parameter_list: @@ -232,9 +253,7 @@ initializer_value: /////////////////////////////////////////////////////// typeof_expression: - 'typeof' '(' ((unbound_type_name) => unbound_type_name - | type - | 'void') ')' ; + ^('typeof' (unbound_type_name | type | 'void') ) ; // unbound type examples //foo>> //bar::foo<> @@ -290,7 +309,7 @@ type_arguments: type (',' type)* ; type: - ^(TYPE (predefined_type | type_name) rank_specifiers? '*'* '?'?); + ^(TYPE (predefined_type | type_name | 'void') rank_specifiers? '*'* '?'?); non_nullable_type: type; non_array_type: @@ -331,27 +350,27 @@ unary_expression: //(cast_expression) => cast_expression ^(CAST_EXPR type unary_expression) | primary_or_array_creation_expression - | '+' unary_expression - | '-' unary_expression - | '!' unary_expression - | '~' unary_expression - | pre_increment_expression - | pre_decrement_expression - | pointer_indirection_expression - | addressof_expression + | ^(MONOPLUS unary_expression) + | ^(MONOMINUS unary_expression) + | ^(MONONOT unary_expression) + | ^(MONOTWIDDLE unary_expression) + | ^(PREINC unary_expression) + | ^(PREDEC unary_expression) + | ^(MONOSTAR unary_expression) + | ^(ADDRESSOF unary_expression) ; -cast_expression: - '(' type ')' non_assignment_expression ; +//cast_expression: +// '(' type ')' non_assignment_expression ; assignment_operator: '=' | '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>' '>=' ; -pre_increment_expression: - '++' unary_expression ; -pre_decrement_expression: - '--' unary_expression ; -pointer_indirection_expression: - '*' unary_expression ; -addressof_expression: - '&' unary_expression ; +//pre_increment_expression: +// '++' unary_expression ; +//pre_decrement_expression: +// '--' unary_expression ; +//pointer_indirection_expression: +// '*' unary_expression ; +//addressof_expression: +// '&' unary_expression ; non_assignment_expression: //'non ASSIGNment' @@ -834,13 +853,17 @@ invocation_part: /////////////////////////////////////////////////////// +// keving: split statement into two parts, there seems to be a problem with the state +// machine if we combine statement and statement_plus. (It fails to recognise dataHelper.Add();) statement: - (declaration_statement) => declaration_statement - | (identifier ':') => labeled_statement - | embedded_statement + (declaration_statement) => declaration_statement + | statement_plus; +statement_plus: + (identifier ':') => labeled_statement + | embedded_statement ; embedded_statement: - block +block | selection_statement // if, switch | iteration_statement // while, do, for, foreach | jump_statement // break, continue, goto, return, throw diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g index 56b0c4f..5674c73 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g @@ -15,6 +15,32 @@ tokens { FINAL; /* final modifier */ IN; OUT; + CONST; + EVENT; + METHOD; + PROPERTY; + INDEXER; + FIELD; + OPERATOR; + ENUM; + DELEGATE; + CONVERSION_OPERATOR; + CONSTRUCTOR; + DESTRUCTOR; + + MONOPLUS; + MONOMINUS; + MONONOT; + MONOTWIDDLE; + MONOSTAR; + ADDRESSOF; + PREINC; + PREDEC; + POSTINC; + POSTDEC; + TEMPPARENS; + INDEX; + APPLY; OPEN_BRACKET='['; CLOSE_BRACKET=']'; @@ -35,6 +61,7 @@ tokens { PAYLOAD; // carries arbitrary text for the output file PAYLOAD_LIST; SEP; + KGHOLE; } @namespace { RusticiSoftware.Translator.CSharp } @@ -115,7 +142,7 @@ class_member_declaration: m=modifiers? ( 'const' type constant_declarators ';' | event_declaration // 'event' - | 'partial' (method_declaration + | 'partial' ('void' method_declaration | interface_declaration | class_declaration | struct_declaration)