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;
9 import javax.xml.transform.*;
10 import javax.xml.transform.stream.*;
13 public Parser(Lexer lex, ComplexSymbolFactory sf) {
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));
22 Parser p = new Parser(lexer,csf);
23 ParseNode pn = (ParseNode) p.parse().value;
27 terminal SEMICOLON, COMMA, LPAR, RPAR, LANG, RANG, BEGIN, END, ASSIGN;
28 terminal PUBLIC, INTERFACE, CAPABILITY, DESCRIPTION, METHOD, REQUIRES, WITH, AS, ENUM, STRUCT;
30 terminal IDENT, STRINGCONST;
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 enumdeclist, enumdec, enumlist, enummem;
37 non terminal ParseNode structdeclist, structdec, structlist, structmem;
41 * A policy file normally consists of:
43 * - Interface definition
44 * - List of capabilities and their contents
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)
51 * Additionally we support declarations for these
58 ParseNode pn = new ParseNode("policy");
64 ParseNode pn = new ParseNode("policy");
70 //1) Interface class definition
71 // 1) Interface definition
76 intface ::= PUBLIC INTERFACE IDENT:idint BEGIN methlist:ml capablist:cl enumdeclist:el structdeclist:sl END
78 ParseNode pn = new ParseNode("interface");
79 pn.addChild("intface_ident").setLiteral(idint);
87 methlist ::= methlist:ml meth:m
94 ParseNode pn = new ParseNode("method_list");
98 meth ::= PUBLIC TYPE:typemeth IDENT:idmeth LPAR paramlist:pl RPAR SEMICOLON
100 ParseNode pn = new ParseNode("method");
101 pn.addChild("method_type").setLiteral(typemeth);
102 pn.addChild("method_ident").setLiteral(idmeth);
106 | PUBLIC IDENT:clsmeth IDENT:idmeth LPAR paramlist:pl RPAR SEMICOLON
108 ParseNode pn = new ParseNode("method");
109 pn.addChild("method_class").setLiteral(clsmeth);
110 pn.addChild("method_ident").setLiteral(idmeth);
114 /* generic/template return value with one type, e.g. set<int> */
115 | PUBLIC IDENT:clsmeth LANG TYPE:typegen RANG IDENT:idmeth LPAR paramlist:pl RPAR SEMICOLON
117 ParseNode pn = new ParseNode("method");
118 pn.addChild("method_class").setLiteral((String)clsmeth + "<" + typegen + ">");
119 pn.addChild("method_ident").setLiteral(idmeth);
123 | PUBLIC IDENT:clsmeth LANG IDENT:clsgen RANG IDENT:idmeth LPAR paramlist:pl RPAR SEMICOLON
125 ParseNode pn = new ParseNode("method");
126 pn.addChild("method_class").setLiteral((String)clsmeth + "<" + clsgen + ">");
127 pn.addChild("method_ident").setLiteral(idmeth);
132 paramlist ::= paramlist:pl param:p
139 ParseNode pn = new ParseNode("param_list");
143 param ::= TYPE:typeprm IDENT:idprm COMMA
145 ParseNode pn = new ParseNode("param");
146 pn.addChild("param_type").setLiteral(typeprm);
147 pn.addChild("param_ident").setLiteral(idprm);
150 | TYPE:typeprm IDENT:idprm
152 ParseNode pn = new ParseNode("param");
153 pn.addChild("param_type").setLiteral(typeprm);
154 pn.addChild("param_ident").setLiteral(idprm);
157 | IDENT:clsprm IDENT:idprm COMMA
159 ParseNode pn = new ParseNode("param");
160 pn.addChild("param_class").setLiteral(clsprm);
161 pn.addChild("param_ident").setLiteral(idprm);
164 | IDENT:clsprm IDENT:idprm
166 ParseNode pn = new ParseNode("param");
167 pn.addChild("param_class").setLiteral(clsprm);
168 pn.addChild("param_ident").setLiteral(idprm);
171 /* generic/template with one type, e.g. set<int> */
172 | IDENT:clsprm LANG TYPE:typegen RANG IDENT:idprm
174 ParseNode pn = new ParseNode("param");
175 pn.addChild("param_class").setLiteral((String)clsprm + "<" + typegen + ">");
176 pn.addChild("param_ident").setLiteral(idprm);
179 | IDENT:clsprm LANG IDENT:clsgen RANG IDENT:idprm
181 ParseNode pn = new ParseNode("param");
182 pn.addChild("param_class").setLiteral((String)clsprm + "<" + clsgen + ">");
183 pn.addChild("param_ident").setLiteral(idprm);
186 /* Add comma at the end... */
187 /* generic/template with one type, e.g. set<int> */
188 | IDENT:clsprm LANG TYPE:typegen RANG IDENT:idprm COMMA
190 ParseNode pn = new ParseNode("param");
191 pn.addChild("param_class").setLiteral((String)clsprm + "<" + typegen + ">");
192 pn.addChild("param_ident").setLiteral(idprm);
195 | IDENT:clsprm LANG IDENT:clsgen RANG IDENT:idprm COMMA
197 ParseNode pn = new ParseNode("param");
198 pn.addChild("param_class").setLiteral((String)clsprm + "<" + clsgen + ">");
199 pn.addChild("param_ident").setLiteral(idprm);
204 //2) List of capabilities and their respective contents, i.e. description, method, etc.
205 capablist ::= capablist:cl capab:cap
212 ParseNode pn = new ParseNode("capab_list");
216 capab ::= CAPABILITY IDENT:idcap BEGIN capabcont:ccont END
218 ParseNode pn = new ParseNode("capability");
219 pn.addChild("capab_ident").setLiteral(idcap);
224 capabcont ::= capabcont:ccont cont:cnt
231 ParseNode pn = new ParseNode("capab_content");
235 cont ::= DESCRIPTION:dsc ASSIGN STRINGCONST:strdsc SEMICOLON
237 ParseNode pn = new ParseNode("capab_content");
238 pn.addChild("capab_desc").setLiteral(strdsc);
241 | METHOD:mtd ASSIGN STRINGCONST:strmeth SEMICOLON
243 ParseNode pn = new ParseNode("capab_content");
244 pn.addChild("capab_meth").setLiteral(strmeth);
249 //3) List of interface generation definitions ("requires" statements)
250 reqlist ::= reqlist:rl require:req
257 ParseNode pn = new ParseNode("reqlist");
261 require ::= REQUIRES IDENT:idint WITH capintlist:cil AS INTERFACE IDENT:idnewint SEMICOLON
263 ParseNode pn = new ParseNode("requires");
264 pn.addChild("intface_ident").setLiteral(idint);
266 pn.addChild("new_intface_ident").setLiteral(idnewint);
270 capintlist ::= IDENT:idcap
272 ParseNode pn = new ParseNode("capab_ident_list");
273 pn.addChild("capab_ident").setLiteral(idcap);
276 | capintlist:cil COMMA IDENT:idcap
278 cil.addChild("capab_ident").setLiteral(idcap);
283 ParseNode pn = new ParseNode("capab_ident_list");
288 //4) Enumeration declaration list
289 enumdeclist ::= enumdeclist:el enumdec:ed
296 ParseNode pn = new ParseNode("enum_dec_list");
300 enumdec ::= ENUM IDENT:idenumdec BEGIN enumlist:el END
302 ParseNode pn = new ParseNode("enum_dec");
303 pn.addChild("enum_dec_ident").setLiteral(idenumdec);
308 enumlist ::= enumlist:el enummem:e
315 ParseNode pn = new ParseNode("enum_list");
319 enummem ::= IDENT:idenum COMMA
321 ParseNode pn = new ParseNode("enum_mem");
322 pn.addChild("enum_ident").setLiteral(idenum);
327 ParseNode pn = new ParseNode("enum_mem");
328 pn.addChild("enum_ident").setLiteral(idenum);
333 //5) Struct declaration list
334 structdeclist ::= structdeclist:sl structdec:sd
341 ParseNode pn = new ParseNode("struct_dec_list");
345 structdec ::= STRUCT IDENT:idstructdec BEGIN structlist:sl END
347 ParseNode pn = new ParseNode("struct_dec");
348 pn.addChild("struct_dec_ident").setLiteral(idstructdec);
353 structlist ::= structlist:sl structmem:s
360 ParseNode pn = new ParseNode("struct_list");
364 structmem ::= TYPE:typestr IDENT:idstr SEMICOLON
366 ParseNode pn = new ParseNode("struct_mem");
367 pn.addChild("struct_type").setLiteral(typestr);
368 pn.addChild("struct_ident").setLiteral(idstr);
371 | IDENT:clsstr IDENT:idstr SEMICOLON
373 ParseNode pn = new ParseNode("struct_mem");
374 pn.addChild("struct_class").setLiteral(clsstr);
375 pn.addChild("struct_ident").setLiteral(idstr);
378 | IDENT:clsstr LANG IDENT:clsgen RANG IDENT:idstr SEMICOLON
380 ParseNode pn = new ParseNode("struct_mem");
381 pn.addChild("struct_class").setLiteral((String)clsstr + "<" + clsgen + ">");
382 pn.addChild("struct_ident").setLiteral(idstr);