Perfecting parser, lexer, and parse-tree handling for policy files; Generating skelet...
[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       /*XMLElement e = (XMLElement)p.parse().value;
26       // create XML output file 
27       XMLOutputFactory outFactory = XMLOutputFactory.newInstance();
28       XMLStreamWriter sw = outFactory.createXMLStreamWriter(new FileOutputStream(args[1]), "UTF-8");
29       // dump XML output to the file
30       XMLElement.dump(lexer,sw,e,"expr","stmt");
31
32        // transform the parse tree into an AST and a rendered HTML version
33       Transformer transformer = TransformerFactory.newInstance()
34             .newTransformer(new StreamSource(new File("tree.xsl")));
35       Source text = new StreamSource(new File(args[1]));
36       transformer.transform(text, new StreamResult(new File("output.xml")));
37       transformer = TransformerFactory.newInstance()
38             .newTransformer(new StreamSource(new File("tree-view.xsl")));
39       text = new StreamSource(new File("output.xml"));
40       transformer.transform(text, new StreamResult(new File("ast.html")));*/
41   }
42 :};
43
44 terminal SEMICOLON, COMMA, DOT, LPAR, RPAR, BEGIN, END, ASSIGN;
45 terminal PUBLIC, INTERFACE, CAPABILITY, DESCRIPTION, METHOD, REQUIRES, WITH, AS; 
46 terminal TYPE;
47 terminal IDENT, STRINGCONST;
48
49 non terminal ParseNode policy, intface, methlist, meth, paramlist, param;
50 non terminal ParseNode capablist, capab, capabcont, cont;
51 non terminal ParseNode reqlist, require, capintlist;
52
53 /**
54  * A policy file normally consists of 3 parts:
55  * 1) Interface (in Java syntax)
56  * 2) List of capabilities and their contents
57  * 3) List of interface generation definitions
58  */
59 policy     ::= 
60         intface:in capablist:cap reqlist:rl
61         {:
62                 ParseNode pn = new ParseNode("policy");
63                 pn.addChild(in);
64                 pn.addChild(cap);
65                 pn.addChild(rl);
66                 RESULT = pn;
67         :}
68     ;
69
70 // Interface class definition
71 intface    ::= PUBLIC INTERFACE IDENT:idint BEGIN methlist:ml END
72         {:
73                 ParseNode pn = new ParseNode("interface");
74                 pn.addChild("intface_ident").setLiteral(idint);
75                 pn.addChild(ml);
76                 RESULT = pn;
77         :}
78     | /* empty */
79         {:
80                 ParseNode pn = new ParseNode("interface");
81                 RESULT = pn;
82         :}
83     ;
84 methlist   ::= methlist:ml meth:m
85         {:
86                 ml.addChild(m);
87                 RESULT = ml;
88         :}
89     | /* empty */
90         {:
91                 ParseNode pn = new ParseNode("method_list");
92                 RESULT = pn;
93         :}
94     ;
95 meth       ::= PUBLIC TYPE:typemeth IDENT:idmeth LPAR paramlist:pl RPAR SEMICOLON
96         {:
97                 ParseNode pn = new ParseNode("method");
98                 pn.addChild("method_type").setLiteral(typemeth);
99                 pn.addChild("method_ident").setLiteral(idmeth);
100                 pn.addChild(pl);
101                 RESULT = pn;
102         :}
103     ;
104 paramlist  ::= paramlist:pl param:p
105         {:
106                 pl.addChild(p);
107                 RESULT = pl;
108         :}
109     | /* empty */
110         {:
111                 ParseNode pn = new ParseNode("param_list");
112                 RESULT = pn;
113         :}
114     ;
115 param      ::= TYPE:typeprm IDENT:idprm COMMA
116         {:
117                 ParseNode pn = new ParseNode("param");
118                 pn.addChild("param_type").setLiteral(typeprm);
119                 pn.addChild("param_ident").setLiteral(idprm);
120                 RESULT = pn;
121         :}
122     | TYPE:typeprm IDENT:idprm
123         {:
124                 ParseNode pn = new ParseNode("param");
125                 pn.addChild("param_type").setLiteral(typeprm);
126                 pn.addChild("param_ident").setLiteral(idprm);
127                 RESULT = pn;
128         :}
129     | IDENT:clsprm IDENT:idprm COMMA
130         {:
131                 ParseNode pn = new ParseNode("param");
132                 pn.addChild("param_class").setLiteral(clsprm);
133                 pn.addChild("param_ident").setLiteral(idprm);
134                 RESULT = pn;
135         :}
136     | IDENT:clsprm IDENT:idprm
137         {:
138                 ParseNode pn = new ParseNode("param");
139                 pn.addChild("param_class").setLiteral(clsprm);
140                 pn.addChild("param_ident").setLiteral(idprm);
141                 RESULT = pn;
142         :}
143     ;
144
145 // List of capabilities and their respective contents, i.e. description, method, etc.
146 capablist  ::= capablist:clist capab:cap
147         {:
148                 clist.addChild(cap);
149                 RESULT = clist;
150         :}
151         | /* empty */
152         {:
153                 ParseNode pn = new ParseNode("capab_list");
154                 RESULT = pn;
155         :}
156         ;
157 capab      ::= CAPABILITY IDENT:idint DOT IDENT:idcap BEGIN capabcont:ccont END
158         {:
159                 ParseNode pn = new ParseNode("capability");
160                 pn.addChild("intface_ident").setLiteral(idint);
161                 pn.addChild("capab_ident").setLiteral(idcap);
162                 pn.addChild(ccont);
163                 RESULT = pn;
164         :}
165     ;
166 capabcont  ::= capabcont:ccont cont:cnt
167         {:
168                 ccont.addChild(cnt);
169                 RESULT = ccont;
170         :}
171         | /* empty */
172         {:
173                 ParseNode pn = new ParseNode("capab_content");
174                 RESULT = pn;
175         :}
176         ;
177 cont       ::= DESCRIPTION:dsc ASSIGN STRINGCONST:str SEMICOLON
178         {:
179                 ParseNode pn = new ParseNode("capab_content");
180                 pn.addChild("capab_desc").setLiteral(str);
181                 RESULT = pn;
182         :}
183         | METHOD:mtd ASSIGN IDENT:idmeth SEMICOLON
184         {:
185                 ParseNode pn = new ParseNode("capab_content");
186                 pn.addChild("capab_ident").setLiteral(idmeth);
187                 RESULT = pn;
188         :}
189         ;
190
191 // List of interface generation definitions
192 reqlist    ::= reqlist:rl require:req
193         {:
194                 rl.addChild(req);
195                 RESULT = rl;
196         :}
197         | /* empty */
198         {:
199                 ParseNode pn = new ParseNode("requires_list");
200                 RESULT = pn;
201         :}
202         ;
203 require    ::= REQUIRES IDENT:idint WITH capintlist:cil AS INTERFACE IDENT:idnewint SEMICOLON
204         {:
205                 ParseNode pn = new ParseNode("requires");
206                 pn.addChild("intface_ident").setLiteral(idint);
207                 pn.addChild(cil);
208                 pn.addChild("new_intface_ident").setLiteral(idnewint);
209                 RESULT = pn;
210         :}
211         ;
212 capintlist ::= IDENT:idcap
213         {:
214                 ParseNode pn = new ParseNode("capab_ident_list");
215                 pn.addChild("capab_ident").setLiteral(idcap);
216                 RESULT = pn;
217         :}
218         | capintlist:cil COMMA IDENT:idcap
219         {:
220                 cil.addChild("capab_ident").setLiteral(idcap);
221                 RESULT = cil;
222         :}
223         | /* empty */
224         {:
225                 ParseNode pn = new ParseNode("capab_ident_list");
226                 RESULT = pn;
227         :}
228         ;
229