All files / src/tokenizer tokenizer.ts

99.53% Statements 212/213
96.61% Branches 171/177
100% Functions 12/12
99.53% Lines 212/213

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 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545                            24x 24x 24x 24x                       5769x       5769x 5769x             31338x 38286x     38286x 12688x 12688x       25598x 3578x       22020x 8870x       13150x 237x       12913x     5740x             3578x   3578x 18386x 18386x 15941x   2445x       3578x 3578x                           3578x 3578x 73x   3505x             8870x     8870x 209x 209x 16x 16x 57x   16x 16x                 193x 5x 5x 4x 4x 4x         5x 5x                     8849x 33965x     8849x     8849x   256x     251x 251x 251x 1454x           8849x 5417x 5417x 75x 75x 75x 75x 75x 73x     75x 142x           8849x 8849x 5412x 5412x     51x 51x 51x     13x 13x 13x     3x 3x 3x   5345x 116x   5345x   3437x 121x     8849x   8849x 8545x         304x 51x   253x     8832x                     8612x 8612x   1x                           8565x 12x   8553x                       51x   51x 51x 7x     44x             237x 237x   237x   237x 237x 237x 1051x   1051x   240x 5x 5x   235x 235x 235x     811x 811x         237x 2x     235x 235x 235x             12913x 12913x   12913x   12913x         1339x     1402x   3x 3x   1399x     3202x   2x 2x   3200x     333x   212x   9x   1129x   1128x   31x   81x   55x   54x   102x   35x   140x   27x       825x   17x 17x   808x         350x   338x 338x   12x         627x   212x 212x   415x         188x   15x 15x   173x         736x   720x 720x   16x   15x 15x   1x         618x   617x 617x   1x         39x   9x 9x   30x   8x 8x   22x           246x 245x 245x   7x 7x   238x 15x       15x 15x     223x 24x       24x 24x           199x 16x       16x 16x 16x                 184x       5x               8033x             5768x   5768x 25569x   5739x 5739x      
import { TokenKind } from './token-kind.js';
import { OPERATOR_KEYWORDS } from './keyword-table.js';
import { Token } from './token.js';
import { isLetter, isDigit, isHexDigit, isWhitespace } from './char-flags.js';
import { foldAsciiUpper } from '../util/ascii.js';
 
/**
 * The bounds an integer literal may hold.
 *
 * An unsuffixed literal is an `int` in SpEL whatever its magnitude, and the `L`
 * suffix widens it to a `long`; `SAFE_INTEGER_MAX` is the largest value a
 * JavaScript number holds exactly. Literal digits are unsigned — a leading `-` is
 * a separate token — so no lower bound is needed here.
 */
const INT_LITERAL_MAX = BigInt('2147483647');
const LONG_MAX = BigInt('9223372036854775807');
const LONG_MIN = BigInt('-9223372036854775808');
const SAFE_INTEGER_MAX = BigInt('9007199254740991');
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 an identifier string as a textual operator or a plain identifier.
   *
   * Spring folds the scanned text to upper case and searches
   * `ALTERNATIVE_OPERATOR_NAMES`, so every casing of a textual operator is
   * accepted: `div`, `Div`, `DIV`. Words such as `and`, `or`, `matches`,
   * `between`, `instanceof`, `new`, `true`, `false` and `null` are *not*
   * operators here — Spring resolves them in the parser with
   * `equalsIgnoreCase`, which keeps them usable as ordinary identifiers.
   */
  private identifierOrKeyword(start: number, text: string): Token {
    const operator = OPERATOR_KEYWORDS.get(foldAsciiUpper(text));
    if (operator !== undefined) {
      return new Token(operator, start, this.pos, text);
    }
    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,
          this.intPayload(this.exactInteger(text, start, SpelMessage.NOT_AN_INTEGER), text, start),
        );
      }
      // 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 E{
            break;
          }
        }
        const text = this.expression.slice(start, this.pos);
        return new Token(
          TokenKind.LITERAL_INT,
          start,
          this.pos,
          text,
          this.intPayload(BigInt(Number.parseInt(text, 8)), text, start),
        );
      }
    }
 
    // 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++;
        Eif (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);
    let value: number | bigint;
    if (kind === TokenKind.LITERAL_INT) {
      value = this.intPayload(
        this.exactInteger(text, start, SpelMessage.NOT_AN_INTEGER),
        text,
        start,
      );
    } else if (kind === TokenKind.LITERAL_LONG) {
      value = this.longPayload(text, start);
    } else {
      value = parseFloat(text);
    }
 
    return new Token(kind, start, this.pos, text, value);
  }
 
  /**
   * Parse an integer literal's digits exactly.
   *
   * `BigInt` is used rather than `parseInt` because a literal near the 64-bit
   * boundary is not representable as a JavaScript number, and rounding it is how
   * `9007199254740993L` previously became `9007199254740992`.
   */
  private exactInteger(text: string, start: number, message: SpelMessage): bigint {
    try {
      return BigInt(text);
    } catch {
      throw new SpelParseException(start, message, text);
    }
  }
 
  /**
   * The payload of an unsuffixed literal, which SpEL types as an `int` whatever
   * its magnitude.
   *
   * Spring converts the digits with `Integer.parseInt` and raises
   * `NOT_AN_INTEGER` when they do not fit, so `3000000000` is a parse error and
   * `3000000000L` the only spelling. This port classified an unsuffixed literal
   * by its size instead, which accepted the first form.
   */
  private intPayload(exact: bigint, text: string, start: number): number {
    if (exact > INT_LITERAL_MAX) {
      throw new SpelParseException(start, SpelMessage.NOT_AN_INTEGER, text);
    }
    return Number(exact);
  }
 
  /**
   * The payload of an `L`-suffixed literal, which is a `long` in Java.
   *
   * Within the range a JavaScript number holds exactly it stays a `number`, so
   * `42L` is unchanged; beyond that the exact value is kept as a `bigint`. A value
   * outside the 64-bit range has no Java counterpart and is rejected, as Java
   * rejects such a literal.
   */
  private longPayload(text: string, start: number): number | bigint {
    const digits = text.endsWith('L') || text.endsWith('l') ? text.slice(0, -1) : text;
 
    const exact = this.exactInteger(digits, start, SpelMessage.INVALID_NUMBER);
    if (exact > LONG_MAX || exact < LONG_MIN) {
      throw new SpelParseException(start, SpelMessage.INVALID_NUMBER, text);
    }
 
    return exact <= SAFE_INTEGER_MAX ? Number(exact) : exact;
  }
 
  /**
   * 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
      // Spring has no '**' operator. Its tokenizer emits two STAR tokens and
      // the parser then rejects the expression; '^' is the power operator.
      case 42: // *
        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 /* '!' */) {
            Eif (
              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 /* '?' */) {
            Eif (
              this.pos + 1 < this.maxPos &&
              this.expression.charCodeAt(this.pos + 1) === 91 /* '[' */
            ) {
              this.pos += 2;
              return new Token(TokenKind.SELECTION, start, this.pos, '.?[');
            }
          }
          // Spring defines '^[' as select-first and '$[' as select-last. The
          // previous mapping sent both to SELECT_FIRST and also accepted '.*[',
          // which is not SpEL at all, so `items.$[x]` returned the first match.
          if (next === 94 /* '^' */ || next === 36 /* '$' */) {
            Eif (
              this.pos + 1 < this.maxPos &&
              this.expression.charCodeAt(this.pos + 1) === 91 /* '[' */
            ) {
              const selectFirst = next === 94;
              this.pos += 2;
              return new Token(
                selectFirst ? TokenKind.SELECT_FIRST : TokenKind.SELECT_LAST,
                start,
                this.pos,
                this.expression.slice(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;
  }
}