All files / src/tokenizer tokenizer.ts

99.47% Statements 378/380
99.42% Branches 173/174
100% Functions 10/10
99.47% Lines 378/380

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 4941x 1x 1x 1x 1x               1x 5382x 5382x 5382x   5382x 5382x 5382x 5382x         5382x 29380x 36175x     36175x 12172x 12172x 12172x     36175x 3118x 3118x     36175x 8469x 8469x     36175x 134x 134x     12282x 12282x   5377x 29380x         5382x 3118x   3118x 15847x 15847x 13797x 15847x 2050x 2050x 15847x   3118x 3118x 3118x         5382x 3118x 3118x 21x 3118x 1299x 3118x 1278x 3118x 3x 3118x 3x 3118x 3x 3118x 3x 3118x 3x 3118x 3x 3118x 11x 3118x 5x 3118x 3x 3118x 3x 3118x 7x 3118x 7x 3118x 14x 3118x 8x 3118x 444x 3118x 3118x         5382x 8469x     8469x 208x 208x 9x 9x 21x 21x 9x 9x 9x   208x 5x 5x 4x 4x 4x 4x     4x 5x 5x 5x 208x     8469x 34120x 34120x   8455x     8469x   212x   212x 207x 207x 207x 1399x 1399x 207x 212x     8469x 5144x 5144x 77x 77x 77x 77x 77x 76x 76x 77x 77x 146x 146x 77x 5144x     8455x 8469x 5140x 5140x 5140x 5140x 7x 7x 7x 5140x 5140x 8x 8x 8x 5140x 5140x 3x 3x 3x 5140x 5122x 86x 86x 5122x 5140x 8469x 111x 111x   8455x 8455x 8469x 8247x 208x   8469x 8469x         5382x 134x 134x   134x   134x 134x 134x 592x   592x   135x 3x 3x 135x 132x 132x 132x 132x 592x 457x 457x 457x 592x     134x 2x 2x   132x 132x 132x 134x         5382x 12282x 12282x   12282x   12282x   12282x 1320x   1x 1x 1x 1319x   12282x 1382x   3x 3x 3x 1379x   12282x 3242x   2x 2x 2x 3240x   12282x 311x 12282x 205x 12282x 5x 12282x 1002x 12282x 1001x 12282x 25x 12282x 51x 12282x 31x 12282x 30x 12282x 50x 12282x 34x 12282x 123x 12282x 27x   12282x   820x   13x 13x 13x 807x 807x   12282x   324x   315x 315x 315x 9x 9x   12282x   626x   211x 211x 211x 415x 415x   12282x   162x   13x 13x 13x 149x 149x   12282x   730x   714x 714x 714x 16x   15x 15x 15x 1x 1x   12282x   614x   613x 613x 613x 1x 1x   12282x   36x   8x 8x 8x 30x   7x 7x 7x 21x 21x   12282x     130x 129x 129x   7x 7x 7x 129x 9x 9x 9x 9x 9x 9x 9x 9x 129x 13x 13x 13x 13x 13x 13x 13x 13x 129x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 129x 4x 4x 4x 4x 4x 4x 4x 4x 129x 92x 92x   12282x 1x 12282x 12282x         5382x 9300x 9300x         5382x 5381x 5381x 5381x 23998x 23998x 5376x 5376x 5381x 5382x  
import { TokenKind } from './token-kind.js';
import { Token } from './token.js';
import { isLetter, isDigit, isHexDigit, isWhitespace } from './char-flags.js';
import { SpelParseException } from '../error/spel-parse-exception.js';
import { SpelMessage } from '../error/spel-message.js';
 
/**
 * Parallels Spring Tokenizer
 *
 * SpEL lexer: decomposes expression string into Token sequence.
 * O(1) character type queries via character classification table.
 */
export class Tokenizer {
  private readonly expression: string;
  private pos = 0;
  private readonly maxPos: number;
 
  constructor(expression: string) {
    this.expression = expression;
    this.maxPos = expression.length;
  }
 
  /**
   * Main lexical analysis loop — returns next Token
   */
  public nextToken(): Token {
    while (this.pos < this.maxPos) {
      const ch = this.expression.charCodeAt(this.pos);
 
      // Skip whitespace
      if (isWhitespace(ch)) {
        this.pos++;
        continue;
      }
 
      // Identifier / keyword
      if (isLetter(ch) || ch === 95 /* _ */ || ch === 36 /* $ */) {
        return this.eatIdentifier();
      }
 
      // Number literal
      if (isDigit(ch)) {
        return this.eatNumberLiteral();
      }
 
      // String literal
      if (ch === 39 /* ' */ || ch === 34 /* " */) {
        return this.eatStringLiteral(ch);
      }
 
      // Operators and delimiters
      return this.eatOperatorOrDelimiter();
    }
 
    return new Token(TokenKind.EOF, this.pos, this.pos);
  }
 
  /**
   * Consume identifier or keyword
   */
  private eatIdentifier(): Token {
    const start = this.pos;
 
    while (this.pos < this.maxPos) {
      const ch = this.expression.charCodeAt(this.pos);
      if (isLetter(ch) || isDigit(ch) || ch === 95 /* _ */ || ch === 36 /* $ */) {
        this.pos++;
      } else {
        break;
      }
    }
 
    const text = this.expression.slice(start, this.pos);
    return this.identifierOrKeyword(start, text);
  }
 
  /**
   * Classify identifier string as Token: keyword or plain identifier
   */
  private identifierOrKeyword(start: number, text: string): Token {
    switch (text) {
      case 'null':
        return new Token(TokenKind.LITERAL_NULL, start, this.pos, text, null);
      case 'true':
        return new Token(TokenKind.LITERAL_BOOLEAN, start, this.pos, text, true);
      case 'false':
        return new Token(TokenKind.LITERAL_BOOLEAN, start, this.pos, text, false);
      case 'eq':
        return new Token(TokenKind.EQ, start, this.pos, text);
      case 'ne':
        return new Token(TokenKind.NE, start, this.pos, text);
      case 'lt':
        return new Token(TokenKind.LT, start, this.pos, text);
      case 'le':
        return new Token(TokenKind.LE, start, this.pos, text);
      case 'gt':
        return new Token(TokenKind.GT, start, this.pos, text);
      case 'ge':
        return new Token(TokenKind.GE, start, this.pos, text);
      case 'and':
        return new Token(TokenKind.AND, start, this.pos, text);
      case 'or':
        return new Token(TokenKind.OR, start, this.pos, text);
      case 'not':
        return new Token(TokenKind.NOT, start, this.pos, text);
      case 'mod':
        return new Token(TokenKind.MOD, start, this.pos, text);
      case 'matches':
        return new Token(TokenKind.MATCHES, start, this.pos, text);
      case 'between':
        return new Token(TokenKind.BETWEEN, start, this.pos, text);
      case 'instanceof':
        return new Token(TokenKind.INSTANCEOF, start, this.pos, text);
      case 'new':
        return new Token(TokenKind.NEW, start, this.pos, text);
      default:
        return new Token(TokenKind.IDENTIFIER, start, this.pos, text);
    }
  }
 
  /**
   * Consume number literal (int, long, float, double, hex)
   */
  private eatNumberLiteral(): Token {
    const start = this.pos;
 
    // Hexadecimal 0x or 0X
    if (this.expression.charCodeAt(this.pos) === 48 /* '0' */ && this.pos + 1 < this.maxPos) {
      const next = this.expression.charCodeAt(this.pos + 1);
      if (next === 120 /* 'x' */ || next === 88 /* 'X' */) {
        this.pos += 2;
        while (this.pos < this.maxPos && isHexDigit(this.expression.charCodeAt(this.pos))) {
          this.pos++;
        }
        const text = this.expression.slice(start, this.pos);
        return new Token(TokenKind.LITERAL_HEX, start, this.pos, text, parseInt(text, 16));
      }
      // Octal: 0[0-7]+
      if (next >= 48 /* '0' */ && next <= 55 /* '7' */) {
        this.pos += 2;
        while (this.pos < this.maxPos) {
          const octCh = this.expression.charCodeAt(this.pos);
          if (octCh >= 48 && octCh <= 55) {
            this.pos++;
          } else {
            break;
          }
        }
        const text = this.expression.slice(start, this.pos);
        return new Token(TokenKind.LITERAL_INT, start, this.pos, text, parseInt(text, 8));
      }
    }
 
    // Integer part
    while (this.pos < this.maxPos && isDigit(this.expression.charCodeAt(this.pos))) {
      this.pos++;
    }
 
    let isFloat = false;
 
    // Fractional part
    if (this.pos < this.maxPos && this.expression.charCodeAt(this.pos) === 46 /* '.' */) {
      // Check not range operator ..
      if (this.pos + 1 < this.maxPos && this.expression.charCodeAt(this.pos + 1) === 46) {
        // This is .. operator, do not consume
      } else {
        isFloat = true;
        this.pos++;
        while (this.pos < this.maxPos && isDigit(this.expression.charCodeAt(this.pos))) {
          this.pos++;
        }
      }
    }
 
    // Exponent part
    if (this.pos < this.maxPos) {
      const ch = this.expression.charCodeAt(this.pos);
      if (ch === 69 /* 'E' */ || ch === 101 /* 'e' */) {
        isFloat = true;
        this.pos++;
        if (this.pos < this.maxPos) {
          const sign = this.expression.charCodeAt(this.pos);
          if (sign === 43 /* '+' */ || sign === 45 /* '-' */) {
            this.pos++;
          }
        }
        while (this.pos < this.maxPos && isDigit(this.expression.charCodeAt(this.pos))) {
          this.pos++;
        }
      }
    }
 
    // Suffix
    let kind = TokenKind.LITERAL_INT;
    if (this.pos < this.maxPos) {
      const suffix = this.expression.charCodeAt(this.pos);
      switch (suffix) {
        case 76:
        case 108: // 'L' 'l'
          kind = TokenKind.LITERAL_LONG;
          this.pos++;
          break;
        case 70:
        case 102: // 'F' 'f'
          kind = TokenKind.LITERAL_FLOAT;
          this.pos++;
          break;
        case 68:
        case 100: // 'D' 'd'
          kind = TokenKind.LITERAL_DOUBLE;
          this.pos++;
          break;
        default:
          if (isFloat) {
            kind = TokenKind.LITERAL_DOUBLE;
          }
          break;
      }
    } else if (isFloat) {
      kind = TokenKind.LITERAL_DOUBLE;
    }
 
    const text = this.expression.slice(start, this.pos);
    const value =
      kind === TokenKind.LITERAL_INT || kind === TokenKind.LITERAL_LONG
        ? parseInt(text, 10)
        : parseFloat(text);
 
    return new Token(kind, start, this.pos, text, value);
  }
 
  /**
   * Consume string literal (supports '' escaping)
   */
  private eatStringLiteral(quote: number): Token {
    const start = this.pos;
    const quoteChar = String.fromCharCode(quote);
 
    this.pos++; // Skip opening quote
 
    const parts: string[] = [];
    let foundClosingQuote = false;
    while (this.pos < this.maxPos) {
      const ch = this.expression.charCodeAt(this.pos);
 
      if (ch === quote) {
        // Check for escaped quote '' or ""
        if (this.pos + 1 < this.maxPos && this.expression.charCodeAt(this.pos + 1) === quote) {
          parts.push(quoteChar);
          this.pos += 2; // Skip two quotes
        } else {
          this.pos++; // Skip closing quote
          foundClosingQuote = true;
          break;
        }
      } else {
        parts.push(this.expression[this.pos]!);
        this.pos++;
      }
    }
 
    // Check if end of expression reached without closing string
    if (!foundClosingQuote) {
      throw new SpelParseException(start, SpelMessage.UNTERMINATED_STRING_LITERAL);
    }
 
    const value = parts.join('');
    const raw = this.expression.slice(start, this.pos);
    return new Token(TokenKind.LITERAL_STRING, start, this.pos, raw, value);
  }
 
  /**
   * Consume operators and delimiters — core multi-char matching
   */
  private eatOperatorOrDelimiter(): Token {
    const start = this.pos;
    const ch = this.expression.charCodeAt(this.pos);
 
    this.pos++; // Consume one character first
 
    switch (ch) {
      // Single character
      case 42: // *
        if (this.matchNext(42)) {
          // **
          this.pos++;
          return new Token(TokenKind.POWER, start, this.pos, '**');
        }
        return new Token(TokenKind.STAR, start, this.pos, '*');
 
      case 43: // +
        if (this.matchNext(43)) {
          // ++
          this.pos++;
          return new Token(TokenKind.INC, start, this.pos, '++');
        }
        return new Token(TokenKind.PLUS, start, this.pos, '+');
 
      case 45: // -
        if (this.matchNext(45)) {
          // --
          this.pos++;
          return new Token(TokenKind.DEC, start, this.pos, '--');
        }
        return new Token(TokenKind.MINUS, start, this.pos, '-');
 
      case 47:
        return new Token(TokenKind.SLASH, start, this.pos, '/'); // /
      case 37:
        return new Token(TokenKind.PERCENT, start, this.pos, '%'); // %
      case 94:
        return new Token(TokenKind.POWER, start, this.pos, '^'); // ^
      case 40:
        return new Token(TokenKind.LPAREN, start, this.pos, '('); // (
      case 41:
        return new Token(TokenKind.RPAREN, start, this.pos, ')'); // )
      case 91:
        return new Token(TokenKind.LBRACKET, start, this.pos, '['); // [
      case 93:
        return new Token(TokenKind.RBRACKET, start, this.pos, ']'); // ]
      case 123:
        return new Token(TokenKind.LBRACE, start, this.pos, '{'); // {
      case 125:
        return new Token(TokenKind.RBRACE, start, this.pos, '}'); // }
      case 44:
        return new Token(TokenKind.COMMA, start, this.pos, ','); // ,
      case 58:
        return new Token(TokenKind.COLON, start, this.pos, ':'); // :
      case 35:
        return new Token(TokenKind.HASH, start, this.pos, '#'); // #
      case 64:
        return new Token(TokenKind.AT, start, this.pos, '@'); // @
 
      case 33: {
        // !
        if (this.matchNext(61)) {
          // !=
          this.pos++;
          return new Token(TokenKind.NE, start, this.pos, '!=');
        }
        return new Token(TokenKind.NOT, start, this.pos, '!');
      }
 
      case 61: {
        // =
        if (this.matchNext(61)) {
          // ==
          this.pos++;
          return new Token(TokenKind.EQ, start, this.pos, '==');
        }
        return new Token(TokenKind.ASSIGN, start, this.pos, '=');
      }
 
      case 60: {
        // <
        if (this.matchNext(61)) {
          // <=
          this.pos++;
          return new Token(TokenKind.LE, start, this.pos, '<=');
        }
        return new Token(TokenKind.LT, start, this.pos, '<');
      }
 
      case 62: {
        // >
        if (this.matchNext(61)) {
          // >=
          this.pos++;
          return new Token(TokenKind.GE, start, this.pos, '>=');
        }
        return new Token(TokenKind.GT, start, this.pos, '>');
      }
 
      case 38: {
        // &
        if (this.matchNext(38)) {
          // &&
          this.pos++;
          return new Token(TokenKind.AND, start, this.pos, '&&');
        }
        if (this.matchNext(64)) {
          // &@
          this.pos++;
          return new Token(TokenKind.AMP_AT, start, this.pos, '&@');
        }
        throw new SpelParseException(SpelMessage.NOT_VALID_CHAR, start, '&');
      }
 
      case 124: {
        // |
        if (this.matchNext(124)) {
          // ||
          this.pos++;
          return new Token(TokenKind.OR, start, this.pos, '||');
        }
        throw new SpelParseException(SpelMessage.NOT_VALID_CHAR, start, '|');
      }
 
      case 63: {
        // ?
        if (this.matchNext(46)) {
          // ?.
          this.pos++;
          return new Token(TokenKind.SAFE_NAV, start, this.pos, '?.');
        }
        if (this.matchNext(58)) {
          // ?:
          this.pos++;
          return new Token(TokenKind.ELVIS, start, this.pos, '?:');
        }
        return new Token(TokenKind.QMARK, start, this.pos, '?');
      }
 
      case 46: {
        // .
        // Check for .![ .?[ .$[ .^[ .*[ or ..
        if (this.pos < this.maxPos) {
          const next = this.expression.charCodeAt(this.pos);
          if (next === 46) {
            // ..
            this.pos++;
            return new Token(TokenKind.DOTDOT, start, this.pos, '..');
          }
          if (next === 33 /* '!' */) {
            if (
              this.pos + 1 < this.maxPos &&
              this.expression.charCodeAt(this.pos + 1) === 91 /* '[' */
            ) {
              this.pos += 2;
              return new Token(TokenKind.PROJECTION, start, this.pos, '.![');
            }
          }
          if (next === 63 /* '?' */) {
            if (
              this.pos + 1 < this.maxPos &&
              this.expression.charCodeAt(this.pos + 1) === 91 /* '[' */
            ) {
              this.pos += 2;
              return new Token(TokenKind.SELECTION, start, this.pos, '.?[');
            }
          }
          if (next === 36 /* '$' */ || next === 94 /* '^' */) {
            if (
              this.pos + 1 < this.maxPos &&
              this.expression.charCodeAt(this.pos + 1) === 91 /* '[' */
            ) {
              this.pos += 2;
              return new Token(
                TokenKind.SELECT_FIRST,
                start,
                this.pos,
                this.expression.slice(start, this.pos),
              );
            }
          }
          if (next === 42 /* '*' */) {
            if (
              this.pos + 1 < this.maxPos &&
              this.expression.charCodeAt(this.pos + 1) === 91 /* '[' */
            ) {
              this.pos += 2;
              return new Token(TokenKind.SELECT_LAST, start, this.pos, '.*[');
            }
          }
        }
        return new Token(TokenKind.DOT, start, this.pos, '.');
      }
 
      default:
        throw new SpelParseException(SpelMessage.NOT_VALID_CHAR, start, String.fromCharCode(ch));
    }
  }
 
  /**
   * Check if current position character matches expected
   */
  private matchNext(expected: number): boolean {
    return this.pos < this.maxPos && this.expression.charCodeAt(this.pos) === expected;
  }
 
  /**
   * Get all tokens (for debugging and testing)
   */
  public tokenize(): Token[] {
    const tokens: Token[] = [];
    let token: Token;
    while ((token = this.nextToken()).kind !== TokenKind.EOF) {
      tokens.push(token);
    }
    tokens.push(token); // Include EOF
    return tokens;
  }
}