View Javadoc

1   package org.devaki.nextobjects.ui.editor.syntax;
2   /*
3   
4   nextobjects Copyright (C) 2001-2005 Emmanuel Florent
5   
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by the
8   Free Software Foundation; either version 2 of the License, or (at your
9   option) any later version.
10  
11  This program is distributed in the hope that it will
12  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the GNU General Public License for more details.
15  
16  You should have received a copy of the GNU General Public License along
17  with this program; if not, write to the Free Software Foundation, Inc., 59
18  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  
20  */
21  
22  
23  /***
24   *
25   * <p>Description: A JavaToken is a token that is returned by a lexer that
26   *  is lexing a java
27   * source file.  It has several attributes describing the token:
28   * The type of token, the text of the token, the line number on which it
29   * occurred, the number of characters into the input at which it started, and
30   * similarly, the number of characters into the inut at which it ended.</p>
31   */
32  
33  public class SqlToken extends Token
34  {
35    /*** Variables **/
36    // Reserved Words
37    public final static int RESERVED_WORD_ADD = 0x101;
38    public final static int RESERVED_WORD_ALTER = 0x105;
39    public final static int RESERVED_WORD_AND = 0x106;
40    public final static int RESERVED_WORD_BEGIN = 0x107;
41    public final static int RESERVED_WORD_CASCADE = 0x108;
42    public final static int RESERVED_WORD_CHAR = 0x109;
43    public final static int RESERVED_WORD_CLOSE = 0x10A;
44    public final static int RESERVED_WORD_CONSTRAINT = 0x10B;
45    public final static int RESERVED_WORD_CONSTRAINTS = 0x10C;
46    public final static int RESERVED_WORD_CREATE = 0x10D;
47    public final static int RESERVED_WORD_CURSOR = 0x10E;
48    public final static int RESERVED_WORD_DATE = 0x10F;
49    public final static int RESERVED_WORD_DATETIME = 0x110;
50    public final static int RESERVED_WORD_DEALLOCATE = 0x111;
51    public final static int RESERVED_WORD_DECIMAL = 0x112;
52    public final static int RESERVED_WORD_DECLARE = 0x113;
53    public final static int RESERVED_WORD_DEFAULT = 0x114;
54    public final static int RESERVED_WORD_DROP = 0x115;
55    public final static int RESERVED_WORD_END = 0x116;
56    public final static int RESERVED_WORD_EXEC = 0x117;
57    public final static int RESERVED_WORD_EXISTS = 0x119;
58    public final static int RESERVED_WORD_FETCH = 0x11A;
59    public final static int RESERVED_WORD_FOR = 0x11B;
60    public final static int RESERVED_WORD_FOREIGN = 0x11D;
61    public final static int RESERVED_WORD_FROM = 0x11F;
62    public final static int RESERVED_WORD_IF = 0x121;
63    public final static int RESERVED_WORD_INDEX = 0x125;
64    public final static int RESERVED_WORD_INTO = 0x127;
65    public final static int RESERVED_WORD_KEY = 0x129;
66    public final static int RESERVED_WORD_NEXT = 0x12B;
67    public final static int RESERVED_WORD_NOT = 0x12D;
68    public final static int RESERVED_WORD_NULL = 0x131;
69    public final static int RESERVED_WORD_NUMBER = 0x132;
70    public final static int RESERVED_WORD_NUMERIC = 0x133;
71    public final static int RESERVED_WORD_NVARCHAR = 0x134;
72    public final static int RESERVED_WORD_OPEN = 0x135;
73    public final static int RESERVED_WORD_PRIMARY = 0x136;
74    public final static int RESERVED_WORD_REFERENCES = 0x139;
75    public final static int RESERVED_WORD_SELECT = 0x13B;
76    public final static int RESERVED_WORD_TABLE = 0x13D;
77    public final static int RESERVED_WORD_USING = 0x141;
78    public final static int RESERVED_WORD_VARCHAR = 0x142;
79    public final static int RESERVED_WORD_VARCHAR2 = 0x143;
80    public final static int RESERVED_WORD_WHERE = 0x145;
81    public final static int RESERVED_WORD_WHILE = 0x149;
82    // Identifier
83    public final static int IDENTIFIER = 0x201;
84    // Literals
85    public final static int LITERAL_INTEGER_DECIMAL = 0x301;
86    public final static int LITERAL_CHARACTER = 0x305;
87    public final static int LITERAL_STRING_1 = 0x309;
88    public final static int LITERAL_STRING_2 = 0x30D;
89    // Separators
90    public final static int SEPARATOR_LPAREN = 0x401;
91    public final static int SEPARATOR_RPAREN = 0x405;
92    public final static int SEPARATOR_SEMICOLON = 0x409;
93    public final static int SEPARATOR_COMMA = 0x40D;
94    public final static int SEPARATOR_PERIOD = 0x411;
95    // Operators
96    public final static int OPERATOR_EQUAL = 0x501;
97    public final static int OPERATOR_GREATER_THAN = 0x505;
98    public final static int OPERATOR_LESS_THAN = 0x509;
99    public final static int OPERATOR_DIFFERENT = 0x50D;
100   public final static int OPERATOR_ADD = 0x511;
101   public final static int OPERATOR_SUBSTRACT = 0x515;
102   public final static int OPERATOR_ASSIGN = 0x519;
103   // Comments
104   public final static int COMMENT_1 = 0xD01;
105   public final static int COMMENT_2 = 0xD05;
106   public final static int COMMENT_3 = 0xD09;
107   // White Spaces
108   public final static int WHITE_SPACE = 0xE01;
109   // Others
110   private int ID;
111   private String contents;
112   private int lineNumber;
113   private int charBegin;
114   private int charEnd;
115   private int state;
116 
117   /***
118    * Create a new token.
119    * @param ID the id number of the token
120    * @param contents A string representing the text of the token
121    * @param lineNumber the line number of the input on which this token started
122    * @param charBegin the offset into the input in characters at which this token started
123    * @param charEnd the offset into the input in characters at which this token ended
124    */
125   public SqlToken(int ID, String contents, int lineNumber, int charBegin,
126                   int charEnd)
127   {
128     this(ID, contents, lineNumber, charBegin, charEnd, Token.UNDEFINED_STATE);
129   }
130 
131   /***
132    * Create a new token.
133    * @param ID the id number of the token
134    * @param contents A string representing the text of the token
135    * @param lineNumber the line number of the input on which this token started
136    * @param charBegin the offset into the input in characters at which this token started
137    * @param charEnd the offset into the input in characters at which this token ended
138    * @param state the state the tokenizer is in after returning this token.
139    */
140   public SqlToken(int ID, String contents, int lineNumber, int charBegin,
141                   int charEnd, int state)
142   {
143     this.ID = ID;
144     this.contents = new String(contents);
145     this.lineNumber = lineNumber;
146     this.charBegin = charBegin;
147     this.charEnd = charEnd;
148     this.state = state;
149   }
150 
151   /***
152    * Get an integer representing the state the tokenizer is in after
153    * returning this token.
154    * Those who are interested in incremental tokenizing for performance
155    * reasons will want to use this method to figure out where the tokenizer
156    * may be restarted.  The tokenizer starts in Token.INITIAL_STATE, so
157    * any time that it reports that it has returned to this state, the
158    * tokenizer may be restarted from there.
159    */
160   public int getState()
161   {
162     return state;
163   }
164 
165   /***
166    * get the ID number of this token
167    * @return the id number of the token
168    */
169   public int getID()
170   {
171     return ID;
172   }
173 
174   /***
175    * get the contents of this token
176    * @return A string representing the text of the token
177    */
178   public String getContents()
179   {
180     return(new String(contents));
181   }
182 
183   /***
184    * get the line number of the input on which this token started
185    * @return the line number of the input on which this token started
186    */
187   public int getLineNumber()
188   {
189     return lineNumber;
190   }
191 
192   /***
193    * get the offset into the input in characters at which this token started
194        * @return the offset into the input in characters at which this token started
195    */
196   public int getCharBegin()
197   {
198     return charBegin;
199   }
200 
201   /***
202    * get the offset into the input in characters at which this token ended
203    * @return the offset into the input in characters at which this token ended
204    */
205   public int getCharEnd()
206   {
207     return charEnd;
208   }
209 
210   /***
211    * Checks this token to see if it is a reserved word.
212    * @return true if this token is a reserved word, false otherwise
213    */
214   public boolean isReservedWord()
215   {
216     return((ID >> 8) == 0x1);
217   }
218 
219   /***
220    * Checks this token to see if it is an identidefer.
221    * @return true if this token is an identidefer, false otherwise
222    */
223   public boolean isIdentifier()
224   {
225     return((ID >> 8) == 0x2);
226   }
227 
228   /***
229    * Checks this token to see if it is a literal.
230    * @return true if this token is a literal, false otherwise
231    */
232   public boolean isLiteral()
233   {
234     return((ID >> 8) == 0x3);
235   }
236 
237   /***
238    * Checks this token to see if it is a Separator.
239    * @return true if this token is a Separator, false otherwise
240    */
241   public boolean isSeparator()
242   {
243     return((ID >> 8) == 0x4);
244   }
245 
246   /***
247    * Checks this token to see if it is a Operator.
248    * @return true if this token is a Operator, false otherwise
249    */
250   public boolean isOperator()
251   {
252     return((ID >> 8) == 0x5);
253   }
254 
255   /***
256    * Checks this token to see if it is a comment.
257    * @return true if this token is a comment, false otherwise
258    */
259   public boolean isComment()
260   {
261     return((ID >> 8) == 0xD);
262   }
263 
264   /***
265    * Checks this token to see if it is White Space.
266    * Usually tabs, line breaks, form feed, spaces, etc.
267    * @return true if this token is White Space, false otherwise
268    */
269   public boolean isWhiteSpace()
270   {
271     return((ID >> 8) == 0xE);
272   }
273 
274   /***
275    * Checks this token to see if it is an Error.
276    * Unfinished comments, numbers that are too big, unclosed strings, etc.
277    * @return true if this token is an Error, false otherwise
278    */
279   public boolean isError()
280   {
281     return((ID >> 8) == 0xF);
282   }
283 
284   /***
285    * A description of this token.
286    * @return a description of this token.
287    */
288   public String getDescription()
289   {
290     if (isReservedWord())
291     {
292       return("reservedWord");
293     }
294     else if (isIdentifier())
295     {
296       return("identifier");
297     }
298     else if (isLiteral())
299     {
300       return("literal");
301     }
302     else if (isSeparator())
303     {
304       return("separator");
305     }
306     else if (isOperator())
307     {
308       return("operator");
309     }
310     else if (isComment())
311     {
312       return("comment");
313     }
314     else if (isWhiteSpace())
315     {
316       return("whitespace");
317     }
318     else if (isError())
319     {
320       return("error");
321     }
322     else
323     {
324       return("unknown");
325     }
326   }
327 
328   /***
329    * get a String that explains the error, if this token is an error.
330    * @return a  String that explains the error, if this token is an error, null otherwise.
331    */
332   public String errorString()
333   {
334     return null;
335   }
336 
337   /***
338    * get a representation of this token as a human readable string.
339    * @return a string representation of this token
340    */
341   public String toString()
342   {
343     return("Token #" + Integer.toHexString(ID) + ": " + getDescription()
344            + " Line " + lineNumber + " from " + charBegin + " to " + charEnd
345            + " : " + contents);
346   }
347 
348 }