Class Lexer

Extract tokens from the input string.

Hierarchy

  • Lexer

Implements

Constructors

Properties

buffer: string

Only feed, reset can modify this var.

debug: boolean
defs: readonly Lexer.Definition[]
errors: Token[]

Error token list.

lineChars: number[]

How many chars in each line. Only update, reset can modify this var.

logger: Logger
offset: number

How many chars are digested. Only update, reset can modify this var.

rest: string

The un-digested string. This var is used to cache the result of buffer.slice(offset) to prevent unnecessary string copy. Only feed, reset, update can modify this var.

trimmed: boolean

Cache whether this lexer already trim start.

Accessors

  • get digested(): number
  • How many chars are digested.

    Returns number

Methods

  • Clone a new lexer with the same state and definitions. If options.debug/logger is omitted, the new lexer will inherit from the original one.

    Parameters

    • Optional options: {
          debug?: boolean;
          logger?: Logger;
      }
      • Optional debug?: boolean
      • Optional logger?: Logger

    Returns Lexer

  • Clone a new lexer with the same definitions, without states. If options.debug/logger is omitted, the new lexer will inherit from the original one.

    Parameters

    • Optional options: {
          debug?: boolean;
          logger?: Logger;
      }
      • Optional debug?: boolean
      • Optional logger?: Logger

    Returns Lexer

  • Get line number (starts from 1) and column number (starts from 1) from the index (starts from 0) of the input string.

    Parameters

    • index: number

    Returns {
        column: number;
        line: number;
    }

    • column: number
    • line: number
  • Try to retrieve a token. If nothing match, return null.

    You can provide expect to limit the token type/content to be accepted.

    Parameters

    • input: string | Readonly<{
          expect?: Readonly<{
              text?: string;
              type?: string;
          }>;
          input?: string;
      }> = ""

    Returns null | Token

  • Try to retrieve a token list exhaustively.

    Parameters

    • input: string | {
          input?: string;
          stopOnError?: boolean;
      } = ""

    Returns Token[]

  • Parameters

    • res: Readonly<{
          accept: true;
          content: string;
          digested: number;
          error?: any;
          muted: boolean;
          rest: string;
      }>
    • def: Lexer.Definition

    Returns Token

  • Take n chars from the rest of buffer and update state. This is useful when you have external logic to handle the token.

    Parameters

    • n: number = 1

    Returns string

  • Parameters

    • digested: number
    • content: string
    • rest: string

    Returns Lexer

Generated using TypeDoc