mirror of
https://github.com/twiglet/cs2j.git
synced 2025-01-18 13:15:17 +01:00
add type parameter constraints
This commit is contained in:
parent
7f5b446e03
commit
3adfb22882
@ -640,8 +640,18 @@ attribute_argument_expression:
|
|||||||
///////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////
|
||||||
|
|
||||||
class_declaration returns [string name]:
|
class_declaration returns [string name]:
|
||||||
c='class' type_or_generic { $name = mkTypeName($type_or_generic.type, $type_or_generic.generic_arguments); } class_base? type_parameter_constraints_clauses? class_body ';'?
|
c='class' identifier type_parameter_list? { $name = mkTypeName($identifier.text, $type_parameter_list.names); } class_base? type_parameter_constraints_clauses? class_body ';'?
|
||||||
-> ^(CLASS[$c.Token] type_or_generic class_base? type_parameter_constraints_clauses? class_body );
|
-> ^(CLASS[$c.Token] identifier type_parameter_constraints_clauses? type_parameter_list? class_base? class_body );
|
||||||
|
|
||||||
|
type_parameter_list returns [List<string> names]
|
||||||
|
@init {
|
||||||
|
List<string> names = new List<string>();
|
||||||
|
}:
|
||||||
|
'<'! attributes? t1=type_parameter { names.Add($t1.name); } ( ','! attributes? tn=type_parameter { names.Add($tn.name); })* '>'! ;
|
||||||
|
|
||||||
|
type_parameter returns [string name]:
|
||||||
|
identifier { $name = $identifier.text; } ;
|
||||||
|
|
||||||
class_base:
|
class_base:
|
||||||
// just put all types in a single list. In NetMaker we will extract the base class if necessary
|
// just put all types in a single list. In NetMaker we will extract the base class if necessary
|
||||||
':' interface_type_list -> ^(IMPLEMENTS interface_type_list);
|
':' interface_type_list -> ^(IMPLEMENTS interface_type_list);
|
||||||
@ -774,7 +784,9 @@ integral_type:
|
|||||||
// B.2.12 Delegates
|
// B.2.12 Delegates
|
||||||
delegate_declaration returns [string name]:
|
delegate_declaration returns [string name]:
|
||||||
'delegate' return_type identifier { $name = $identifier.text; } variant_generic_parameter_list?
|
'delegate' return_type identifier { $name = $identifier.text; } variant_generic_parameter_list?
|
||||||
'(' formal_parameter_list? ')' type_parameter_constraints_clauses? ';' ;
|
'(' formal_parameter_list? ')' type_parameter_constraints_clauses? ';' ->
|
||||||
|
'delegate' return_type identifier type_parameter_constraints_clauses? variant_generic_parameter_list?
|
||||||
|
'(' formal_parameter_list? ')' ';';
|
||||||
delegate_modifiers:
|
delegate_modifiers:
|
||||||
modifier+ ;
|
modifier+ ;
|
||||||
// 4.0
|
// 4.0
|
||||||
@ -782,33 +794,34 @@ variant_generic_parameter_list returns [List<string> tyargs]
|
|||||||
@init {
|
@init {
|
||||||
$tyargs = new List<string>();
|
$tyargs = new List<string>();
|
||||||
}:
|
}:
|
||||||
'<' variant_type_parameters[$tyargs] '>' ;
|
'<'! variant_type_parameters[$tyargs] '>'! ;
|
||||||
variant_type_parameters [List<String> tyargs]:
|
variant_type_parameters [List<String> tyargs]:
|
||||||
v1=variant_type_variable_name { tyargs.Add($v1.text); } (',' vn=variant_type_variable_name { tyargs.Add($vn.text); })* ;
|
v1=variant_type_variable_name { tyargs.Add($v1.text); } (',' vn=variant_type_variable_name { tyargs.Add($vn.text); })* -> variant_type_variable_name+ ;
|
||||||
variant_type_variable_name:
|
variant_type_variable_name:
|
||||||
attributes? variance_annotation? type_variable_name ;
|
attributes? variance_annotation? type_variable_name ;
|
||||||
variance_annotation:
|
variance_annotation:
|
||||||
'in' | 'out' ;
|
'in' -> IN | 'out' -> OUT;
|
||||||
|
|
||||||
type_parameter_constraints_clauses:
|
type_parameter_constraints_clauses:
|
||||||
type_parameter_constraints_clause (',' type_parameter_constraints_clause)* ;
|
type_parameter_constraints_clause (',' type_parameter_constraints_clause)* -> type_parameter_constraints_clause+ ;
|
||||||
type_parameter_constraints_clause:
|
type_parameter_constraints_clause:
|
||||||
'where' type_variable_name ':' type_parameter_constraint_list ;
|
'where' type_variable_name ':' type_parameter_constraint_list -> ^(TYPE_PARAM_CONSTRAINT type_variable_name type_parameter_constraint_list?) ;
|
||||||
// class, Circle, new()
|
// class, Circle, new()
|
||||||
type_parameter_constraint_list:
|
type_parameter_constraint_list:
|
||||||
('class' | 'struct') (',' secondary_constraint_list)? (',' constructor_constraint)?
|
('class' | 'struct') (',' secondary_constraint_list)? (',' constructor_constraint)? -> secondary_constraint_list?
|
||||||
| secondary_constraint_list (',' constructor_constraint)?
|
| secondary_constraint_list (',' constructor_constraint)? -> secondary_constraint_list
|
||||||
| constructor_constraint ;
|
| constructor_constraint -> ;
|
||||||
//primary_constraint:
|
//primary_constraint:
|
||||||
// class_type
|
// class_type
|
||||||
// | 'class'
|
// | 'class'
|
||||||
// | 'struct' ;
|
// | 'struct' ;
|
||||||
secondary_constraint_list:
|
secondary_constraint_list:
|
||||||
secondary_constraint (',' secondary_constraint)* ;
|
secondary_constraint (',' secondary_constraint)* -> secondary_constraint+ ;
|
||||||
secondary_constraint:
|
secondary_constraint:
|
||||||
type_name ; // | type_variable_name) ;
|
type_name ; // | type_variable_name) ;
|
||||||
type_variable_name:
|
type_variable_name:
|
||||||
identifier ;
|
identifier ;
|
||||||
|
// keving: TOTEST we drop new constraints, but what will happen in Java for this case?
|
||||||
constructor_constraint:
|
constructor_constraint:
|
||||||
'new' '(' ')' ;
|
'new' '(' ')' ;
|
||||||
return_type:
|
return_type:
|
||||||
@ -836,7 +849,7 @@ parameter_array:
|
|||||||
interface_declaration returns [string name]:
|
interface_declaration returns [string name]:
|
||||||
c='interface' identifier { $name = $identifier.text; } variant_generic_parameter_list?
|
c='interface' identifier { $name = $identifier.text; } variant_generic_parameter_list?
|
||||||
interface_base? type_parameter_constraints_clauses? interface_body ';'?
|
interface_base? type_parameter_constraints_clauses? interface_body ';'?
|
||||||
-> ^(INTERFACE[$c.Token] identifier variant_generic_parameter_list? interface_base? type_parameter_constraints_clauses? interface_body );
|
-> ^(INTERFACE[$c.Token] identifier type_parameter_constraints_clauses? variant_generic_parameter_list? interface_base? interface_body );
|
||||||
|
|
||||||
interface_base:
|
interface_base:
|
||||||
':' interface_type_list -> ^(EXTENDS interface_type_list);
|
':' interface_type_list -> ^(EXTENDS interface_type_list);
|
||||||
@ -880,8 +893,8 @@ method_modifiers:
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////
|
||||||
struct_declaration returns [string name]:
|
struct_declaration returns [string name]:
|
||||||
c='struct' type_or_generic { $name = mkTypeName($type_or_generic.type, $type_or_generic.generic_arguments); } class_base? type_parameter_constraints_clauses? class_body ';'?
|
c='struct' identifier type_parameter_list? { $name = mkTypeName($identifier.text, $type_parameter_list.names); } class_base? type_parameter_constraints_clauses? class_body ';'?
|
||||||
-> ^(CLASS[$c.Token] type_or_generic class_base? type_parameter_constraints_clauses? class_body );
|
-> ^(CLASS[$c.Token] identifier type_parameter_constraints_clauses? type_parameter_list? class_base? class_body );
|
||||||
|
|
||||||
// UNUSED, HOPEFULLY
|
// UNUSED, HOPEFULLY
|
||||||
struct_modifiers:
|
struct_modifiers:
|
||||||
|
@ -318,9 +318,9 @@ qid_start:
|
|||||||
qid_part:
|
qid_part:
|
||||||
access_identifier ;
|
access_identifier ;
|
||||||
|
|
||||||
generic_argument_list:
|
generic_argument_list:
|
||||||
'<' type_arguments '>' -> template(args={ $type_arguments.st }) "\<<args>\>";
|
'<' type_arguments '>' -> template(args={ $type_arguments.st }) "\<<args>\>";
|
||||||
type_arguments:
|
type_arguments:
|
||||||
ts+=type (',' ts+=type)* -> template(types = { $ts }) "<types; separator=\",\">";
|
ts+=type (',' ts+=type)* -> template(types = { $ts }) "<types; separator=\",\">";
|
||||||
|
|
||||||
type
|
type
|
||||||
@ -556,11 +556,21 @@ class_declaration[StringTemplate modifiersST]
|
|||||||
@init {
|
@init {
|
||||||
List<string> preComments = null;
|
List<string> preComments = null;
|
||||||
}:
|
}:
|
||||||
^(c=CLASS { preComments = collectComments($c.TokenStartIndex); } type_or_generic
|
^(c=CLASS { preComments = collectComments($c.TokenStartIndex); }
|
||||||
class_extends? class_implements? type_parameter_constraints_clauses? class_body )
|
identifier type_parameter_constraints_clauses? type_parameter_list[$type_parameter_constraints_clauses.tpConstraints]?
|
||||||
-> class(modifiers = {modifiersST}, name={ $type_or_generic.st }, comments = { preComments },
|
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 }) ;
|
||||||
|
|
||||||
|
type_parameter_list [Dictionary<string,StringTemplate> tpConstraints]:
|
||||||
|
(attributes? t+=type_parameter[tpConstraints])+ -> template(params={ $t }) "\<<params; separator=\",\">\>";
|
||||||
|
|
||||||
|
type_parameter [Dictionary<string,StringTemplate> tpConstraints]
|
||||||
|
@init {
|
||||||
|
StringTemplate mySt = null;
|
||||||
|
}:
|
||||||
|
identifier {if (tpConstraints == null || !tpConstraints.TryGetValue($identifier.text, out mySt)) {mySt = $identifier.st;}; } -> { mySt } ;
|
||||||
|
|
||||||
class_extends:
|
class_extends:
|
||||||
^(EXTENDS ts+=type*) -> extends(types = { $ts }) ;
|
^(EXTENDS ts+=type*) -> extends(types = { $ts }) ;
|
||||||
class_implements:
|
class_implements:
|
||||||
@ -658,39 +668,32 @@ integral_type:
|
|||||||
|
|
||||||
// B.2.12 Delegates
|
// B.2.12 Delegates
|
||||||
delegate_declaration:
|
delegate_declaration:
|
||||||
'delegate' return_type identifier variant_generic_parameter_list?
|
'delegate' return_type identifier type_parameter_constraints_clauses? variant_generic_parameter_list[$type_parameter_constraints_clauses.tpConstraints]?
|
||||||
'(' formal_parameter_list? ')' type_parameter_constraints_clauses? ';' ;
|
'(' formal_parameter_list? ')' ';' ;
|
||||||
delegate_modifiers:
|
delegate_modifiers:
|
||||||
modifier+ ;
|
modifier+ ;
|
||||||
// 4.0
|
// 4.0
|
||||||
variant_generic_parameter_list:
|
variant_generic_parameter_list [Dictionary<string,StringTemplate> tpConstraints]:
|
||||||
'<' variant_type_parameters '>' ;
|
(ps+=variant_generic_parameter[$tpConstraints])+ -> template(params={$ps}) "<params; separator=\",\">";
|
||||||
variant_type_parameters:
|
variant_generic_parameter [Dictionary<string,StringTemplate> tpConstraints]:
|
||||||
variant_type_variable_name (',' variant_type_variable_name)* ;
|
attributes? variance_annotation? t=type_parameter[$tpConstraints] -> template(param={$t.st}, annotation={$variance_annotation.st}) "/* <annotation> */ <param>" ;
|
||||||
variant_type_variable_name:
|
|
||||||
attributes? variance_annotation? type_variable_name ;
|
|
||||||
variance_annotation:
|
variance_annotation:
|
||||||
'in' | 'out' ;
|
IN -> template() "in" | OUT -> template() "out" ;
|
||||||
|
|
||||||
type_parameter_constraints_clauses:
|
// tpConstraints is a map from type variable name to a string expressing the extends constraints
|
||||||
type_parameter_constraints_clause (',' type_parameter_constraints_clause)* ;
|
type_parameter_constraints_clauses returns [Dictionary<string,StringTemplate> tpConstraints]
|
||||||
type_parameter_constraints_clause:
|
@init {
|
||||||
'where' type_variable_name ':' type_parameter_constraint_list ;
|
$tpConstraints = new Dictionary<string,StringTemplate>();
|
||||||
// class, Circle, new()
|
}
|
||||||
type_parameter_constraint_list:
|
:
|
||||||
('class' | 'struct') (',' secondary_constraint_list)? (',' constructor_constraint)?
|
ts+=type_parameter_constraints_clause[$tpConstraints]+ -> ;
|
||||||
| secondary_constraint_list (',' constructor_constraint)?
|
type_parameter_constraints_clause [Dictionary<string,StringTemplate> tpConstraints]
|
||||||
| constructor_constraint ;
|
@after{
|
||||||
//primary_constraint:
|
tpConstraints[$t.text] = $type_parameter_constraints_clause.st;
|
||||||
// class_type
|
}:
|
||||||
// | 'class'
|
^(TYPE_PARAM_CONSTRAINT t=type_variable_name ts+=type_name+) -> type_param_constraint(param= { $type_variable_name.st }, constraints = { $ts }) ;
|
||||||
// | 'struct' ;
|
|
||||||
secondary_constraint_list:
|
|
||||||
secondary_constraint (',' secondary_constraint)* ;
|
|
||||||
secondary_constraint:
|
|
||||||
type_name ; // | type_variable_name) ;
|
|
||||||
type_variable_name:
|
type_variable_name:
|
||||||
identifier ;
|
identifier -> { $identifier.st } ;
|
||||||
constructor_constraint:
|
constructor_constraint:
|
||||||
'new' '(' ')' ;
|
'new' '(' ')' ;
|
||||||
return_type:
|
return_type:
|
||||||
@ -719,9 +722,10 @@ interface_declaration[StringTemplate modifiersST]
|
|||||||
@init {
|
@init {
|
||||||
List<string> preComments = null;
|
List<string> preComments = null;
|
||||||
}:
|
}:
|
||||||
^(c=INTERFACE { preComments = collectComments($c.TokenStartIndex); } identifier variant_generic_parameter_list?
|
^(c=INTERFACE { preComments = collectComments($c.TokenStartIndex); }
|
||||||
class_extends? type_parameter_constraints_clauses? interface_body )
|
identifier type_parameter_constraints_clauses? variant_generic_parameter_list[$type_parameter_constraints_clauses.tpConstraints]?
|
||||||
-> iface(modifiers = {modifiersST}, name={ $identifier.st }, comments = { preComments },
|
class_extends? interface_body )
|
||||||
|
-> iface(modifiers = {modifiersST}, name={ $identifier.st }, typeparams={$variant_generic_parameter_list.st} ,comments = { preComments },
|
||||||
imps = { $class_extends.st }) ;
|
imps = { $class_extends.st }) ;
|
||||||
interface_modifiers:
|
interface_modifiers:
|
||||||
modifier+ ;
|
modifier+ ;
|
||||||
|
@ -513,8 +513,15 @@ attribute_argument_expression:
|
|||||||
///////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////
|
||||||
|
|
||||||
class_declaration:
|
class_declaration:
|
||||||
^(CLASS type_or_generic
|
^(CLASS identifier type_parameter_constraints_clauses? type_parameter_list?
|
||||||
class_implements? type_parameter_constraints_clauses? class_body ) ;
|
class_implements? class_body ) ;
|
||||||
|
|
||||||
|
type_parameter_list:
|
||||||
|
(attributes? type_parameter)+ ;
|
||||||
|
|
||||||
|
type_parameter:
|
||||||
|
identifier ;
|
||||||
|
|
||||||
class_extends:
|
class_extends:
|
||||||
^(EXTENDS type*) ;
|
^(EXTENDS type*) ;
|
||||||
class_implements:
|
class_implements:
|
||||||
@ -610,37 +617,24 @@ integral_type:
|
|||||||
|
|
||||||
// B.2.12 Delegates
|
// B.2.12 Delegates
|
||||||
delegate_declaration:
|
delegate_declaration:
|
||||||
'delegate' return_type identifier variant_generic_parameter_list?
|
'delegate' return_type identifier type_parameter_constraints_clauses? variant_generic_parameter_list?
|
||||||
'(' formal_parameter_list? ')' type_parameter_constraints_clauses? ';' ;
|
'(' formal_parameter_list? ')' ';' ;
|
||||||
delegate_modifiers:
|
delegate_modifiers:
|
||||||
modifier+ ;
|
modifier+ ;
|
||||||
// 4.0
|
// 4.0
|
||||||
variant_generic_parameter_list:
|
variant_generic_parameter_list:
|
||||||
'<' variant_type_parameters '>' ;
|
variant_type_variable_name+ ;
|
||||||
variant_type_parameters:
|
|
||||||
variant_type_variable_name (',' variant_type_variable_name)* ;
|
|
||||||
variant_type_variable_name:
|
variant_type_variable_name:
|
||||||
attributes? variance_annotation? type_variable_name ;
|
attributes? variance_annotation? type_variable_name ;
|
||||||
variance_annotation:
|
variance_annotation:
|
||||||
'in' | 'out' ;
|
IN | OUT ;
|
||||||
|
|
||||||
type_parameter_constraints_clauses:
|
type_parameter_constraints_clauses:
|
||||||
type_parameter_constraints_clause (',' type_parameter_constraints_clause)* ;
|
type_parameter_constraints_clause+ -> type_parameter_constraints_clause*;
|
||||||
type_parameter_constraints_clause:
|
type_parameter_constraints_clause:
|
||||||
'where' type_variable_name ':' type_parameter_constraint_list ;
|
// If there are no type constraints on this variable then drop this constraint
|
||||||
// class, Circle, new()
|
^(TYPE_PARAM_CONSTRAINT type_variable_name) ->
|
||||||
type_parameter_constraint_list:
|
| ^(TYPE_PARAM_CONSTRAINT type_variable_name type_name+) ;
|
||||||
('class' | 'struct') (',' secondary_constraint_list)? (',' constructor_constraint)?
|
|
||||||
| secondary_constraint_list (',' constructor_constraint)?
|
|
||||||
| constructor_constraint ;
|
|
||||||
//primary_constraint:
|
|
||||||
// class_type
|
|
||||||
// | 'class'
|
|
||||||
// | 'struct' ;
|
|
||||||
secondary_constraint_list:
|
|
||||||
secondary_constraint (',' secondary_constraint)* ;
|
|
||||||
secondary_constraint:
|
|
||||||
type_name ; // | type_variable_name) ;
|
|
||||||
type_variable_name:
|
type_variable_name:
|
||||||
identifier ;
|
identifier ;
|
||||||
constructor_constraint:
|
constructor_constraint:
|
||||||
@ -668,8 +662,8 @@ parameter_array:
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////
|
||||||
interface_declaration:
|
interface_declaration:
|
||||||
^(INTERFACE identifier variant_generic_parameter_list?
|
^(INTERFACE identifier type_parameter_constraints_clauses? variant_generic_parameter_list?
|
||||||
class_extends? type_parameter_constraints_clauses? interface_body ) ;
|
class_extends? interface_body ) ;
|
||||||
interface_modifiers:
|
interface_modifiers:
|
||||||
modifier+ ;
|
modifier+ ;
|
||||||
interface_base:
|
interface_base:
|
||||||
|
@ -8,13 +8,17 @@ options {
|
|||||||
|
|
||||||
tokens {
|
tokens {
|
||||||
PACKAGE;
|
PACKAGE;
|
||||||
ENUM_BODY;
|
|
||||||
CLASS;
|
CLASS;
|
||||||
EXTENDS;
|
EXTENDS;
|
||||||
IMPLEMENTS;
|
IMPLEMENTS;
|
||||||
INTERFACE;
|
INTERFACE;
|
||||||
FINAL; /* final modifier */
|
FINAL; /* final modifier */
|
||||||
|
IN;
|
||||||
|
OUT;
|
||||||
|
|
||||||
|
ENUM_BODY;
|
||||||
|
TYPE_PARAM_CONSTRAINT;
|
||||||
|
|
||||||
PAYLOAD; // carries arbitrary text for the output file
|
PAYLOAD; // carries arbitrary text for the output file
|
||||||
PAYLOAD_LIST;
|
PAYLOAD_LIST;
|
||||||
}
|
}
|
||||||
|
@ -28,9 +28,9 @@ package <packageName>;
|
|||||||
|
|
||||||
// ******* CLASSES ***********
|
// ******* CLASSES ***********
|
||||||
|
|
||||||
class(modifiers, comments, attributes, name, inherits, body) ::= <<
|
class(modifiers, comments, attributes, name, typeparams, inherits, body) ::= <<
|
||||||
<comments; separator="\n">
|
<comments; separator="\n">
|
||||||
<modifiers(modifiers)>class <name> <inherits>
|
<modifiers(modifiers)>class <name> <typeparams> <inherits>
|
||||||
{
|
{
|
||||||
<body>
|
<body>
|
||||||
}
|
}
|
||||||
@ -44,6 +44,8 @@ iface(modifiers, comments, attributes, name, imps, body) ::= <<
|
|||||||
}
|
}
|
||||||
>>
|
>>
|
||||||
|
|
||||||
|
type_param_constraint(param, constraints) ::= "<param> extends <constraints; separator=\" & \">"
|
||||||
|
|
||||||
// ******* ENUMS ***********
|
// ******* ENUMS ***********
|
||||||
|
|
||||||
enum(modifiers,comments, attributes, name, body) ::= <<
|
enum(modifiers,comments, attributes, name, body) ::= <<
|
||||||
|
Loading…
x
Reference in New Issue
Block a user