001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.script.el.parser;
020:
021: import org.apache.beehive.netui.script.el.ExpressionTerm;
022: import org.apache.beehive.netui.script.el.LiteralTerm;
023: import org.apache.beehive.netui.script.el.ParsedExpression;
024: import org.apache.beehive.netui.script.el.Term;
025: import org.apache.beehive.netui.script.el.tokens.*;
026:
027: public class NetUIELParser implements NetUIELParserConstants {
028:
029: public static void main(String[] args) throws Exception {
030: NetUIELParser parser = new NetUIELParser(System.in);
031: parser.parse();
032: }
033:
034: // 3.
035: final public ParsedExpression parse() throws ParseException {
036: Token t = null;
037: ParsedExpression pe = new ParsedExpression();
038: Term term = null;
039: label_1: while (true) {
040: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
041: case NON_EXPRESSION_TEXT:
042: case START_EXPRESSION:
043: case ESCAPED_START_EXPRESSION:
044: ;
045: break;
046: default:
047: jj_la1[0] = jj_gen;
048: break label_1;
049: }
050: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
051: case NON_EXPRESSION_TEXT:
052: term = parseLiteralTerm();
053: pe.addTerm(term);
054: break;
055: case START_EXPRESSION:
056: jj_consume_token(START_EXPRESSION);
057: term = parseExpression();
058: jj_consume_token(END_EXPRESSION);
059: pe.addTerm(term);
060: break;
061: case ESCAPED_START_EXPRESSION:
062: jj_consume_token(ESCAPED_START_EXPRESSION);
063: term = parseExpression();
064: jj_consume_token(END_EXPRESSION);
065: pe.addTerm(new LiteralTerm("\\"));
066: pe.addTerm(term);
067: break;
068: default:
069: jj_la1[1] = jj_gen;
070: jj_consume_token(-1);
071: throw new ParseException();
072: }
073: }
074: jj_consume_token(0);
075: {
076: if (true)
077: return pe;
078: }
079: throw new Error("Missing return statement in function");
080: }
081:
082: final public LiteralTerm parseLiteralTerm() throws ParseException {
083: Token t = null;
084: LiteralTerm ls = null;
085: t = jj_consume_token(NON_EXPRESSION_TEXT);
086: ls = new LiteralTerm(t.image);
087: {
088: if (true)
089: return ls;
090: }
091: throw new Error("Missing return statement in function");
092: }
093:
094: final public ExpressionTerm parseExpression() throws ParseException {
095: ExpressionTerm expr = new ExpressionTerm();
096: ExpressionToken eTok = null;
097: eTok = Context();
098: expr.addToken(eTok);
099: label_2: while (true) {
100: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
101: case DOT:
102: case LBRACKET:
103: ;
104: break;
105: default:
106: jj_la1[2] = jj_gen;
107: break label_2;
108: }
109: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
110: case DOT:
111: jj_consume_token(DOT);
112: eTok = ExprIdentifier();
113: break;
114: default:
115: jj_la1[3] = jj_gen;
116: if (jj_2_1(2)) {
117: eTok = MapKey();
118: } else {
119: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
120: case LBRACKET:
121: eTok = ArrayIndex();
122: break;
123: default:
124: jj_la1[4] = jj_gen;
125: jj_consume_token(-1);
126: throw new ParseException();
127: }
128: }
129: }
130: {
131: expr.addToken(eTok);
132: }
133: }
134: expr.seal();
135: {
136: if (true)
137: return expr;
138: }
139: throw new Error("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)
150: return eTok;
151: }
152: throw new Error("Missing return statement in function");
153: }
154:
155: final public ExpressionToken ExprIdentifier() throws ParseException {
156: Token t = null;
157: ExpressionToken eTok = null;
158: t = jj_consume_token(IDENTIFIER);
159: //System.out.println("** Parser found identifier: " + t.image);
160: eTok = new IdentifierToken(t.image);
161: {
162: if (true)
163: return eTok;
164: }
165: throw new Error("Missing return statement in function");
166: }
167:
168: // handle text inside of map braces as ["..."] or ['...']
169: final public ExpressionToken MapKey() throws ParseException {
170: Token t = null;
171: ExpressionToken eTok = null;
172: jj_consume_token(LBRACKET);
173: t = jj_consume_token(STRING_LITERAL);
174: jj_consume_token(RBRACKET);
175: eTok = new MapKeyToken(t.image);
176: {
177: if (true)
178: return eTok;
179: }
180: throw new Error("Missing return statement in function");
181: }
182:
183: final public ExpressionToken ArrayIndex() throws ParseException {
184: Token t = null;
185: ExpressionToken eTok = null;
186: jj_consume_token(LBRACKET);
187: t = jj_consume_token(INTEGER);
188: jj_consume_token(RBRACKET);
189: //System.out.println("** Parser found array index: " + t.image);
190: eTok = new ArrayIndexToken(t.image);
191: {
192: if (true)
193: return eTok;
194: }
195: throw new Error("Missing return statement in function");
196: }
197:
198: final private boolean jj_2_1(int xla) {
199: jj_la = xla;
200: jj_lastpos = jj_scanpos = token;
201: boolean retval = !jj_3_1();
202: jj_save(0, xla);
203: return retval;
204: }
205:
206: final private boolean jj_3_1() {
207: if (jj_3R_3())
208: return true;
209: if (jj_la == 0 && jj_scanpos == jj_lastpos)
210: return false;
211: return false;
212: }
213:
214: final private boolean jj_3R_3() {
215: if (jj_scan_token(LBRACKET))
216: return true;
217: if (jj_la == 0 && jj_scanpos == jj_lastpos)
218: return false;
219: if (jj_scan_token(STRING_LITERAL))
220: return true;
221: if (jj_la == 0 && jj_scanpos == jj_lastpos)
222: return false;
223: return false;
224: }
225:
226: public NetUIELParserTokenManager token_source;
227: SimpleCharStream jj_input_stream;
228: public Token token, jj_nt;
229: private int jj_ntk;
230: private Token jj_scanpos, jj_lastpos;
231: private int jj_la;
232: public boolean lookingAhead = false;
233: private boolean jj_semLA;
234: private int jj_gen;
235: final private int[] jj_la1 = new int[5];
236: static private int[] jj_la1_0;
237:
238: static {
239: jj_la1_0();
240: }
241:
242: private static void jj_la1_0() {
243: jj_la1_0 = new int[] { 0xe, 0xe, 0x12000, 0x2000, 0x10000, };
244: }
245:
246: final private JJCalls[] jj_2_rtns = new JJCalls[1];
247: private boolean jj_rescan = false;
248: private int jj_gc = 0;
249:
250: public NetUIELParser(java.io.InputStream stream) {
251: jj_input_stream = new SimpleCharStream(stream, 1, 1);
252: token_source = new NetUIELParserTokenManager(jj_input_stream);
253: token = new Token();
254: jj_ntk = -1;
255: jj_gen = 0;
256: for (int i = 0; i < 5; i++)
257: jj_la1[i] = -1;
258: for (int i = 0; i < jj_2_rtns.length; i++)
259: jj_2_rtns[i] = new JJCalls();
260: }
261:
262: public void ReInit(java.io.InputStream stream) {
263: jj_input_stream.ReInit(stream, 1, 1);
264: token_source.ReInit(jj_input_stream);
265: token = new Token();
266: jj_ntk = -1;
267: jj_gen = 0;
268: for (int i = 0; i < 5; i++)
269: jj_la1[i] = -1;
270: for (int i = 0; i < jj_2_rtns.length; i++)
271: jj_2_rtns[i] = new JJCalls();
272: }
273:
274: public NetUIELParser(java.io.Reader stream) {
275: jj_input_stream = new SimpleCharStream(stream, 1, 1);
276: token_source = new NetUIELParserTokenManager(jj_input_stream);
277: token = new Token();
278: jj_ntk = -1;
279: jj_gen = 0;
280: for (int i = 0; i < 5; i++)
281: jj_la1[i] = -1;
282: for (int i = 0; i < jj_2_rtns.length; i++)
283: jj_2_rtns[i] = new JJCalls();
284: }
285:
286: public void ReInit(java.io.Reader stream) {
287: jj_input_stream.ReInit(stream, 1, 1);
288: token_source.ReInit(jj_input_stream);
289: token = new Token();
290: jj_ntk = -1;
291: jj_gen = 0;
292: for (int i = 0; i < 5; i++)
293: jj_la1[i] = -1;
294: for (int i = 0; i < jj_2_rtns.length; i++)
295: jj_2_rtns[i] = new JJCalls();
296: }
297:
298: public NetUIELParser(NetUIELParserTokenManager tm) {
299: token_source = tm;
300: token = new Token();
301: jj_ntk = -1;
302: jj_gen = 0;
303: for (int i = 0; i < 5; i++)
304: jj_la1[i] = -1;
305: for (int i = 0; i < jj_2_rtns.length; i++)
306: jj_2_rtns[i] = new JJCalls();
307: }
308:
309: public void ReInit(NetUIELParserTokenManager tm) {
310: token_source = tm;
311: token = new Token();
312: jj_ntk = -1;
313: jj_gen = 0;
314: for (int i = 0; i < 5; i++)
315: jj_la1[i] = -1;
316: for (int i = 0; i < jj_2_rtns.length; i++)
317: jj_2_rtns[i] = new JJCalls();
318: }
319:
320: final private Token jj_consume_token(int kind)
321: throws ParseException {
322: Token oldToken;
323: if ((oldToken = token).next != null)
324: token = token.next;
325: else
326: token = token.next = token_source.getNextToken();
327: jj_ntk = -1;
328: if (token.kind == kind) {
329: jj_gen++;
330: if (++jj_gc > 100) {
331: jj_gc = 0;
332: for (int i = 0; i < jj_2_rtns.length; i++) {
333: JJCalls c = jj_2_rtns[i];
334: while (c != null) {
335: if (c.gen < jj_gen)
336: c.first = null;
337: c = c.next;
338: }
339: }
340: }
341: return token;
342: }
343: token = oldToken;
344: jj_kind = kind;
345: throw generateParseException();
346: }
347:
348: final private boolean jj_scan_token(int kind) {
349: if (jj_scanpos == jj_lastpos) {
350: jj_la--;
351: if (jj_scanpos.next == null) {
352: jj_lastpos = jj_scanpos = jj_scanpos.next = token_source
353: .getNextToken();
354: } else {
355: jj_lastpos = jj_scanpos = jj_scanpos.next;
356: }
357: } else {
358: jj_scanpos = jj_scanpos.next;
359: }
360: if (jj_rescan) {
361: int i = 0;
362: Token tok = token;
363: while (tok != null && tok != jj_scanpos) {
364: i++;
365: tok = tok.next;
366: }
367: if (tok != null)
368: jj_add_error_token(kind, i);
369: }
370: return (jj_scanpos.kind != kind);
371: }
372:
373: final public Token getNextToken() {
374: if (token.next != null)
375: token = token.next;
376: else
377: token = token.next = token_source.getNextToken();
378: jj_ntk = -1;
379: jj_gen++;
380: return token;
381: }
382:
383: final public Token getToken(int index) {
384: Token t = lookingAhead ? jj_scanpos : token;
385: for (int i = 0; i < index; i++) {
386: if (t.next != null)
387: t = t.next;
388: else
389: t = t.next = token_source.getNextToken();
390: }
391: return t;
392: }
393:
394: final private int jj_ntk() {
395: if ((jj_nt = token.next) == null)
396: return (jj_ntk = (token.next = token_source.getNextToken()).kind);
397: else
398: return (jj_ntk = jj_nt.kind);
399: }
400:
401: private java.util.Vector jj_expentries = new java.util.Vector();
402: private int[] jj_expentry;
403: private int jj_kind = -1;
404: private int[] jj_lasttokens = new int[100];
405: private int jj_endpos;
406:
407: private void jj_add_error_token(int kind, int pos) {
408: if (pos >= 100)
409: return;
410: if (pos == jj_endpos + 1) {
411: jj_lasttokens[jj_endpos++] = kind;
412: } else if (jj_endpos != 0) {
413: jj_expentry = new int[jj_endpos];
414: for (int i = 0; i < jj_endpos; i++) {
415: jj_expentry[i] = jj_lasttokens[i];
416: }
417: boolean exists = false;
418: for (java.util.Enumeration e = jj_expentries.elements(); e
419: .hasMoreElements();) {
420: int[] oldentry = (int[]) (e.nextElement());
421: if (oldentry.length == jj_expentry.length) {
422: exists = true;
423: for (int i = 0; i < jj_expentry.length; i++) {
424: if (oldentry[i] != jj_expentry[i]) {
425: exists = false;
426: break;
427: }
428: }
429: if (exists)
430: break;
431: }
432: }
433: if (!exists)
434: jj_expentries.addElement(jj_expentry);
435: if (pos != 0)
436: jj_lasttokens[(jj_endpos = pos) - 1] = kind;
437: }
438: }
439:
440: public ParseException generateParseException() {
441: jj_expentries.removeAllElements();
442: boolean[] la1tokens = new boolean[18];
443: for (int i = 0; i < 18; i++) {
444: la1tokens[i] = false;
445: }
446: if (jj_kind >= 0) {
447: la1tokens[jj_kind] = true;
448: jj_kind = -1;
449: }
450: for (int i = 0; i < 5; i++) {
451: if (jj_la1[i] == jj_gen) {
452: for (int j = 0; j < 32; j++) {
453: if ((jj_la1_0[i] & (1 << j)) != 0) {
454: la1tokens[j] = true;
455: }
456: }
457: }
458: }
459: for (int i = 0; i < 18; i++) {
460: if (la1tokens[i]) {
461: jj_expentry = new int[1];
462: jj_expentry[0] = i;
463: jj_expentries.addElement(jj_expentry);
464: }
465: }
466: jj_endpos = 0;
467: jj_rescan_token();
468: jj_add_error_token(0, 0);
469: int[][] exptokseq = new int[jj_expentries.size()][];
470: for (int i = 0; i < jj_expentries.size(); i++) {
471: exptokseq[i] = (int[]) jj_expentries.elementAt(i);
472: }
473: return new ParseException(token, exptokseq, tokenImage);
474: }
475:
476: final public void enable_tracing() {
477: }
478:
479: final public void disable_tracing() {
480: }
481:
482: final private void jj_rescan_token() {
483: jj_rescan = true;
484: for (int i = 0; i < 1; i++) {
485: JJCalls p = jj_2_rtns[i];
486: do {
487: if (p.gen > jj_gen) {
488: jj_la = p.arg;
489: jj_lastpos = jj_scanpos = p.first;
490: switch (i) {
491: case 0:
492: jj_3_1();
493: break;
494: }
495: }
496: p = p.next;
497: } while (p != null);
498: }
499: jj_rescan = false;
500: }
501:
502: final private void jj_save(int index, int xla) {
503: JJCalls p = jj_2_rtns[index];
504: while (p.gen > jj_gen) {
505: if (p.next == null) {
506: p = p.next = new JJCalls();
507: break;
508: }
509: p = p.next;
510: }
511: p.gen = jj_gen + xla - jj_la;
512: p.first = token;
513: p.arg = xla;
514: }
515:
516: static final class JJCalls {
517:
518: int gen;
519: Token first;
520: int arg;
521: JJCalls next;
522: }
523:
524: }
|