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 enumdec, enumlist, enummem;
37 non terminal ParseNode 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 ParseNode pn = new ParseNode("policy");
76 ParseNode pn = new ParseNode("policy");
82 //1) Interface class definition
83 // 1) Interface definition
88 intface ::= PUBLIC INTERFACE IDENT:idint BEGIN methlist:ml capablist:cl END
90 ParseNode pn = new ParseNode("interface");
91 pn.addChild("intface_ident").setLiteral(idint);
97 methlist ::= methlist:ml meth:m
104 ParseNode pn = new ParseNode("method_list");
108 meth ::= PUBLIC TYPE:typemeth IDENT:idmeth LPAR paramlist:pl RPAR SEMICOLON
110 ParseNode pn = new ParseNode("method");
111 pn.addChild("method_type").setLiteral(typemeth);
112 pn.addChild("method_ident").setLiteral(idmeth);
116 | PUBLIC IDENT:clsmeth IDENT:idmeth LPAR paramlist:pl RPAR SEMICOLON
118 ParseNode pn = new ParseNode("method");
119 pn.addChild("method_class").setLiteral(clsmeth);
120 pn.addChild("method_ident").setLiteral(idmeth);
124 /* generic/template return value with one type, e.g. set<int> */
125 | PUBLIC IDENT:clsmeth LANG TYPE:typegen RANG IDENT:idmeth LPAR paramlist:pl RPAR SEMICOLON
127 ParseNode pn = new ParseNode("method");
128 pn.addChild("method_class").setLiteral((String)clsmeth + "<" + typegen + ">");
129 pn.addChild("method_ident").setLiteral(idmeth);
133 | PUBLIC IDENT:clsmeth LANG IDENT:clsgen RANG IDENT:idmeth LPAR paramlist:pl RPAR SEMICOLON
135 ParseNode pn = new ParseNode("method");
136 pn.addChild("method_class").setLiteral((String)clsmeth + "<" + clsgen + ">");
137 pn.addChild("method_ident").setLiteral(idmeth);
142 paramlist ::= paramlist:pl param:p
149 ParseNode pn = new ParseNode("param_list");
153 param ::= TYPE:typeprm IDENT:idprm COMMA
155 ParseNode pn = new ParseNode("param");
156 pn.addChild("param_type").setLiteral(typeprm);
157 pn.addChild("param_ident").setLiteral(idprm);
160 | TYPE:typeprm IDENT:idprm
162 ParseNode pn = new ParseNode("param");
163 pn.addChild("param_type").setLiteral(typeprm);
164 pn.addChild("param_ident").setLiteral(idprm);
167 | IDENT:clsprm IDENT:idprm COMMA
169 ParseNode pn = new ParseNode("param");
170 pn.addChild("param_class").setLiteral(clsprm);
171 pn.addChild("param_ident").setLiteral(idprm);
174 | IDENT:clsprm IDENT:idprm
176 ParseNode pn = new ParseNode("param");
177 pn.addChild("param_class").setLiteral(clsprm);
178 pn.addChild("param_ident").setLiteral(idprm);
181 /* generic/template with one type, e.g. set<int> */
182 | IDENT:clsprm LANG TYPE:typegen RANG IDENT:idprm
184 ParseNode pn = new ParseNode("param");
185 pn.addChild("param_class").setLiteral((String)clsprm + "<" + typegen + ">");
186 pn.addChild("param_ident").setLiteral(idprm);
189 | IDENT:clsprm LANG IDENT:clsgen RANG IDENT:idprm
191 ParseNode pn = new ParseNode("param");
192 pn.addChild("param_class").setLiteral((String)clsprm + "<" + clsgen + ">");
193 pn.addChild("param_ident").setLiteral(idprm);
196 /* Add comma at the end... */
197 /* generic/template with one type, e.g. set<int> */
198 | IDENT:clsprm LANG TYPE:typegen RANG IDENT:idprm COMMA
200 ParseNode pn = new ParseNode("param");
201 pn.addChild("param_class").setLiteral((String)clsprm + "<" + typegen + ">");
202 pn.addChild("param_ident").setLiteral(idprm);
205 | IDENT:clsprm LANG IDENT:clsgen RANG IDENT:idprm COMMA
207 ParseNode pn = new ParseNode("param");
208 pn.addChild("param_class").setLiteral((String)clsprm + "<" + clsgen + ">");
209 pn.addChild("param_ident").setLiteral(idprm);
214 //2) List of capabilities and their respective contents, i.e. description, method, etc.
215 capablist ::= capablist:cl capab:cap
222 ParseNode pn = new ParseNode("capab_list");
226 capab ::= CAPABILITY IDENT:idcap BEGIN capabcont:ccont END
228 ParseNode pn = new ParseNode("capability");
229 pn.addChild("capab_ident").setLiteral(idcap);
234 capabcont ::= capabcont:ccont cont:cnt
241 ParseNode pn = new ParseNode("capab_content");
245 cont ::= DESCRIPTION:dsc ASSIGN STRINGCONST:strdsc SEMICOLON
247 ParseNode pn = new ParseNode("capab_content");
248 pn.addChild("capab_desc").setLiteral(strdsc);
251 | METHOD:mtd ASSIGN STRINGCONST:strmeth SEMICOLON
253 ParseNode pn = new ParseNode("capab_content");
254 pn.addChild("capab_meth").setLiteral(strmeth);
259 //3) List of interface generation definitions ("requires" statements)
260 reqlist ::= reqlist:rl require:req
267 ParseNode pn = new ParseNode("reqlist");
271 require ::= REQUIRES IDENT:idint WITH capintlist:cil AS INTERFACE IDENT:idnewint SEMICOLON
273 ParseNode pn = new ParseNode("requires");
274 pn.addChild("intface_ident").setLiteral(idint);
276 pn.addChild("new_intface_ident").setLiteral(idnewint);
280 capintlist ::= IDENT:idcap
282 ParseNode pn = new ParseNode("capab_ident_list");
283 pn.addChild("capab_ident").setLiteral(idcap);
286 | capintlist:cil COMMA IDENT:idcap
288 cil.addChild("capab_ident").setLiteral(idcap);
293 ParseNode pn = new ParseNode("capab_ident_list");
298 //4) Enumeration declaration
299 enumdec ::= ENUM IDENT:idenumdec BEGIN enumlist:el END
301 ParseNode pn = new ParseNode("enum_dec");
302 pn.addChild("enum_dec_ident").setLiteral(idenumdec);
307 enumlist ::= enumlist:el enummem:e
314 ParseNode pn = new ParseNode("enum_list");
318 enummem ::= IDENT:idenum COMMA
320 ParseNode pn = new ParseNode("enum_mem");
321 pn.addChild("enum_ident").setLiteral(idenum);
326 ParseNode pn = new ParseNode("enum_mem");
327 pn.addChild("enum_ident").setLiteral(idenum);
332 //5) Struct declaration
333 structdec ::= STRUCT IDENT:idstructdec BEGIN structlist:sl END
335 ParseNode pn = new ParseNode("struct_dec");
336 pn.addChild("struct_dec_ident").setLiteral(idstructdec);
341 structlist ::= structlist:sl structmem:s
348 ParseNode pn = new ParseNode("enum_list");
352 structmem ::= TYPE:typestr IDENT:idstr SEMICOLON
354 ParseNode pn = new ParseNode("struct_mem");
355 pn.addChild("struct_type").setLiteral(typestr);
356 pn.addChild("struct_ident").setLiteral(idstr);
359 | IDENT:clsstr IDENT:idstr SEMICOLON
361 ParseNode pn = new ParseNode("struct_mem");
362 pn.addChild("struct_class").setLiteral(clsstr);
363 pn.addChild("struct_ident").setLiteral(idstr);
366 | IDENT:clsstr LANG IDENT:clsgen RANG IDENT:idstr SEMICOLON
368 ParseNode pn = new ParseNode("struct_mem");
369 pn.addChild("struct_class").setLiteral((String)clsstr + "<" + clsgen + ">");
370 pn.addChild("struct_ident").setLiteral(idstr);