mirror of
https://github.com/twiglet/cs2j.git
synced 2025-01-18 13:15:17 +01:00
collect comments as we go, and emit collected comments periodically, emit (some of) field declarations
This commit is contained in:
parent
01fdc1d195
commit
9f2015ed92
@ -20,11 +20,31 @@ options {
|
|||||||
{
|
{
|
||||||
|
|
||||||
public bool IsLast { get; set; }
|
public bool IsLast { get; set; }
|
||||||
public int EmittedCommentTokenIdx { get; set; }
|
public int EmittedCommentTokenIdx { get; set; }
|
||||||
|
|
||||||
|
private List<string> collectedComments = null;
|
||||||
|
List<string> CollectedComments {
|
||||||
|
get {
|
||||||
|
List<string> rets = collectedComments;
|
||||||
|
collectedComments = null;
|
||||||
|
return rets;
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
if (collectedComments == null)
|
||||||
|
collectedComments = new List<string>();
|
||||||
|
foreach (string c in value) {
|
||||||
|
collectedComments.Add(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Collect all comments from previous position to endIdx
|
// Collect all comments from previous position to endIdx
|
||||||
// comments are the text from tokens on the Hidden channel
|
// comments are the text from tokens on the Hidden channel
|
||||||
protected List<string> 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<string> rets = new List<string>();
|
List<string> rets = new List<string>();
|
||||||
List<IToken> toks = ((CommonTokenStream)this.GetTreeNodeStream().TokenStream).GetTokens(EmittedCommentTokenIdx,endIdx);
|
List<IToken> toks = ((CommonTokenStream)this.GetTreeNodeStream().TokenStream).GetTokens(EmittedCommentTokenIdx,endIdx);
|
||||||
if (toks != null) {
|
if (toks != null) {
|
||||||
@ -35,19 +55,20 @@ options {
|
|||||||
}
|
}
|
||||||
EmittedCommentTokenIdx = endIdx+1;
|
EmittedCommentTokenIdx = endIdx+1;
|
||||||
}
|
}
|
||||||
return rets;
|
collectedComments = rets;
|
||||||
}
|
}
|
||||||
protected List<string> collectComments() {
|
|
||||||
return collectComments(((CommonTokenStream)this.GetTreeNodeStream().TokenStream).GetTokens().Count - 1);
|
protected void collectComments() {
|
||||||
|
collectComments(((CommonTokenStream)this.GetTreeNodeStream().TokenStream).GetTokens().Count - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
compilation_unit
|
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},
|
package(now = {DateTime.Now}, includeDate = {true}, packageName = {$nm.text},
|
||||||
type = {$type_declaration.st},
|
type = {$type_declaration.st},
|
||||||
endComments = {IsLast ? collectComments() : null});
|
endComments = { CollectedComments });
|
||||||
|
|
||||||
type_declaration [StringTemplate modifiersST]:
|
type_declaration [StringTemplate modifiersST]:
|
||||||
class_declaration[modifiersST] -> { $class_declaration.st }
|
class_declaration[modifiersST] -> { $class_declaration.st }
|
||||||
@ -72,7 +93,7 @@ modifier
|
|||||||
class_member_declaration:
|
class_member_declaration:
|
||||||
attributes?
|
attributes?
|
||||||
m=modifiers?
|
m=modifiers?
|
||||||
( 'const' type constant_declarators ';'
|
( 'const' t1=type constant_declarators ';'
|
||||||
| event_declaration // 'event'
|
| event_declaration // 'event'
|
||||||
| 'partial' (method_declaration
|
| 'partial' (method_declaration
|
||||||
| interface_declaration[$m.st]
|
| interface_declaration[$m.st]
|
||||||
@ -80,11 +101,11 @@ class_member_declaration:
|
|||||||
| struct_declaration)
|
| struct_declaration)
|
||||||
| interface_declaration[$m.st] // 'interface'
|
| interface_declaration[$m.st] // 'interface'
|
||||||
| 'void' method_declaration
|
| 'void' method_declaration
|
||||||
| type ( (member_name '(') => method_declaration
|
| t2=type ( (member_name '(') => method_declaration
|
||||||
| (member_name '{') => property_declaration
|
| (member_name '{') => property_declaration
|
||||||
| (member_name '.' 'this') => type_name '.' indexer_declaration
|
| (member_name '.' 'this') => type_name '.' indexer_declaration
|
||||||
| indexer_declaration //this
|
| indexer_declaration //this
|
||||||
| field_declaration // qid
|
| field_declaration -> field(modifiers={$m.st}, type={$t2.st}, field={$field_declaration.st}) // qid
|
||||||
| operator_declaration
|
| operator_declaration
|
||||||
)
|
)
|
||||||
// common_modifiers// (method_modifiers | field_modifiers)
|
// common_modifiers// (method_modifiers | field_modifiers)
|
||||||
@ -557,11 +578,11 @@ class_declaration[StringTemplate modifiersST]
|
|||||||
@init {
|
@init {
|
||||||
List<string> preComments = null;
|
List<string> 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]?
|
identifier type_parameter_constraints_clauses? type_parameter_list[$type_parameter_constraints_clauses.tpConstraints]?
|
||||||
class_extends? class_implements? class_body )
|
class_extends? class_implements? class_body )
|
||||||
-> class(modifiers = {modifiersST}, name={ $identifier.st }, typeparams= {$type_parameter_list.st}, comments = { preComments },
|
-> 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<string,StringTemplate> tpConstraints]:
|
type_parameter_list [Dictionary<string,StringTemplate> tpConstraints]:
|
||||||
(attributes? t+=type_parameter[tpConstraints])+ -> template(params={ $t }) "\<<params; separator=\",\">\>";
|
(attributes? t+=type_parameter[tpConstraints])+ -> template(params={ $t }) "\<<params; separator=\",\">\>";
|
||||||
@ -581,9 +602,13 @@ interface_type_list:
|
|||||||
ts+=type (',' ts+=type)* -> template(types={ $ts }) "<types; separator=\",\">";
|
ts+=type (',' ts+=type)* -> template(types={ $ts }) "<types; separator=\",\">";
|
||||||
|
|
||||||
class_body:
|
class_body:
|
||||||
'{' class_member_declarations? '}' ;
|
'{' cs+=class_member_declaration_aux* '}' -> class_body(entries={$cs}) ;
|
||||||
class_member_declarations:
|
class_member_declaration_aux
|
||||||
class_member_declaration+ ;
|
@init{
|
||||||
|
List<string> preComments = null;
|
||||||
|
}:
|
||||||
|
{ preComments = CollectedComments; } member=class_member_declaration -> class_member(comments={ preComments }, member={ $member.st }) ;
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////
|
||||||
constant_declaration:
|
constant_declaration:
|
||||||
@ -652,14 +677,14 @@ enum_declaration[StringTemplate modifiersST]
|
|||||||
@init {
|
@init {
|
||||||
List<string> preComments = null;
|
List<string> 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(comments = { preComments}, modifiers = { $modifiersST }, name={$identifier.text}, body={$enum_body.st}) ;
|
||||||
enum_base:
|
enum_base:
|
||||||
':' integral_type ;
|
':' integral_type ;
|
||||||
enum_body:
|
enum_body:
|
||||||
^(ENUM_BODY es+=enum_member_declaration+) -> enum_body(values={$es});
|
^(ENUM_BODY es+=enum_member_declaration+) -> enum_body(values={$es});
|
||||||
enum_member_declaration:
|
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_modifiers:
|
||||||
// enum_modifier+ ;
|
// enum_modifier+ ;
|
||||||
//enum_modifier:
|
//enum_modifier:
|
||||||
@ -695,11 +720,12 @@ type_parameter_constraints_clause [Dictionary<string,StringTemplate> tpConstrain
|
|||||||
^(TYPE_PARAM_CONSTRAINT t=type_variable_name ts+=type_name+) -> type_param_constraint(param= { $type_variable_name.st }, constraints = { $ts }) ;
|
^(TYPE_PARAM_CONSTRAINT t=type_variable_name ts+=type_name+) -> type_param_constraint(param= { $type_variable_name.st }, constraints = { $ts }) ;
|
||||||
type_variable_name:
|
type_variable_name:
|
||||||
identifier -> { $identifier.st } ;
|
identifier -> { $identifier.st } ;
|
||||||
constructor_constraint:
|
// keving: stripped
|
||||||
'new' '(' ')' ;
|
//constructor_constraint:
|
||||||
|
// 'new' '(' ')' ;
|
||||||
return_type:
|
return_type:
|
||||||
type
|
type -> { $type.st }
|
||||||
| 'void';
|
| 'void' -> string(payload={"void"});
|
||||||
formal_parameter_list:
|
formal_parameter_list:
|
||||||
formal_parameter (',' formal_parameter)* ;
|
formal_parameter (',' formal_parameter)* ;
|
||||||
formal_parameter:
|
formal_parameter:
|
||||||
@ -723,7 +749,7 @@ interface_declaration[StringTemplate modifiersST]
|
|||||||
@init {
|
@init {
|
||||||
List<string> preComments = null;
|
List<string> 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]?
|
identifier type_parameter_constraints_clauses? variant_generic_parameter_list[$type_parameter_constraints_clauses.tpConstraints]?
|
||||||
class_extends? interface_body )
|
class_extends? interface_body )
|
||||||
-> iface(modifiers = {modifiersST}, name={ $identifier.st }, typeparams={$variant_generic_parameter_list.st} ,comments = { preComments },
|
-> 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});
|
| t='short' | t='string' | t='uint' | t='ulong' | t='ushort') -> string(payload={$t.text});
|
||||||
|
|
||||||
identifier:
|
identifier:
|
||||||
IDENTIFIER -> template(v= { $IDENTIFIER.text }) "<v>" | also_keyword -> template(v= { $also_keyword.text }) "<v>";
|
i=IDENTIFIER { collectComments($i.TokenStartIndex); } -> template(v= { $IDENTIFIER.text }) "<v>" | also_keyword -> template(v= { $also_keyword.text }) "<v>";
|
||||||
|
|
||||||
keyword:
|
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' ;
|
'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' ;
|
||||||
|
@ -44,6 +44,17 @@ iface(modifiers, comments, attributes, name, imps, body) ::= <<
|
|||||||
}
|
}
|
||||||
>>
|
>>
|
||||||
|
|
||||||
|
class_body(entries) ::= <<
|
||||||
|
<entries; separator="\n">
|
||||||
|
>>
|
||||||
|
|
||||||
|
class_member(comments, member) ::= <<
|
||||||
|
<comments; separator="\n">
|
||||||
|
<member>
|
||||||
|
>>
|
||||||
|
|
||||||
|
field(modifiers, type, field, comments) ::= "<comments><modifiers> <type> <field>;"
|
||||||
|
|
||||||
type_param_constraint(param, constraints) ::= "<param> extends <constraints; separator=\" & \">"
|
type_param_constraint(param, constraints) ::= "<param> extends <constraints; separator=\" & \">"
|
||||||
|
|
||||||
// ******* ENUMS ***********
|
// ******* ENUMS ***********
|
||||||
@ -63,7 +74,7 @@ enum_member(comments, value) ::= <<
|
|||||||
<value>
|
<value>
|
||||||
>>
|
>>
|
||||||
|
|
||||||
type(name, stars, opt) ::= "<name><stars><opt>"
|
type(name, rs, stars, opt) ::= "<name><rs><stars><opt>"
|
||||||
namespace_or_type(type1, type2, types) ::= "<type1><if(type2)>::<type2><endif><if(types)>.<types; separator=\".\"><endif>"
|
namespace_or_type(type1, type2, types) ::= "<type1><if(type2)>::<type2><endif><if(types)>.<types; separator=\".\"><endif>"
|
||||||
|
|
||||||
modifiers(mods) ::= "<mods; separator=\" \"><if(mods)> <endif>"
|
modifiers(mods) ::= "<mods; separator=\" \"><if(mods)> <endif>"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user