1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
package com.jclark.xsl.expr;
import com.jclark.xsl.om.*;
class ExprTokenizer {
static final int TOK_EOF = 0;
static final int TOK_QNAME = TOK_EOF + 1;
static final int TOK_STAR = TOK_QNAME + 1;
static final int TOK_NAME_COLON_STAR = TOK_STAR + 1;
static final int TOK_AT = TOK_NAME_COLON_STAR + 1;
static final int TOK_DOT = TOK_AT + 1;
static final int TOK_DOT_DOT = TOK_DOT + 1;
static final int TOK_COMMENT_LPAR = TOK_DOT_DOT + 1;
static final int TOK_PROCESSING_INSTRUCTION_LPAR = TOK_COMMENT_LPAR + 1;
static final int TOK_TEXT_LPAR = TOK_PROCESSING_INSTRUCTION_LPAR + 1;
static final int TOK_NODE_LPAR = TOK_TEXT_LPAR + 1;
static final int TOK_MULTIPLY = TOK_NODE_LPAR + 1;
static final int TOK_LPAR = TOK_MULTIPLY + 1;
static final int TOK_RPAR = TOK_LPAR + 1;
static final int TOK_LSQB = TOK_RPAR + 1;
static final int TOK_RSQB = TOK_LSQB + 1;
static final int TOK_LITERAL = TOK_RSQB + 1;
static final int TOK_NUMBER = TOK_LITERAL + 1;
static final int TOK_AXIS = TOK_NUMBER + 1;
static final int TOK_FUNCTION_LPAR = TOK_AXIS + 1;
static final int TOK_CNAME_LPAR = TOK_FUNCTION_LPAR + 1;
static final int TOK_VARIABLE_REF = TOK_CNAME_LPAR + 1;
static final int TOK_SLASH = TOK_VARIABLE_REF + 1;
static final int TOK_SLASH_SLASH = TOK_SLASH + 1;
static final int TOK_VBAR = TOK_SLASH_SLASH + 1;
static final int TOK_COMMA = TOK_VBAR + 1;
static final int TOK_PLUS = TOK_COMMA + 1;
static final int TOK_MINUS = TOK_PLUS + 1;
static final int TOK_EQUALS = TOK_MINUS + 1;
static final int TOK_NOT_EQUALS = TOK_EQUALS + 1;
static final int TOK_GT = TOK_NOT_EQUALS + 1;
static final int TOK_LT = TOK_GT + 1;
static final int TOK_GTE = TOK_LT + 1;
static final int TOK_LTE = TOK_GTE + 1;
static final int TOK_AND = TOK_LTE + 1;
static final int TOK_OR = TOK_AND + 1;
static final int TOK_MOD = TOK_OR + 1;
static final int TOK_DIV = TOK_MOD + 1;
int currentToken = TOK_EOF;
String currentTokenValue = null;
private int currentTokenStartIndex = 0;
private final String expr;
private int exprIndex = 0;
private int exprLength;
private boolean recognizeOperator = false;
ExprTokenizer(String s) {
this.expr = s;
this.exprLength = s.length();
}
void next() throws ParseException {
currentTokenValue = null;
currentTokenStartIndex = exprIndex;
boolean currentMaybeOperator = recognizeOperator;
recognizeOperator = true;
for (;;) {
if (exprIndex >= exprLength) {
currentToken = TOK_EOF;
return;
}
char c = expr.charAt(exprIndex++);
switch (c) {
case ' ':
case '\t':
case '\r':
case '\n':
currentTokenStartIndex = exprIndex;
break;
case '<':
recognizeOperator = false;
if (exprIndex < exprLength && expr.charAt(exprIndex) == '=') {
exprIndex++;
currentToken = TOK_LTE;
}
else
currentToken = TOK_LT;
return;
case '>':
recognizeOperator = false;
if (exprIndex < exprLength && expr.charAt(exprIndex) == '=') {
exprIndex++;
currentToken = TOK_GTE;
}
else
currentToken = TOK_GT;
return;
case '/':
recognizeOperator = false;
if (exprIndex < exprLength && expr.charAt(exprIndex) == '/') {
exprIndex++;
currentToken = TOK_SLASH_SLASH;
}
else
currentToken = TOK_SLASH;
return;
case '=':
recognizeOperator = false;
currentToken = TOK_EQUALS;
return;
case '!':
if (exprIndex < exprLength && expr.charAt(exprIndex) == '=') {
exprIndex++;
currentToken = TOK_NOT_EQUALS;
recognizeOperator = false;
return;
}
throw new ParseException("illegal character");
case ',':
recognizeOperator = false;
currentToken = TOK_COMMA;
return;
case '|':
recognizeOperator = false;
currentToken = TOK_VBAR;
return;
case '+':
recognizeOperator = false;
currentToken = TOK_PLUS;
return;
case '-':
recognizeOperator = false;
currentToken = TOK_MINUS;
return;
case '(':
currentToken = TOK_LPAR;
recognizeOperator = false;
return;
case ')':
currentToken = TOK_RPAR;
return;
case '[':
currentToken = TOK_LSQB;
recognizeOperator = false;
return;
case ']':
currentToken = TOK_RSQB;
return;
case '"':
case '\'':
exprIndex = expr.indexOf(c, exprIndex);
if (exprIndex < 0) {
exprIndex = currentTokenStartIndex + 1;
throw new ParseException("missing quote");
}
currentTokenValue = expr.substring(currentTokenStartIndex + 1,
exprIndex++);
currentToken = TOK_LITERAL;
return;
case '$':
scanName();
if (exprIndex == currentTokenStartIndex + 1)
throw new ParseException("illegal character");
currentTokenValue = expr.substring(currentTokenStartIndex + 1,
exprIndex);
currentToken = TOK_VARIABLE_REF;
return;
case '*':
if (currentMaybeOperator) {
recognizeOperator = false;
currentToken = TOK_MULTIPLY;
}
else
currentToken = TOK_STAR;
return;
case '@':
currentToken = TOK_AT;
recognizeOperator = false;
return;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
scanDigits();
if (exprIndex + 1 < exprLength
&& expr.charAt(exprIndex) == '.'
&& isDigit(expr.charAt(exprIndex + 1))) {
exprIndex += 2;
scanDigits();
}
currentTokenValue = expr.substring(currentTokenStartIndex,
exprIndex);
currentToken = TOK_NUMBER;
return;
case '.':
if (exprIndex < exprLength && isDigit(expr.charAt(exprIndex))) {
++exprIndex;
scanDigits();
currentTokenValue = expr.substring(currentTokenStartIndex,
exprIndex);
currentToken = TOK_NUMBER;
return;
}
if (exprIndex < exprLength && expr.charAt(exprIndex) == '.') {
exprIndex++;
currentToken = TOK_DOT_DOT;
}
else
currentToken = TOK_DOT;
recognizeOperator = false;
return;
default:
--exprIndex;
scanName();
if (exprIndex == currentTokenStartIndex)
throw new ParseException("illegal character");
if (isAxis()) {
recognizeOperator = false;
currentToken = TOK_AXIS;
return;
}
if (exprIndex < exprLength && expr.charAt(exprIndex) == ':') {
exprIndex++;
if (exprIndex < exprLength && expr.charAt(exprIndex) == '*') {
currentTokenValue = expr.substring(currentTokenStartIndex,
exprIndex++ - 1);
currentToken = TOK_NAME_COLON_STAR;
return;
}
scanName();
if (expr.charAt(exprIndex - 1) == ':')
throw new ParseException("bad character after :");
currentTokenValue = expr.substring(currentTokenStartIndex,
exprIndex);
if (followingParen()) {
recognizeOperator = false;
currentToken = TOK_CNAME_LPAR;
}
else
currentToken = TOK_QNAME;
return;
}
currentTokenValue = expr.substring(currentTokenStartIndex,
exprIndex);
if (currentMaybeOperator) {
if (currentTokenValue.equals("and"))
currentToken = TOK_AND;
else if (currentTokenValue.equals("or"))
currentToken = TOK_OR;
else if (currentTokenValue.equals("mod"))
currentToken = TOK_MOD;
else if (currentTokenValue.equals("div"))
currentToken = TOK_DIV;
else
throw new ParseException("unrecognized operator name");
recognizeOperator = false;
return;
}
if (followingParen()) {
if (currentTokenValue.equals("processing-instruction"))
currentToken = TOK_PROCESSING_INSTRUCTION_LPAR;
else if (currentTokenValue.equals("comment"))
currentToken = TOK_COMMENT_LPAR;
else if (currentTokenValue.equals("node"))
currentToken = TOK_NODE_LPAR;
else if (currentTokenValue.equals("text"))
currentToken = TOK_TEXT_LPAR;
else
currentToken = TOK_FUNCTION_LPAR;
recognizeOperator = false;
}
else
currentToken = TOK_QNAME;
return;
}
}
}
private void scanName() {
if (exprIndex < exprLength
&& isNameStartChar(expr.charAt(exprIndex)))
while (++exprIndex < exprLength
&& isNameChar(expr.charAt(exprIndex)))
;
}
private void scanDigits() {
while (exprIndex < exprLength && isDigit(expr.charAt(exprIndex)))
exprIndex++;
}
private boolean followingParen() {
for (int i = exprIndex; i < exprLength; i++) {
switch (expr.charAt(i)) {
case '(':
exprIndex = i + 1;
return true;
case ' ':
case '\r':
case '\n':
case '\t':
break;
default:
return false;
}
}
return false;
}
private boolean isAxis() {
for (int i = exprIndex; i < exprLength; i++) {
switch (expr.charAt(i)) {
case ':':
if (i + 1 < exprLength && expr.charAt(i + 1) == ':') {
currentTokenValue = expr.substring(currentTokenStartIndex,
exprIndex);
exprIndex = i + 2;
return true;
}
break;
case ' ':
case '\r':
case '\n':
case '\t':
break;
default:
return false;
}
}
return false;
}
static private final String nameStartChars =
"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
static private final String nameChars = ".-0123456789";
static private final String digits = "0123456789";
private static final boolean isDigit(char c) {
return digits.indexOf(c) >= 0;
}
private static final boolean isSpace(char c) {
switch(c) {
case ' ':
case '\r':
case '\n':
case '\t':
return true;
}
return false;
}
private static final boolean isNameStartChar(char c) {
return nameStartChars.indexOf(c) >= 0 || c >= 0x80;
}
private static final boolean isNameChar(char c) {
return nameStartChars.indexOf(c) >= 0 || nameChars.indexOf(c) >= 0 || c >= 0x80;
}
}
|