KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > script > el > parser > NetUIELParser


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.script.el.parser;
19
20 import org.apache.beehive.netui.script.el.ExpressionTerm;
21 import org.apache.beehive.netui.script.el.LiteralTerm;
22 import org.apache.beehive.netui.script.el.ParsedExpression;
23 import org.apache.beehive.netui.script.el.Term;
24 import org.apache.beehive.netui.script.el.tokens.*;
25
26 public class NetUIELParser
27     implements NetUIELParserConstants {
28
29     public static void main(String JavaDoc[] args)
30         throws Exception JavaDoc {
31         NetUIELParser parser = new NetUIELParser(System.in);
32         parser.parse();
33     }
34
35 // 3.
36
final public ParsedExpression parse() throws ParseException {
37         Token t = null;
38         ParsedExpression pe = new ParsedExpression();
39         Term term = null;
40         label_1:
41         while(true) {
42             switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
43                 case NON_EXPRESSION_TEXT:
44                 case START_EXPRESSION:
45                 case ESCAPED_START_EXPRESSION:
46                     ;
47                     break;
48                 default:
49                     jj_la1[0] = jj_gen;
50                     break label_1;
51             }
52             switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
53                 case NON_EXPRESSION_TEXT:
54                     term = parseLiteralTerm();
55                     pe.addTerm(term);
56                     break;
57                 case START_EXPRESSION:
58                     jj_consume_token(START_EXPRESSION);
59                     term = parseExpression();
60                     jj_consume_token(END_EXPRESSION);
61                     pe.addTerm(term);
62                     break;
63                 case ESCAPED_START_EXPRESSION:
64                     jj_consume_token(ESCAPED_START_EXPRESSION);
65                     term = parseExpression();
66                     jj_consume_token(END_EXPRESSION);
67                     pe.addTerm(new LiteralTerm("\\"));
68                     pe.addTerm(term);
69                     break;
70                 default:
71                     jj_la1[1] = jj_gen;
72                     jj_consume_token(-1);
73                     throw new ParseException();
74             }
75         }
76         jj_consume_token(0);
77         {
78             if(true) return pe;
79         }
80         throw new Error JavaDoc("Missing return statement in function");
81     }
82
83     final public LiteralTerm parseLiteralTerm() throws ParseException {
84         Token t = null;
85         LiteralTerm ls = null;
86         t = jj_consume_token(NON_EXPRESSION_TEXT);
87         ls = new LiteralTerm(t.image);
88         {
89             if(true) return ls;
90         }
91         throw new Error JavaDoc("Missing return statement in function");
92     }
93
94     final public ExpressionTerm parseExpression() throws ParseException {
95         ExpressionTerm expr = new ExpressionTerm();
96         ExpressionToken eTok = null;
97         eTok = Context();
98         expr.addToken(eTok);
99         label_2:
100         while(true) {
101             switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
102                 case DOT:
103                 case LBRACKET:
104                     ;
105                     break;
106                 default:
107                     jj_la1[2] = jj_gen;
108                     break label_2;
109             }
110             switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
111                 case DOT:
112                     jj_consume_token(DOT);
113                     eTok = ExprIdentifier();
114                     break;
115                 default:
116                     jj_la1[3] = jj_gen;
117                     if(jj_2_1(2)) {
118                         eTok = MapKey();
119                     } else {
120                         switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
121                             case LBRACKET:
122                                 eTok = ArrayIndex();
123                                 break;
124                             default:
125                                 jj_la1[4] = jj_gen;
126                                 jj_consume_token(-1);
127                                 throw new ParseException();
128                         }
129                     }
130             }
131             {
132                 expr.addToken(eTok);
133             }
134         }
135         expr.seal();
136         {
137             if(true) return expr;
138         }
139         throw new Error JavaDoc("Missing return statement in function");
140     }
141
142     final public ExpressionToken Context() throws ParseException {
143         Token t = null;
144         ExpressionToken eTok = null;
145         t = jj_consume_token(IDENTIFIER);
146 //System.out.println("** Parser found context: " + t.image);
147
eTok = new ContextToken(t.image);
148         {
149             if(true) return eTok;
150         }
151         throw new Error JavaDoc("Missing return statement in function");
152     }
153
154     final public ExpressionToken ExprIdentifier() throws ParseException {
155         Token t = null;
156         ExpressionToken eTok = null;
157         t = jj_consume_token(IDENTIFIER);
158 //System.out.println("** Parser found identifier: " + t.image);
159
eTok = new IdentifierToken(t.image);
160         {
161             if(true) return eTok;
162         }
163         throw new Error JavaDoc("Missing return statement in function");
164     }
165
166 // handle text inside of map braces as ["..."] or ['...']
167
final public ExpressionToken MapKey() throws ParseException {
168         Token t = null;
169         ExpressionToken eTok = null;
170         jj_consume_token(LBRACKET);
171         t = jj_consume_token(STRING_LITERAL);
172         jj_consume_token(RBRACKET);
173         eTok = new MapKeyToken(t.image);
174         {
175             if(true) return eTok;
176         }
177         throw new Error JavaDoc("Missing return statement in function");
178     }
179
180     final public ExpressionToken ArrayIndex() throws ParseException {
181         Token t = null;
182         ExpressionToken eTok = null;
183         jj_consume_token(LBRACKET);
184         t = jj_consume_token(INTEGER);
185         jj_consume_token(RBRACKET);
186 //System.out.println("** Parser found array index: " + t.image);
187
eTok = new ArrayIndexToken(t.image);
188         {
189             if(true) return eTok;
190         }
191         throw new Error JavaDoc("Missing return statement in function");
192     }
193
194     final private boolean jj_2_1(int xla) {
195         jj_la = xla;
196         jj_lastpos = jj_scanpos = token;
197         boolean retval = !jj_3_1();
198         jj_save(0, xla);
199         return retval;
200     }
201
202     final private boolean jj_3_1() {
203         if(jj_3R_3()) return true;
204         if(jj_la == 0 && jj_scanpos == jj_lastpos) return false;
205         return false;
206     }
207
208     final private boolean jj_3R_3() {
209         if(jj_scan_token(LBRACKET)) return true;
210         if(jj_la == 0 && jj_scanpos == jj_lastpos) return false;
211         if(jj_scan_token(STRING_LITERAL)) return true;
212         if(jj_la == 0 && jj_scanpos == jj_lastpos) return false;
213         return false;
214     }
215
216     public NetUIELParserTokenManager token_source;
217     SimpleCharStream jj_input_stream;
218     public Token token, jj_nt;
219     private int jj_ntk;
220     private Token jj_scanpos, jj_lastpos;
221     private int jj_la;
222     public boolean lookingAhead = false;
223     private boolean jj_semLA;
224     private int jj_gen;
225     final private int[] jj_la1 = new int[5];
226     static private int[] jj_la1_0;
227
228     static {
229         jj_la1_0();
230     }
231
232     private static void jj_la1_0() {
233         jj_la1_0 = new int[]{0xe, 0xe, 0x12000, 0x2000, 0x10000, };
234     }
235
236     final private JJCalls[] jj_2_rtns = new JJCalls[1];
237     private boolean jj_rescan = false;
238     private int jj_gc = 0;
239
240     public NetUIELParser(java.io.InputStream JavaDoc stream) {
241         jj_input_stream = new SimpleCharStream(stream, 1, 1);
242         token_source = new NetUIELParserTokenManager(jj_input_stream);
243         token = new Token();
244         jj_ntk = -1;
245         jj_gen = 0;
246         for(int i = 0; i < 5; i++) jj_la1[i] = -1;
247         for(int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
248     }
249
250     public void ReInit(java.io.InputStream JavaDoc stream) {
251         jj_input_stream.ReInit(stream, 1, 1);
252         token_source.ReInit(jj_input_stream);
253         token = new Token();
254         jj_ntk = -1;
255         jj_gen = 0;
256         for(int i = 0; i < 5; i++) jj_la1[i] = -1;
257         for(int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
258     }
259
260     public NetUIELParser(java.io.Reader JavaDoc stream) {
261         jj_input_stream = new SimpleCharStream(stream, 1, 1);
262         token_source = new NetUIELParserTokenManager(jj_input_stream);
263         token = new Token();
264         jj_ntk = -1;
265         jj_gen = 0;
266         for(int i = 0; i < 5; i++) jj_la1[i] = -1;
267         for(int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
268     }
269
270     public void ReInit(java.io.Reader JavaDoc stream) {
271         jj_input_stream.ReInit(stream, 1, 1);
272         token_source.ReInit(jj_input_stream);
273         token = new Token();
274         jj_ntk = -1;
275         jj_gen = 0;
276         for(int i = 0; i < 5; i++) jj_la1[i] = -1;
277         for(int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
278     }
279
280     public NetUIELParser(NetUIELParserTokenManager tm) {
281         token_source = tm;
282         token = new Token();
283         jj_ntk = -1;
284         jj_gen = 0;
285         for(int i = 0; i < 5; i++) jj_la1[i] = -1;
286         for(int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
287     }
288
289     public void ReInit(NetUIELParserTokenManager tm) {
290         token_source = tm;
291         token = new Token();
292         jj_ntk = -1;
293         jj_gen = 0;
294         for(int i = 0; i < 5; i++) jj_la1[i] = -1;
295         for(int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
296     }
297
298     final private Token jj_consume_token(int kind) throws ParseException {
299         Token oldToken;
300         if((oldToken = token).next != null)
301             token = token.next;
302         else
303             token = token.next = token_source.getNextToken();
304         jj_ntk = -1;
305         if(token.kind == kind) {
306             jj_gen++;
307             if(++jj_gc > 100) {
308                 jj_gc = 0;
309                 for(int i = 0; i < jj_2_rtns.length; i++) {
310                     JJCalls c = jj_2_rtns[i];
311                     while(c != null) {
312                         if(c.gen < jj_gen) c.first = null;
313                         c = c.next;
314                     }
315                 }
316             }
317             return token;
318         }
319         token = oldToken;
320         jj_kind = kind;
321         throw generateParseException();
322     }
323
324     final private boolean jj_scan_token(int kind) {
325         if(jj_scanpos == jj_lastpos) {
326             jj_la--;
327             if(jj_scanpos.next == null) {
328                 jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken();
329             } else {
330                 jj_lastpos = jj_scanpos = jj_scanpos.next;
331             }
332         } else {
333             jj_scanpos = jj_scanpos.next;
334         }
335         if(jj_rescan) {
336             int i = 0;
337             Token tok = token;
338             while(tok != null && tok != jj_scanpos) {
339                 i++;
340                 tok = tok.next;
341             }
342             if(tok != null) jj_add_error_token(kind, i);
343         }
344         return (jj_scanpos.kind != kind);
345     }
346
347     final public Token getNextToken() {
348         if(token.next != null)
349             token = token.next;
350         else
351             token = token.next = token_source.getNextToken();
352         jj_ntk = -1;
353         jj_gen++;
354         return token;
355     }
356
357     final public Token getToken(int index) {
358         Token t = lookingAhead ? jj_scanpos : token;
359         for(int i = 0; i < index; i++) {
360             if(t.next != null)
361                 t = t.next;
362             else
363                 t = t.next = token_source.getNextToken();
364         }
365         return t;
366     }
367
368     final private int jj_ntk() {
369         if((jj_nt = token.next) == null)
370             return (jj_ntk = (token.next = token_source.getNextToken()).kind);
371         else
372             return (jj_ntk = jj_nt.kind);
373     }
374
375     private java.util.Vector JavaDoc jj_expentries = new java.util.Vector JavaDoc();
376     private int[] jj_expentry;
377     private int jj_kind = -1;
378     private int[] jj_lasttokens = new int[100];
379     private int jj_endpos;
380
381     private void jj_add_error_token(int kind, int pos) {
382         if(pos >= 100) return;
383         if(pos == jj_endpos + 1) {
384             jj_lasttokens[jj_endpos++] = kind;
385         } else if(jj_endpos != 0) {
386             jj_expentry = new int[jj_endpos];
387             for(int i = 0; i < jj_endpos; i++) {
388                 jj_expentry[i] = jj_lasttokens[i];
389             }
390             boolean exists = false;
391             for(java.util.Enumeration JavaDoc e = jj_expentries.elements(); e.hasMoreElements();) {
392                 int[] oldentry = (int[])(e.nextElement());
393                 if(oldentry.length == jj_expentry.length) {
394                     exists = true;
395                     for(int i = 0; i < jj_expentry.length; i++) {
396                         if(oldentry[i] != jj_expentry[i]) {
397                             exists = false;
398                             break;
399                         }
400                     }
401                     if(exists) break;
402                 }
403             }
404             if(!exists) jj_expentries.addElement(jj_expentry);
405             if(pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind;
406         }
407     }
408
409     public ParseException generateParseException() {
410         jj_expentries.removeAllElements();
411         boolean[] la1tokens = new boolean[18];
412         for(int i = 0; i < 18; i++) {
413             la1tokens[i] = false;
414         }
415         if(jj_kind >= 0) {
416             la1tokens[jj_kind] = true;
417             jj_kind = -1;
418         }
419         for(int i = 0; i < 5; i++) {
420             if(jj_la1[i] == jj_gen) {
421                 for(int j = 0; j < 32; j++) {
422                     if((jj_la1_0[i] & (1 << j)) != 0) {
423                         la1tokens[j] = true;
424                     }
425                 }
426             }
427         }
428         for(int i = 0; i < 18; i++) {
429             if(la1tokens[i]) {
430                 jj_expentry = new int[1];
431                 jj_expentry[0] = i;
432                 jj_expentries.addElement(jj_expentry);
433             }
434         }
435         jj_endpos = 0;
436         jj_rescan_token();
437         jj_add_error_token(0, 0);
438         int[][] exptokseq = new int[jj_expentries.size()][];
439         for(int i = 0; i < jj_expentries.size(); i++) {
440             exptokseq[i] = (int[])jj_expentries.elementAt(i);
441         }
442         return new ParseException(token, exptokseq, tokenImage);
443     }
444
445     final public void enable_tracing() {
446     }
447
448     final public void disable_tracing() {
449     }
450
451     final private void jj_rescan_token() {
452         jj_rescan = true;
453         for(int i = 0; i < 1; i++) {
454             JJCalls p = jj_2_rtns[i];
455             do {
456                 if(p.gen > jj_gen) {
457                     jj_la = p.arg;
458                     jj_lastpos = jj_scanpos = p.first;
459                     switch(i) {
460                         case 0:
461                             jj_3_1();
462                             break;
463                     }
464                 }
465                 p = p.next;
466             } while(p != null);
467         }
468         jj_rescan = false;
469     }
470
471     final private void jj_save(int index, int xla) {
472         JJCalls p = jj_2_rtns[index];
473         while(p.gen > jj_gen) {
474             if(p.next == null) {
475                 p = p.next = new JJCalls();
476                 break;
477             }
478             p = p.next;
479         }
480         p.gen = jj_gen + xla - jj_la;
481         p.first = token;
482         p.arg = xla;
483     }
484
485     static final class JJCalls {
486
487         int gen;
488         Token first;
489         int arg;
490         JJCalls next;
491     }
492
493 }
494
Popular Tags