mirror of
https://github.com/twiglet/cs2j.git
synced 2025-01-18 13:15:17 +01:00
added common statement types
This commit is contained in:
parent
55363bc038
commit
5a8a1c4974
@ -66,14 +66,6 @@ scope NSContext {
|
||||
return stripped;
|
||||
}
|
||||
|
||||
protected CommonTree mkType(CommonTree t) {
|
||||
CommonTree root_1 = (CommonTree)adaptor.Nil;
|
||||
root_1 = (CommonTree)adaptor.BecomeRoot((CommonTree)adaptor.Create(TYPE, t.token, "TYPE"), root_1);
|
||||
adaptor.AddChild(root_1, adaptor.DupTree(t));
|
||||
|
||||
return root_1;
|
||||
}
|
||||
|
||||
// TODO: Read reserved words from a file so that they can be extended by customer
|
||||
private readonly static string[] javaReserved = new string[] { "int", "protected", "package" };
|
||||
|
||||
@ -105,6 +97,9 @@ scope NSContext {
|
||||
protected CommonTree mkHole(IToken tok) {
|
||||
return (CommonTree)adaptor.Create(KGHOLE, tok, "KGHOLE");
|
||||
}
|
||||
|
||||
// counter to ensure that the catch vars we introduce are unique
|
||||
protected int dummyCatchVarCtr = 0;
|
||||
}
|
||||
|
||||
/********************************************************************************************
|
||||
@ -192,12 +187,12 @@ class_member_declaration:
|
||||
m=modifiers?
|
||||
( 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[$a.tree, $m.tree, mkType($v1)] -> $m3 //-> ^(METHOD[$v1.token, "METHOD"] $a? $m? ^(TYPE $v1) $m3)
|
||||
| p='partial' { Warning($p.line, "[UNSUPPORTED] 'partial' definition"); } (v1=void_type m3=method_declaration[$a.tree, $m.tree, $v1.tree] -> $m3 //-> ^(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'
|
||||
| v2='void' m1=method_declaration[$a.tree, $m.tree, mkType($v2) ] -> $m1 //-> ^(METHOD[$v.token, "METHOD"] $a? $m? ^(TYPE[$v.token, "TYPE"] $v) $m1)
|
||||
| v2=void_type m1=method_declaration[$a.tree, $m.tree, $v2.tree] -> $m1 //-> ^(METHOD[$v.token, "METHOD"] $a? $m? ^(TYPE[$v.token, "TYPE"] $v) $m1)
|
||||
| t=type ( (member_name type_parameter_list? '(') => m2=method_declaration[$a.tree, $m.tree, $t.tree] -> $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)
|
||||
@ -250,8 +245,8 @@ primary_expression_start:
|
||||
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) } )
|
||||
| p='++' -> ^(POSTINC[$p.token, "++"] { (CommonTree)adaptor.DupTree($lhs) } )
|
||||
| m='--' -> ^(POSTDEC[$m.token, "--"] { (CommonTree)adaptor.DupTree($lhs) } )
|
||||
;
|
||||
access_identifier [CommonTree lhs]:
|
||||
access_operator type_or_generic -> ^(access_operator { (CommonTree)adaptor.DupTree($lhs) } type_or_generic);
|
||||
@ -395,7 +390,7 @@ initializer_value:
|
||||
typeof_expression:
|
||||
'typeof'^ '('! ((unbound_type_name) => unbound_type_name
|
||||
| type
|
||||
| 'void') ')'! ;
|
||||
| void_type) ')'! ;
|
||||
// unbound type examples
|
||||
//foo<bar<X<>>>
|
||||
//bar::foo<>
|
||||
@ -889,7 +884,7 @@ constructor_constraint:
|
||||
'new' '(' ')' ;
|
||||
return_type:
|
||||
type
|
||||
| v='void' -> ^(TYPE[$v.token, "TYPE"] $v);
|
||||
| void_type ;
|
||||
formal_parameter_list:
|
||||
formal_parameter (',' formal_parameter)* -> ^(PARAMS formal_parameter+);
|
||||
formal_parameter:
|
||||
@ -925,7 +920,7 @@ interface_member_declarations:
|
||||
interface_member_declaration+ ;
|
||||
interface_member_declaration:
|
||||
attributes? modifiers?
|
||||
('void' interface_method_declaration
|
||||
(void_type interface_method_declaration
|
||||
| interface_event_declaration
|
||||
| type ( (member_name '(') => interface_method_declaration
|
||||
| (member_name '{') => interface_property_declaration
|
||||
@ -974,14 +969,14 @@ struct_member_declaration:
|
||||
attributes? m=modifiers?
|
||||
( 'const' type constant_declarators ';'
|
||||
| event_declaration // 'event'
|
||||
| p='partial' { Warning($p.line, "[UNSUPPORTED] 'partial' definition"); } (v1='void' method_declaration[$attributes.tree, $modifiers.tree, mkType($v1)]
|
||||
| p='partial' { Warning($p.line, "[UNSUPPORTED] 'partial' definition"); } (v1=void_type method_declaration[$attributes.tree, $modifiers.tree, $v1.tree]
|
||||
| interface_declaration
|
||||
| class_declaration
|
||||
| struct_declaration)
|
||||
|
||||
| interface_declaration // 'interface'
|
||||
| class_declaration // 'class'
|
||||
| v2='void' method_declaration[$attributes.tree, $modifiers.tree, mkType($v2) ]
|
||||
| v2=void_type method_declaration[$attributes.tree, $modifiers.tree, $v2.tree]
|
||||
| t1=type ( (member_name type_parameter_list? '(') => method_declaration[$attributes.tree, $modifiers.tree, $t1.tree]
|
||||
| (member_name '{') => property_declaration
|
||||
| (member_name '.' 'this') => type_name '.' indexer_declaration
|
||||
@ -1102,9 +1097,9 @@ fixed_pointer_initializer:
|
||||
//'&' variable_reference // unary_expression covers this
|
||||
expression;
|
||||
unsafe_statement:
|
||||
'unsafe' block;
|
||||
'unsafe'^ block;
|
||||
labeled_statement:
|
||||
identifier ':' statement ;
|
||||
identifier ':'^ statement ;
|
||||
declaration_statement:
|
||||
(local_variable_declaration
|
||||
| local_constant_declaration) ';' ;
|
||||
@ -1144,29 +1139,30 @@ if_statement:
|
||||
else_statement:
|
||||
'else' embedded_statement ;
|
||||
switch_statement:
|
||||
'switch' '(' expression ')' switch_block ;
|
||||
s='switch' '(' expression ')' switch_block -> ^($s expression switch_block);
|
||||
switch_block:
|
||||
'{' switch_sections? '}' ;
|
||||
switch_sections:
|
||||
switch_section+ ;
|
||||
'{'! switch_section* '}'! ;
|
||||
//switch_sections:
|
||||
// switch_section+ ;
|
||||
switch_section:
|
||||
switch_labels statement_list ;
|
||||
switch_labels:
|
||||
switch_label+ ;
|
||||
switch_label+ statement_list -> ^(SWITCH_SECTION switch_label+ statement_list);
|
||||
//switch_labels:
|
||||
// switch_label+ ;
|
||||
switch_label:
|
||||
('case' constant_expression ':')
|
||||
| ('default' ':') ;
|
||||
('case'^ constant_expression ':'!)
|
||||
| ('default' ':'!);
|
||||
iteration_statement:
|
||||
while_statement
|
||||
| do_statement
|
||||
| for_statement
|
||||
| foreach_statement ;
|
||||
while_statement:
|
||||
'while' '(' boolean_expression ')' embedded_statement ;
|
||||
w='while' '(' boolean_expression ')' embedded_statement -> ^($w boolean_expression SEP embedded_statement);
|
||||
do_statement:
|
||||
'do' embedded_statement 'while' '(' boolean_expression ')' ';' ;
|
||||
for_statement:
|
||||
'for' '(' for_initializer? ';' for_condition? ';' for_iterator? ')' embedded_statement ;
|
||||
f='for' '(' for_initializer? ';' for_condition? ';' for_iterator? ')' embedded_statement
|
||||
-> ^($f for_initializer? SEP for_condition? SEP for_iterator? SEP embedded_statement);
|
||||
for_initializer:
|
||||
(local_variable_declaration) => local_variable_declaration
|
||||
| statement_expression_list
|
||||
@ -1178,7 +1174,8 @@ for_iterator:
|
||||
statement_expression_list:
|
||||
statement_expression (',' statement_expression)* ;
|
||||
foreach_statement:
|
||||
'foreach' '(' local_variable_type identifier 'in' expression ')' embedded_statement ;
|
||||
f='foreach' '(' local_variable_type identifier 'in' expression ')' embedded_statement
|
||||
-> ^($f local_variable_type identifier expression SEP embedded_statement);
|
||||
jump_statement:
|
||||
break_statement
|
||||
| continue_statement
|
||||
@ -1194,23 +1191,25 @@ goto_statement:
|
||||
| 'case' constant_expression
|
||||
| 'default') ';' ;
|
||||
return_statement:
|
||||
'return' expression? ';' ;
|
||||
'return'^ expression? ';'! ;
|
||||
throw_statement:
|
||||
'throw' expression? ';' ;
|
||||
'throw'^ expression? ';'! ;
|
||||
try_statement:
|
||||
'try' block ( catch_clauses finally_clause?
|
||||
| finally_clause);
|
||||
//TODO one or both
|
||||
t='try' block ( catch_clauses finally_clause?
|
||||
| finally_clause) -> ^($t block catch_clauses? finally_clause?);
|
||||
// We rewrite the catch clauses so that they all have the form "(catch Type Var)" by introducing
|
||||
// Throwable and dummy vars as necessary
|
||||
catch_clauses:
|
||||
'catch' (specific_catch_clauses | general_catch_clause) ;
|
||||
'catch'^ general_catch_clause
|
||||
| c='catch' specific_catch_clause catch_clauses* -> ^($c specific_catch_clause) catch_clauses*;
|
||||
specific_catch_clauses:
|
||||
specific_catch_clause ('catch' (specific_catch_clause | general_catch_clause))*;
|
||||
specific_catch_clause:
|
||||
'(' class_type identifier? ')' block ;
|
||||
'(' class_type (identifier|magicCatchVar) ')' block -> class_type identifier? magicCatchVar? block ;
|
||||
general_catch_clause:
|
||||
block ;
|
||||
block magicThrowableType magicCatchVar -> magicThrowableType magicCatchVar block;
|
||||
finally_clause:
|
||||
'finally' block ;
|
||||
'finally'^ block ;
|
||||
checked_statement:
|
||||
'checked' block ;
|
||||
unchecked_statement:
|
||||
@ -1282,3 +1281,11 @@ literal:
|
||||
| NULL
|
||||
;
|
||||
|
||||
void_type:
|
||||
v='void' -> ^(TYPE[$v.token, "TYPE"] $v);
|
||||
|
||||
magicThrowableType:
|
||||
-> ^(TYPE["TYPE"] IDENTIFIER["Throwable"]);
|
||||
|
||||
magicCatchVar:
|
||||
-> IDENTIFIER["__dummyCatchVar" + dummyCatchVarCtr++];
|
||||
|
@ -300,17 +300,20 @@ class_member_declaration returns [List<String> preComments]:
|
||||
// )
|
||||
// ;
|
||||
//
|
||||
primary_expression:
|
||||
^(INDEX expression expression_list?)
|
||||
| ^(APPLY expression argument_list?) -> application(func= { $expression.st }, funcparens = { comparePrecedence(APPLY, $expression.precedence) <= 0 }, args = { $argument_list.st } )
|
||||
| ^(POSTINC expression)
|
||||
| ^(POSTDEC expression)
|
||||
primary_expression returns [int precedence]
|
||||
@init {
|
||||
$precedence = int.MaxValue;
|
||||
}:
|
||||
^(INDEX expression expression_list?) { $precedence = precedence[INDEX]; } -> index(func= { $expression.st }, funcparens = { comparePrecedence(INDEX, $expression.precedence) <= 0 }, args = { $expression_list.st } )
|
||||
| ^(APPLY expression argument_list?) { $precedence = precedence[APPLY]; } -> application(func= { $expression.st }, funcparens = { comparePrecedence(APPLY, $expression.precedence) <= 0 }, args = { $argument_list.st } )
|
||||
| ^((op=POSTINC|op=POSTDEC) expression) { $precedence = precedence[$op.token.Type]; }
|
||||
-> op(pre={$expression.st}, op={ $op.token.Text }, preparens= { comparePrecedence($op.token, $expression.precedence) <= 0 })
|
||||
| primary_expression_start -> { $primary_expression_start.st }
|
||||
| ^(access_operator expression type_or_generic) -> op(pre={ $expression.st }, op={ $access_operator.st }, post={ $type_or_generic.st })
|
||||
| ^(access_operator expression type_or_generic) { $precedence = $access_operator.precedence; } -> op(pre={ $expression.st }, op={ $access_operator.st }, post={ $type_or_generic.st })
|
||||
// ('this' brackets) => 'this' brackets primary_expression_part*
|
||||
// | ('base' brackets) => 'this' brackets primary_expression_part*
|
||||
// | primary_expression_start primary_expression_part*
|
||||
| ^(NEW type argument_list? object_or_collection_initializer?) -> construct(type = {$type.st}, args = {$argument_list.st}, inits = {$object_or_collection_initializer.st})
|
||||
| ^(NEW type argument_list? object_or_collection_initializer?) { $precedence = precedence[NEW]; }-> construct(type = {$type.st}, args = {$argument_list.st}, inits = {$object_or_collection_initializer.st})
|
||||
| 'new' (
|
||||
// try the simple one first, this has no argS and no expressions
|
||||
// symantically could be object creation
|
||||
@ -348,7 +351,9 @@ primary_expression_start:
|
||||
| primary_expression_extalias -> unsupported(reason = {"external aliases are not yet supported"}, text= { $primary_expression_extalias.st } )
|
||||
| 'this'
|
||||
| 'base'
|
||||
| typeof_expression // typeof(Foo).Name
|
||||
// keving: needs fixing in javamaker - > type.class
|
||||
| ^('typeof' unbound_type_name ) -> typeof(type= { $unbound_type_name.st })
|
||||
| ^('typeof' type ) -> typeof(type= { $type.st })
|
||||
| literal -> { $literal.st }
|
||||
;
|
||||
|
||||
@ -364,8 +369,8 @@ primary_expression_part:
|
||||
| '--' ;
|
||||
access_identifier:
|
||||
access_operator type_or_generic ;
|
||||
access_operator:
|
||||
(op='.' | op='->') -> string(payload = { $op.token.Text }) ;
|
||||
access_operator returns [int precedence]:
|
||||
(op='.' | op='->') { $precedence = precedence[$op.token.Type]; } -> string(payload = { $op.token.Text }) ;
|
||||
brackets_or_arguments:
|
||||
brackets | arguments ;
|
||||
brackets:
|
||||
@ -496,8 +501,6 @@ initializer_value:
|
||||
|
||||
///////////////////////////////////////////////////////
|
||||
|
||||
typeof_expression:
|
||||
^('typeof' (unbound_type_name | type | 'void') ) ;
|
||||
// unbound type examples
|
||||
//foo<bar<X<>>>
|
||||
//bar::foo<>
|
||||
@ -575,8 +578,6 @@ array_type:
|
||||
type -> { $type.st } ;
|
||||
unmanaged_type:
|
||||
type -> { $type.st } ;
|
||||
class_type:
|
||||
type -> { $type.st } ;
|
||||
pointer_type:
|
||||
type -> { $type.st } ;
|
||||
|
||||
@ -591,9 +592,6 @@ block returns [bool isSemi]
|
||||
';' { $isSemi = true; } ->
|
||||
| '{' s+=statement* '}' -> statement_list(statements = { $s });
|
||||
|
||||
statement_list:
|
||||
statement+ ;
|
||||
|
||||
///////////////////////////////////////////////////////
|
||||
// Expression Section
|
||||
///////////////////////////////////////////////////////
|
||||
@ -602,7 +600,7 @@ expression returns [int precedence]:
|
||||
| non_assignment_expression { $precedence = $non_assignment_expression.precedence; } -> { $non_assignment_expression.st }
|
||||
;
|
||||
expression_list:
|
||||
expression (',' expression)* ;
|
||||
e+=expression (',' e+=expression)* -> list(items= { $e }, sep = {", "});
|
||||
assignment returns [int precedence]:
|
||||
unary_expression assignment_operator expression { $precedence = $assignment_operator.precedence; }
|
||||
-> assign(lhs={ $unary_expression.st }, assign = { $assignment_operator.st }, rhs = { $expression.st },
|
||||
@ -833,7 +831,7 @@ constant_declarators:
|
||||
constant_declarator:
|
||||
identifier ('=' constant_expression)? ;
|
||||
constant_expression:
|
||||
expression;
|
||||
expression -> { $expression.st };
|
||||
|
||||
///////////////////////////////////////////////////////
|
||||
field_declaration:
|
||||
@ -1120,7 +1118,7 @@ invocation_start:
|
||||
| 'this'
|
||||
| 'base'
|
||||
| identifier ('::' identifier)?
|
||||
| typeof_expression // typeof(Foo).Name
|
||||
| ^('typeof' (unbound_type_name | type ) ) // typeof(Foo).Name
|
||||
;
|
||||
invocation_part:
|
||||
access_identifier
|
||||
@ -1140,20 +1138,27 @@ statement_plus:
|
||||
;
|
||||
embedded_statement returns [bool isSemi, bool isBraces, bool isIf]
|
||||
@init {
|
||||
StringTemplate someText = null;
|
||||
$isBraces = false;
|
||||
$isIf = false;
|
||||
}:
|
||||
block { $isSemi = $block.isSemi; $isBraces = !$block.isSemi;} -> { $block.st }
|
||||
| selection_statement { $isIf = $selection_statement.isIf; }-> { $selection_statement.st } // if, switch
|
||||
| ^(IF boolean_expression SEP t=embedded_statement e=else_statement?) { $isIf = true; }
|
||||
-> if(cond= { $boolean_expression.st },
|
||||
then = { $t.st }, thensemi = { $t.isSemi }, thenbraces = { $t.isBraces },
|
||||
else = { $e.st }, elsesemi = { $e.isSemi }, elsebraces = { $e.isBraces }, elseisif = { $e.isIf })
|
||||
| ^('switch' expression s+=switch_section*) -> switch(scrutinee = { $expression.st }, sections = { $s })
|
||||
| iteration_statement -> { $iteration_statement.st } // while, do, for, foreach
|
||||
| jump_statement -> { $jump_statement.st } // break, continue, goto, return, throw
|
||||
| try_statement -> { $try_statement.st }
|
||||
| ^('try' b=block catch_clauses? finally_clause?) -> try(block = {$b.st}, blocksemi = {$b.isSemi}, blockbraces = { !$b.isSemi },
|
||||
catches = { $catch_clauses.st }, fin = { $finally_clause.st } )
|
||||
| checked_statement
|
||||
| unchecked_statement
|
||||
| lock_statement
|
||||
| lock_statement -> { $lock_statement.st }
|
||||
| using_statement
|
||||
| yield_statement
|
||||
| unsafe_statement
|
||||
| ^('unsafe' block { someText = %op(); %{someText}.op="unsafe"; %{someText}.post = $block.st; })
|
||||
-> unsupported(reason = {"unsafe blocks are not supported"}, text = { someText } )
|
||||
| fixed_statement
|
||||
| expression_statement -> op( pre={ $expression_statement.st }, op={ ";" }) // make an expression a statement, need to terminate with semi
|
||||
;
|
||||
@ -1166,13 +1171,11 @@ fixed_pointer_declarator:
|
||||
fixed_pointer_initializer:
|
||||
//'&' variable_reference // unary_expression covers this
|
||||
expression;
|
||||
unsafe_statement:
|
||||
'unsafe' block;
|
||||
labeled_statement:
|
||||
identifier ':' statement ;
|
||||
declaration_statement:
|
||||
(local_variable_declaration -> { $local_variable_declaration.st }
|
||||
| local_constant_declaration -> { $local_constant_declaration.st }) ';' ;
|
||||
(local_variable_declaration -> op(pre = { $local_variable_declaration.st }, op = { ";" })
|
||||
| local_constant_declaration -> op(pre = { $local_constant_declaration.st }, op = { ";" }) ) ';' ;
|
||||
local_variable_declaration:
|
||||
local_variable_type local_variable_declarators -> local_variable_declaration(type={ $local_variable_type.st }, decs = { $local_variable_declarators.st } );
|
||||
local_variable_type:
|
||||
@ -1198,94 +1201,69 @@ expression_statement:
|
||||
|
||||
// TODO: should be assignment, call, increment, decrement, and new object expressions
|
||||
statement_expression:
|
||||
expression
|
||||
expression -> { $expression.st }
|
||||
;
|
||||
selection_statement returns [bool isIf]:
|
||||
if_statement { $isIf = true; }-> { $if_statement.st }
|
||||
| switch_statement { $isIf = false; }-> { $switch_statement.st };
|
||||
if_statement:
|
||||
// else goes with closest if
|
||||
^(IF boolean_expression SEP t=embedded_statement e=else_statement?)
|
||||
-> if(cond= { $boolean_expression.st },
|
||||
then = { $t.st }, thensemi = { $t.isSemi }, thenbraces = { $t.isBraces },
|
||||
else = { $else_statement.st }, elsesemi = { $e.isSemi }, elsebraces = { $e.isBraces }, elseisif = { $e.isIf })
|
||||
|
||||
;
|
||||
else_statement returns [bool isSemi, bool isBraces, bool isIf]:
|
||||
'else' s=embedded_statement { $isSemi = $s.isSemi; $isBraces = $s.isBraces; $isIf = $s.isIf; } -> { $embedded_statement.st } ;
|
||||
switch_statement:
|
||||
'switch' '(' expression ')' switch_block ;
|
||||
switch_block:
|
||||
'{' switch_sections? '}' ;
|
||||
switch_sections:
|
||||
switch_section+ ;
|
||||
switch_section:
|
||||
switch_labels statement_list ;
|
||||
switch_labels:
|
||||
switch_label+ ;
|
||||
^(SWITCH_SECTION lab+=switch_label+ stat+=statement+) -> switch_section(labels = { $lab }, statements = { $stat });
|
||||
switch_label:
|
||||
('case' constant_expression ':')
|
||||
| ('default' ':') ;
|
||||
^('case' constant_expression) -> case(what = { $constant_expression.st })
|
||||
| 'default' -> default_template() ;
|
||||
iteration_statement:
|
||||
while_statement
|
||||
| do_statement
|
||||
| for_statement
|
||||
| foreach_statement ;
|
||||
while_statement:
|
||||
'while' '(' boolean_expression ')' embedded_statement ;
|
||||
^('while' boolean_expression SEP embedded_statement)
|
||||
-> while(cond = { $boolean_expression.st }, block = { $embedded_statement.st }, blocksemi = { $embedded_statement.isSemi }, blockbraces = { $embedded_statement.isBraces })
|
||||
| do_statement -> { $do_statement.st }
|
||||
| ^('for' for_initializer? SEP expression? SEP for_iterator? SEP embedded_statement)
|
||||
-> for(init = { $for_initializer.st }, cond = { $expression.st }, iter = { $for_iterator.st },
|
||||
block = { $embedded_statement.st }, blocksemi = { $embedded_statement.isSemi }, blockbraces = { $embedded_statement.isBraces })
|
||||
| ^('foreach' local_variable_type identifier expression SEP embedded_statement)
|
||||
-> foreach(type = { $local_variable_type.st }, loopid = { $identifier.st }, fromexp = { $expression.st },
|
||||
block = { $embedded_statement.st }, blocksemi = { $embedded_statement.isSemi }, blockbraces = { $embedded_statement.isBraces });
|
||||
do_statement:
|
||||
'do' embedded_statement 'while' '(' boolean_expression ')' ';' ;
|
||||
for_statement:
|
||||
'for' '(' for_initializer? ';' for_condition? ';' for_iterator? ')' embedded_statement ;
|
||||
for_initializer:
|
||||
(local_variable_declaration) => local_variable_declaration
|
||||
| statement_expression_list
|
||||
(local_variable_declaration) => local_variable_declaration -> { $local_variable_declaration.st }
|
||||
| statement_expression_list -> { $statement_expression_list.st }
|
||||
;
|
||||
for_condition:
|
||||
boolean_expression ;
|
||||
for_iterator:
|
||||
statement_expression_list ;
|
||||
statement_expression_list -> { $statement_expression_list.st };
|
||||
statement_expression_list:
|
||||
statement_expression (',' statement_expression)* ;
|
||||
foreach_statement:
|
||||
'foreach' '(' local_variable_type identifier 'in' expression ')' embedded_statement ;
|
||||
s+=statement_expression (',' s+=statement_expression)* -> list(items = { $s }, sep = { ", " });
|
||||
jump_statement:
|
||||
break_statement-> { $break_statement.st }
|
||||
| continue_statement-> { $continue_statement.st }
|
||||
'break' ';' -> string(payload={"break;"})
|
||||
| 'continue' ';' -> string(payload={"continue;"})
|
||||
| goto_statement-> { $goto_statement.st }
|
||||
| return_statement -> { $return_statement.st }
|
||||
| throw_statement -> { $throw_statement.st };
|
||||
break_statement:
|
||||
'break' ';' -> string(payload={"break"});
|
||||
continue_statement:
|
||||
'continue' ';' -> string(payload={"continue"});
|
||||
| ^('return' expression?) -> return(exp = { $expression.st })
|
||||
| ^('throw' expression?) -> throw(exp = { $expression.st });
|
||||
goto_statement:
|
||||
'goto' ( identifier
|
||||
| 'case' constant_expression
|
||||
| 'default') ';' ;
|
||||
return_statement:
|
||||
'return' expression? ';' -> return(exp = { $expression.st });
|
||||
throw_statement:
|
||||
'throw' expression? ';' -> throw(exp = { $expression.st });
|
||||
try_statement:
|
||||
'try' block ( catch_clauses finally_clause?
|
||||
| finally_clause);
|
||||
//TODO one or both
|
||||
catch_clauses:
|
||||
'catch' (specific_catch_clauses | general_catch_clause) ;
|
||||
specific_catch_clauses:
|
||||
specific_catch_clause ('catch' (specific_catch_clause | general_catch_clause))*;
|
||||
specific_catch_clause:
|
||||
'(' class_type identifier? ')' block ;
|
||||
general_catch_clause:
|
||||
block ;
|
||||
c+=catch_clause+ -> list(items={ $c }, sep = {"\n" }) ;
|
||||
catch_clause:
|
||||
^('catch' type identifier block) -> catch_template(type = { $type.st }, id = { $identifier.st }, block = {$block.st}, blocksemi = { $block.isSemi }, blockbraces = { !$block.isSemi } );
|
||||
finally_clause:
|
||||
'finally' block ;
|
||||
^('finally' block) -> fin(block = {$block.st}, blocksemi = { $block.isSemi }, blockbraces = { !$block.isSemi });
|
||||
checked_statement:
|
||||
'checked' block ;
|
||||
unchecked_statement:
|
||||
'unchecked' block ;
|
||||
lock_statement:
|
||||
'lock' '(' expression ')' embedded_statement ;
|
||||
lock_statement
|
||||
@init {
|
||||
StringTemplate someText = null;
|
||||
}:
|
||||
'lock' '(' expression ')' embedded_statement
|
||||
{ someText = %lock();
|
||||
%{someText}.exp = $expression.st;
|
||||
%{someText}.block = $embedded_statement.st;
|
||||
%{someText}.blocksemi = $embedded_statement.isSemi;
|
||||
%{someText}.blockbraces = $embedded_statement.isBraces; } -> unsupported(reason = {"lock() statements are not supported"}, text = { someText } );
|
||||
using_statement:
|
||||
'using' '(' resource_acquisition ')' embedded_statement ;
|
||||
resource_acquisition:
|
||||
|
@ -872,16 +872,17 @@ statement_plus:
|
||||
;
|
||||
embedded_statement:
|
||||
block
|
||||
| selection_statement // if, switch
|
||||
| ^(IF boolean_expression SEP embedded_statement else_statement?)
|
||||
| ^('switch' expression switch_section*)
|
||||
| iteration_statement // while, do, for, foreach
|
||||
| jump_statement // break, continue, goto, return, throw
|
||||
| try_statement
|
||||
| ^('try' block catch_clauses? finally_clause?)
|
||||
| checked_statement
|
||||
| unchecked_statement
|
||||
| lock_statement
|
||||
| using_statement
|
||||
| yield_statement
|
||||
| unsafe_statement
|
||||
| ^('unsafe' block)
|
||||
| fixed_statement
|
||||
| expression_statement // expression!
|
||||
;
|
||||
@ -894,8 +895,6 @@ fixed_pointer_declarator:
|
||||
fixed_pointer_initializer:
|
||||
//'&' variable_reference // unary_expression covers this
|
||||
expression;
|
||||
unsafe_statement:
|
||||
'unsafe' block;
|
||||
labeled_statement:
|
||||
identifier ':' statement ;
|
||||
declaration_statement:
|
||||
@ -926,40 +925,20 @@ expression_statement:
|
||||
statement_expression:
|
||||
expression
|
||||
;
|
||||
selection_statement:
|
||||
if_statement
|
||||
| switch_statement ;
|
||||
if_statement:
|
||||
// else goes with closest if
|
||||
// 'if' '(' boolean_expression ')' embedded_statement (('else') => else_statement)?
|
||||
^(IF boolean_expression SEP embedded_statement else_statement?)
|
||||
;
|
||||
else_statement:
|
||||
'else' embedded_statement ;
|
||||
switch_statement:
|
||||
'switch' '(' expression ')' switch_block ;
|
||||
switch_block:
|
||||
'{' switch_sections? '}' ;
|
||||
switch_sections:
|
||||
switch_section+ ;
|
||||
switch_section:
|
||||
switch_labels statement_list ;
|
||||
switch_labels:
|
||||
switch_label+ ;
|
||||
^(SWITCH_SECTION switch_label+ statement_list) ;
|
||||
switch_label:
|
||||
('case' constant_expression ':')
|
||||
| ('default' ':') ;
|
||||
^('case' constant_expression)
|
||||
| 'default';
|
||||
iteration_statement:
|
||||
while_statement
|
||||
^('while' boolean_expression SEP embedded_statement)
|
||||
| do_statement
|
||||
| for_statement
|
||||
| foreach_statement ;
|
||||
while_statement:
|
||||
'while' '(' boolean_expression ')' embedded_statement ;
|
||||
| ^('for' for_initializer? SEP for_condition? SEP for_iterator? SEP embedded_statement)
|
||||
| ^('foreach' local_variable_type identifier expression SEP embedded_statement);
|
||||
do_statement:
|
||||
'do' embedded_statement 'while' '(' boolean_expression ')' ';' ;
|
||||
for_statement:
|
||||
'for' '(' for_initializer? ';' for_condition? ';' for_iterator? ')' embedded_statement ;
|
||||
for_initializer:
|
||||
(local_variable_declaration) => local_variable_declaration
|
||||
| statement_expression_list
|
||||
@ -970,14 +949,12 @@ for_iterator:
|
||||
statement_expression_list ;
|
||||
statement_expression_list:
|
||||
statement_expression (',' statement_expression)* ;
|
||||
foreach_statement:
|
||||
'foreach' '(' local_variable_type identifier 'in' expression ')' embedded_statement ;
|
||||
jump_statement:
|
||||
break_statement
|
||||
| continue_statement
|
||||
| goto_statement
|
||||
| return_statement
|
||||
| throw_statement ;
|
||||
| ^('return' expression?)
|
||||
| ^('throw' expression?);
|
||||
break_statement:
|
||||
'break' ';' ;
|
||||
continue_statement:
|
||||
@ -986,24 +963,12 @@ goto_statement:
|
||||
'goto' ( identifier
|
||||
| 'case' constant_expression
|
||||
| 'default') ';' ;
|
||||
return_statement:
|
||||
'return' expression? ';' ;
|
||||
throw_statement:
|
||||
'throw' expression? ';' ;
|
||||
try_statement:
|
||||
'try' block ( catch_clauses finally_clause?
|
||||
| finally_clause);
|
||||
//TODO one or both
|
||||
catch_clauses:
|
||||
'catch' (specific_catch_clauses | general_catch_clause) ;
|
||||
specific_catch_clauses:
|
||||
specific_catch_clause ('catch' (specific_catch_clause | general_catch_clause))*;
|
||||
specific_catch_clause:
|
||||
'(' class_type identifier? ')' block ;
|
||||
general_catch_clause:
|
||||
block ;
|
||||
catch_clause+ ;
|
||||
catch_clause:
|
||||
^('catch' class_type identifier block) ;
|
||||
finally_clause:
|
||||
'finally' block ;
|
||||
^('finally' block) ;
|
||||
checked_statement:
|
||||
'checked' block ;
|
||||
unchecked_statement:
|
||||
|
@ -29,6 +29,7 @@ tokens {
|
||||
DESTRUCTOR;
|
||||
METHOD_HEADER;
|
||||
PARAMS;
|
||||
SWITCH_SECTION;
|
||||
|
||||
MONOPLUS;
|
||||
MONOMINUS;
|
||||
|
@ -85,7 +85,7 @@ statement(statement) ::= <<
|
||||
|
||||
//***** local var declarations:
|
||||
|
||||
local_variable_declaration(type,decs) ::= "<type> <decs>;"
|
||||
local_variable_declaration(type,decs) ::= "<type> <decs>"
|
||||
local_variable_declarator(name, init) ::= "<name><if(init)> = <init><endif>"
|
||||
|
||||
return(exp) ::= "return <exp>;"
|
||||
@ -132,6 +132,63 @@ else<if(elseisif)> <block(statements = else, issemi = elsesemi, isbraces = elseb
|
||||
<endif>
|
||||
>>
|
||||
|
||||
while(cond,block,blocksemi, blockbraces) ::= <<
|
||||
while (<cond>)
|
||||
<block(statements = block, issemi = blocksemi, isbraces = blockbraces)>
|
||||
>>
|
||||
|
||||
for(init,cond,iter,block,blocksemi, blockbraces) ::= <<
|
||||
for (<init>;<cond>;<iter>)
|
||||
<block(statements = block, issemi = blocksemi, isbraces = blockbraces)>
|
||||
>>
|
||||
|
||||
foreach(type,loopid,fromexp,block,blocksemi, blockbraces) ::= <<
|
||||
for (<type> <loopid> : <fromexp>)
|
||||
<block(statements = block, issemi = blocksemi, isbraces = blockbraces)>
|
||||
>>
|
||||
|
||||
try(block,blocksemi, blockbraces, catches, fin) ::= <<
|
||||
try
|
||||
<block(statements = block, issemi = blocksemi, isbraces = blockbraces)>
|
||||
<catches>
|
||||
<fin>
|
||||
>>
|
||||
|
||||
catch_template(type, id, block,blocksemi, blockbraces) ::= <<
|
||||
catch (<type> <id>)
|
||||
<block(statements = block, issemi = blocksemi, isbraces = blockbraces)>
|
||||
>>
|
||||
|
||||
fin(block,blocksemi, blockbraces) ::= <<
|
||||
finally
|
||||
<block(statements = block, issemi = blocksemi, isbraces = blockbraces)>
|
||||
>>
|
||||
|
||||
|
||||
switch(scrutinee, sections) ::= <<
|
||||
switch(<scrutinee>)
|
||||
<sections>
|
||||
>>
|
||||
|
||||
switch_section(labels,statements) ::= <<
|
||||
<labels>
|
||||
<statements; separator="\n">
|
||||
|
||||
>>
|
||||
|
||||
case(what) ::= <<
|
||||
case <what>:
|
||||
>>
|
||||
|
||||
default_template() ::= <<
|
||||
default:
|
||||
>>
|
||||
|
||||
lock(exp,block,blocksemi, blockbraces) ::= <<
|
||||
lock(<exp>)
|
||||
<block(statements = block, issemi = blocksemi, isbraces = blockbraces)>
|
||||
>>
|
||||
|
||||
block(statements,issemi,isbraces) ::= <<
|
||||
<if(issemi)>
|
||||
;
|
||||
@ -152,7 +209,9 @@ block(statements,issemi,isbraces) ::= <<
|
||||
cast_expr(type, exp) ::= "(<type>)<exp>"
|
||||
construct(type, args, inits) ::= "new <type>(<args>)<if(inits)> /* [UNIMPLEMENTED] <inits> */<endif>"
|
||||
application(func, funcparens, args) ::= "<optparens(parens=funcparens,e=func)>(<args>)"
|
||||
index(func, funcparens, args) ::= "<optparens(parens=funcparens,e=func)>[<args>]"
|
||||
stackalloc(type, exp) ::= "stackalloc <type>[<exp>]"
|
||||
typeof(type) ::= "<type>.class"
|
||||
|
||||
// ******* TYPES ***********
|
||||
void() ::= "void"
|
||||
@ -162,7 +221,7 @@ void() ::= "void"
|
||||
optparens(parens, e) ::= "<if(parens)>(<endif><e><if(parens)>)<endif>"
|
||||
parens(e) ::= "(<e>)"
|
||||
rank_specifiers(rs) ::= "<rs>"
|
||||
op(pre,op,post,mkparen,space) ::= "<if(mkparen)>(<endif><pre><space><op><space><post><if(mkparen)>)<endif>"
|
||||
op(pre,op,post,mkparen,space) ::= "<if(mkparen)>(<endif><if(pre)><pre><space><endif><op><if(post)><space><post><endif><if(mkparen)>)<endif>"
|
||||
assign(lhs,lhsparen,assign,rhs,rhsparen) ::= "<if(lhsparen)>(<endif><lhs><if(lhsparen)>)<endif> <assign> <if(rhsparen)>(<endif><rhs><if(rhsparen)>)<endif>"
|
||||
generic_args(args) ::= "\<<args>\>"
|
||||
parameter(annotation,param) ::= "/* <annotation> */ <param>"
|
||||
|
Loading…
x
Reference in New Issue
Block a user