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);
125 paramlist ::= paramlist:pl param:p
132 ParseNode pn = new ParseNode("param_list");
136 param ::= TYPE:typeprm IDENT:idprm COMMA
138 ParseNode pn = new ParseNode("param");
139 pn.addChild("param_type").setLiteral(typeprm);
140 pn.addChild("param_ident").setLiteral(idprm);
143 | TYPE:typeprm IDENT:idprm
145 ParseNode pn = new ParseNode("param");
146 pn.addChild("param_type").setLiteral(typeprm);
147 pn.addChild("param_ident").setLiteral(idprm);
150 | IDENT:clsprm IDENT:idprm COMMA
152 ParseNode pn = new ParseNode("param");
153 pn.addChild("param_class").setLiteral(clsprm);
154 pn.addChild("param_ident").setLiteral(idprm);
157 | IDENT:clsprm IDENT:idprm
159 ParseNode pn = new ParseNode("param");
160 pn.addChild("param_class").setLiteral(clsprm);
161 pn.addChild("param_ident").setLiteral(idprm);
164 /* generic/template with one type, e.g. list<int> */
165 | IDENT:clsprm LANG TYPE:typegen RANG IDENT:idprm
167 ParseNode pn = new ParseNode("param");
168 pn.addChild("param_class").setLiteral((String)clsprm + "<" + typegen + ">");
169 pn.addChild("param_ident").setLiteral(idprm);
172 | IDENT:clsprm LANG IDENT:clsgen RANG IDENT:idprm
174 ParseNode pn = new ParseNode("param");
175 pn.addChild("param_class").setLiteral((String)clsprm + "<" + clsgen + ">");
176 pn.addChild("param_ident").setLiteral(idprm);
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
183 ParseNode pn = new ParseNode("param");
184 pn.addChild("param_class").setLiteral((String)clsprm + "<" + typegen + ">");
185 pn.addChild("param_ident").setLiteral(idprm);
188 | IDENT:clsprm LANG IDENT:clsgen RANG IDENT:idprm COMMA
190 ParseNode pn = new ParseNode("param");
191 pn.addChild("param_class").setLiteral((String)clsprm + "<" + clsgen + ">");
192 pn.addChild("param_ident").setLiteral(idprm);
197 //2) List of capabilities and their respective contents, i.e. description, method, etc.
198 capablist ::= capablist:cl capab:cap
205 ParseNode pn = new ParseNode("capab_list");
209 capab ::= CAPABILITY IDENT:idcap BEGIN capabcont:ccont END
211 ParseNode pn = new ParseNode("capability");
212 pn.addChild("capab_ident").setLiteral(idcap);
217 capabcont ::= capabcont:ccont cont:cnt
224 ParseNode pn = new ParseNode("capab_content");
228 cont ::= DESCRIPTION:dsc ASSIGN STRINGCONST:strdsc SEMICOLON
230 ParseNode pn = new ParseNode("capab_content");
231 pn.addChild("capab_desc").setLiteral(strdsc);
234 | METHOD:mtd ASSIGN STRINGCONST:strmeth SEMICOLON
236 ParseNode pn = new ParseNode("capab_content");
237 pn.addChild("capab_meth").setLiteral(strmeth);
242 //3) List of interface generation definitions ("requires" statements)
243 reqlist ::= reqlist:rl require:req
250 ParseNode pn = new ParseNode("reqlist");
254 require ::= REQUIRES IDENT:idint WITH capintlist:cil AS INTERFACE IDENT:idnewint SEMICOLON
256 ParseNode pn = new ParseNode("requires");
257 pn.addChild("intface_ident").setLiteral(idint);
259 pn.addChild("new_intface_ident").setLiteral(idnewint);
263 capintlist ::= IDENT:idcap
265 ParseNode pn = new ParseNode("capab_ident_list");
266 pn.addChild("capab_ident").setLiteral(idcap);
269 | capintlist:cil COMMA IDENT:idcap
271 cil.addChild("capab_ident").setLiteral(idcap);
276 ParseNode pn = new ParseNode("capab_ident_list");
281 //4) Enumeration declaration
282 enumdec ::= ENUM IDENT:idenumdec BEGIN enumlist:el END
284 ParseNode pn = new ParseNode("enum_dec");
285 pn.addChild("enum_dec_ident").setLiteral(idenumdec);
290 enumlist ::= enumlist:el enummem:e
297 ParseNode pn = new ParseNode("enum_list");
301 enummem ::= IDENT:idenum COMMA
303 ParseNode pn = new ParseNode("enum_mem");
304 pn.addChild("enum_ident").setLiteral(idenum);
309 ParseNode pn = new ParseNode("enum_mem");
310 pn.addChild("enum_ident").setLiteral(idenum);
315 //5) Struct declaration
316 structdec ::= STRUCT IDENT:idstructdec BEGIN structlist:sl END
318 ParseNode pn = new ParseNode("struct_dec");
319 pn.addChild("struct_dec_ident").setLiteral(idstructdec);
324 structlist ::= structlist:sl structmem:s
331 ParseNode pn = new ParseNode("enum_list");
335 structmem ::= TYPE:typestr IDENT:idstr SEMICOLON
337 ParseNode pn = new ParseNode("struct_mem");
338 pn.addChild("struct_type").setLiteral(typestr);
339 pn.addChild("struct_ident").setLiteral(idstr);
342 | IDENT:clsstr IDENT:idstr SEMICOLON
344 ParseNode pn = new ParseNode("struct_mem");
345 pn.addChild("struct_class").setLiteral(clsstr);
346 pn.addChild("struct_ident").setLiteral(idstr);
349 | IDENT:clsstr LANG IDENT:clsgen RANG IDENT:idstr SEMICOLON
351 ParseNode pn = new ParseNode("struct_mem");
352 pn.addChild("struct_class").setLiteral((String)clsstr + "<" + clsgen + ">");
353 pn.addChild("struct_ident").setLiteral(idstr);