001    /* 
002     * Copyright (c) Holger Pfaff - http://pfaff.ws
003     *
004     * This software maybe used for any purpose provided the
005     * above copyright notice is retained. It is supplied as is.
006     * No warranty expressed or implied - Use at your own risk.
007     */
008    
009    import java.util.Stack;
010    
011    /**
012      * (#)Parser.java
013      * @author   Holger Pfaff
014      * @version  3.2 19-Mar-2004<br><br> 
015      *
016      * Parse texts for variables e.g. ${var}
017      */
018    
019    /**
020     * This class is a scanner generated by 
021     * <a href="http://www.jflex.de/">JFlex</a> 1.3.5
022     */
023    public class Parser {
024    
025      /** This character denotes the end of file */
026      final public static int YYEOF = -1;
027    
028      /** initial size of the lookahead buffer */
029      final private static int YY_BUFFERSIZE = 16384;
030    
031      /** lexical states */
032      final public static int YYINITIAL = 0;
033    
034      /** 
035       * Translates characters to character classes
036       */
037      final private static String yycmap_packed = 
038        "\11\2\1\0\1\0\3\0\16\2\10\0\1\4\11\0\1\2\1\0"+
039        "\12\2\7\0\32\1\1\0\1\3\2\0\1\1\1\0\32\1\1\5"+
040        "\1\0\1\6\1\0\41\2\2\0\4\1\4\0\1\1\12\0\1\1"+
041        "\4\0\1\1\5\0\27\1\1\0\37\1\1\0\376\1\4\0\36\1"+
042        "\70\0\131\1\7\0\11\1\2\0\7\1\16\0\2\1\16\0\5\1"+
043        "\33\0\106\2\32\0\2\2\30\0\1\1\13\0\1\1\1\0\3\1"+
044        "\1\0\1\1\1\0\24\1\1\0\54\1\1\0\7\1\3\0\1\1"+
045        "\1\0\1\1\1\0\1\1\1\0\1\1\1\0\22\1\15\0\14\1"+
046        "\1\0\102\1\1\0\14\1\1\0\44\1\1\0\4\2\11\0\65\1"+
047        "\2\0\2\1\2\0\2\1\3\0\34\1\2\0\10\1\2\0\2\1"+
048        "\67\0\46\1\2\0\1\1\7\0\47\1\11\0\21\2\1\0\27\2"+
049        "\1\0\3\2\1\0\1\2\1\0\2\2\1\0\1\2\13\0\33\1"+
050        "\5\0\3\1\56\0\32\1\5\0\13\1\10\2\15\0\12\2\6\0"+
051        "\1\2\107\1\2\0\5\1\1\0\17\1\1\0\4\1\1\0\1\1"+
052        "\7\2\2\0\6\2\2\1\2\2\1\0\4\2\2\0\12\2\u0207\0"+
053        "\3\2\1\0\65\1\2\0\1\2\1\1\20\2\3\0\4\2\3\0"+
054        "\12\1\2\2\2\0\12\2\21\0\3\2\1\0\10\1\2\0\2\1"+
055        "\2\0\26\1\1\0\7\1\1\0\1\1\3\0\4\1\2\0\1\2"+
056        "\1\0\7\2\2\0\2\2\2\0\3\2\11\0\1\2\4\0\2\1"+
057        "\1\0\3\1\2\2\2\0\12\2\4\1\16\0\1\2\2\0\6\1"+
058        "\4\0\2\1\2\0\26\1\1\0\7\1\1\0\2\1\1\0\2\1"+
059        "\1\0\2\1\2\0\1\2\1\0\5\2\4\0\2\2\2\0\3\2"+
060        "\13\0\4\1\1\0\1\1\7\0\14\2\3\1\14\0\3\2\1\0"+
061        "\7\1\1\0\1\1\1\0\3\1\1\0\26\1\1\0\7\1\1\0"+
062        "\2\1\1\0\5\1\2\0\1\2\1\1\10\2\1\0\3\2\1\0"+
063        "\3\2\22\0\1\1\5\0\12\2\21\0\3\2\1\0\10\1\2\0"+
064        "\2\1\2\0\26\1\1\0\7\1\1\0\2\1\2\0\4\1\2\0"+
065        "\1\2\1\1\6\2\3\0\2\2\2\0\3\2\10\0\2\2\4\0"+
066        "\2\1\1\0\3\1\4\0\12\2\22\0\2\2\1\0\6\1\3\0"+
067        "\3\1\1\0\4\1\3\0\2\1\1\0\1\1\1\0\2\1\3\0"+
068        "\2\1\3\0\3\1\3\0\10\1\1\0\3\1\4\0\5\2\3\0"+
069        "\3\2\1\0\4\2\11\0\1\2\17\0\11\2\21\0\3\2\1\0"+
070        "\10\1\1\0\3\1\1\0\27\1\1\0\12\1\1\0\5\1\4\0"+
071        "\7\2\1\0\3\2\1\0\4\2\7\0\2\2\11\0\2\1\4\0"+
072        "\12\2\22\0\2\2\1\0\10\1\1\0\3\1\1\0\27\1\1\0"+
073        "\12\1\1\0\5\1\4\0\7\2\1\0\3\2\1\0\4\2\7\0"+
074        "\2\2\7\0\1\1\1\0\2\1\4\0\12\2\22\0\2\2\1\0"+
075        "\10\1\1\0\3\1\1\0\27\1\1\0\20\1\4\0\6\2\2\0"+
076        "\3\2\1\0\4\2\11\0\1\2\10\0\2\1\4\0\12\2\221\0"+
077        "\56\1\1\0\1\1\1\2\2\1\7\2\4\0\10\1\10\2\1\0"+
078        "\12\2\47\0\2\1\1\0\1\1\2\0\2\1\1\0\1\1\2\0"+
079        "\1\1\6\0\4\1\1\0\7\1\1\0\3\1\1\0\1\1\1\0"+
080        "\1\1\2\0\2\1\1\0\2\1\1\0\1\1\1\2\2\1\6\2"+
081        "\1\0\2\2\1\1\2\0\5\1\1\0\1\1\1\0\6\2\2\0"+
082        "\12\2\2\0\2\1\72\0\2\2\6\0\12\2\13\0\1\2\1\0"+
083        "\1\2\1\0\1\2\4\0\2\2\10\1\1\0\41\1\7\0\24\2"+
084        "\1\0\6\2\4\0\6\2\1\0\1\2\1\0\25\2\3\0\7\2"+
085        "\1\0\1\2\346\0\46\1\12\0\47\1\11\0\132\1\5\0\104\1"+
086        "\5\0\122\1\u0c06\0\234\1\4\0\132\1\6\0\26\1\2\0\6\1"+
087        "\2\0\46\1\2\0\6\1\2\0\10\1\1\0\1\1\1\0\1\1"+
088        "\1\0\1\1\1\0\37\1\2\0\65\1\1\0\7\1\1\0\1\1"+
089        "\3\0\3\1\1\0\7\1\3\0\4\1\2\0\6\1\4\0\15\1"+
090        "\5\0\3\1\1\0\7\1\17\0\4\2\32\0\5\2\20\0\2\1"+
091        "\51\0\6\2\17\0\1\1\40\0\15\1\43\0\15\2\4\0\1\2"+
092        "\40\0\1\1\4\0\1\1\2\0\12\1\1\0\1\1\2\0\6\1"+
093        "\6\0\1\1\1\0\1\1\1\0\1\1\1\0\10\1\1\0\6\1"+
094        "\47\0\43\1\u0e82\0\1\1\1\0\1\1\31\0\11\1\6\2\1\0"+
095        "\5\1\13\0\124\1\4\0\2\2\4\1\2\0\132\1\1\0\3\1"+
096        "\6\0\50\1\4\0\136\1\u1c71\0\u51a6\1\u0c5a\0\u2ba4\1\u215c\0\u012e\1"+
097        "\322\0\7\1\14\0\5\1\6\0\1\2\12\1\1\0\15\1\1\0"+
098        "\5\1\1\0\1\1\1\0\2\1\1\0\2\1\1\0\154\1\41\0"+
099        "\u016b\1\22\0\100\1\2\0\66\1\50\0\14\1\44\0\4\2\17\0"+
100        "\2\1\30\0\3\1\31\0\1\1\6\0\3\1\1\0\1\1\1\0"+
101        "\207\1\2\0\1\2\4\0\1\1\13\0\12\2\7\0\32\1\4\0"+
102        "\1\1\1\0\32\1\13\0\131\1\3\0\6\1\2\0\6\1\2\0"+
103        "\6\1\2\0\3\1\3\0\2\1\3\0\2\1\31\0";
104    
105      /** 
106       * Translates characters to character classes
107       */
108      final private static char [] yycmap = yy_unpack_cmap(yycmap_packed);
109    
110    
111      /* error codes */
112      final private static int YY_UNKNOWN_ERROR = 0;
113      final private static int YY_ILLEGAL_STATE = 1;
114      final private static int YY_NO_MATCH = 2;
115      final private static int YY_PUSHBACK_2BIG = 3;
116    
117      /* error messages for the codes above */
118      final private static String YY_ERROR_MSG[] = {
119        "Unkown internal scanner error",
120        "Internal error: unknown state",
121        "Error: could not match input",
122        "Error: pushback value was too large"
123      };
124    
125      /** the input device */
126      private java.io.Reader yy_reader;
127    
128      /** the current state of the DFA */
129      private int yy_state;
130    
131      /** the current lexical state */
132      private int yy_lexical_state = YYINITIAL;
133    
134      /** this buffer contains the current text to be matched and is
135          the source of the yytext() string */
136      private char yy_buffer[] = new char[YY_BUFFERSIZE];
137    
138      /** the textposition at the last accepting state */
139      private int yy_markedPos;
140    
141      /** the textposition at the last state to be included in yytext */
142      private int yy_pushbackPos;
143    
144      /** the current text position in the buffer */
145      private int yy_currentPos;
146    
147      /** startRead marks the beginning of the yytext() string in the buffer */
148      private int yy_startRead;
149    
150      /** endRead marks the last character in the buffer, that has been read
151          from input */
152      private int yy_endRead;
153    
154      /** number of newlines encountered up to the start of the matched text */
155      private int yyline;
156    
157      /** the number of characters up to the start of the matched text */
158      private int yychar;
159    
160      /**
161       * the number of characters from the last newline up to the start of the 
162       * matched text
163       */
164      private int yycolumn; 
165    
166      /** 
167       * yy_atBOL == true <=> the scanner is currently at the beginning of a line
168       */
169      private boolean yy_atBOL = true;
170    
171      /** yy_atEOF == true <=> the scanner is at the EOF */
172      private boolean yy_atEOF;
173    
174      /* user code: */
175      private int count;
176      private String input = null;
177      private String result = null;
178      private ParserListener listener = null;
179      
180      private static final int MAXDEPTH = 10;
181      private static Stack stack = Parser.initStack();
182      private static Stack initStack() {
183        Stack s = new Stack();
184        for(int i = 0; i < MAXDEPTH; ++i) {
185          s.push(new Parser());
186        }
187        return s;
188      }
189      
190      private String doreplace(String input, ParserListener listener) throws Exception {
191        this.count = 0;
192        this.result = "";
193        this.input = input;
194        this.listener = listener;
195        yyreset(new java.io.StringReader(input));
196        while ( yy_atEOF == false ) yylex();
197        return result;
198      }
199      
200      private void dovariable(String v) {
201        String r = listener.getString(v);
202        if(r == null) {
203          listener.parseError("Unknown variable: $" + v + "\n" + input);
204        } else {
205          result += r; ++count;
206        }
207      }
208      
209      private Parser() {};
210      
211      public static String replace(String input, ParserListener listener) {
212        Parser p = null;
213        try {
214          if(input.indexOf('$') > -1) {
215            p = (Parser) stack.pop();
216            do {
217              input = p.doreplace(input, listener);
218            } while(p.count > 0);
219          }
220        } catch (Exception x) {
221          listener.parseError(x.toString() + "\n" + input);
222        }
223        if(p != null) { 
224          stack.push(p);
225        }
226        return input;
227      }
228    
229    
230      /**
231       * Creates a new scanner
232       * There is also a java.io.InputStream version of this constructor.
233       *
234       * @param   in  the java.io.Reader to read input from.
235       */
236      public Parser(java.io.Reader in) {
237        this.yy_reader = in;
238      }
239    
240      /**
241       * Creates a new scanner.
242       * There is also java.io.Reader version of this constructor.
243       *
244       * @param   in  the java.io.Inputstream to read input from.
245       */
246      public Parser(java.io.InputStream in) {
247        this(new java.io.InputStreamReader(in));
248      }
249    
250      /** 
251       * Unpacks the compressed character translation table.
252       *
253       * @param packed   the packed character translation table
254       * @return         the unpacked character translation table
255       */
256      private static char [] yy_unpack_cmap(String packed) {
257        char [] map = new char[0x10000];
258        int i = 0;  /* index in packed string  */
259        int j = 0;  /* index in unpacked array */
260        while (i < 1316) {
261          int  count = packed.charAt(i++);
262          char value = packed.charAt(i++);
263          do map[j++] = value; while (--count > 0);
264        }
265        return map;
266      }
267    
268    
269      /**
270       * Refills the input buffer.
271       *
272       * @return      <code>false</code>, iff there was new input.
273       * 
274       * @exception   IOException  if any I/O-Error occurs
275       */
276      private boolean yy_refill() throws java.io.IOException {
277    
278        /* first: make room (if you can) */
279        if (yy_startRead > 0) {
280          System.arraycopy(yy_buffer, yy_startRead, 
281                           yy_buffer, 0, 
282                           yy_endRead-yy_startRead);
283    
284          /* translate stored positions */
285          yy_endRead-= yy_startRead;
286          yy_currentPos-= yy_startRead;
287          yy_markedPos-= yy_startRead;
288          yy_pushbackPos-= yy_startRead;
289          yy_startRead = 0;
290        }
291    
292        /* is the buffer big enough? */
293        if (yy_currentPos >= yy_buffer.length) {
294          /* if not: blow it up */
295          char newBuffer[] = new char[yy_currentPos*2];
296          System.arraycopy(yy_buffer, 0, newBuffer, 0, yy_buffer.length);
297          yy_buffer = newBuffer;
298        }
299    
300        /* finally: fill the buffer with new input */
301        int numRead = yy_reader.read(yy_buffer, yy_endRead, 
302                                                yy_buffer.length-yy_endRead);
303    
304        if (numRead < 0) {
305          return true;
306        }
307        else {
308          yy_endRead+= numRead;  
309          return false;
310        }
311      }
312    
313    
314      /**
315       * Closes the input stream.
316       */
317      final public void yyclose() throws java.io.IOException {
318        yy_atEOF = true;            /* indicate end of file */
319        yy_endRead = yy_startRead;  /* invalidate buffer    */
320    
321        if (yy_reader != null)
322          yy_reader.close();
323      }
324    
325    
326      /**
327       * Closes the current stream, and resets the
328       * scanner to read from a new input stream.
329       *
330       * All internal variables are reset, the old input stream 
331       * <b>cannot</b> be reused (internal buffer is discarded and lost).
332       * Lexical state is set to <tt>YY_INITIAL</tt>.
333       *
334       * @param reader   the new input stream 
335       */
336      final public void yyreset(java.io.Reader reader) throws java.io.IOException {
337        yyclose();
338        yy_reader = reader;
339        yy_atBOL  = true;
340        yy_atEOF  = false;
341        yy_endRead = yy_startRead = 0;
342        yy_currentPos = yy_markedPos = yy_pushbackPos = 0;
343        yyline = yychar = yycolumn = 0;
344        yy_lexical_state = YYINITIAL;
345      }
346    
347    
348      /**
349       * Returns the current lexical state.
350       */
351      final public int yystate() {
352        return yy_lexical_state;
353      }
354    
355    
356      /**
357       * Enters a new lexical state
358       *
359       * @param newState the new lexical state
360       */
361      final public void yybegin(int newState) {
362        yy_lexical_state = newState;
363      }
364    
365    
366      /**
367       * Returns the text matched by the current regular expression.
368       */
369      final public String yytext() {
370        return new String( yy_buffer, yy_startRead, yy_markedPos-yy_startRead );
371      }
372    
373    
374      /**
375       * Returns the character at position <tt>pos</tt> from the 
376       * matched text. 
377       * 
378       * It is equivalent to yytext().charAt(pos), but faster
379       *
380       * @param pos the position of the character to fetch. 
381       *            A value from 0 to yylength()-1.
382       *
383       * @return the character at position pos
384       */
385      final public char yycharat(int pos) {
386        return yy_buffer[yy_startRead+pos];
387      }
388    
389    
390      /**
391       * Returns the length of the matched text region.
392       */
393      final public int yylength() {
394        return yy_markedPos-yy_startRead;
395      }
396    
397    
398      /**
399       * Reports an error that occured while scanning.
400       *
401       * In a wellformed scanner (no or only correct usage of 
402       * yypushback(int) and a match-all fallback rule) this method 
403       * will only be called with things that "Can't Possibly Happen".
404       * If this method is called, something is seriously wrong
405       * (e.g. a JFlex bug producing a faulty scanner etc.).
406       *
407       * Usual syntax/scanner level error handling should be done
408       * in error fallback rules.
409       *
410       * @param   errorCode  the code of the errormessage to display
411       */
412      private void yy_ScanError(int errorCode) {
413        String message;
414        try {
415          message = YY_ERROR_MSG[errorCode];
416        }
417        catch (ArrayIndexOutOfBoundsException e) {
418          message = YY_ERROR_MSG[YY_UNKNOWN_ERROR];
419        }
420    
421        throw new Error(message);
422      } 
423    
424    
425      /**
426       * Pushes the specified amount of characters back into the input stream.
427       *
428       * They will be read again by then next call of the scanning method
429       *
430       * @param number  the number of characters to be read again.
431       *                This number must not be greater than yylength()!
432       */
433      private void yypushback(int number)  {
434        if ( number > yylength() )
435          yy_ScanError(YY_PUSHBACK_2BIG);
436    
437        yy_markedPos -= number;
438      }
439    
440    
441      /**
442       * Resumes scanning until the next regular expression is matched,
443       * the end of input is encountered or an I/O-Error occurs.
444       *
445       * @return      the next token
446       * @exception   IOException  if any I/O-Error occurs
447       */
448      public int yylex() throws java.io.IOException {
449        int yy_input;
450        int yy_action;
451    
452        // cached fields:
453        int yy_currentPos_l;
454        int yy_startRead_l;
455        int yy_markedPos_l;
456        int yy_endRead_l = yy_endRead;
457        char [] yy_buffer_l = yy_buffer;
458        char [] yycmap_l = yycmap;
459    
460    
461        while (true) {
462          yy_markedPos_l = yy_markedPos;
463    
464          yy_action = -1;
465    
466          yy_startRead_l = yy_currentPos_l = yy_currentPos = 
467                           yy_startRead = yy_markedPos_l;
468    
469          yy_state = yy_lexical_state;
470    
471    
472          yy_forAction: {
473            while (true) {
474    
475              if (yy_currentPos_l < yy_endRead_l)
476                yy_input = yy_buffer_l[yy_currentPos_l++];
477              else if (yy_atEOF) {
478                yy_input = YYEOF;
479                break yy_forAction;
480              }
481              else {
482                // store back cached positions
483                yy_currentPos  = yy_currentPos_l;
484                yy_markedPos   = yy_markedPos_l;
485                boolean eof = yy_refill();
486                // get translated positions and possibly new buffer
487                yy_currentPos_l  = yy_currentPos;
488                yy_markedPos_l   = yy_markedPos;
489                yy_buffer_l      = yy_buffer;
490                yy_endRead_l     = yy_endRead;
491                if (eof) {
492                  yy_input = YYEOF;
493                  break yy_forAction;
494                }
495                else {
496                  yy_input = yy_buffer_l[yy_currentPos_l++];
497                }
498              }
499              yy_input = yycmap_l[yy_input];
500    
501              boolean yy_isFinal = false;
502              boolean yy_noLookAhead = false;
503    
504              yy_forNext: { switch (yy_state) {
505                case 0:
506                  switch (yy_input) {
507                    case 3: yy_isFinal = true; yy_state = 2; break yy_forNext;
508                    case 4: yy_isFinal = true; yy_state = 3; break yy_forNext;
509                    default: yy_isFinal = true; yy_noLookAhead = true; yy_state = 1; break yy_forNext;
510                  }
511    
512                case 2:
513                  switch (yy_input) {
514                    case 4: yy_state = 4; break yy_forNext;
515                    default: break yy_forAction;
516                  }
517    
518                case 3:
519                  switch (yy_input) {
520                    case 5: yy_isFinal = true; yy_state = 5; break yy_forNext;
521                    default: break yy_forAction;
522                  }
523    
524                case 4:
525                  switch (yy_input) {
526                    case 5: yy_isFinal = true; yy_noLookAhead = true; yy_state = 6; break yy_forNext;
527                    default: break yy_forAction;
528                  }
529    
530                case 5:
531                  switch (yy_input) {
532                    case 1: 
533                    case 4: yy_state = 7; break yy_forNext;
534                    default: break yy_forAction;
535                  }
536    
537                case 7:
538                  switch (yy_input) {
539                    case 6: yy_isFinal = true; yy_noLookAhead = true; yy_state = 8; break yy_forNext;
540                    case 0: 
541                    case 3: 
542                    case 5: break yy_forAction;
543                    default: yy_state = 7; break yy_forNext;
544                  }
545    
546                default:
547                  yy_ScanError(YY_ILLEGAL_STATE);
548                  break;
549              } }
550    
551              if ( yy_isFinal ) {
552                yy_action = yy_state; 
553                yy_markedPos_l = yy_currentPos_l; 
554                if ( yy_noLookAhead ) break yy_forAction;
555              }
556    
557            }
558          }
559    
560          // store back cached position
561          yy_markedPos = yy_markedPos_l;
562    
563          switch (yy_action) {
564    
565            case 6: 
566              {  result += "${";  }
567            case 10: break;
568            case 1: 
569            case 2: 
570            case 3: 
571              {  result += yytext();  }
572            case 11: break;
573            case 8: 
574              {  dovariable(yytext().substring(2, yylength() - 1));  }
575            case 12: break;
576            case 5: 
577              {  listener.parseError("Variable Syntax: " + input);  }
578            case 13: break;
579            default: 
580              if (yy_input == YYEOF && yy_startRead == yy_currentPos) {
581                yy_atEOF = true;
582                return YYEOF;
583              } 
584              else {
585                yy_ScanError(YY_NO_MATCH);
586              }
587          }
588        }
589      }
590    }