tokenizr
Version:
String Tokenization Library for JavaScript
117 lines (116 loc) • 3.42 kB
TypeScript
declare class Token {
type: string;
value: unknown;
text: string;
pos: number;
line: number;
column: number;
constructor(type: string, value: unknown, text: string, pos?: number, line?: number, column?: number);
toString(colorize?: (type: string, text: string) => string): string;
isA(type: string, value?: unknown): boolean;
}
declare class ParsingError extends Error {
name: string;
message: string;
pos: number;
line: number;
column: number;
input: string;
constructor(message: string, pos: number, line: number, column: number, input: string);
toString(): string;
}
export interface TokenInfo {
line: number;
column: number;
pos: number;
len: number;
}
declare class ActionContext {
private _tokenizr;
private _data;
_repeat: boolean;
_reject: boolean;
_ignore: boolean;
_match: RegExpExecArray | null;
constructor(tokenizr: Tokenizr);
data(key: string, value?: unknown): unknown;
info(): TokenInfo;
push(state: string): this;
pop(): string;
state(): string;
state(state: string): this;
tag(tag: string): this;
tagged(tag: string): boolean;
untag(tag: string): this;
repeat(): this;
reject(): this;
ignore(): this;
accept(type: string, value?: unknown): this;
stop(): this;
}
export interface RuleState {
state: string;
tags: string[];
}
export type RuleAction = (this: ActionContext, ctx: ActionContext, found: RegExpExecArray) => void;
export interface Rule {
state: RuleState[];
pattern: RegExp;
action: RuleAction;
name: string;
}
export type BeforeAfterAction = (this: ActionContext, ctx: ActionContext, match: RegExpExecArray, rule: Rule) => void;
export type FinishAction = (this: ActionContext, ctx: ActionContext) => void;
export default class Tokenizr {
private _before;
private _after;
private _finish;
private _rules;
private _debug;
private _input;
private _len;
private _eof;
_pos: number;
_line: number;
_column: number;
private _state;
private _tag;
private _transaction;
_pending: Token[];
_stopped: boolean;
private _ctx;
constructor();
reset(): this;
error(message: string): ParsingError;
debug(debug: boolean): this;
_log(msg: string): void;
input(input: string): this;
push(state: string): this;
pop(): string;
state(): string;
state(state: string): this;
tag(tag: string): this;
tagged(tag: string): boolean;
untag(tag: string): this;
before(action: BeforeAfterAction): this;
after(action: BeforeAfterAction): this;
finish(action: FinishAction): this;
rule(state: string, pattern: RegExp, action: RuleAction, name?: string): this;
rule(pattern: RegExp, action: RuleAction, name?: string): this;
_progress(from: number, until: number): void;
_tokenize(): void;
token(): Token | null;
tokens(): Token[];
peek(offset?: number): Token;
skip(len?: number): this;
consume(type: string, value?: unknown): Token;
begin(): this;
depth(): number;
commit(): this;
rollback(): this;
alternatives(...alternatives: ((this: Tokenizr) => unknown)[]): unknown;
static readonly Token: typeof Token;
static readonly ParsingError: typeof ParsingError;
static readonly ActionContext: typeof ActionContext;
}
export {};