| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.phoenix.sqlparser; |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | import java.io.ObjectStreamException; |
|---|
| 37 | import java.io.Serializable; |
|---|
| 38 | import java.util.Arrays; |
|---|
| 39 | import java.util.Collections; |
|---|
| 40 | import java.util.Map; |
|---|
| 41 | import java.util.HashMap; |
|---|
| 42 | import java.util.List; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * Enumerated type of a token type. |
|---|
| 47 | * |
|---|
| 48 | * Instances of this class are immutable. |
|---|
| 49 | * |
|---|
| 50 | * The following token types are defined: |
|---|
| 51 | * <ul> |
|---|
| 52 | * <li>TokenType.CREATE</li> |
|---|
| 53 | * <li>TokenType.TABLE</li> |
|---|
| 54 | * <li>TokenType.OPEN_PAREN</li> |
|---|
| 55 | * <li>TokenType.CLOSE_PAREN</li> |
|---|
| 56 | * <li>TokenType.IDENTIFIER</li> |
|---|
| 57 | * <li>TokenType.COMMA</li> |
|---|
| 58 | * <li>TokenType.SEMICOLON</li> |
|---|
| 59 | * <li>TokenType.WHITESPACE</li> |
|---|
| 60 | * <li>TokenType.NEWLINE</li> |
|---|
| 61 | * <li>TokenType.STRING_LITERAL</li> |
|---|
| 62 | * <li>TokenType.NUMERIC_LITERAL</li> |
|---|
| 63 | * <li>TokenType.COMMENT</li> |
|---|
| 64 | * <li>TokenType.NOT</li> |
|---|
| 65 | * <li>TokenType.NULL</li> |
|---|
| 66 | * <li>TokenType.CONSTRAINT</li> |
|---|
| 67 | * <li>TokenType.PRIMARY</li> |
|---|
| 68 | * <li>TokenType.KEY</li> |
|---|
| 69 | * <li>TokenType.UNIQUE</li> |
|---|
| 70 | * <li>TokenType.CHECK</li> |
|---|
| 71 | * <li>TokenType.IN</li> |
|---|
| 72 | * <li>TokenType.EOF</li> |
|---|
| 73 | * </ul> |
|---|
| 74 | * |
|---|
| 75 | * @author Michael Griffel |
|---|
| 76 | */ |
|---|
| 77 | public final class TokenType |
|---|
| 78 | implements Serializable |
|---|
| 79 | { |
|---|
| 80 | private static final long serialVersionUID = 1L; |
|---|
| 81 | |
|---|
| 82 | /** The name of the token type */ |
|---|
| 83 | private final transient String mName; |
|---|
| 84 | |
|---|
| 85 | /** Ordinal of next token type to be created */ |
|---|
| 86 | private static int sNextOrdinal = 0; |
|---|
| 87 | |
|---|
| 88 | /** Assign a ordinal to this token type */ |
|---|
| 89 | private final int mOrdinal = sNextOrdinal++; |
|---|
| 90 | |
|---|
| 91 | /** Maps a string representation to an enumerated value */ |
|---|
| 92 | private static final Map FROM_STRING = new HashMap(); |
|---|
| 93 | |
|---|
| 94 | /** The token type create. */ |
|---|
| 95 | public static final TokenType CREATE = new TokenType("create"); |
|---|
| 96 | |
|---|
| 97 | /** The token type table. */ |
|---|
| 98 | public static final TokenType TABLE = new TokenType("table"); |
|---|
| 99 | |
|---|
| 100 | /** The token type open_paren. */ |
|---|
| 101 | public static final TokenType OPEN_PAREN = new TokenType("open_paren"); |
|---|
| 102 | |
|---|
| 103 | /** The token type close_paren. */ |
|---|
| 104 | public static final TokenType CLOSE_PAREN = new TokenType("close_paren"); |
|---|
| 105 | |
|---|
| 106 | /** The token type identifier. */ |
|---|
| 107 | public static final TokenType IDENTIFIER = new TokenType("identifier"); |
|---|
| 108 | |
|---|
| 109 | /** The token type comma. */ |
|---|
| 110 | public static final TokenType COMMA = new TokenType("comma"); |
|---|
| 111 | |
|---|
| 112 | /** The token type semicolon. */ |
|---|
| 113 | public static final TokenType SEMICOLON = new TokenType("semicolon"); |
|---|
| 114 | |
|---|
| 115 | /** The token type slash. */ |
|---|
| 116 | public static final TokenType SLASH = new TokenType("slash"); |
|---|
| 117 | |
|---|
| 118 | /** The token type whitespace. */ |
|---|
| 119 | public static final TokenType WHITESPACE = new TokenType("whitespace"); |
|---|
| 120 | |
|---|
| 121 | /** The token type newline. */ |
|---|
| 122 | public static final TokenType NEWLINE = new TokenType("newline"); |
|---|
| 123 | |
|---|
| 124 | /** The token type string_literal. */ |
|---|
| 125 | public static final TokenType STRING_LITERAL = new TokenType( |
|---|
| 126 | "string_literal"); |
|---|
| 127 | |
|---|
| 128 | /** The token type numeric_literal. */ |
|---|
| 129 | public static final TokenType NUMERIC_LITERAL = new TokenType( |
|---|
| 130 | "numeric_literal"); |
|---|
| 131 | |
|---|
| 132 | /** The token type comment. */ |
|---|
| 133 | public static final TokenType COMMENT = new TokenType("comment"); |
|---|
| 134 | |
|---|
| 135 | /** The token type not. */ |
|---|
| 136 | public static final TokenType NOT = new TokenType("not"); |
|---|
| 137 | |
|---|
| 138 | /** The token type null. */ |
|---|
| 139 | public static final TokenType NULL = new TokenType("null"); |
|---|
| 140 | |
|---|
| 141 | /** The token type constraint. */ |
|---|
| 142 | public static final TokenType CONSTRAINT = new TokenType("constraint"); |
|---|
| 143 | |
|---|
| 144 | /** The token type primary. */ |
|---|
| 145 | public static final TokenType PRIMARY = new TokenType("primary"); |
|---|
| 146 | |
|---|
| 147 | /** The token type key. */ |
|---|
| 148 | public static final TokenType KEY = new TokenType("key"); |
|---|
| 149 | |
|---|
| 150 | /** The token type unique. */ |
|---|
| 151 | public static final TokenType UNIQUE = new TokenType("unique"); |
|---|
| 152 | |
|---|
| 153 | /** The token type check. */ |
|---|
| 154 | public static final TokenType CHECK = new TokenType("check"); |
|---|
| 155 | |
|---|
| 156 | /** The token type in. */ |
|---|
| 157 | public static final TokenType IN = new TokenType("in"); |
|---|
| 158 | |
|---|
| 159 | /** The token type eof. */ |
|---|
| 160 | public static final TokenType EOF = new TokenType("eof"); |
|---|
| 161 | |
|---|
| 162 | /** The token type foreign. */ |
|---|
| 163 | public static final TokenType FOREIGN = new TokenType("foreign"); |
|---|
| 164 | |
|---|
| 165 | /** The token type default. */ |
|---|
| 166 | public static final TokenType DEFAULT = new TokenType("default"); |
|---|
| 167 | |
|---|
| 168 | /** The token type references. */ |
|---|
| 169 | public static final TokenType REFERENCES = new TokenType("references"); |
|---|
| 170 | |
|---|
| 171 | /** The token type on. */ |
|---|
| 172 | public static final TokenType ON = new TokenType("on"); |
|---|
| 173 | |
|---|
| 174 | /** The token type delete. */ |
|---|
| 175 | public static final TokenType DELETE = new TokenType("delete"); |
|---|
| 176 | |
|---|
| 177 | /** The token type set. */ |
|---|
| 178 | public static final TokenType SET = new TokenType("set"); |
|---|
| 179 | |
|---|
| 180 | /** The token type cascade. */ |
|---|
| 181 | public static final TokenType CASCADE = new TokenType("cascade"); |
|---|
| 182 | |
|---|
| 183 | /** The token type enable. */ |
|---|
| 184 | public static final TokenType ENABLE = new TokenType("enable"); |
|---|
| 185 | |
|---|
| 186 | /** The token type disable. */ |
|---|
| 187 | public static final TokenType DISABLE = new TokenType("disable"); |
|---|
| 188 | |
|---|
| 189 | /** The token type alter. */ |
|---|
| 190 | public static final TokenType ALTER = new TokenType("alter"); |
|---|
| 191 | |
|---|
| 192 | /** The token type drop. */ |
|---|
| 193 | public static final TokenType DROP = new TokenType("drop"); |
|---|
| 194 | |
|---|
| 195 | /** The token type insert. */ |
|---|
| 196 | public static final TokenType INSERT = new TokenType("insert"); |
|---|
| 197 | |
|---|
| 198 | /** The token type delete. */ |
|---|
| 199 | public static final TokenType SELECT = new TokenType("select"); |
|---|
| 200 | |
|---|
| 201 | /** The token type index. */ |
|---|
| 202 | public static final TokenType INDEX = new TokenType("index"); |
|---|
| 203 | |
|---|
| 204 | /** The token type bitmap. */ |
|---|
| 205 | public static final TokenType BITMAP = new TokenType("bitmap"); |
|---|
| 206 | |
|---|
| 207 | /** The token type sequence. */ |
|---|
| 208 | public static final TokenType SEQUENCE = new TokenType("sequence"); |
|---|
| 209 | |
|---|
| 210 | /** The token type increment. */ |
|---|
| 211 | public static final TokenType INCREMENT = new TokenType("increment"); |
|---|
| 212 | |
|---|
| 213 | public static final TokenType BY = new TokenType("by"); |
|---|
| 214 | |
|---|
| 215 | public static final TokenType START = new TokenType("start"); |
|---|
| 216 | |
|---|
| 217 | public static final TokenType WITH = new TokenType("with"); |
|---|
| 218 | |
|---|
| 219 | public static final TokenType MAXVALUE = new TokenType("maxvalue"); |
|---|
| 220 | |
|---|
| 221 | public static final TokenType NOMAXVALUE = new TokenType("nomaxvalue"); |
|---|
| 222 | |
|---|
| 223 | public static final TokenType MINVALUE = new TokenType("minvalue"); |
|---|
| 224 | |
|---|
| 225 | public static final TokenType NOMINVALUE = new TokenType("nominvalue"); |
|---|
| 226 | |
|---|
| 227 | public static final TokenType CYCLE = new TokenType("cycle"); |
|---|
| 228 | |
|---|
| 229 | public static final TokenType NOCYCLE = new TokenType("nocycle"); |
|---|
| 230 | |
|---|
| 231 | public static final TokenType CACHE = new TokenType("cache"); |
|---|
| 232 | |
|---|
| 233 | public static final TokenType NOCACHE = new TokenType("nocache"); |
|---|
| 234 | |
|---|
| 235 | public static final TokenType ORDER = new TokenType("order"); |
|---|
| 236 | |
|---|
| 237 | public static final TokenType NOORDER = new TokenType("noorder"); |
|---|
| 238 | |
|---|
| 239 | public static final TokenType OPERATOR = new TokenType("operator"); |
|---|
| 240 | |
|---|
| 241 | /** Internal list of all available token types */ |
|---|
| 242 | private static final TokenType[] PRIVATE_VALUES = {TokenType.CREATE, |
|---|
| 243 | TokenType.TABLE, TokenType.OPEN_PAREN, TokenType.CLOSE_PAREN, |
|---|
| 244 | TokenType.IDENTIFIER, TokenType.COMMA, TokenType.SEMICOLON, |
|---|
| 245 | TokenType.SLASH, TokenType.WHITESPACE, TokenType.NEWLINE, |
|---|
| 246 | TokenType.STRING_LITERAL, TokenType.NUMERIC_LITERAL, |
|---|
| 247 | TokenType.COMMENT, TokenType.NOT, TokenType.NULL, |
|---|
| 248 | TokenType.CONSTRAINT, TokenType.PRIMARY, TokenType.KEY, |
|---|
| 249 | TokenType.UNIQUE, TokenType.CHECK, TokenType.IN, TokenType.EOF, |
|---|
| 250 | TokenType.FOREIGN, TokenType.DEFAULT, TokenType.REFERENCES, |
|---|
| 251 | TokenType.ON, TokenType.DELETE, TokenType.SET, TokenType.CASCADE, |
|---|
| 252 | TokenType.ENABLE, TokenType.DISABLE, TokenType.ALTER, |
|---|
| 253 | TokenType.DROP, TokenType.INSERT, TokenType.SELECT, |
|---|
| 254 | TokenType.INDEX, TokenType.BITMAP, TokenType.SEQUENCE, |
|---|
| 255 | TokenType.INCREMENT, TokenType.BY, TokenType.START, TokenType.WITH, |
|---|
| 256 | TokenType.MAXVALUE, TokenType.NOMAXVALUE, TokenType.MINVALUE, |
|---|
| 257 | TokenType.NOMINVALUE, TokenType.CYCLE, TokenType.NOCYCLE, |
|---|
| 258 | TokenType.CACHE, TokenType.NOCACHE, TokenType.ORDER, |
|---|
| 259 | TokenType.NOORDER, TokenType.OPERATOR}; |
|---|
| 260 | |
|---|
| 261 | /** Immutable list of the token types. */ |
|---|
| 262 | public static final List VALUES = Collections.unmodifiableList(Arrays |
|---|
| 263 | .asList(PRIVATE_VALUES)); |
|---|
| 264 | |
|---|
| 265 | /** Private Constructor */ |
|---|
| 266 | private TokenType (String name) |
|---|
| 267 | { |
|---|
| 268 | mName = name; |
|---|
| 269 | FROM_STRING.put(mName, this); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | /** |
|---|
| 273 | * Creates a TokenType object from its int representation. |
|---|
| 274 | * |
|---|
| 275 | * @param i the int representation of the token type to be returned. |
|---|
| 276 | * @return the TokenType object represented by this int. |
|---|
| 277 | * @throws IllegalArgumentException If the assigned int value isn't |
|---|
| 278 | * listed in the internal token type table |
|---|
| 279 | */ |
|---|
| 280 | public static TokenType fromInt (int i) |
|---|
| 281 | throws IllegalArgumentException |
|---|
| 282 | { |
|---|
| 283 | try |
|---|
| 284 | { |
|---|
| 285 | return PRIVATE_VALUES[i]; |
|---|
| 286 | } |
|---|
| 287 | catch (ArrayIndexOutOfBoundsException e) |
|---|
| 288 | { |
|---|
| 289 | final IllegalArgumentException ex = new IllegalArgumentException( |
|---|
| 290 | "Illegal int representation of TokenType"); |
|---|
| 291 | ex.initCause(e); |
|---|
| 292 | throw ex; |
|---|
| 293 | } |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | /** |
|---|
| 297 | * Creates a TokenType object from its String representation. |
|---|
| 298 | * |
|---|
| 299 | * @param str the str representation of the token type to be returned. |
|---|
| 300 | * @return the TokenType object represented by this str. |
|---|
| 301 | * @throws IllegalArgumentException If the given str value isn't listed |
|---|
| 302 | * in the internal token type table. |
|---|
| 303 | */ |
|---|
| 304 | public static TokenType fromString (String str) |
|---|
| 305 | throws IllegalArgumentException |
|---|
| 306 | { |
|---|
| 307 | final TokenType result = (TokenType) FROM_STRING.get(str); |
|---|
| 308 | if (result == null) |
|---|
| 309 | { |
|---|
| 310 | throw new IllegalArgumentException( |
|---|
| 311 | "Illegal string representation of TokenType"); |
|---|
| 312 | } |
|---|
| 313 | return result; |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | /** |
|---|
| 317 | * Returns the int representation of this token type. |
|---|
| 318 | * |
|---|
| 319 | * @return the int representation of this token type. |
|---|
| 320 | */ |
|---|
| 321 | public int toInt () |
|---|
| 322 | { |
|---|
| 323 | return mOrdinal; |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | /** |
|---|
| 327 | * Returns the String representation of this token type. |
|---|
| 328 | * |
|---|
| 329 | * @return the String representation of this token type. |
|---|
| 330 | */ |
|---|
| 331 | public String toString () |
|---|
| 332 | { |
|---|
| 333 | return mName; |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | /** |
|---|
| 337 | * Resolves instances being deserialized to a single instance |
|---|
| 338 | * per token type. |
|---|
| 339 | */ |
|---|
| 340 | private Object readResolve () |
|---|
| 341 | throws ObjectStreamException |
|---|
| 342 | { |
|---|
| 343 | return PRIVATE_VALUES[mOrdinal]; |
|---|
| 344 | } |
|---|
| 345 | } |
|---|