All files / src/tokenizer token.ts

100% Statements 51/51
100% Branches 22/22
100% Functions 6/6
100% Lines 51/51

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 631x             1x 29387x 29387x 29387x 29387x 29387x   29387x 29387x 29387x 29387x 29387x 29387x 29387x 29387x 29387x 29387x 29387x 29387x 29387x   29387x 2x 2x   29387x 2x 2x   29387x 2x 2x   29387x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 8x 9x 1x 9x 9x 29387x  
import { TokenKind } from './token-kind.js';
 
/**
 * Parallels Spring Token
 *
 * Token produced by lexical analysis with position, kind, and optional text/payload.
 */
export class Token {
  public readonly kind: TokenKind;
  public readonly startPos: number;
  public readonly endPos: number;
  public readonly literal?: string;
  public readonly payload?: unknown;
 
  constructor(
    kind: TokenKind,
    startPos: number,
    endPos: number,
    literal?: string,
    payload?: unknown,
  ) {
    this.kind = kind;
    this.startPos = startPos;
    this.endPos = endPos;
    this.literal = literal;
    this.payload = payload;
  }
 
  public get length(): number {
    return this.endPos - this.startPos;
  }
 
  public isOperator(): boolean {
    return this.kind >= TokenKind.PLUS && this.kind <= TokenKind.INSTANCEOF;
  }
 
  public isLiteral(): boolean {
    return this.kind >= TokenKind.LITERAL_INT && this.kind <= TokenKind.LITERAL_NULL;
  }
 
  public isKeyword(): boolean {
    switch (this.kind) {
      case TokenKind.MOD:
      case TokenKind.EQ:
      case TokenKind.NE:
      case TokenKind.LT:
      case TokenKind.LE:
      case TokenKind.GT:
      case TokenKind.GE:
      case TokenKind.AND:
      case TokenKind.OR:
      case TokenKind.NOT:
      case TokenKind.MATCHES:
      case TokenKind.BETWEEN:
      case TokenKind.INSTANCEOF:
      case TokenKind.NEW:
        return true;
      default:
        return false;
    }
  }
}