Extending parser for struct/enum (policy) definition files; Removing parser support...
[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, ENUM, STRUCT; 
29 terminal TYPE;
30 terminal IDENT, STRINGCONST;
31
32 non terminal ParseNode policy;
33 non terminal ParseNode intface, methlist, meth, paramlist, param, paramtype;
34 non terminal ParseNode capablist, capab, capabcont, cont;
35 non terminal ParseNode reqlist, require, capintlist;
36 non terminal ParseNode enumdec, enumlist, enummem;
37 non terminal ParseNode structdec, structlist, structmem;
38
39
40 /**
41  * A policy file normally consists of:
42  * 1) Interface 
43  *    - Interface definition
44  *    - List of capabilities and their contents
45  *
46  * We also define "requires" statements for users
47  * to declare their required capabilities in the
48  * generated interfaces
49  * 2) List of generated interfaces (requires)
50  *
51  * Additionally we support declarations for these
52  * data types:
53  * 3) Enumeration
54  * 4) Struct
55  */
56 policy     ::= intface:in
57         {:
58                 ParseNode pn = new ParseNode("policy");
59                 pn.addChild(in);
60                 RESULT = pn;
61         :}
62         | reqlist:rl
63         {:
64                 ParseNode pn = new ParseNode("policy");
65                 pn.addChild(rl);
66                 RESULT = pn;
67         :}
68         | enumdec:en
69         {:
70                 ParseNode pn = new ParseNode("policy");
71                 pn.addChild(en);
72                 RESULT = pn;
73         :}
74         | structdec:st
75         {:
76                 ParseNode pn = new ParseNode("policy");
77                 pn.addChild(st);
78                 RESULT = pn;
79         :}
80     ;
81
82 //1) Interface class definition
83 // 1) Interface definition
84 // 2) Library list
85 // 3) Driver list
86
87 // Interface
88 intface    ::= PUBLIC INTERFACE IDENT:idint BEGIN methlist:ml capablist:cl END
89         {:
90                 ParseNode pn = new ParseNode("interface");
91                 pn.addChild("intface_ident").setLiteral(idint);
92                 pn.addChild(ml);
93                 pn.addChild(cl);
94                 RESULT = pn;
95         :}
96     ;
97 methlist   ::= methlist:ml meth:m
98         {:
99                 ml.addChild(m);
100                 RESULT = ml;
101         :}
102     | /* empty */
103         {:
104                 ParseNode pn = new ParseNode("method_list");
105                 RESULT = pn;
106         :}
107     ;
108 meth       ::= PUBLIC TYPE:typemeth IDENT:idmeth LPAR paramlist:pl RPAR SEMICOLON
109         {:
110                 ParseNode pn = new ParseNode("method");
111                 pn.addChild("method_type").setLiteral(typemeth);
112                 pn.addChild("method_ident").setLiteral(idmeth);
113                 pn.addChild(pl);
114                 RESULT = pn;
115         :}
116     | PUBLIC IDENT:clsmeth IDENT:idmeth LPAR paramlist:pl RPAR SEMICOLON
117         {:
118                 ParseNode pn = new ParseNode("method");
119                 pn.addChild("method_class").setLiteral(clsmeth);
120                 pn.addChild("method_ident").setLiteral(idmeth);
121                 pn.addChild(pl);
122                 RESULT = pn;
123         :}
124     ;
125 paramlist  ::= paramlist:pl param:p
126         {:
127                 pl.addChild(p);
128                 RESULT = pl;
129         :}
130     | /* empty */
131         {:
132                 ParseNode pn = new ParseNode("param_list");
133                 RESULT = pn;
134         :}
135     ;
136 param      ::= TYPE:typeprm IDENT:idprm COMMA
137         {:
138                 ParseNode pn = new ParseNode("param");
139                 pn.addChild("param_type").setLiteral(typeprm);
140                 pn.addChild("param_ident").setLiteral(idprm);
141                 RESULT = pn;
142         :}
143     | TYPE:typeprm IDENT:idprm
144         {:
145                 ParseNode pn = new ParseNode("param");
146                 pn.addChild("param_type").setLiteral(typeprm);
147                 pn.addChild("param_ident").setLiteral(idprm);
148                 RESULT = pn;
149         :}
150     | IDENT:clsprm IDENT:idprm COMMA
151         {:
152                 ParseNode pn = new ParseNode("param");
153                 pn.addChild("param_class").setLiteral(clsprm);
154                 pn.addChild("param_ident").setLiteral(idprm);
155                 RESULT = pn;
156         :}
157     | IDENT:clsprm IDENT:idprm
158         {:
159                 ParseNode pn = new ParseNode("param");
160                 pn.addChild("param_class").setLiteral(clsprm);
161                 pn.addChild("param_ident").setLiteral(idprm);
162                 RESULT = pn;
163         :}
164         /* generic/template with one type, e.g. list<int> */
165     | IDENT:clsprm LANG TYPE:typegen RANG IDENT:idprm
166         {:
167                 ParseNode pn = new ParseNode("param");
168                 pn.addChild("param_class").setLiteral((String)clsprm + "<" + typegen + ">");
169                 pn.addChild("param_ident").setLiteral(idprm);
170                 RESULT = pn;
171         :}
172     | IDENT:clsprm LANG IDENT:clsgen RANG IDENT:idprm
173         {:
174                 ParseNode pn = new ParseNode("param");
175                 pn.addChild("param_class").setLiteral((String)clsprm + "<" + clsgen + ">");
176                 pn.addChild("param_ident").setLiteral(idprm);
177                 RESULT = pn;
178         :}
179         /* Add comma at the end... */
180         /* generic/template with one type, e.g. list<int> */
181     | IDENT:clsprm LANG TYPE:typegen RANG IDENT:idprm COMMA
182         {:
183                 ParseNode pn = new ParseNode("param");
184                 pn.addChild("param_class").setLiteral((String)clsprm + "<" + typegen + ">");
185                 pn.addChild("param_ident").setLiteral(idprm);
186                 RESULT = pn;
187         :}
188     | IDENT:clsprm LANG IDENT:clsgen RANG IDENT:idprm COMMA
189         {:
190                 ParseNode pn = new ParseNode("param");
191                 pn.addChild("param_class").setLiteral((String)clsprm + "<" + clsgen + ">");
192                 pn.addChild("param_ident").setLiteral(idprm);
193                 RESULT = pn;
194         :}
195     ;
196
197 //2) List of capabilities and their respective contents, i.e. description, method, etc.
198 capablist  ::= capablist:cl capab:cap
199         {:
200                 cl.addChild(cap);
201                 RESULT = cl;
202         :}
203         | /* empty */
204         {:
205                 ParseNode pn = new ParseNode("capab_list");
206                 RESULT = pn;
207         :}
208         ;
209 capab      ::= CAPABILITY IDENT:idcap BEGIN capabcont:ccont END
210         {:
211                 ParseNode pn = new ParseNode("capability");
212                 pn.addChild("capab_ident").setLiteral(idcap);
213                 pn.addChild(ccont);
214                 RESULT = pn;
215         :}
216     ;
217 capabcont  ::= capabcont:ccont cont:cnt
218         {:
219                 ccont.addChild(cnt);
220                 RESULT = ccont;
221         :}
222         | /* empty */
223         {:
224                 ParseNode pn = new ParseNode("capab_content");
225                 RESULT = pn;
226         :}
227         ;
228 cont       ::= DESCRIPTION:dsc ASSIGN STRINGCONST:strdsc SEMICOLON
229         {:
230                 ParseNode pn = new ParseNode("capab_content");
231                 pn.addChild("capab_desc").setLiteral(strdsc);
232                 RESULT = pn;
233         :}
234         | METHOD:mtd ASSIGN STRINGCONST:strmeth SEMICOLON
235         {:
236                 ParseNode pn = new ParseNode("capab_content");
237                 pn.addChild("capab_meth").setLiteral(strmeth);
238                 RESULT = pn;
239         :}
240         ;
241
242 //3) List of interface generation definitions ("requires" statements)
243 reqlist    ::= reqlist:rl require:req
244         {:
245                 rl.addChild(req);
246                 RESULT = rl;
247         :}
248         | /* empty */
249         {:
250                 ParseNode pn = new ParseNode("reqlist");
251                 RESULT = pn;
252         :}
253         ;
254 require    ::= REQUIRES IDENT:idint WITH capintlist:cil AS INTERFACE IDENT:idnewint SEMICOLON
255         {:
256                 ParseNode pn = new ParseNode("requires");
257                 pn.addChild("intface_ident").setLiteral(idint);
258                 pn.addChild(cil);
259                 pn.addChild("new_intface_ident").setLiteral(idnewint);
260                 RESULT = pn;
261         :}
262         ;
263 capintlist ::= IDENT:idcap
264         {:
265                 ParseNode pn = new ParseNode("capab_ident_list");
266                 pn.addChild("capab_ident").setLiteral(idcap);
267                 RESULT = pn;
268         :}
269         | capintlist:cil COMMA IDENT:idcap
270         {:
271                 cil.addChild("capab_ident").setLiteral(idcap);
272                 RESULT = cil;
273         :}
274         | /* empty */
275         {:
276                 ParseNode pn = new ParseNode("capab_ident_list");
277                 RESULT = pn;
278         :}
279         ;
280
281 //4) Enumeration declaration
282 enumdec         ::= ENUM IDENT:idenumdec BEGIN enumlist:el END
283         {:
284                 ParseNode pn = new ParseNode("enum_dec");
285                 pn.addChild("enum_dec_ident").setLiteral(idenumdec);
286                 pn.addChild(el);
287                 RESULT = pn;
288         :}
289     ;
290 enumlist        ::= enumlist:el enummem:e
291         {:
292                 el.addChild(e);
293                 RESULT = el;
294         :}
295     | /* empty */
296         {:
297                 ParseNode pn = new ParseNode("enum_list");
298                 RESULT = pn;
299         :}
300     ;
301 enummem         ::= IDENT:idenum COMMA
302         {:
303                 ParseNode pn = new ParseNode("enum_mem");
304                 pn.addChild("enum_ident").setLiteral(idenum);
305                 RESULT = pn;
306         :}
307     | IDENT:idenum
308         {:
309                 ParseNode pn = new ParseNode("enum_mem");
310                 pn.addChild("enum_ident").setLiteral(idenum);
311                 RESULT = pn;
312         :}
313     ;
314
315 //5) Struct declaration
316 structdec       ::= STRUCT IDENT:idstructdec BEGIN structlist:sl END
317         {:
318                 ParseNode pn = new ParseNode("struct_dec");
319                 pn.addChild("struct_dec_ident").setLiteral(idstructdec);
320                 pn.addChild(sl);
321                 RESULT = pn;
322         :}
323     ;
324 structlist      ::= structlist:sl structmem:s
325         {:
326                 sl.addChild(s);
327                 RESULT = sl;
328         :}
329     | /* empty */
330         {:
331                 ParseNode pn = new ParseNode("enum_list");
332                 RESULT = pn;
333         :}
334     ;
335 structmem       ::= TYPE:typestr IDENT:idstr SEMICOLON
336         {:
337                 ParseNode pn = new ParseNode("struct_mem");
338                 pn.addChild("struct_type").setLiteral(typestr);
339                 pn.addChild("struct_ident").setLiteral(idstr);
340                 RESULT = pn;
341         :}
342     | IDENT:clsstr IDENT:idstr SEMICOLON
343         {:
344                 ParseNode pn = new ParseNode("struct_mem");
345                 pn.addChild("struct_class").setLiteral(clsstr);
346                 pn.addChild("struct_ident").setLiteral(idstr);
347                 RESULT = pn;
348         :}
349     | IDENT:clsstr LANG IDENT:clsgen RANG IDENT:idstr SEMICOLON
350         {:
351                 ParseNode pn = new ParseNode("struct_mem");
352                 pn.addChild("struct_class").setLiteral((String)clsstr + "<" + clsgen + ">");
353                 pn.addChild("struct_ident").setLiteral(idstr);
354                 RESULT = pn;
355         :}
356     ;