102ba142ccd0e763fba8523c975c25ba6e5b22dc
[charm.git] / src / langs / charj / src / charj / translator / AtomicBlocks.g
1
2 tree grammar AtomicBlocks;
3
4 options {
5     tokenVocab = Charj;
6     ASTLabelType = CharjAST;
7     filter = true;
8     output = AST;
9 }
10
11 @header {
12 package charj.translator;
13 import java.util.Iterator;
14 }
15
16 @members {
17     SymbolTable symtab;
18
19     public AtomicBlocks(TreeNodeStream input, SymbolTable symtab) {
20         this(input);
21         this.symtab = symtab;
22     }
23 }
24
25
26 topdown
27     :   block
28     ;
29
30
31 parenthesizedExpression
32     :   ^(PAREN_EXPR exp=expression)
33     ;
34
35 expression 
36     :   ^(EXPR .*)
37     ;
38
39 assignment
40     :   ^(ASSIGNMENT IDENT expression)
41     ;
42
43
44 type
45     :   VOID 
46     |   ^(SIMPLE_TYPE .*)
47     |   ^(OBJECT_TYPE .*)
48     |   ^(REFERENCE_TYPE .*)
49     |   ^(PROXY_TYPE .*)
50     |   ^(POINTER_TYPE .*)
51     ;
52
53 classType
54     :   CLASS
55     |   CHARE
56     |   GROUP
57     |   NODEGROUP
58     |   MAINCHARE
59     |   ^(CHARE_ARRAY ARRAY_DIMENSION)
60     ;
61
62
63 block returns [boolean containsSDAG]
64 @init { boolean hasSDAGstatement = false; }
65     :   ^(BLOCK (sdagNonLeader |
66             sdagLeader {hasSDAGstatement = $sdagLeader.containsSDAG; } |
67             b=block {hasSDAGstatement |= $b.containsSDAG;})*) {
68             $containsSDAG = hasSDAGstatement;
69             ((LocalScope)$BLOCK.def).hasSDAG = $containsSDAG;
70         }
71     ;
72     
73 statement
74     :   sdagNonLeader
75     |   sdagLeader
76     |   block
77     ;
78
79 sdagLeader returns [boolean containsSDAG]
80 @init { $containsSDAG = false; }
81     :   ^(OVERLAP block) {$containsSDAG = true;}
82     |   ^(WHEN (IDENT expression? ^(FORMAL_PARAM_LIST .*))* block) {$containsSDAG = true;}
83     |   ^(IF parenthesizedExpression ifblock=block elseblock=block?
84             {$ifblock.containsSDAG || $elseblock.containsSDAG}?)
85         {
86             $containsSDAG = true;
87         }
88         -> ^(SDAG_IF parenthesizedExpression $ifblock $elseblock)
89     |   ^(FOR forInit? FOR_EXPR expression? FOR_UPDATE expression* block {$block.containsSDAG}?) {
90             $containsSDAG = true;
91         }
92         -> ^(SDAG_FOR forInit? FOR_EXPR expression? FOR_UPDATE expression* block)
93     |   ^(WHILE parenthesizedExpression block {$block.containsSDAG}?) {
94             $containsSDAG = true;
95         }
96         -> ^(SDAG_WHILE parenthesizedExpression block)
97     |   ^(DO block parenthesizedExpression {$block.containsSDAG}?) {
98             $containsSDAG = true;
99         }
100         -> ^(SDAG_DO block parenthesizedExpression)
101     ;
102
103 sdagNonLeader
104     :   ^(PRIMITIVE_VAR_DECLARATION .*)
105     |   ^(OBJECT_VAR_DECLARATION .*)
106     |   ^(ASSERT expression expression?)
107     |   ^(SWITCH parenthesizedExpression switchCaseLabel*)
108     |   ^(RETURN expression?)
109     |   ^(THROW expression)
110     |   ^(BREAK IDENT?)
111     |   ^(CONTINUE IDENT?)
112     |   ^(LABELED_STATEMENT IDENT statement)
113     |   expression
114     |   ^('delete' expression)
115     |   ^(EMBED STRING_LITERAL EMBED_BLOCK)
116     |   ';' // Empty statement.
117     |   ^(PRINT expression*)
118     |   ^(PRINTLN expression*)
119     |   ^(EXIT expression?)
120     |   EXITALL
121     ;
122         
123 switchCaseLabel
124     :   ^(CASE expression statement*)
125     |   ^(DEFAULT statement*)
126     ;
127     
128 forInit
129     :   ^(PRIMITIVE_VAR_DECLARATION .*)
130     |   ^(OBJECT_VAR_DECLARATION .*)
131     |   expression+
132     ;
133