KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > tools > ant > SQLScriptParser


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdo.tools.ant;
13
14
15 import java.io.*;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Iterator JavaDoc;
18
19 /**
20  * Use this class like this:
21  *
22  * SQLScriptParser shredder = new SQLScriptParser();
23  * ArrayList list = shredder.parse(sql,true);
24  * for (Iterator iter = list.iterator(); iter.hasNext();) {
25  * SQLScriptPart scriptPart = (SQLScriptPart) iter.next();
26  * System.out.println("start = "+ scriptPart.getStart());
27  * System.out.println("end = "+ scriptPart.getEnd());
28  * System.out.println( scriptPart.getSql());
29  * }
30  * @keep-all
31  * SQLScriptParser is a sql lexer. Created with JFlex.
32  * The tokens returned should comply with the sql Language Specification
33  *
34  *
35  */

36 public class SQLScriptParser {
37
38     /** This character denotes the end of file */
39     final public static int YYEOF = -1;
40
41     /** initial size of the lookahead buffer */
42     final private static int YY_BUFFERSIZE = 16384;
43
44     /** lexical states */
45     final public static int YYINITIAL = 0;
46     final public static int COMMENT = 1;
47
48     /**
49      * Translates characters to character classes
50      */

51     final private static char[] yycmap = {
52         0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 13, 0, 4, 13, 0, 0,
53         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
54         4, 10, 6, 10, 8, 10, 10, 12, 9, 9, 11, 19, 9, 20, 17, 21,
55         16, 16, 7, 7, 7, 7, 7, 7, 7, 7, 9, 1, 10, 10, 10, 10,
56         10, 0, 15, 0, 0, 18, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3,
57         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 14, 9, 10, 5,
58         10, 0, 15, 0, 0, 18, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3,
59         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 10, 0,
60         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
68     };
69
70     /**
71      * Translates a state to a row index in the transition table
72      */

73     final private static int yy_rowMap [] = {
74         0, 22, 44, 66, 88, 110, 66, 132, 154, 66,
75         66, 176, 198, 220, 242, 264, 286, 308, 330, 44,
76         352, 374, 154, 176, 66, 66, 396, 418, 440, 66,
77         462, 484, 308, 66, 66, 506, 176, 528, 550, 66,
78         572, 594, 506, 616, 550, 528, 66, 638
79     };
80
81     /**
82      * The packed transition table of the DFA (part 0)
83      */

84     final private static String JavaDoc yy_packed0 =
85             "\1\3\1\4\1\5\1\3\1\6\2\7\1\10\1\11" +
86             "\1\12\1\13\1\12\1\14\1\6\1\3\1\15\1\10" +
87             "\1\16\1\3\1\13\1\17\1\20\13\21\1\22\11\21" +
88             "\1\23\1\3\1\0\2\3\1\0\1\3\1\0\1\3" +
89             "\6\0\3\3\1\0\1\3\31\0\1\3\1\0\1\3" +
90             "\1\24\1\0\1\3\1\0\1\3\6\0\3\3\1\0" +
91             "\1\3\7\0\1\6\10\0\1\6\17\0\1\10\10\0" +
92             "\1\10\1\25\1\26\12\0\1\27\10\0\1\27\5\0" +
93             "\14\30\1\31\1\32\1\33\7\30\1\3\1\0\2\3" +
94             "\1\0\1\3\1\0\1\3\4\0\1\34\1\0\3\3" +
95             "\1\0\1\3\12\0\1\25\10\0\1\25\31\0\1\35" +
96             "\14\0\1\36\12\0\13\21\1\37\11\21\1\40\13\21" +
97             "\1\41\11\21\1\42\13\21\1\43\11\21\1\40\7\0" +
98             "\1\25\10\0\1\25\1\0\1\26\26\0\2\44\1\0" +
99             "\14\30\1\45\1\32\1\33\7\30\14\46\1\47\1\50" +
100             "\1\51\1\46\1\52\5\46\15\35\1\0\10\35\13\21" +
101             "\1\37\11\21\1\0\13\21\1\0\11\21\1\40\7\0" +
102             "\1\53\10\0\1\53\5\0\14\46\1\47\1\50\1\51" +
103             "\1\46\1\54\5\46\14\55\1\47\1\0\2\55\1\0" +
104             "\5\55\14\46\1\56\1\50\1\51\1\46\1\54\5\46" +
105             "\14\54\1\57\1\50\1\60\1\54\1\52\21\54\1\0" +
106             "\1\50\1\60\24\54\1\50\1\60\7\54";
107
108     /**
109      * The transition table of the DFA
110      */

111     final private static int yytrans [] = yy_unpack();
112
113
114     /* error codes */
115     final private static int YY_UNKNOWN_ERROR = 0;
116     final private static int YY_ILLEGAL_STATE = 1;
117     final private static int YY_NO_MATCH = 2;
118     final private static int YY_PUSHBACK_2BIG = 3;
119
120     /* error messages for the codes above */
121     final private static String JavaDoc YY_ERROR_MSG[] = {
122         "Unkown internal scanner error",
123         "Internal error: unknown state",
124         "Error: could not match input",
125         "Error: pushback value was too large"
126     };
127
128     /**
129      * YY_ATTRIBUTE[aState] contains the attributes of state <code>aState</code>
130      */

131     private final static byte YY_ATTRIBUTE[] = {
132         1, 1, 1, 9, 1, 1, 9, 1, 1, 9, 9, 1, 1, 1, 1, 1,
133         1, 1, 1, 1, 1, 0, 1, 0, 9, 9, 0, 0, 1, 9, 0, 0,
134         0, 9, 9, 0, 1, 0, 1, 9, 0, 0, 1, 0, 0, 1, 9, 0
135     };
136
137     /** the input device */
138     private java.io.Reader JavaDoc yy_reader;
139
140     /** the current state of the DFA */
141     private int yy_state;
142
143     /** the current lexical state */
144     private int yy_lexical_state = YYINITIAL;
145
146     /** this buffer contains the current text to be matched and is
147      the source of the yytext() string */

148     private char yy_buffer[] = new char[YY_BUFFERSIZE];
149
150     /** the textposition at the last accepting state */
151     private int yy_markedPos;
152
153     /** the textposition at the last state to be included in yytext */
154     private int yy_pushbackPos;
155
156     /** the current text position in the buffer */
157     private int yy_currentPos;
158
159     /** startRead marks the beginning of the yytext() string in the buffer */
160     private int yy_startRead;
161
162     /** endRead marks the last character in the buffer, that has been read
163      from input */

164     private int yy_endRead;
165
166     /** number of newlines encountered up to the start of the matched text */
167     private int yyline;
168
169     /** the number of characters up to the start of the matched text */
170     private int yychar;
171
172     /**
173      * the number of characters from the last newline up to the start of the
174      * matched text
175      */

176     private int yycolumn;
177
178     /**
179      * yy_atBOL == true <=> the scanner is currently at the beginning of a line
180      */

181     private boolean yy_atBOL = true;
182
183     /** yy_atEOF == true <=> the scanner is at the EOF */
184     private boolean yy_atEOF;
185
186     /* user code: */
187     private int lastToken;
188     private int nextState = YYINITIAL;
189     private StringBuffer JavaDoc commentBuffer = new StringBuffer JavaDoc();
190     private int commentNestCount = 0;
191     private int commentStartLine = 0;
192     private int commentStartChar = 0;
193
194     /**
195      * next Token method that allows you to control if whitespace and comments are
196      * returned as tokens.
197      */

198     public Token getNextToken(boolean returnComments, boolean returnWhiteSpace) throws IOException {
199         Token t = getNextToken();
200         while (t != null && ((!returnWhiteSpace && t.isWhiteSpace()) || (!returnComments && t.isComment()))) {
201             t = getNextToken();
202         }
203         return (t);
204     }
205
206     public SQLScriptParser() {
207     }
208
209     /**
210      * Prints out tokens from a file or System.in.
211      * If no arguments are given, System.in will be used for input.
212      * If more arguments are given, the first argument will be used as
213      * the name of the file to use as input
214      *
215      * @param args program arguments, of which the first is a filename
216      */

217     public static void main(String JavaDoc[] args) {
218         String JavaDoc sql =
219                 "123456789 ;-- kdlfhjkl; sdfghjdl;fkhkdfjghl fghldfkklh flhkdfhj \n" +
220                 " lkh"+
221                 "CREATE TABLE temp_abstract1_a (\n" +
222                 " ';;;;;\\n'" +
223                 " abstract1_id INTEGER NOT NULL, /* <pk> */\n" +
224                 " abs1 VARCHAR(25), /* abs1 */\n" +
225                 " jdo_class INTEGER NOT NULL, /* <class-id> */\n" +
226                 " jdo_version SMALLINT NOT NULL, /* <opt-lock> */\n" +
227                 " abs2 VARCHAR(190), /* Abstract2.abs2 */\n" +
228                 " conc1 VARCHAR(190), /* Concrete1.conc1 */\n" +
229                 " conc2 VARCHAR(190) /* Concrete2.conc2 */\n" +
230                 ");\n" +
231                 "INSERT INTO temp_abstract1_a (abstract1_id, abs1, jdo_class, jdo_version, abs2, conc1, conc2) \n" +
232                 "SELECT abstract1_id, \n" +
233                 " CAST(abs1 AS VARCHAR(25)), \n" +
234                 " jdo_class, \n" +
235                 " jdo_version, \n" +
236                 " abs2, \n" +
237                 " conc1, \n" +
238                 " conc2\n" +
239                 " FROM abstract1;\n" +
240                 "DROP TABLE abstract1;\n" +
241                 "CREATE TABLE abstract1 (\n" +
242                 " abstract1_id INTEGER NOT NULL, /* <pk> */\n" +
243                 " abs1 VARCHAR(25), /* abs1 */\n" +
244                 " jdo_class INTEGER NOT NULL, /* <class-id> */\n" +
245                 " jdo_version SMALLINT NOT NULL, /* <opt-lock> */\n" +
246                 " abs2 VARCHAR(190), /* Abstract2.abs2 */\n" +
247                 " conc1 VARCHAR(190), /* Concrete1.conc1 */\n" +
248                 " conc2 VARCHAR(190), /* Concrete2.conc2 */\n" +
249                 " CONSTRAINT pk_abstract1 PRIMARY KEY (abstract1_id)\n" +
250                 ");\n" +
251                 "INSERT INTO abstract1 (abstract1_id, abs1, jdo_class, jdo_version, abs2, conc1, conc2) \n" +
252                 "SELECT abstract1_id, \n" +
253                 " abs1, \n" +
254                 " jdo_class, \n" +
255                 " jdo_version, \n" +
256                 " abs2, \n" +
257                 " conc1, \n" +
258                 " conc2\n" +
259
260                 " FROM temp_abstract1_a;\n" +
261                 "DROP TABLE temp_abstract1_a;\n" +
262                 "\n" +
263                 "ALTER TABLE emp_super_person3 ADD CONSTRAINT ref_emp_super_person3_person FOREIGN KEY (person_id) REFERENCES person(person_id);\n" +
264                 "\n" +
265                 "ALTER TABLE person3_person ADD CONSTRAINT ref_person3_person_person FOREIGN KEY (person_id) REFERENCES person(person_id);\n" +
266                 "";
267
268         try {
269             SQLScriptParser shredder = new SQLScriptParser();
270             ArrayList JavaDoc list = shredder.parse(sql, true);
271             for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
272                 SQLScriptPart scriptPart = (SQLScriptPart) iter.next();
273                 System.out.println("start = " + scriptPart.getStart());
274                 System.out.println("end = " + scriptPart.getEnd());
275                 System.out.println(scriptPart.getSql());
276             }
277
278
279         } catch (IOException e) {
280             System.out.println(e.getMessage());
281         }
282     }
283
284     public ArrayList JavaDoc parse(String JavaDoc sql, boolean eraseNewLine) throws IOException {
285         StringReader in = new StringReader(sql+ "\n;");
286         return parse(in, eraseNewLine);
287     }
288
289     private ArrayList JavaDoc parse(Reader sql, boolean eraseNewLine) throws IOException {
290         SQLScriptParser shredder = new SQLScriptParser(sql);
291         Token t;
292         boolean first = true;
293         ArrayList JavaDoc list = new ArrayList JavaDoc();
294         SQLScriptPart part = new SQLScriptPart();
295         while ((t = shredder.getNextToken()) != null) {
296
297             switch (t.getID()) {
298                 case Token.WHITE_SPACE:
299                     if (!first) {
300                         if (eraseNewLine) {
301                             part.addSql(' ');
302                         } else {
303                             part.addSql(t.getContents());
304                         }
305                     }
306                     break;
307
308                 case Token.COMMENT_END_OF_LINE:
309                 case Token.COMMENT_TRADITIONAL:
310                     break;
311
312                 case Token.RESERVED_WORD:
313                     if (!first) {
314                         list.add(part);
315                         part = new SQLScriptPart();
316                         first = true;
317                     }
318                     break;
319
320                 default:
321                     if (first) {
322                         part.setStart(t.getCharBegin());
323                         first = false;
324                     }
325                     part.addSql(t.getContents());
326                     part.setEnd(t.getCharEnd());
327                     break;
328             }
329         }
330
331
332         return list;
333     }
334
335     /**
336      * @keep-all
337      */

338     public static class SQLScriptPart {
339         private int start;
340         private int end;
341         private StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
342
343         public int getEnd() {
344             return end;
345         }
346
347         public void setEnd(int end) {
348             this.end = end;
349         }
350
351         public String JavaDoc getSql() {
352             return sql.toString().trim();
353         }
354
355         public void addSql(String JavaDoc sql) {
356             this.sql.append(sql);
357         }
358
359         public void addSql(char sql) {
360             this.sql.append(sql);
361         }
362
363         public int getStart() {
364             return start;
365         }
366
367         public void setStart(int start) {
368             this.start = start;
369         }
370     }
371
372     /**
373      * @keep-all
374      * A Token is a token that is returned by a lexer that is lexing an SQL
375      * source file. It has several attributes describing the token:
376      * The type of token, the text of the token, the line number on which it
377      * occurred, the number of characters into the input at which it started, and
378      * similarly, the number of characters into the input at which it ended. <br>
379      */

380     public static class Token {
381         /**
382          * The state of the tokenizer is undefined.
383          */

384         public static final int UNDEFINED_STATE = -1;
385
386         /**
387          * The initial state of the tokenizer.
388          * Anytime the tokenizer returns to this state,
389          * the tokenizer could be restarted from that point
390          * with side effects.
391          */

392         public static final int INITIAL_STATE = 0;
393         /**
394          * A reserved word (keyword)
395          */

396         public final static int RESERVED_WORD = 0x100;
397
398         /**
399          * A variable, name, or other identifier
400          */

401         public final static int IDENTIFIER = 0x200;
402
403         /**
404          * A string literal
405          */

406         public final static int LITERAL_STRING = 0x300;
407         /**
408          * A bit-string
409          */

410         public final static int LITERAL_BIT_STRING = 0x310;
411         /**
412          * An integer
413          */

414         public final static int LITERAL_INTEGER = 0x320;
415         /**
416          * A floating point
417          */

418         public final static int LITERAL_FLOAT = 0x330;
419
420         /**
421          * A separator
422          */

423         public final static int SEPARATOR = 0x400;
424
425         /**
426          * A separator
427          */

428         public final static int DOT_SEPARATOR = 0x410;
429
430         /**
431          * An operator
432          */

433         public final static int OPERATOR = 0x500;
434
435         /**
436          * C style comment, (except possibly nested)
437          */

438         public final static int COMMENT_TRADITIONAL = 0xD00;
439
440         /**
441          * a -- to end of line comment.
442          */

443         public final static int COMMENT_END_OF_LINE = 0xD10;
444
445         /**
446          * White space
447          */

448         public final static int WHITE_SPACE = 0xE00;
449
450         /**
451          * An error
452          */

453         public final static int ERROR = 0xF00;
454         /**
455          * An comment start embedded in an operator
456          */

457         public final static int ERROR_UNCLOSED_COMMENT = 0xF02;
458         /**
459          * An comment start embedded in an operator
460          */

461         public final static int ERROR_UNCLOSED_STRING = 0xF03;
462         /**
463          * An comment start embedded in an operator
464          */

465         public final static int ERROR_UNCLOSED_BIT_STRING = 0xF04;
466         /**
467          * An comment start embedded in an operator
468          */

469         public final static int ERROR_BAD_BIT_STRING = 0xF05;
470
471         private int ID;
472         private String JavaDoc contents;
473         private int lineNumber;
474         private int charBegin;
475         private int charEnd;
476         private int state;
477
478         /**
479          * Create a new token.
480          * The constructor is typically called by the lexer
481          *
482          * @param ID the id number of the token
483          * @param contents A string representing the text of the token
484          * @param lineNumber the line number of the input on which this token started
485          * @param charBegin the offset into the input in characters at which this token started
486          * @param charEnd the offset into the input in characters at which this token ended
487          */

488         public Token(int ID, String JavaDoc contents, int lineNumber, int charBegin, int charEnd) {
489             this(ID, contents, lineNumber, charBegin, charEnd, Token.UNDEFINED_STATE);
490         }
491
492         /**
493          * Create a new token.
494          * The constructor is typically called by the lexer
495          *
496          * @param ID the id number of the token
497          * @param contents A string representing the text of the token
498          * @param lineNumber the line number of the input on which this token started
499          * @param charBegin the offset into the input in characters at which this token started
500          * @param charEnd the offset into the input in characters at which this token ended
501          * @param state the state the tokenizer is in after returning this token.
502          */

503         public Token(int ID, String JavaDoc contents, int lineNumber, int charBegin, int charEnd, int state) {
504             this.ID = ID;
505             this.contents = new String JavaDoc(contents);
506             this.lineNumber = lineNumber;
507             this.charBegin = charBegin;
508             this.charEnd = charEnd;
509             this.state = state;
510         }
511
512         /**
513          * Get an integer representing the state the tokenizer is in after
514          * returning this token.
515          * Those who are interested in incremental tokenizing for performance
516          * reasons will want to use this method to figure out where the tokenizer
517          * may be restarted. The tokenizer starts in Token.INITIAL_STATE, so
518          * any time that it reports that it has returned to this state, the
519          * tokenizer may be restarted from there.
520          */

521         public int getState() {
522             return state;
523         }
524
525         /**
526          * get the ID number of this token
527          *
528          * @return the id number of the token
529          */

530         public int getID() {
531             return ID;
532         }
533
534         /**
535          * get the contents of this token
536          *
537          * @return A string representing the text of the token
538          */

539         public String JavaDoc getContents() {
540             return (new String JavaDoc(contents));
541         }
542
543         /**
544          * get the line number of the input on which this token started
545          *
546          * @return the line number of the input on which this token started
547          */

548         public int getLineNumber() {
549             return lineNumber;
550         }
551
552         /**
553          * get the offset into the input in characters at which this token started
554          *
555          * @return the offset into the input in characters at which this token started
556          */

557         public int getCharBegin() {
558             return charBegin;
559         }
560
561         /**
562          * get the offset into the input in characters at which this token ended
563          *
564          * @return the offset into the input in characters at which this token ended
565          */

566         public int getCharEnd() {
567             return charEnd;
568         }
569
570         /**
571          * Checks this token to see if it is a reserved word.
572          * Reserved words are explained in <A Href=http://java.sun.com/docs/books/jls/html/>Java
573          * Language Specification</A>.
574          *
575          * @return true if this token is a reserved word, false otherwise
576          */

577         public boolean isReservedWord() {
578             return ((ID >> 8) == 0x1);
579         }
580
581         /**
582          * Checks this token to see if it is an identifier.
583          * Identifiers are explained in <A Href=http://java.sun.com/docs/books/jls/html/>Java
584          * Language Specification</A>.
585          *
586          * @return true if this token is an identifier, false otherwise
587          */

588         public boolean isIdentifier() {
589             return ((ID >> 8) == 0x2);
590         }
591
592         /**
593          * Checks this token to see if it is a literal.
594          * Literals are explained in <A Href=http://java.sun.com/docs/books/jls/html/>Java
595          * Language Specification</A>.
596          *
597          * @return true if this token is a literal, false otherwise
598          */

599         public boolean isLiteral() {
600             return ((ID >> 8) == 0x3);
601         }
602
603         /**
604          * Checks this token to see if it is a Separator.
605          * Separators are explained in <A Href=http://java.sun.com/docs/books/jls/html/>Java
606          * Language Specification</A>.
607          *
608          * @return true if this token is a Separator, false otherwise
609          */

610         public boolean isSeparator() {
611             return ((ID >> 8) == 0x4);
612         }
613
614         /**
615          * Checks this token to see if it is a Operator.
616          * Operators are explained in <A Href=http://java.sun.com/docs/books/jls/html/>Java
617          * Language Specification</A>.
618          *
619          * @return true if this token is a Operator, false otherwise
620          */

621         public boolean isOperator() {
622             return ((ID >> 8) == 0x5);
623         }
624
625         /**
626          * Checks this token to see if it is a comment.
627          *
628          * @return true if this token is a comment, false otherwise
629          */

630         public boolean isComment() {
631             return ((ID >> 8) == 0xD);
632         }
633
634         /**
635          * Checks this token to see if it is White Space.
636          * Usually tabs, line breaks, form feed, spaces, etc.
637          *
638          * @return true if this token is White Space, false otherwise
639          */

640         public boolean isWhiteSpace() {
641             return ((ID >> 8) == 0xE);
642         }
643
644         /**
645          * Checks this token to see if it is an Error.
646          * Unfinished comments, numbers that are too big, unclosed strings, etc.
647          *
648          * @return true if this token is an Error, false otherwise
649          */

650         public boolean isError() {
651             return ((ID >> 8) == 0xF);
652         }
653
654         /**
655          * A description of this token. The description should
656          * be appropriate for syntax highlighting. For example
657          * "comment" is returned for a comment.
658          *
659          * @return a description of this token.
660          */

661         public String JavaDoc getDescription() {
662             if (isReservedWord()) {
663                 return ("reservedWord");
664             } else if (isIdentifier()) {
665                 return ("identifier");
666             } else if (isLiteral()) {
667                 return ("literal");
668             } else if (isSeparator()) {
669                 return ("separator");
670             } else if (isOperator()) {
671                 return ("operator");
672             } else if (isComment()) {
673                 return ("comment");
674             } else if (isWhiteSpace()) {
675                 return ("whitespace");
676             } else if (isError()) {
677                 return ("error");
678             } else {
679                 return ("unknown");
680             }
681         }
682
683         /**
684          * get a String that explains the error, if this token is an error.
685          *
686          * @return a String that explains the error, if this token is an error, null otherwise.
687          */

688         public String JavaDoc errorString() {
689             String JavaDoc s;
690             if (isError()) {
691                 s = "Error on line " + lineNumber + ": ";
692                 switch (ID) {
693                     case ERROR:
694                         s += "Unexpected token: " + contents;
695                         break;
696                     case ERROR_UNCLOSED_COMMENT:
697                         s += "Unclosed comment: " + contents;
698                         break;
699                     case ERROR_UNCLOSED_STRING:
700                         s += "Unclosed string literal: " + contents;
701                         break;
702                     case ERROR_UNCLOSED_BIT_STRING:
703                         s += "Unclosed bit-string literal: " + contents;
704                         break;
705                     case ERROR_BAD_BIT_STRING:
706                         s += "Bit-strings can only contain 0 and 1: " + contents;
707                         break;
708                 }
709
710             } else {
711                 s = null;
712             }
713             return (s);
714         }
715
716         /**
717          * get a representation of this token as a human readable string.
718          * The format of this string is subject to change and should only be used
719          * for debugging purposes.
720          *
721          * @return a string representation of this token
722          */

723         public String JavaDoc toString() {
724             return ("Token #" + Integer.toHexString(ID) + ": " + getDescription() + " Line " +
725                     lineNumber + " from " + charBegin + " to " + charEnd + " : " + contents);
726         }
727     }
728
729
730     /**
731      * Closes the current input stream, and resets the scanner to read from a new input stream.
732      * All internal variables are reset, the old input stream cannot be reused
733      * (content of the internal buffer is discarded and lost).
734      * The lexical state is set to the initial state.
735      * Subsequent tokens read from the lexer will start with the line, char, and column
736      * values given here.
737      *
738      * @param reader The new input.
739      * @param yyline The line number of the first token.
740      * @param yychar The position (relative to the start of the stream) of the first token.
741      * @param yycolumn The position (relative to the line) of the first token.
742      * @throws IOException if an IOExecption occurs while switching readers.
743      */

744     public void reset(java.io.Reader JavaDoc reader, int yyline, int yychar, int yycolumn) throws IOException {
745         yyreset(reader);
746         this.yyline = yyline;
747         this.yychar = yychar;
748         this.yycolumn = yycolumn;
749     }
750
751     public void setReader(java.io.Reader JavaDoc in) {
752         this.yy_reader = in;
753     }
754
755
756     /**
757      * Creates a new scanner
758      * There is also a java.io.InputStream version of this constructor.
759      *
760      * @param in the java.io.Reader to read input from.
761      */

762     public SQLScriptParser(java.io.Reader JavaDoc in) {
763         this.yy_reader = in;
764     }
765
766     /**
767      * Creates a new scanner.
768      * There is also java.io.Reader version of this constructor.
769      *
770      * @param in the java.io.Inputstream to read input from.
771      */

772     public SQLScriptParser(java.io.InputStream JavaDoc in) {
773         this(new java.io.InputStreamReader JavaDoc(in));
774     }
775
776     /**
777      * Unpacks the split, compressed DFA transition table.
778      *
779      * @return the unpacked transition table
780      */

781     private static int[] yy_unpack() {
782         int[] trans = new int[660];
783         int offset = 0;
784         offset = yy_unpack(yy_packed0, offset, trans);
785         return trans;
786     }
787
788     /**
789      * Unpacks the compressed DFA transition table.
790      *
791      * @param packed the packed transition table
792      * @return the index of the last entry
793      */

794     private static int yy_unpack(String JavaDoc packed, int offset, int[] trans) {
795         int i = 0; /* index in packed string */
796         int j = offset; /* index in unpacked array */
797         int l = packed.length();
798         while (i < l) {
799             int count = packed.charAt(i++);
800             int value = packed.charAt(i++);
801             value--;
802             do trans[j++] = value; while (--count > 0);
803         }
804         return j;
805     }
806
807
808     /**
809      * Refills the input buffer.
810      *
811      * @return <code>false</code>, iff there was new input.
812      *
813      * @exception IOException if any I/O-Error occurs
814      */

815     private boolean yy_refill() throws java.io.IOException JavaDoc {
816
817         /* first: make room (if you can) */
818         if (yy_startRead > 0) {
819             System.arraycopy(yy_buffer, yy_startRead,
820                     yy_buffer, 0,
821                     yy_endRead - yy_startRead);
822
823             /* translate stored positions */
824             yy_endRead -= yy_startRead;
825             yy_currentPos -= yy_startRead;
826             yy_markedPos -= yy_startRead;
827             yy_pushbackPos -= yy_startRead;
828             yy_startRead = 0;
829         }
830
831         /* is the buffer big enough? */
832         if (yy_currentPos >= yy_buffer.length) {
833             /* if not: blow it up */
834             char newBuffer[] = new char[yy_currentPos * 2];
835             System.arraycopy(yy_buffer, 0, newBuffer, 0, yy_buffer.length);
836             yy_buffer = newBuffer;
837         }
838
839         /* finally: fill the buffer with new input */
840         int numRead = yy_reader.read(yy_buffer, yy_endRead,
841                 yy_buffer.length - yy_endRead);
842
843         if (numRead < 0) {
844             return true;
845         } else {
846             yy_endRead += numRead;
847             return false;
848         }
849     }
850
851
852     /**
853      * Closes the input stream.
854      */

855     final public void yyclose() throws java.io.IOException JavaDoc {
856         yy_atEOF = true; /* indicate end of file */
857         yy_endRead = yy_startRead; /* invalidate buffer */
858
859         if (yy_reader != null)
860             yy_reader.close();
861     }
862
863
864     /**
865      * Closes the current stream, and resets the
866      * scanner to read from a new input stream.
867      *
868      * All internal variables are reset, the old input stream
869      * <b>cannot</b> be reused (internal buffer is discarded and lost).
870      * Lexical state is set to <tt>YY_INITIAL</tt>.
871      *
872      * @param reader the new input stream
873      */

874     final public void yyreset(java.io.Reader JavaDoc reader) throws java.io.IOException JavaDoc {
875         yyclose();
876         yy_reader = reader;
877         yy_atBOL = true;
878         yy_atEOF = false;
879         yy_endRead = yy_startRead = 0;
880         yy_currentPos = yy_markedPos = yy_pushbackPos = 0;
881         yyline = yychar = yycolumn = 0;
882         yy_lexical_state = YYINITIAL;
883     }
884
885
886     /**
887      * Returns the current lexical state.
888      */

889     final public int yystate() {
890         return yy_lexical_state;
891     }
892
893
894     /**
895      * Enters a new lexical state
896      *
897      * @param newState the new lexical state
898      */

899     final public void yybegin(int newState) {
900         yy_lexical_state = newState;
901     }
902
903
904     /**
905      * Returns the text matched by the current regular expression.
906      */

907     final public String JavaDoc yytext() {
908         return new String JavaDoc(yy_buffer, yy_startRead, yy_markedPos - yy_startRead);
909     }
910
911
912     /**
913      * Returns the character at position <tt>pos</tt> from the
914      * matched text.
915      *
916      * It is equivalent to yytext().charAt(pos), but faster
917      *
918      * @param pos the position of the character to fetch.
919      * A value from 0 to yylength()-1.
920      *
921      * @return the character at position pos
922      */

923     final public char yycharat(int pos) {
924         return yy_buffer[yy_startRead + pos];
925     }
926
927
928     /**
929      * Returns the length of the matched text region.
930      */

931     final public int yylength() {
932         return yy_markedPos - yy_startRead;
933     }
934
935
936     /**
937      * Reports an error that occured while scanning.
938      *
939      * In a wellformed scanner (no or only correct usage of
940      * yypushback(int) and a match-all fallback rule) this method
941      * will only be called with things that "Can't Possibly Happen".
942      * If this method is called, something is seriously wrong
943      * (e.g. a JFlex bug producing a faulty scanner etc.).
944      *
945      * Usual syntax/scanner level error handling should be done
946      * in error fallback rules.
947      *
948      * @param errorCode the code of the errormessage to display
949      */

950     private void yy_ScanError(int errorCode) {
951         String JavaDoc message;
952         try {
953             message = YY_ERROR_MSG[errorCode];
954         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
955             message = YY_ERROR_MSG[YY_UNKNOWN_ERROR];
956         }
957
958         throw new Error JavaDoc(message);
959     }
960
961
962     /**
963      * Pushes the specified amount of characters back into the input stream.
964      *
965      * They will be read again by then next call of the scanning method
966      *
967      * @param number the number of characters to be read again.
968      * This number must not be greater than yylength()!
969      */

970     private void yypushback(int number) {
971         if (number > yylength())
972             yy_ScanError(YY_PUSHBACK_2BIG);
973
974         yy_markedPos -= number;
975     }
976
977
978     /**
979      * Resumes scanning until the next regular expression is matched,
980      * the end of input is encountered or an I/O-Error occurs.
981      *
982      * @return the next token
983      * @exception IOException if any I/O-Error occurs
984      */

985     public Token getNextToken() throws java.io.IOException JavaDoc {
986         int yy_input;
987         int yy_action;
988
989         // cached fields:
990
int yy_currentPos_l;
991         int yy_startRead_l;
992         int yy_markedPos_l;
993         int yy_endRead_l = yy_endRead;
994         char[] yy_buffer_l = yy_buffer;
995         char[] yycmap_l = yycmap;
996
997         int[] yytrans_l = yytrans;
998         int[] yy_rowMap_l = yy_rowMap;
999         byte[] yy_attr_l = YY_ATTRIBUTE;
1000
1001        while (true) {
1002            yy_markedPos_l = yy_markedPos;
1003
1004            yychar += yy_markedPos_l - yy_startRead;
1005
1006            boolean yy_r = false;
1007            for (yy_currentPos_l = yy_startRead; yy_currentPos_l < yy_markedPos_l;
1008                 yy_currentPos_l++) {
1009                switch (yy_buffer_l[yy_currentPos_l]) {
1010                    case '\u000B':
1011                    case '\u000C':
1012                    case '\u0085':
1013                    case '\u2028':
1014                    case '\u2029':
1015                        yyline++;
1016                        yy_r = false;
1017                        break;
1018                    case '\r':
1019                        yyline++;
1020                        yy_r = true;
1021                        break;
1022                    case '\n':
1023                        if (yy_r)
1024                            yy_r = false;
1025                        else {
1026                            yyline++;
1027                        }
1028                        break;
1029                    default:
1030                        yy_r = false;
1031                }
1032            }
1033
1034            if (yy_r) {
1035                // peek one character ahead if it is \n (if we have counted one line too much)
1036
boolean yy_peek;
1037                if (yy_markedPos_l < yy_endRead_l)
1038                    yy_peek = yy_buffer_l[yy_markedPos_l] == '\n';
1039                else if (yy_atEOF)
1040                    yy_peek = false;
1041                else {
1042                    boolean eof = yy_refill();
1043                    yy_markedPos_l = yy_markedPos;
1044                    yy_buffer_l = yy_buffer;
1045                    if (eof)
1046                        yy_peek = false;
1047                    else
1048                        yy_peek = yy_buffer_l[yy_markedPos_l] == '\n';
1049                }
1050                if (yy_peek) yyline--;
1051            }
1052            yy_action = -1;
1053
1054            yy_startRead_l = yy_currentPos_l = yy_currentPos =
1055                    yy_startRead = yy_markedPos_l;
1056
1057            yy_state = yy_lexical_state;
1058
1059
1060            yy_forAction: {
1061                while (true) {
1062
1063                    if (yy_currentPos_l < yy_endRead_l)
1064                        yy_input = yy_buffer_l[yy_currentPos_l++];
1065                    else if (yy_atEOF) {
1066                        yy_input = YYEOF;
1067                        break yy_forAction;
1068                    } else {
1069                        // store back cached positions
1070
yy_currentPos = yy_currentPos_l;
1071                        yy_markedPos = yy_markedPos_l;
1072                        boolean eof = yy_refill();
1073                        // get translated positions and possibly new buffer
1074
yy_currentPos_l = yy_currentPos;
1075                        yy_markedPos_l = yy_markedPos;
1076                        yy_buffer_l = yy_buffer;
1077                        yy_endRead_l = yy_endRead;
1078                        if (eof) {
1079                            yy_input = YYEOF;
1080                            break yy_forAction;
1081                        } else {
1082                            yy_input = yy_buffer_l[yy_currentPos_l++];
1083                        }
1084                    }
1085                    int yy_next = yytrans_l[yy_rowMap_l[yy_state] + yycmap_l[yy_input]];
1086                    if (yy_next == -1) break yy_forAction;
1087                    yy_state = yy_next;
1088
1089                    int yy_attributes = yy_attr_l[yy_state];
1090                    if ((yy_attributes & 1) == 1) {
1091                        yy_action = yy_state;
1092                        yy_markedPos_l = yy_currentPos_l;
1093                        if ((yy_attributes & 8) == 8) break yy_forAction;
1094                    }
1095
1096                }
1097            }
1098
1099            // store back cached position
1100
yy_markedPos = yy_markedPos_l;
1101
1102            switch (yy_action) {
1103
1104                case 6:
1105                case 11:
1106                case 17:
1107                    {
1108                        nextState = YYINITIAL;
1109                        lastToken = Token.ERROR;
1110                        String JavaDoc text = yytext();
1111                        Token t = (new Token(lastToken, text, yyline, yychar, yychar + text.length(), nextState));
1112                        yybegin(nextState);
1113                        return (t);
1114                    }
1115                case 49:
1116                    break;
1117                case 46:
1118                    {
1119                        nextState = YYINITIAL;
1120                        lastToken = Token.LITERAL_BIT_STRING;
1121                        String JavaDoc text = yytext();
1122                        Token t = (new Token(lastToken, text, yyline, yychar, yychar + text.length(), nextState));
1123                        yybegin(nextState);
1124                        return (t);
1125                    }
1126                case 50:
1127                    break;
1128                case 8:
1129                case 10:
1130                case 14:
1131                case 15:
1132                    {
1133                        nextState = YYINITIAL;
1134                        lastToken = Token.OPERATOR;
1135                        String JavaDoc text = yytext();
1136                        Token t = (new Token(lastToken, text, yyline, yychar, yychar + text.length(), nextState));
1137                        yybegin(nextState);
1138                        return (t);
1139                    }
1140                case 51:
1141                    break;
1142                case 25:
1143                    {
1144                        nextState = YYINITIAL;
1145                        lastToken = Token.ERROR_UNCLOSED_STRING;
1146                        String JavaDoc text = yytext();
1147                        Token t = (new Token(lastToken, text, yyline, yychar, yychar + text.length(), nextState));
1148                        yybegin(nextState);
1149                        return (t);
1150                    }
1151                case 52:
1152                    break;
1153                case 38:
1154                case 45:
1155                    {
1156                        nextState = YYINITIAL;
1157                        lastToken = Token.ERROR_UNCLOSED_BIT_STRING;
1158                        String JavaDoc text = yytext();
1159                        Token t = (new Token(lastToken, text, yyline, yychar, yychar + text.length(), nextState));
1160                        yybegin(nextState);
1161                        return (t);
1162                    }
1163                case 53:
1164                    break;
1165                case 34:
1166                    {
1167                        nextState = COMMENT;
1168                        commentBuffer.append(yytext());
1169                        commentNestCount++;
1170                        yybegin(nextState);
1171                    }
1172                case 54:
1173                    break;
1174                case 9:
1175                case 13:
1176                case 22:
1177                    {
1178                        nextState = YYINITIAL;
1179                        lastToken = Token.SEPARATOR;
1180                        String JavaDoc text = yytext();
1181                        Token t = (new Token(lastToken, text, yyline, yychar, yychar + text.length(), nextState));
1182                        yybegin(nextState);
1183                        return (t);
1184                    }
1185                case 55:
1186                    break;
1187                case 2:
1188                case 4:
1189                case 12:
1190                    {
1191                        nextState = YYINITIAL;
1192                        lastToken = Token.IDENTIFIER;
1193                        String JavaDoc text = yytext();
1194                        Token t = (new Token(lastToken, text, yyline, yychar, yychar + text.length(), nextState));
1195                        yybegin(nextState);
1196                        return (t);
1197                    }
1198                case 56:
1199                    break;
1200                case 3:
1201                case 19:
1202                    {
1203                        nextState = YYINITIAL;
1204                        lastToken = Token.RESERVED_WORD;
1205                        String JavaDoc text = yytext();
1206                        Token t = (new Token(lastToken, text, yyline, yychar, yychar + text.length(), nextState));
1207                        yybegin(nextState);
1208                        return (t);
1209                    }
1210                case 57:
1211                    break;
1212                case 18:
1213                case 33:
1214                    {
1215                        commentNestCount--;
1216                        commentBuffer.append(yytext());
1217                        if (commentNestCount == 0) {
1218                            nextState = YYINITIAL;
1219                            lastToken = Token.COMMENT_TRADITIONAL;
1220                            Token t = (new Token(lastToken, commentBuffer.toString(), commentStartLine, commentStartChar, commentStartChar + commentBuffer.length(), nextState));
1221                            yybegin(nextState);
1222                            return (t);
1223                        }
1224                    }
1225                case 58:
1226                    break;
1227                case 20:
1228                case 42:
1229                    {
1230                        nextState = YYINITIAL;
1231                        lastToken = Token.LITERAL_FLOAT;
1232                        String JavaDoc text = yytext();
1233                        Token t = (new Token(lastToken, text, yyline, yychar, yychar + text.length(), nextState));
1234                        yybegin(nextState);
1235                        return (t);
1236                    }
1237                case 59:
1238                    break;
1239                case 24:
1240                case 36:
1241                    {
1242                        nextState = YYINITIAL;
1243                        lastToken = Token.LITERAL_STRING;
1244                        String JavaDoc text = yytext();
1245                        Token t = (new Token(lastToken, text, yyline, yychar, yychar + text.length(), nextState));
1246                        yybegin(nextState);
1247                        return (t);
1248                    }
1249                case 60:
1250                    break;
1251                case 29:
1252                    {
1253                        nextState = COMMENT;
1254                        commentBuffer.setLength(0);
1255                        commentBuffer.append(yytext());
1256                        commentNestCount = 1;
1257                        commentStartLine = yyline;
1258                        commentStartChar = yychar;
1259                        yybegin(nextState);
1260                    }
1261                case 61:
1262                    break;
1263                case 28:
1264                    {
1265                        nextState = YYINITIAL;
1266                        lastToken = Token.COMMENT_END_OF_LINE;
1267                        String JavaDoc text = yytext();
1268                        Token t = (new Token(lastToken, text, yyline, yychar, yychar + text.length(), nextState));
1269                        yybegin(nextState);
1270                        return (t);
1271                    }
1272                case 62:
1273                    break;
1274                case 39:
1275                    {
1276                        nextState = YYINITIAL;
1277                        lastToken = Token.ERROR_BAD_BIT_STRING;
1278                        String JavaDoc text = yytext();
1279                        Token t = (new Token(lastToken, text, yyline, yychar, yychar + text.length(), nextState));
1280                        yybegin(nextState);
1281                        return (t);
1282                    }
1283                case 63:
1284                    break;
1285                case 1:
1286                case 16:
1287                    {
1288                        nextState = COMMENT;
1289                        commentBuffer.append(yytext());
1290                        yybegin(nextState);
1291                    }
1292                case 64:
1293                    break;
1294                case 0:
1295                case 5:
1296                    {
1297                        nextState = YYINITIAL;
1298                        lastToken = Token.WHITE_SPACE;
1299                        String JavaDoc text = yytext();
1300                        Token t = (new Token(lastToken, text, yyline, yychar, yychar + text.length(), nextState));
1301                        yybegin(nextState);
1302                        return (t);
1303                    }
1304                case 65:
1305                    break;
1306                case 7:
1307                    {
1308                        nextState = YYINITIAL;
1309                        lastToken = Token.LITERAL_INTEGER;
1310                        String JavaDoc text = yytext();
1311                        Token t = (new Token(lastToken, text, yyline, yychar, yychar + text.length(), nextState));
1312                        yybegin(nextState);
1313                        return (t);
1314                    }
1315                case 66:
1316                    break;
1317                default:
1318                    if (yy_input == YYEOF && yy_startRead == yy_currentPos) {
1319                        yy_atEOF = true;
1320                        switch (yy_lexical_state) {
1321                            case COMMENT:
1322                                {
1323                                    nextState = YYINITIAL;
1324                                    lastToken = Token.ERROR_UNCLOSED_COMMENT;
1325                                    Token t = (new Token(lastToken, commentBuffer.toString(), commentStartLine, commentStartChar, commentStartChar + commentBuffer.length(), nextState));
1326                                    yybegin(nextState);
1327                                    return (t);
1328                                }
1329                            case 49:
1330                                break;
1331                            default:
1332                                return null;
1333                        }
1334                    } else {
1335                        yy_ScanError(YY_NO_MATCH);
1336                    }
1337            }
1338        }
1339    }
1340
1341
1342}
1343
1344
Popular Tags