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;
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;
38 * A policy file normally consists of:
40 * - Interface definition
41 * - List of capabilities and their contents
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)
50 ParseNode pn = new ParseNode("policy");
56 ParseNode pn = new ParseNode("policy");
62 //1) Interface class definition
63 // 1) Interface definition
68 intface ::= PUBLIC INTERFACE IDENT:idint BEGIN methlist:ml capablist:cl END
70 ParseNode pn = new ParseNode("interface");
71 pn.addChild("intface_ident").setLiteral(idint);
77 methlist ::= methlist:ml meth:m
84 ParseNode pn = new ParseNode("method_list");
88 meth ::= PUBLIC TYPE:typemeth IDENT:idmeth LPAR paramlist:pl RPAR SEMICOLON
90 ParseNode pn = new ParseNode("method");
91 pn.addChild("method_type").setLiteral(typemeth);
92 pn.addChild("method_ident").setLiteral(idmeth);
96 | PUBLIC IDENT:clsmeth IDENT:idmeth LPAR paramlist:pl RPAR SEMICOLON
98 ParseNode pn = new ParseNode("method");
99 pn.addChild("method_class").setLiteral(clsmeth);
100 pn.addChild("method_ident").setLiteral(idmeth);
105 paramlist ::= paramlist:pl param:p
112 ParseNode pn = new ParseNode("param_list");
116 param ::= TYPE:typeprm IDENT:idprm COMMA
118 ParseNode pn = new ParseNode("param");
119 pn.addChild("param_type").setLiteral(typeprm);
120 pn.addChild("param_ident").setLiteral(idprm);
123 | TYPE:typeprm IDENT:idprm
125 ParseNode pn = new ParseNode("param");
126 pn.addChild("param_type").setLiteral(typeprm);
127 pn.addChild("param_ident").setLiteral(idprm);
130 | IDENT:clsprm IDENT:idprm COMMA
132 ParseNode pn = new ParseNode("param");
133 pn.addChild("param_class").setLiteral(clsprm);
134 pn.addChild("param_ident").setLiteral(idprm);
137 | IDENT:clsprm IDENT:idprm
139 ParseNode pn = new ParseNode("param");
140 pn.addChild("param_class").setLiteral(clsprm);
141 pn.addChild("param_ident").setLiteral(idprm);
144 /* generic/template with one type, e.g. set<int> */
145 | IDENT:clsprm LANG TYPE:typegen RANG IDENT:idprm
147 ParseNode pn = new ParseNode("param");
148 pn.addChild("param_class").setLiteral((String)clsprm + "<" + typegen + ">");
149 pn.addChild("param_ident").setLiteral(idprm);
152 | IDENT:clsprm LANG IDENT:clsgen RANG IDENT:idprm
154 ParseNode pn = new ParseNode("param");
155 pn.addChild("param_class").setLiteral((String)clsprm + "<" + clsgen + ">");
156 pn.addChild("param_ident").setLiteral(idprm);
159 /* generic/template with two types, e.g. map<int,string> */
160 | IDENT:clsprm LANG TYPE:typegen1 COMMA TYPE:typegen2 RANG IDENT:idprm
162 ParseNode pn = new ParseNode("param");
163 pn.addChild("param_class").setLiteral((String)clsprm
164 + "<" + typegen1 + "," + typegen2 + ">");
165 pn.addChild("param_ident").setLiteral(idprm);
168 | IDENT:clsprm LANG TYPE:typegen COMMA IDENT:clsgen RANG IDENT:idprm
170 ParseNode pn = new ParseNode("param");
171 pn.addChild("param_class").setLiteral((String)clsprm
172 + "<" + typegen + "," + clsgen + ">");
173 pn.addChild("param_ident").setLiteral(idprm);
176 | IDENT:clsprm LANG IDENT:clsgen COMMA TYPE:typegen RANG IDENT:idprm
178 ParseNode pn = new ParseNode("param");
179 pn.addChild("param_class").setLiteral((String)clsprm
180 + "<" + clsgen + "," + typegen + ">");
181 pn.addChild("param_ident").setLiteral(idprm);
184 | IDENT:clsprm LANG IDENT:clsgen1 COMMA IDENT:clsgen2 RANG IDENT:idprm
186 ParseNode pn = new ParseNode("param");
187 pn.addChild("param_class").setLiteral((String)clsprm
188 + "<" + clsgen1 + "," + clsgen2 + ">");
189 pn.addChild("param_ident").setLiteral(idprm);
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
196 ParseNode pn = new ParseNode("param");
197 pn.addChild("param_class").setLiteral((String)clsprm + "<" + typegen + ">");
198 pn.addChild("param_ident").setLiteral(idprm);
201 | IDENT:clsprm LANG IDENT:clsgen RANG IDENT:idprm COMMA
203 ParseNode pn = new ParseNode("param");
204 pn.addChild("param_class").setLiteral((String)clsprm + "<" + clsgen + ">");
205 pn.addChild("param_ident").setLiteral(idprm);
208 /* generic/template with two types, e.g. map<int,string> */
209 | IDENT:clsprm LANG TYPE:typegen1 COMMA TYPE:typegen2 RANG IDENT:idprm COMMA
211 ParseNode pn = new ParseNode("param");
212 pn.addChild("param_class").setLiteral((String)clsprm
213 + "<" + typegen1 + "," + typegen2 + ">");
214 pn.addChild("param_ident").setLiteral(idprm);
217 | IDENT:clsprm LANG TYPE:typegen COMMA IDENT:clsgen RANG IDENT:idprm COMMA
219 ParseNode pn = new ParseNode("param");
220 pn.addChild("param_class").setLiteral((String)clsprm
221 + "<" + typegen + "," + clsgen + ">");
222 pn.addChild("param_ident").setLiteral(idprm);
225 | IDENT:clsprm LANG IDENT:clsgen COMMA TYPE:typegen RANG IDENT:idprm COMMA
227 ParseNode pn = new ParseNode("param");
228 pn.addChild("param_class").setLiteral((String)clsprm
229 + "<" + clsgen + "," + typegen + ">");
230 pn.addChild("param_ident").setLiteral(idprm);
233 | IDENT:clsprm LANG IDENT:clsgen1 COMMA IDENT:clsgen2 RANG IDENT:idprm COMMA
235 ParseNode pn = new ParseNode("param");
236 pn.addChild("param_class").setLiteral((String)clsprm
237 + "<" + clsgen1 + "," + clsgen2 + ">");
238 pn.addChild("param_ident").setLiteral(idprm);
243 //2) List of capabilities and their respective contents, i.e. description, method, etc.
244 capablist ::= capablist:cl capab:cap
251 ParseNode pn = new ParseNode("capab_list");
255 capab ::= CAPABILITY IDENT:idcap BEGIN capabcont:ccont END
257 ParseNode pn = new ParseNode("capability");
258 pn.addChild("capab_ident").setLiteral(idcap);
263 capabcont ::= capabcont:ccont cont:cnt
270 ParseNode pn = new ParseNode("capab_content");
274 cont ::= DESCRIPTION:dsc ASSIGN STRINGCONST:strdsc SEMICOLON
276 ParseNode pn = new ParseNode("capab_content");
277 pn.addChild("capab_desc").setLiteral(strdsc);
280 | METHOD:mtd ASSIGN STRINGCONST:strmeth SEMICOLON
282 ParseNode pn = new ParseNode("capab_content");
283 pn.addChild("capab_meth").setLiteral(strmeth);
288 //3) List of interface generation definitions ("requires" statements)
289 reqlist ::= reqlist:rl require:req
296 ParseNode pn = new ParseNode("reqlist");
300 require ::= REQUIRES IDENT:idint WITH capintlist:cil AS INTERFACE IDENT:idnewint SEMICOLON
302 ParseNode pn = new ParseNode("requires");
303 pn.addChild("intface_ident").setLiteral(idint);
305 pn.addChild("new_intface_ident").setLiteral(idnewint);
309 capintlist ::= IDENT:idcap
311 ParseNode pn = new ParseNode("capab_ident_list");
312 pn.addChild("capab_ident").setLiteral(idcap);
315 | capintlist:cil COMMA IDENT:idcap
317 cil.addChild("capab_ident").setLiteral(idcap);
322 ParseNode pn = new ParseNode("capab_ident_list");