From 9f2015ed926687af0ea954554e7c8071e809396f Mon Sep 17 00:00:00 2001 From: Kevin Glynn Date: Sat, 20 Nov 2010 15:29:39 +0100 Subject: [PATCH] collect comments as we go, and emit collected comments periodically, emit (some of) field declarations --- .../antlr3/src/cs2j/CSharp/JavaPrettyPrint.g | 72 +++++++++++++------ .../src/cs2j/bin/Debug/templates/java.stg | 13 +++- 2 files changed, 61 insertions(+), 24 deletions(-) diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g index b1b0cbb..3cc50c0 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g @@ -20,11 +20,31 @@ options { { public bool IsLast { get; set; } - public int EmittedCommentTokenIdx { get; set; } + public int EmittedCommentTokenIdx { get; set; } + + private List collectedComments = null; + List CollectedComments { + get { + List rets = collectedComments; + collectedComments = null; + return rets; + } + set { + if (collectedComments == null) + collectedComments = new List(); + foreach (string c in value) { + collectedComments.Add(c); + } + } + } // Collect all comments from previous position to endIdx // comments are the text from tokens on the Hidden channel - protected List collectComments(int endIdx) { + protected void collectComments(IToken tok) { + // TokenIndex may be -1, no sweat we just won't collect anything + collectComments(tok.TokenIndex); + } + protected void collectComments(int endIdx) { List rets = new List(); List toks = ((CommonTokenStream)this.GetTreeNodeStream().TokenStream).GetTokens(EmittedCommentTokenIdx,endIdx); if (toks != null) { @@ -35,19 +55,20 @@ options { } EmittedCommentTokenIdx = endIdx+1; } - return rets; + collectedComments = rets; } - protected List collectComments() { - return collectComments(((CommonTokenStream)this.GetTreeNodeStream().TokenStream).GetTokens().Count - 1); + + protected void collectComments() { + collectComments(((CommonTokenStream)this.GetTreeNodeStream().TokenStream).GetTokens().Count - 1); } } compilation_unit : - ^(PACKAGE nm=PAYLOAD modifiers? type_declaration[$modifiers.st]) -> + ^(PACKAGE nm=PAYLOAD modifiers? type_declaration[$modifiers.st] { if (IsLast) collectComments(); }) -> package(now = {DateTime.Now}, includeDate = {true}, packageName = {$nm.text}, type = {$type_declaration.st}, - endComments = {IsLast ? collectComments() : null}); + endComments = { CollectedComments }); type_declaration [StringTemplate modifiersST]: class_declaration[modifiersST] -> { $class_declaration.st } @@ -72,7 +93,7 @@ modifier class_member_declaration: attributes? m=modifiers? - ( 'const' type constant_declarators ';' + ( 'const' t1=type constant_declarators ';' | event_declaration // 'event' | 'partial' (method_declaration | interface_declaration[$m.st] @@ -80,11 +101,11 @@ class_member_declaration: | struct_declaration) | interface_declaration[$m.st] // 'interface' | 'void' method_declaration - | type ( (member_name '(') => method_declaration + | t2=type ( (member_name '(') => method_declaration | (member_name '{') => property_declaration | (member_name '.' 'this') => type_name '.' indexer_declaration | indexer_declaration //this - | field_declaration // qid + | field_declaration -> field(modifiers={$m.st}, type={$t2.st}, field={$field_declaration.st}) // qid | operator_declaration ) // common_modifiers// (method_modifiers | field_modifiers) @@ -557,11 +578,11 @@ class_declaration[StringTemplate modifiersST] @init { List preComments = null; }: - ^(c=CLASS { preComments = collectComments($c.TokenStartIndex); } + ^(c=CLASS { preComments = CollectedComments; } identifier type_parameter_constraints_clauses? type_parameter_list[$type_parameter_constraints_clauses.tpConstraints]? class_extends? class_implements? class_body ) -> class(modifiers = {modifiersST}, name={ $identifier.st }, typeparams= {$type_parameter_list.st}, comments = { preComments }, - extends = { $class_extends.st }, imps = { $class_implements.st }) ; + extends = { $class_extends.st }, imps = { $class_implements.st }, body={$class_body.st}) ; type_parameter_list [Dictionary tpConstraints]: (attributes? t+=type_parameter[tpConstraints])+ -> template(params={ $t }) "\<\>"; @@ -581,9 +602,13 @@ interface_type_list: ts+=type (',' ts+=type)* -> template(types={ $ts }) ""; class_body: - '{' class_member_declarations? '}' ; -class_member_declarations: - class_member_declaration+ ; + '{' cs+=class_member_declaration_aux* '}' -> class_body(entries={$cs}) ; +class_member_declaration_aux +@init{ + List preComments = null; +}: + { preComments = CollectedComments; } member=class_member_declaration -> class_member(comments={ preComments }, member={ $member.st }) ; + /////////////////////////////////////////////////////// constant_declaration: @@ -652,14 +677,14 @@ enum_declaration[StringTemplate modifiersST] @init { List preComments = null; }: - e='enum' { preComments = collectComments($e.TokenStartIndex); } identifier enum_base? enum_body ';'? + e='enum' { preComments = CollectedComments; } identifier enum_base? enum_body ';'? -> enum(comments = { preComments}, modifiers = { $modifiersST }, name={$identifier.text}, body={$enum_body.st}) ; enum_base: ':' integral_type ; enum_body: ^(ENUM_BODY es+=enum_member_declaration+) -> enum_body(values={$es}); enum_member_declaration: - attributes? identifier -> enum_member(comments = {collectComments($identifier.start.TokenStartIndex)}, value={ $identifier.st }); + attributes? identifier -> enum_member(comments = { CollectedComments }, value={ $identifier.st }); //enum_modifiers: // enum_modifier+ ; //enum_modifier: @@ -695,11 +720,12 @@ type_parameter_constraints_clause [Dictionary tpConstrain ^(TYPE_PARAM_CONSTRAINT t=type_variable_name ts+=type_name+) -> type_param_constraint(param= { $type_variable_name.st }, constraints = { $ts }) ; type_variable_name: identifier -> { $identifier.st } ; -constructor_constraint: - 'new' '(' ')' ; +// keving: stripped +//constructor_constraint: +// 'new' '(' ')' ; return_type: - type - | 'void'; + type -> { $type.st } + | 'void' -> string(payload={"void"}); formal_parameter_list: formal_parameter (',' formal_parameter)* ; formal_parameter: @@ -723,7 +749,7 @@ interface_declaration[StringTemplate modifiersST] @init { List preComments = null; }: - ^(c=INTERFACE { preComments = collectComments($c.TokenStartIndex); } + ^(c=INTERFACE { preComments = CollectedComments; } identifier type_parameter_constraints_clauses? variant_generic_parameter_list[$type_parameter_constraints_clauses.tpConstraints]? class_extends? interface_body ) -> iface(modifiers = {modifiersST}, name={ $identifier.st }, typeparams={$variant_generic_parameter_list.st} ,comments = { preComments }, @@ -1044,7 +1070,7 @@ predefined_type: | t='short' | t='string' | t='uint' | t='ulong' | t='ushort') -> string(payload={$t.text}); identifier: - IDENTIFIER -> template(v= { $IDENTIFIER.text }) "" | also_keyword -> template(v= { $also_keyword.text }) ""; + i=IDENTIFIER { collectComments($i.TokenStartIndex); } -> template(v= { $IDENTIFIER.text }) "" | also_keyword -> template(v= { $also_keyword.text }) ""; keyword: 'abstract' | 'as' | 'base' | 'bool' | 'break' | 'byte' | 'case' | 'catch' | 'char' | 'checked' | 'class' | 'const' | 'continue' | 'decimal' | 'default' | 'delegate' | 'do' | 'double' | 'else' | 'enum' | 'event' | 'explicit' | 'extern' | 'false' | 'finally' | 'fixed' | 'float' | 'for' | 'foreach' | 'goto' | 'if' | 'implicit' | 'in' | 'int' | 'interface' | 'internal' | 'is' | 'lock' | 'long' | 'namespace' | 'new' | 'null' | 'object' | 'operator' | 'out' | 'override' | 'params' | 'private' | 'protected' | 'public' | 'readonly' | 'ref' | 'return' | 'sbyte' | 'sealed' | 'short' | 'sizeof' | 'stackalloc' | 'static' | 'string' | 'struct' | 'switch' | 'this' | 'throw' | 'true' | 'try' | 'typeof' | 'uint' | 'ulong' | 'unchecked' | 'unsafe' | 'ushort' | 'using' | 'virtual' | 'void' | 'volatile' ; diff --git a/CSharpTranslator/antlr3/src/cs2j/bin/Debug/templates/java.stg b/CSharpTranslator/antlr3/src/cs2j/bin/Debug/templates/java.stg index 0516ba9..be37684 100644 --- a/CSharpTranslator/antlr3/src/cs2j/bin/Debug/templates/java.stg +++ b/CSharpTranslator/antlr3/src/cs2j/bin/Debug/templates/java.stg @@ -44,6 +44,17 @@ iface(modifiers, comments, attributes, name, imps, body) ::= << } >> +class_body(entries) ::= << + +>> + +class_member(comments, member) ::= << + + +>> + +field(modifiers, type, field, comments) ::= " ;" + type_param_constraint(param, constraints) ::= " extends " // ******* ENUMS *********** @@ -63,7 +74,7 @@ enum_member(comments, value) ::= << >> -type(name, stars, opt) ::= "" +type(name, rs, stars, opt) ::= "" namespace_or_type(type1, type2, types) ::= "::." modifiers(mods) ::= " "