/**
* The source location.
*/
export interface Position {
row: number;
column: number;
}
/**
* Location range of the source.
* (See also `mapWithRange()`)
*/
export interface Range {
start: Position;
end: Position;
}
/**
* This error is thrown by `run()` when the parser fails.
* Unexpected errors (e.g. "undefined is not a function") won't be wraped with this error.
*/
export interface ParseError extends Error {
offset: number;
position: Position;
explain(): string;
}
/**
* Judge if an error (or anything else) is a ParseError.
*/
export declare function isParseError(e: any): e is ParseError;
export declare function calcPosition(source: string, offset: number): Position;
interface Failure {
scope: Scope;
offset: number;
message: string;
}
declare class Scope {
offset: number;
name: string;
parent?: Scope;
constructor(offset: number, name: string, parent?: Scope);
}
declare class Context {
offset: number;
scope: Scope;
}
/**
* `Parser<A>` returns `A` when it succeeds.
*/
export declare type Parser<A> = (source: string, context: Context) => A | Failure;
/**
* Run a parser. It throws ParseError when it fails.
*/
export declare function run<A>(parser: Parser<A>, source: string): A;
/**
* Apply given parsers and convert the results to another value.
*/
export declare function seq<A extends Array<any>, B>(map: (...args: {
[I in keyof A]: A[I];
}) => B, ...parsers: {
[I in keyof A]: Parser<A[I]>;
}): Parser<B>;
/**
* `seq($null, ...)` will return null.
*/
export declare function $null(..._: unknown[]): null;
/**
* `seq($1, ...)` will return the first result.
*/
export declare function $1<A>(a: A): A;
/**
* `seq($2, ...)` will return the second result.
*/
export declare function $2<A>(_1: any, a: A): A;
/**
* `seq($3, ...)` will return the third result.
*/
export declare function $3<A>(_1: any, _2: any, a: A): A;
/**
* Apply given parser and convert the result to another value.
*/
export declare function map<A, B>(f: (a: A, toError: (message: string) => Failure) => B | Failure, parser: Parser<A>): Parser<B>;
/**
* Apply given parser and convert the result to another value along with the source location.
*/
export declare function mapWithRange<A, B>(f: (value: A, range: Range, toError: (message: string) => Failure) => B, parser: Parser<A>): Parser<B>;
/**
* Only succeeds when position is at the end of the source.
*/
export declare const end: Parser<null>;
/**
* Succeeds when one of given parsers succeeds.
* Note that no fallback will occur if any one of them consumes even a single character.
* (See also `attempt()`)
*/
export declare function oneOf<A>(...parsers: Parser<A>[]): Parser<A>;
/**
* If the first parser fails, the second will be applied.
* It looks similar to `oneOf()`, but it will say nothing about the first error when the second fails.
*/
export declare function guard<A>(guarder: Parser<A>, parser: Parser<A>): Parser<A>;
/**
* When the given parser fails, offset will return to the first position
* it started parsing, even if it consists of multiple parsers.
* This can be used to force fallback in `oneOf()`, but overuse can lead to poor performance.
*/
export declare function attempt<A>(parser: Parser<A>): Parser<A>;
/**
* Add helpful name (ex. array, object, ...) to given parser.
*/
export declare function withContext<A>(name: string, parser: Parser<A>): Parser<A>;
/**
* Recursively declared parsers cause infinite loop (and stack overflow).
* To avoid that, `lazy()` gets the parser only when it is needed.
*/
export declare function lazy<A>(getParser: () => Parser<A>): Parser<A>;
/**
* Get string that matched the regex.
*/
export declare function match(regexString: string): Parser<string>;
/**
* Skip a part of source that matched the regex.
*/
export declare function skip(regexString: string): Parser<null>;
/**
* Succeeds if the rest of source starts with the given string.
* The optional type indicates what that string means.
*/
export declare function expectString(s: string, type?: string): Parser<null>;
/**
* Gets the string before the given pattern but does not consume the last.
*/
export declare function stringBefore(regexString: string): Parser<string>;
/**
* Get the string before the given pattern and consume the last.
*/
export declare function stringUntil(regexString: string): Parser<string>;
/**
* Gets the string before the given pattern or the end of the source.
*/
export declare function stringBeforeEndOr(regexString: string): Parser<string>;
/**
* Do nothing
*/
export declare const noop: Parser<null>;
/**
* Always succeed and return the constant value.
*/
export declare function constant<T>(t: T): Parser<T>;
/**
* This can be used when the implementation is not done.
*/
export declare function todo<A>(name: string): Parser<A>;
/**
* Parse many items while it is possible.
* If the item parser *partially* succeeds, then the entire parser fails.
* (See also `attempt()`)
*/
export declare function many<A>(itemParser: Parser<A>): Parser<A[]>;
/**
* Parse zero or more items with given separator.
*/
export declare function sepBy<A>(separator: Parser<unknown>, itemParser: Parser<A>): Parser<A[]>;
/**
* Parse one or more items with given separator.
*/
export declare function sepBy1<A>(separator: Parser<unknown>, itemParser: Parser<A>): Parser<A[]>;
/**
* Parse many items until something.
*/
export declare function manyUntil<A>(end: Parser<unknown>, itemParser: Parser<A>): Parser<A[]>;
/**
* Parse zero or more items with given separator until something.
*/
export declare function sepUntil<A>(end: Parser<unknown>, separator: Parser<unknown>, itemParser: Parser<A>): Parser<A[]>;
/**
* Parse one or more items with given separator until something.
*/
export declare function sepUntil1<A>(end: Parser<unknown>, separator: Parser<unknown>, itemParser: Parser<A>): Parser<A[]>;
/**
* Expect a symbol like `,`, `"`, `[`, etc.
*/
export declare function symbol(s: string): Parser<null>;
/**
* Expect a keyword like `true`, `null`, `for`, etc.
* Return the second argument if provided.
*/
export declare function keyword<A = null>(s: string, value?: A): Parser<A>;
/**
* Parse integer with given regex.
*/
export declare function int(regexString: string): Parser<number>;
/**
* Parse float number with given regex.
*/
export declare function float(regexString: string): Parser<number>;
/**
* Skip whitespace (`\\s*`)
*/
export declare const whitespace: Parser<null>;
/**
* Alias of `whitespace`
*/
export declare const _: Parser<null>;
/**
* Parse something between symbols with padding (`whitespace`).
* (Note: should be renamed to `between`)
*/
export declare function braced<A>(start: string, end: string, itemParser: Parser<A>): Parser<A>;
/**
* Parse something like `[ 1, 2, 3 ]`
*/
export declare function bracedSep<A>(start: string, end: string, separator: Parser<unknown>, itemParser: Parser<A>): Parser<A[]>;
export {};