UNPKG

parjs

Version:

A parser-combinator library for JavaScript.

97 lines (96 loc) 2.58 kB
/** * @module parjs/internal */ /** */ import { ResultKind } from "./result"; import { Parjser } from "./parjser"; /** * Container type for user state data. */ export interface UserState { [key: string]: any; } /** * Maintains progress for parsing a single input. */ export interface ParsingState { /** * The original string input on which parsing is performed. Should not be mutated while parsing. */ readonly input: string; /** * The next character waiting to be parsed. */ position: number; /** * The value from the last parser action performed on this state. */ value: any; /** * Additional state data. */ userState: UserState; /** * Initial user state. */ readonly initialUserState: any; /** * A stack that indicates entered parsers. Should not be modified by user code. */ stack: Parjser<any>[]; /** * If the result is a failure, this field will indicate the reason for the failure. * If the result is OK, this must be undefined. */ reason: string | object; /** * The result of the last parser action: OK, SoftFailure, HardFailure, FatalFailure. */ kind: ResultKind; /** * Shorthand for this.result == Okay */ readonly isOk: boolean; /** * Shorthand for this.result == SoftFailure */ readonly isSoft: boolean; /** * Shorthand for this.result == HardFailure */ readonly isHard: boolean; /** * Shorthand for this.result == FatalFailure */ readonly isFatal: boolean; atLeast(kind: ResultKind): any; atMost(kind: ResultKind): any; } /** * Basic implementation of the ParsingState interface. */ export declare class BasicParsingState implements ParsingState { input: string; userState: UserState; position: number; stack: never[]; initialUserState: undefined; value: undefined; kind: ResultKind; reason: string; constructor(input: string, userState: UserState); readonly isOk: boolean; readonly isSoft: boolean; readonly isHard: boolean; readonly isFatal: boolean; atLeast(kind: ResultKind): boolean | undefined; atMost(kind: ResultKind): boolean | undefined; } /** * A unique object value indicating the reuslt of a failed parser. */ export declare const FAIL_RESULT: any; /** * A unique object value indicating that a parser did not initialize the ParsingState's value property before terminating, which is an error. */ export declare const UNINITIALIZED_RESULT: any;