Merged arrays into Charj mainline.
[charm.git] / src / langs / charj / src / charj / translator / Charj.g
index 8bd1ba11c9705a7f5f03ce5bc085907e8e231752..94e671962dcc279bec1f9f866ce21ac51745ff53 100644 (file)
@@ -165,6 +165,7 @@ tokens {
     CLASS_STATIC_INITIALIZER;
     CLASS_TOP_LEVEL_SCOPE;
     CONSTRUCTOR_DECL;
     CLASS_STATIC_INITIALIZER;
     CLASS_TOP_LEVEL_SCOPE;
     CONSTRUCTOR_DECL;
+    DOMAIN_EXPRESSION;
     ENUM_TOP_LEVEL_SCOPE;
     EXPR;
     EXTENDS_BOUND_LIST;
     ENUM_TOP_LEVEL_SCOPE;
     EXPR;
     EXTENDS_BOUND_LIST;
@@ -185,14 +186,17 @@ tokens {
     METHOD_CALL;
     ENTRY_METHOD_CALL;
     MODIFIER_LIST;
     METHOD_CALL;
     ENTRY_METHOD_CALL;
     MODIFIER_LIST;
+    NEW_EXPRESSION;
     PAREN_EXPR;
     POST_DEC;
     POST_INC;
     PRE_DEC;
     PRE_INC;
     QUALIFIED_TYPE_IDENT;
     PAREN_EXPR;
     POST_DEC;
     POST_INC;
     PRE_DEC;
     PRE_INC;
     QUALIFIED_TYPE_IDENT;
+    RANGE_EXPRESSION;
     STATIC_ARRAY_CREATOR;
     SUPER_CONSTRUCTOR_CALL;
     STATIC_ARRAY_CREATOR;
     SUPER_CONSTRUCTOR_CALL;
+    TEMPLATE_INST;
     THIS_CONSTRUCTOR_CALL;
     TYPE;
     SIMPLE_TYPE;
     THIS_CONSTRUCTOR_CALL;
     TYPE;
     SIMPLE_TYPE;
@@ -257,15 +261,24 @@ readonlyDeclaration
 
 typeDeclaration
     :   classDefinition
 
 typeDeclaration
     :   classDefinition
+    |   templateDeclaration
     |   interfaceDefinition
     |   enumDefinition
     |   chareDefinition
     ;
 
     |   interfaceDefinition
     |   enumDefinition
     |   chareDefinition
     ;
 
+templateList
+    : 'class'! IDENT (','! 'class'! IDENT)*
+    ;
+
+templateDeclaration
+    : 'template' '<' templateList '>' classDefinition
+        -> ^('template' templateList classDefinition)
+    ;
+
 classDefinition
     :   PUBLIC? CLASS IDENT (EXTENDS type)? ('implements' typeList)? '{'
 classDefinition
     :   PUBLIC? CLASS IDENT (EXTENDS type)? ('implements' typeList)? '{'
-            classScopeDeclaration*
-        '}' ';'?
+      classScopeDeclaration* '}' ';'?
         -> ^(TYPE CLASS IDENT ^(EXTENDS type)? ^('implements' typeList)? classScopeDeclaration*)
     ;
 
         -> ^(TYPE CLASS IDENT ^(EXTENDS type)? ^('implements' typeList)? classScopeDeclaration*)
     ;
 
@@ -313,9 +326,11 @@ typeList
 classScopeDeclaration
     :   modifierList?
         (   genericTypeParameterList?
 classScopeDeclaration
     :   modifierList?
         (   genericTypeParameterList?
-            (   type IDENT formalParameterList arrayDeclaratorList? (block | ';')
+            (   type IDENT formalParameterList (block | ';')
                 ->  ^(FUNCTION_METHOD_DECL modifierList? genericTypeParameterList? type IDENT
                 ->  ^(FUNCTION_METHOD_DECL modifierList? genericTypeParameterList? type IDENT
-                    formalParameterList arrayDeclaratorList? block?)
+                    formalParameterList block?)
+            /*|   'void' IDENT formalParameterList (block | ';')
+                ->  ^(VOID_METHOD_DECL modifierList? genericTypeParameterList? IDENT formalParameterList block?)*/
             |   ident=IDENT formalParameterList block
                 ->  ^(CONSTRUCTOR_DECL[$ident, "CONSTRUCTOR_DECL"] modifierList? genericTypeParameterList? IDENT
                         formalParameterList block)
             |   ident=IDENT formalParameterList block
                 ->  ^(CONSTRUCTOR_DECL[$ident, "CONSTRUCTOR_DECL"] modifierList? genericTypeParameterList? IDENT
                         formalParameterList block)
@@ -330,9 +345,11 @@ classScopeDeclaration
 interfaceScopeDeclaration
     :   modifierList?
         (   genericTypeParameterList?
 interfaceScopeDeclaration
     :   modifierList?
         (   genericTypeParameterList?
-            (   type IDENT formalParameterList arrayDeclaratorList? ';'
+            (   type IDENT formalParameterList ';'
                 ->  ^(FUNCTION_METHOD_DECL modifierList? genericTypeParameterList?
                 ->  ^(FUNCTION_METHOD_DECL modifierList? genericTypeParameterList?
-                        type IDENT formalParameterList arrayDeclaratorList?)
+                        type IDENT formalParameterList)
+            /*|   'void' IDENT formalParameterList ';'
+                ->  ^(VOID_METHOD_DECL modifierList? genericTypeParameterList? IDENT formalParameterList)*/
             )
         |   simpleType interfaceFieldDeclaratorList ';'
             ->  ^(PRIMITIVE_VAR_DECLARATION modifierList? simpleType interfaceFieldDeclaratorList)
             )
         |   simpleType interfaceFieldDeclaratorList ';'
             ->  ^(PRIMITIVE_VAR_DECLARATION modifierList? simpleType interfaceFieldDeclaratorList)
@@ -363,7 +380,7 @@ interfaceFieldDeclarator
 
 
 variableDeclaratorId
 
 
 variableDeclaratorId
-    :   IDENT^ arrayDeclaratorList?
+    :   ^(IDENT domainExpression?)
     ;
 
 variableInitializer
     ;
 
 variableInitializer
@@ -371,7 +388,7 @@ variableInitializer
     |   expression
     ;
 
     |   expression
     ;
 
-arrayDeclarator
+/*arrayDeclarator
     :   '[' ']'
         ->  ARRAY_DECLARATOR
     ;
     :   '[' ']'
         ->  ARRAY_DECLARATOR
     ;
@@ -379,13 +396,29 @@ arrayDeclarator
 arrayDeclaratorList
     :   arrayDeclarator+
         ->  ^(ARRAY_DECLARATOR_LIST arrayDeclarator+)   
 arrayDeclaratorList
     :   arrayDeclarator+
         ->  ^(ARRAY_DECLARATOR_LIST arrayDeclarator+)   
-    ;
+    ;*/
 
 arrayInitializer
     :   lc='{' (variableInitializer (',' variableInitializer)* ','?)? '}'
         ->  ^(ARRAY_INITIALIZER[$lc, "ARRAY_INITIALIZER"] variableInitializer*)
     ;
 
 
 arrayInitializer
     :   lc='{' (variableInitializer (',' variableInitializer)* ','?)? '}'
         ->  ^(ARRAY_INITIALIZER[$lc, "ARRAY_INITIALIZER"] variableInitializer*)
     ;
 
+templateArg
+    : genericTypeArgument
+    | literal
+    ;
+
+templateArgList
+    :   templateArg (','! templateArg)*
+    ;
+
+templateInstantiation
+    :    '<' templateArgList '>'
+        -> ^(TEMPLATE_INST templateArgList)
+    |    '<' templateInstantiation '>'
+        -> ^(TEMPLATE_INST templateInstantiation)
+    ;
+
 genericTypeParameterList
     :   lt='<' genericTypeParameter (',' genericTypeParameter)* genericTypeListClosing
         ->  ^(GENERIC_TYPE_PARAM_LIST[$lt, "GENERIC_TYPE_PARAM_LIST"] genericTypeParameter+)
 genericTypeParameterList
     :   lt='<' genericTypeParameter (',' genericTypeParameter)* genericTypeListClosing
         ->  ^(GENERIC_TYPE_PARAM_LIST[$lt, "GENERIC_TYPE_PARAM_LIST"] genericTypeParameter+)
@@ -443,23 +476,23 @@ type
     ;
 
 constructorType
     ;
 
 constructorType
-    :   qualifiedTypeIdent AT arrayDeclaratorList?
-        ->  ^(PROXY_TYPE qualifiedTypeIdent arrayDeclaratorList?)
-    |   qualifiedTypeIdent arrayDeclaratorList?
-        ->  ^(OBJECT_TYPE qualifiedTypeIdent arrayDeclaratorList?)
+    :   qualifiedTypeIdent AT domainExpression?
+        ->  ^(PROXY_TYPE qualifiedTypeIdent domainExpression?)
+    |   qualifiedTypeIdent domainExpression?
+        ->  ^(OBJECT_TYPE qualifiedTypeIdent domainExpression?)
     ;
 
 
 simpleType
     ;
 
 
 simpleType
-    :   primitiveType arrayDeclaratorList?
-        ->  ^(SIMPLE_TYPE primitiveType arrayDeclaratorList?)  
+    :   primitiveType domainExpression?
+        ->  ^(SIMPLE_TYPE primitiveType domainExpression?)  
     ;
 
 objectType
     ;
 
 objectType
-    :   qualifiedTypeIdent AT arrayDeclaratorList?
-        ->  ^(PROXY_TYPE qualifiedTypeIdent arrayDeclaratorList?)
-    |   qualifiedTypeIdent arrayDeclaratorList?
-        ->  ^(POINTER_TYPE qualifiedTypeIdent arrayDeclaratorList?)
+    :   qualifiedTypeIdent AT domainExpression?
+        ->  ^(PROXY_TYPE qualifiedTypeIdent domainExpression?)
+    |   qualifiedTypeIdent domainExpression?
+        ->  ^(POINTER_TYPE qualifiedTypeIdent domainExpression?)
     ;
 
 qualifiedTypeIdent
     ;
 
 qualifiedTypeIdent
@@ -468,7 +501,7 @@ qualifiedTypeIdent
     ;
 
 typeIdent
     ;
 
 typeIdent
-    :   IDENT^ genericTypeArgumentList?
+    :   IDENT^ templateInstantiation?
     ;
 
 primitiveType
     ;
 
 primitiveType
@@ -482,10 +515,10 @@ primitiveType
     |   DOUBLE
     ;
 
     |   DOUBLE
     ;
 
-genericTypeArgumentList
+/*genericTypeArgumentList
     :   lt='<' genericTypeArgument (',' genericTypeArgument)* genericTypeListClosing
         ->  ^(GENERIC_TYPE_ARG_LIST[$lt, "GENERIC_TYPE_ARG_LIST"] genericTypeArgument+)
     :   lt='<' genericTypeArgument (',' genericTypeArgument)* genericTypeListClosing
         ->  ^(GENERIC_TYPE_ARG_LIST[$lt, "GENERIC_TYPE_ARG_LIST"] genericTypeArgument+)
-    ;
+    ;*/
 
 genericTypeArgument
     :   type
 
 genericTypeArgument
     :   type
@@ -623,6 +656,29 @@ parenthesizedExpression
         ->  ^(PAREN_EXPR[$lp, "PAREN_EXPR"] expression)
     ;
     
         ->  ^(PAREN_EXPR[$lp, "PAREN_EXPR"] expression)
     ;
     
+rangeItem
+    :   DECIMAL_LITERAL
+    |   IDENT
+    ;
+
+rangeExpression
+    :   rangeItem
+        -> ^(RANGE_EXPRESSION rangeItem)
+    |   rangeItem ':' rangeItem
+        -> ^(RANGE_EXPRESSION rangeItem rangeItem)
+    |   rangeItem ':' rangeItem ':' rangeItem
+        -> ^(RANGE_EXPRESSION rangeItem rangeItem rangeItem)
+    ;
+
+rangeList
+    :   rangeExpression (','! rangeExpression)*
+    ;
+
+domainExpression
+    :   '[' rangeList ']'
+        -> ^(DOMAIN_EXPRESSION rangeList)
+    ;
+
 expressionList
     :   expression (','! expression)*
     ;
 expressionList
     :   expression (','! expression)*
     ;
@@ -754,17 +810,16 @@ postfixedExpression
     :   (   primaryExpression
             ->  primaryExpression
         )
     :   (   primaryExpression
             ->  primaryExpression
         )
-        // ... and than the optional things that may follow a primary
-        // expression 0 or more times.
+        // ... and than the optional things that may follow a primary expression 0 or more times.
         (   outerDot=DOT                 
             // Note: generic type arguments are only valid for method calls,
             // i.e. if there is an argument list
         (   outerDot=DOT                 
             // Note: generic type arguments are only valid for method calls,
             // i.e. if there is an argument list
-            (   (   genericTypeArgumentList?  
+            (   (   templateInstantiation?  
                     IDENT
                     ->  ^($outerDot $postfixedExpression IDENT)
                 ) 
                 (   arguments
                     IDENT
                     ->  ^($outerDot $postfixedExpression IDENT)
                 ) 
                 (   arguments
-                    ->  ^(METHOD_CALL $postfixedExpression genericTypeArgumentList? arguments)
+                    ->  ^(METHOD_CALL $postfixedExpression templateInstantiation? arguments)
                 )?
             |   THIS
                 ->  ^($outerDot $postfixedExpression THIS)
                 )?
             |   THIS
                 ->  ^($outerDot $postfixedExpression THIS)
@@ -777,8 +832,8 @@ postfixedExpression
                     ->  ^(METHOD_CALL $postfixedExpression arguments)
                 )?
             )
                     ->  ^(METHOD_CALL $postfixedExpression arguments)
                 )?
             )
-        |   (AT genericTypeArgumentList? IDENT arguments)
-            ->  ^(ENTRY_METHOD_CALL ^(AT $postfixedExpression IDENT) genericTypeArgumentList? arguments)
+        |   (AT templateInstantiation? IDENT arguments)
+            ->  ^(ENTRY_METHOD_CALL ^(AT $postfixedExpression IDENT) templateInstantiation? arguments)
         |   '[' expression ']'
             ->  ^(ARRAY_ELEMENT_ACCESS $postfixedExpression expression)
         )*
         |   '[' expression ']'
             ->  ^(ARRAY_ELEMENT_ACCESS $postfixedExpression expression)
         )*
@@ -793,17 +848,17 @@ primaryExpression
     |   literal
     |   newExpression
     |   qualifiedIdentExpression
     |   literal
     |   newExpression
     |   qualifiedIdentExpression
-    |   genericTypeArgumentList 
+    |   templateInstantiation
         (   s=SUPER
             (   arguments
         (   s=SUPER
             (   arguments
-                ->  ^(SUPER_CONSTRUCTOR_CALL[$s, "SUPER_CONSTRUCTOR_CALL"] genericTypeArgumentList arguments)
+                ->  ^(SUPER_CONSTRUCTOR_CALL[$s, "SUPER_CONSTRUCTOR_CALL"] templateInstantiation arguments)
             |   IDENT arguments
             |   IDENT arguments
-                ->  ^(METHOD_CALL ^(DOT SUPER IDENT) genericTypeArgumentList arguments)
+                ->  ^(METHOD_CALL ^(DOT SUPER IDENT) templateInstantiation arguments)
             )
         |   IDENT arguments
             )
         |   IDENT arguments
-            ->  ^(METHOD_CALL IDENT genericTypeArgumentList arguments)
+            ->  ^(METHOD_CALL IDENT templateInstantiation arguments)
         |   t=THIS arguments
         |   t=THIS arguments
-            ->  ^(THIS_CONSTRUCTOR_CALL[$t, "THIS_CONSTRUCTOR_CALL"] genericTypeArgumentList arguments)
+            ->  ^(THIS_CONSTRUCTOR_CALL[$t, "THIS_CONSTRUCTOR_CALL"] templateInstantiation arguments)
         )
     |   (   THIS
             ->  THIS
         )
     |   (   THIS
             ->  THIS
@@ -840,15 +895,15 @@ qualifiedIdentExpression
         (   arguments
             ->  ^(METHOD_CALL qualifiedIdentifier arguments)
         |   outerDot=DOT
         (   arguments
             ->  ^(METHOD_CALL qualifiedIdentifier arguments)
         |   outerDot=DOT
-            (   genericTypeArgumentList 
+            (   templateInstantiation
                 (   s=SUPER arguments
                     ->  ^(SUPER_CONSTRUCTOR_CALL[$s, "SUPER_CONSTRUCTOR_CALL"]
                 (   s=SUPER arguments
                     ->  ^(SUPER_CONSTRUCTOR_CALL[$s, "SUPER_CONSTRUCTOR_CALL"]
-                            qualifiedIdentifier genericTypeArgumentList arguments)
+                            qualifiedIdentifier templateInstantiation arguments)
                 |   SUPER innerDot=DOT IDENT arguments
                     ->  ^(METHOD_CALL ^($innerDot ^($outerDot qualifiedIdentifier SUPER) IDENT)
                 |   SUPER innerDot=DOT IDENT arguments
                     ->  ^(METHOD_CALL ^($innerDot ^($outerDot qualifiedIdentifier SUPER) IDENT)
-                            genericTypeArgumentList arguments)
+                            templateInstantiation arguments)
                 |   IDENT arguments
                 |   IDENT arguments
-                    ->  ^(METHOD_CALL ^($outerDot qualifiedIdentifier IDENT) genericTypeArgumentList arguments)
+                    ->  ^(METHOD_CALL ^($outerDot qualifiedIdentifier IDENT) templateInstantiation arguments)
                 )
             |   THIS
                 ->  ^($outerDot qualifiedIdentifier THIS)
                 )
             |   THIS
                 ->  ^($outerDot qualifiedIdentifier THIS)
@@ -860,20 +915,18 @@ qualifiedIdentExpression
 
 newExpression
     :   n=NEW
 
 newExpression
     :   n=NEW
-        (   primitiveType newArrayConstruction          // new static array of primitive type elements
-            ->  ^(STATIC_ARRAY_CREATOR[$n, "STATIC_ARRAY_CREATOR"] primitiveType newArrayConstruction)
-        |   genericTypeArgumentList? qualifiedTypeIdent
-                newArrayConstruction                // new static array of object type reference elements
-            ->  ^(STATIC_ARRAY_CREATOR[$n, "STATIC_ARRAY_CREATOR"] genericTypeArgumentList? qualifiedTypeIdent newArrayConstruction)
+        (
+            domainExpression arguments?
+            ->  ^(NEW_EXPRESSION arguments? domainExpression)
         |   constructorType arguments
             -> ^(NEW constructorType arguments)
         )
     ;
     
         |   constructorType arguments
             -> ^(NEW constructorType arguments)
         )
     ;
     
-newArrayConstruction
+/*newArrayConstruction
     :   arrayDeclaratorList arrayInitializer
     |   '['! expression ']'! ('['! expression ']'!)* arrayDeclaratorList?
     :   arrayDeclaratorList arrayInitializer
     |   '['! expression ']'! ('['! expression ']'!)* arrayDeclaratorList?
-    ;
+    ;*/
 
 arguments
     :   lp='(' expressionList? ')'
 
 arguments
     :   lp='(' expressionList? ')'