Separating policy file into main policy and generated interfaces; fixing lexer, parse...
[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 :};
26
27 terminal SEMICOLON, COMMA, LPAR, RPAR, BEGIN, END, ASSIGN;
28 terminal PUBLIC, INTERFACE, CAPABILITY, DESCRIPTION, METHOD, REQUIRES, WITH, AS; 
29 terminal TYPE;
30 terminal IDENT, STRINGCONST;
31
32 non terminal ParseNode policy;
33 non terminal ParseNode intface, methlist, meth, paramlist, param;
34 non terminal ParseNode capablist, capab, capabcont, cont;
35 non terminal ParseNode reqlist, require, capintlist;
36
37 /**
38  * A policy file normally consists of:
39  * 1) Interface 
40  *    - Interface definition
41  *    - List of capabilities and their contents
42  *
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)
47  */
48 policy     ::= intface:in
49         {:
50                 ParseNode pn = new ParseNode("policy");
51                 pn.addChild(in);
52                 RESULT = pn;
53         :}
54         | reqlist:rl
55         {:
56                 ParseNode pn = new ParseNode("policy");
57                 pn.addChild(rl);
58                 RESULT = pn;
59         :}
60     ;
61
62 //1) Interface class definition
63 // 1) Interface definition
64 // 2) Library list
65 // 3) Driver list
66
67 // Interface
68 intface    ::= PUBLIC INTERFACE IDENT:idint BEGIN methlist:ml capablist:cl END
69         {:
70                 ParseNode pn = new ParseNode("interface");
71                 pn.addChild("intface_ident").setLiteral(idint);
72                 pn.addChild(ml);
73                 pn.addChild(cl);
74                 RESULT = pn;
75         :}
76     ;
77 methlist   ::= methlist:ml meth:m
78         {:
79                 ml.addChild(m);
80                 RESULT = ml;
81         :}
82     | /* empty */
83         {:
84                 ParseNode pn = new ParseNode("method_list");
85                 RESULT = pn;
86         :}
87     ;
88 meth       ::= PUBLIC TYPE:typemeth IDENT:idmeth LPAR paramlist:pl RPAR SEMICOLON
89         {:
90                 ParseNode pn = new ParseNode("method");
91                 pn.addChild("method_type").setLiteral(typemeth);
92                 pn.addChild("method_ident").setLiteral(idmeth);
93                 pn.addChild(pl);
94                 RESULT = pn;
95         :}
96     | PUBLIC IDENT:clsmeth IDENT:idmeth LPAR paramlist:pl RPAR SEMICOLON
97         {:
98                 ParseNode pn = new ParseNode("method");
99                 pn.addChild("method_class").setLiteral(clsmeth);
100                 pn.addChild("method_ident").setLiteral(idmeth);
101                 pn.addChild(pl);
102                 RESULT = pn;
103         :}
104     ;
105 paramlist  ::= paramlist:pl param:p
106         {:
107                 pl.addChild(p);
108                 RESULT = pl;
109         :}
110     | /* empty */
111         {:
112                 ParseNode pn = new ParseNode("param_list");
113                 RESULT = pn;
114         :}
115     ;
116 param      ::= TYPE:typeprm IDENT:idprm COMMA
117         {:
118                 ParseNode pn = new ParseNode("param");
119                 pn.addChild("param_type").setLiteral(typeprm);
120                 pn.addChild("param_ident").setLiteral(idprm);
121                 RESULT = pn;
122         :}
123     | TYPE:typeprm IDENT:idprm
124         {:
125                 ParseNode pn = new ParseNode("param");
126                 pn.addChild("param_type").setLiteral(typeprm);
127                 pn.addChild("param_ident").setLiteral(idprm);
128                 RESULT = pn;
129         :}
130     | IDENT:clsprm IDENT:idprm COMMA
131         {:
132                 ParseNode pn = new ParseNode("param");
133                 pn.addChild("param_class").setLiteral(clsprm);
134                 pn.addChild("param_ident").setLiteral(idprm);
135                 RESULT = pn;
136         :}
137     | IDENT:clsprm IDENT:idprm
138         {:
139                 ParseNode pn = new ParseNode("param");
140                 pn.addChild("param_class").setLiteral(clsprm);
141                 pn.addChild("param_ident").setLiteral(idprm);
142                 RESULT = pn;
143         :}
144     ;
145
146 //2) List of capabilities and their respective contents, i.e. description, method, etc.
147 capablist  ::= capablist:cl capab:cap
148         {:
149                 cl.addChild(cap);
150                 RESULT = cl;
151         :}
152         | /* empty */
153         {:
154                 ParseNode pn = new ParseNode("capab_list");
155                 RESULT = pn;
156         :}
157         ;
158 capab      ::= CAPABILITY IDENT:idcap BEGIN capabcont:ccont END
159         {:
160                 ParseNode pn = new ParseNode("capability");
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:strdsc SEMICOLON
178         {:
179                 ParseNode pn = new ParseNode("capab_content");
180                 pn.addChild("capab_desc").setLiteral(strdsc);
181                 RESULT = pn;
182         :}
183         | METHOD:mtd ASSIGN STRINGCONST:strmeth SEMICOLON
184         {:
185                 ParseNode pn = new ParseNode("capab_content");
186                 pn.addChild("capab_meth").setLiteral(strmeth);
187                 RESULT = pn;
188         :}
189         ;
190
191 //3) List of interface generation definitions ("requires" statements)
192 reqlist    ::= reqlist:rl require:req
193         {:
194                 rl.addChild(req);
195                 RESULT = rl;
196         :}
197         | /* empty */
198         {:
199                 ParseNode pn = new ParseNode("reqlist");
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