Adding handling for primitives, non-primitives, and user-defined types; doesn't handl...
[iot2.git] / others / javacup / iotparser.cup
1 /* Minijava Grammar */
2 import java_cup.runtime.ComplexSymbolFactory;
3 import java_cup.runtime.ScannerBuffer;
4 import java_cup.runtime.XMLElement;
5 import javax.xml.stream.XMLOutputFactory;
6 import javax.xml.stream.XMLStreamWriter;
7 import java.io.*;
8
9 import javax.xml.transform.*;
10 import javax.xml.transform.stream.*;
11
12 parser code {:
13   public Parser(Lexer lex, ComplexSymbolFactory sf) {
14     super(lex,sf);
15   }
16   public static void main(String[] args) throws Exception {
17       // initialize the symbol factory
18       ComplexSymbolFactory csf = new ComplexSymbolFactory();
19       // create a buffering scanner wrapper
20       ScannerBuffer lexer = new ScannerBuffer(new Lexer(new BufferedReader(new FileReader(args[0])),csf));
21       // start parsing
22       Parser p = new Parser(lexer,csf);
23       ParseNode pn = (ParseNode) p.parse().value;
24   }
25 :};
26
27 terminal SEMICOLON, COMMA, LPAR, RPAR, LANG, RANG, BEGIN, END, ASSIGN;
28 terminal PUBLIC, INTERFACE, CAPABILITY, DESCRIPTION, METHOD, REQUIRES, WITH, AS; 
29 terminal TYPE;
30 terminal IDENT, STRINGCONST;
31
32 non terminal ParseNode policy;
33 non terminal ParseNode intface, methlist, meth, paramlist, param;
34 non terminal ParseNode capablist, capab, capabcont, cont;
35 non terminal ParseNode reqlist, require, capintlist;
36
37 /**
38  * A policy file normally consists of:
39  * 1) Interface 
40  *    - Interface definition
41  *    - List of capabilities and their contents
42  *
43  * We also define "requires" statements for users
44  * to declare their required capabilities in the
45  * generated interfaces
46  * 2) List of generated interfaces (requires)
47  */
48 policy     ::= intface:in
49         {:
50                 ParseNode pn = new ParseNode("policy");
51                 pn.addChild(in);
52                 RESULT = pn;
53         :}
54         | reqlist:rl
55         {:
56                 ParseNode pn = new ParseNode("policy");
57                 pn.addChild(rl);
58                 RESULT = pn;
59         :}
60     ;
61
62 //1) Interface class definition
63 // 1) Interface definition
64 // 2) Library list
65 // 3) Driver list
66
67 // Interface
68 intface    ::= PUBLIC INTERFACE IDENT:idint BEGIN methlist:ml capablist:cl END
69         {:
70                 ParseNode pn = new ParseNode("interface");
71                 pn.addChild("intface_ident").setLiteral(idint);
72                 pn.addChild(ml);
73                 pn.addChild(cl);
74                 RESULT = pn;
75         :}
76     ;
77 methlist   ::= methlist:ml meth:m
78         {:
79                 ml.addChild(m);
80                 RESULT = ml;
81         :}
82     | /* empty */
83         {:
84                 ParseNode pn = new ParseNode("method_list");
85                 RESULT = pn;
86         :}
87     ;
88 meth       ::= PUBLIC TYPE:typemeth IDENT:idmeth LPAR paramlist:pl RPAR SEMICOLON
89         {:
90                 ParseNode pn = new ParseNode("method");
91                 pn.addChild("method_type").setLiteral(typemeth);
92                 pn.addChild("method_ident").setLiteral(idmeth);
93                 pn.addChild(pl);
94                 RESULT = pn;
95         :}
96     | PUBLIC IDENT:clsmeth IDENT:idmeth LPAR paramlist:pl RPAR SEMICOLON
97         {:
98                 ParseNode pn = new ParseNode("method");
99                 pn.addChild("method_class").setLiteral(clsmeth);
100                 pn.addChild("method_ident").setLiteral(idmeth);
101                 pn.addChild(pl);
102                 RESULT = pn;
103         :}
104     ;
105 paramlist  ::= paramlist:pl param:p
106         {:
107                 pl.addChild(p);
108                 RESULT = pl;
109         :}
110     | /* empty */
111         {:
112                 ParseNode pn = new ParseNode("param_list");
113                 RESULT = pn;
114         :}
115     ;
116 param      ::= TYPE:typeprm IDENT:idprm COMMA
117         {:
118                 ParseNode pn = new ParseNode("param");
119                 pn.addChild("param_type").setLiteral(typeprm);
120                 pn.addChild("param_ident").setLiteral(idprm);
121                 RESULT = pn;
122         :}
123     | TYPE:typeprm IDENT:idprm
124         {:
125                 ParseNode pn = new ParseNode("param");
126                 pn.addChild("param_type").setLiteral(typeprm);
127                 pn.addChild("param_ident").setLiteral(idprm);
128                 RESULT = pn;
129         :}
130     | IDENT:clsprm IDENT:idprm COMMA
131         {:
132                 ParseNode pn = new ParseNode("param");
133                 pn.addChild("param_class").setLiteral(clsprm);
134                 pn.addChild("param_ident").setLiteral(idprm);
135                 RESULT = pn;
136         :}
137     | IDENT:clsprm IDENT:idprm
138         {:
139                 ParseNode pn = new ParseNode("param");
140                 pn.addChild("param_class").setLiteral(clsprm);
141                 pn.addChild("param_ident").setLiteral(idprm);
142                 RESULT = pn;
143         :}
144         /* generic/template with one type, e.g. set<int> */
145     | IDENT:clsprm LANG TYPE:typegen RANG IDENT:idprm
146         {:
147                 ParseNode pn = new ParseNode("param");
148                 pn.addChild("param_class").setLiteral((String)clsprm + "<" + typegen + ">");
149                 pn.addChild("param_ident").setLiteral(idprm);
150                 RESULT = pn;
151         :}
152     | IDENT:clsprm LANG IDENT:clsgen RANG IDENT:idprm
153         {:
154                 ParseNode pn = new ParseNode("param");
155                 pn.addChild("param_class").setLiteral((String)clsprm + "<" + clsgen + ">");
156                 pn.addChild("param_ident").setLiteral(idprm);
157                 RESULT = pn;
158         :}
159         /* generic/template with two types, e.g. map<int,string> */
160     | IDENT:clsprm LANG TYPE:typegen1 COMMA TYPE:typegen2 RANG IDENT:idprm
161         {:
162                 ParseNode pn = new ParseNode("param");
163                 pn.addChild("param_class").setLiteral((String)clsprm 
164                         + "<" + typegen1 + "," + typegen2 + ">");
165                 pn.addChild("param_ident").setLiteral(idprm);
166                 RESULT = pn;
167         :}
168     | IDENT:clsprm LANG TYPE:typegen COMMA IDENT:clsgen RANG IDENT:idprm
169         {:
170                 ParseNode pn = new ParseNode("param");
171                 pn.addChild("param_class").setLiteral((String)clsprm 
172                         + "<" + typegen + "," + clsgen + ">");
173                 pn.addChild("param_ident").setLiteral(idprm);
174                 RESULT = pn;
175         :}
176     | IDENT:clsprm LANG IDENT:clsgen COMMA TYPE:typegen RANG IDENT:idprm
177         {:
178                 ParseNode pn = new ParseNode("param");
179                 pn.addChild("param_class").setLiteral((String)clsprm 
180                         + "<" + clsgen + "," + typegen + ">");
181                 pn.addChild("param_ident").setLiteral(idprm);
182                 RESULT = pn;
183         :}
184     | IDENT:clsprm LANG IDENT:clsgen1 COMMA IDENT:clsgen2 RANG IDENT:idprm
185         {:
186                 ParseNode pn = new ParseNode("param");
187                 pn.addChild("param_class").setLiteral((String)clsprm 
188                         + "<" + clsgen1 + "," + clsgen2 + ">");
189                 pn.addChild("param_ident").setLiteral(idprm);
190                 RESULT = pn;
191         :}
192         /* Add comma at the end... */
193         /* generic/template with one type, e.g. set<int> */
194     | IDENT:clsprm LANG TYPE:typegen RANG IDENT:idprm COMMA
195         {:
196                 ParseNode pn = new ParseNode("param");
197                 pn.addChild("param_class").setLiteral((String)clsprm + "<" + typegen + ">");
198                 pn.addChild("param_ident").setLiteral(idprm);
199                 RESULT = pn;
200         :}
201     | IDENT:clsprm LANG IDENT:clsgen RANG IDENT:idprm COMMA
202         {:
203                 ParseNode pn = new ParseNode("param");
204                 pn.addChild("param_class").setLiteral((String)clsprm + "<" + clsgen + ">");
205                 pn.addChild("param_ident").setLiteral(idprm);
206                 RESULT = pn;
207         :}
208         /* generic/template with two types, e.g. map<int,string> */
209     | IDENT:clsprm LANG TYPE:typegen1 COMMA TYPE:typegen2 RANG IDENT:idprm COMMA
210         {:
211                 ParseNode pn = new ParseNode("param");
212                 pn.addChild("param_class").setLiteral((String)clsprm 
213                         + "<" + typegen1 + "," + typegen2 + ">");
214                 pn.addChild("param_ident").setLiteral(idprm);
215                 RESULT = pn;
216         :}
217     | IDENT:clsprm LANG TYPE:typegen COMMA IDENT:clsgen RANG IDENT:idprm COMMA
218         {:
219                 ParseNode pn = new ParseNode("param");
220                 pn.addChild("param_class").setLiteral((String)clsprm 
221                         + "<" + typegen + "," + clsgen + ">");
222                 pn.addChild("param_ident").setLiteral(idprm);
223                 RESULT = pn;
224         :}
225     | IDENT:clsprm LANG IDENT:clsgen COMMA TYPE:typegen RANG IDENT:idprm COMMA
226         {:
227                 ParseNode pn = new ParseNode("param");
228                 pn.addChild("param_class").setLiteral((String)clsprm 
229                         + "<" + clsgen + "," + typegen + ">");
230                 pn.addChild("param_ident").setLiteral(idprm);
231                 RESULT = pn;
232         :}
233     | IDENT:clsprm LANG IDENT:clsgen1 COMMA IDENT:clsgen2 RANG IDENT:idprm COMMA
234         {:
235                 ParseNode pn = new ParseNode("param");
236                 pn.addChild("param_class").setLiteral((String)clsprm 
237                         + "<" + clsgen1 + "," + clsgen2 + ">");
238                 pn.addChild("param_ident").setLiteral(idprm);
239                 RESULT = pn;
240         :}
241     ;
242
243 //2) List of capabilities and their respective contents, i.e. description, method, etc.
244 capablist  ::= capablist:cl capab:cap
245         {:
246                 cl.addChild(cap);
247                 RESULT = cl;
248         :}
249         | /* empty */
250         {:
251                 ParseNode pn = new ParseNode("capab_list");
252                 RESULT = pn;
253         :}
254         ;
255 capab      ::= CAPABILITY IDENT:idcap BEGIN capabcont:ccont END
256         {:
257                 ParseNode pn = new ParseNode("capability");
258                 pn.addChild("capab_ident").setLiteral(idcap);
259                 pn.addChild(ccont);
260                 RESULT = pn;
261         :}
262     ;
263 capabcont  ::= capabcont:ccont cont:cnt
264         {:
265                 ccont.addChild(cnt);
266                 RESULT = ccont;
267         :}
268         | /* empty */
269         {:
270                 ParseNode pn = new ParseNode("capab_content");
271                 RESULT = pn;
272         :}
273         ;
274 cont       ::= DESCRIPTION:dsc ASSIGN STRINGCONST:strdsc SEMICOLON
275         {:
276                 ParseNode pn = new ParseNode("capab_content");
277                 pn.addChild("capab_desc").setLiteral(strdsc);
278                 RESULT = pn;
279         :}
280         | METHOD:mtd ASSIGN STRINGCONST:strmeth SEMICOLON
281         {:
282                 ParseNode pn = new ParseNode("capab_content");
283                 pn.addChild("capab_meth").setLiteral(strmeth);
284                 RESULT = pn;
285         :}
286         ;
287
288 //3) List of interface generation definitions ("requires" statements)
289 reqlist    ::= reqlist:rl require:req
290         {:
291                 rl.addChild(req);
292                 RESULT = rl;
293         :}
294         | /* empty */
295         {:
296                 ParseNode pn = new ParseNode("reqlist");
297                 RESULT = pn;
298         :}
299         ;
300 require    ::= REQUIRES IDENT:idint WITH capintlist:cil AS INTERFACE IDENT:idnewint SEMICOLON
301         {:
302                 ParseNode pn = new ParseNode("requires");
303                 pn.addChild("intface_ident").setLiteral(idint);
304                 pn.addChild(cil);
305                 pn.addChild("new_intface_ident").setLiteral(idnewint);
306                 RESULT = pn;
307         :}
308         ;
309 capintlist ::= IDENT:idcap
310         {:
311                 ParseNode pn = new ParseNode("capab_ident_list");
312                 pn.addChild("capab_ident").setLiteral(idcap);
313                 RESULT = pn;
314         :}
315         | capintlist:cil COMMA IDENT:idcap
316         {:
317                 cil.addChild("capab_ident").setLiteral(idcap);
318                 RESULT = cil;
319         :}
320         | /* empty */
321         {:
322                 ParseNode pn = new ParseNode("capab_ident_list");
323                 RESULT = pn;
324         :}
325         ;
326