/** Represents some range in the source input we are parsing or parsed. */ type Span = [start: number, end: number]; /** Parsers of this type always succeed, e.g. `many` and `sepBy`. */ interface SucceedingParser { parse(input: string, pos: number): Success; } /** Parsers of this type always fail. */ interface FailingParser { parse(input: string, pos: number): Failure; } /** Parsers of this type may fail. */ interface UnsafeParser { parse(input: string, pos: number): Result; } /** Parser interface that all parsers and combinators consume and resolve to. */ type Parser = FailingParser | SucceedingParser | UnsafeParser; /** Represents failed execution. */ type Failure = { readonly isOk: false; readonly span: Span; readonly pos: number; readonly expected: string; }; /** Represents successful execution. */ type Success = { readonly isOk: true; readonly span: Span; readonly pos: number; readonly value: T; }; /** Interface describing the result of parsers and combinators execution. */ type Result = Success | Failure; /** @internal */ interface Runnable$1 { with(input: string): Result; } /** * Runs a parser with provided input. * * @param parser - Parser to run * * @returns Parser result */ declare function run(parser: Parser): Runnable$1; /** @internal */ interface Runnable { with(input: string): Success; } /** @internal */ type ErrorResult = Omit; declare class ParserError extends Error { readonly name = "ParserError"; readonly span: Span; readonly pos: number; constructor(res: ErrorResult); } /** * Runs a parser with provided input, throwing on failure. * * @param parser - Parser to run * @throws {@link ParserError} Parser error with `message` (`expected`) `span`, and `pos` * * @returns Parser result */ declare function tryRun(parser: Parser): Runnable; /** * This type extracts the return-types from the parser initializers. */ type Grammar = { [P in keyof T]: T[P] extends () => unknown ? ReturnType : never; }; /** * This type injects the initialized parser types into `this`, allowing for * type-safe self-contained and mutually recursive grammars. */ type GrammarInit = T & ThisType>; /** * This defines the input to the `grammar` function - the parser initializers. */ type GrammarType = { [name: string]: () => Parser; }; /** * This is a utility function to simplify the creation of a self-contained grammar. * * Similarly to `defer`, this allows for the creation of mutually recursive parsers, * but lets you define all of the component parsers within a single call. * * The function takes an object with parser initializers, and returns an object with * all of those parsers initialized. Within the parser initializers, use `this` to * reference other initialized parsers, as in the example below. * * The properties of the resulting object are just regular parsers - you can freely * destructure these, pass them around individually, or compose them with other * grammars, parsers or combinators as needed. * * @example * * ```typescript * interface NumberNode { * type: 'number' * value: number * } * * interface ListNode { * type: 'list' * value: Array * } * * const tupleGrammar = grammar({ * tupleNumber(): Parser { * return map(integer(), (value, span) => ({ type: 'number', span, value })) * }, * tupleList(): Parser { * return map( * takeMid( * string('('), * sepBy(choice(this.tupleList, this.tupleNumber), string(',')), * string(')') * ), * (value, span) => ({ type: 'list', span, value }) * ) * } * }) * * const result = run(tupleGrammar.tupleList).with('(1,2,(3,4))') * ``` */ declare function grammar(init: GrammarInit): Grammar; /** * Parses any single character from the input and returns it. Fails at the end of input. * * @returns A single parsed character. */ declare function any(): Parser; /** * Intersection type to add a method for deferred parser definition. * * @internal */ type Deferred = Parser & { with(parser: Parser): void; }; /** * This is a special parser that has an additional `with` method, which should be used to define the * parser. This parser is tailored for creating mutually recursive parsers. * * @example * * ```typescript * interface NumberNode { * type: 'number' * value: number * } * * interface ListNode { * type: 'list' * value: Array * } * * // Here we create 'dummies' * * const TupleList = defer() * const TupleNumber = defer() * * // And below we actually define parsers * * TupleNumber.with( * map( * int(), * (value) => ({ type: 'number', value }) * ) * ) * * TupleList.with( * map( * takeMid( * string('('), * sepBy(choice(TupleList, TupleNumber), string(',')), * string(')') * ), * (value) => ({ type: 'list', value }) * ) * ) * * console.log( * run(TupleList).with('(1,2,(3,(4,5)))') * ) * ``` */ declare function defer(): Deferred; /** * Only succeeds at the end of the input. * * @returns `null` */ declare function eof(): Parser; /** * Only succeeds at the end of the line, either `\n` or `\r\n`. * * @returns Matched line break character */ declare function eol(): Parser; /** * Parses a single alphabetical character. Unicode friendly. * * @returns Matched character. */ declare function letter(): Parser; /** * Parses a sequence of alphabetical characters. Unicode friendly. * * @returns Matched characters as a string. */ declare function letters(): Parser; /** * Ensures that none of the characters in the given string matches the current character. * * @param chars - A string of characters that current character should not match * * @returns Current character */ declare function noneOf(chars: string): Parser; /** * Simply resolves to `null`. * * @returns `null`. */ declare function nothing(): Parser; /** * Parses a hexadecimal number prefixed with `0x` or `0X`, e.g. `0xFF`, `0XFF`, `0xff`. * * @returns Parsed hexadecimal number as a decimal one */ declare function hex(): Parser; /** * Parses a binary number prefixed with `0b` or `0B`, e.g. `0b101`, `0B101`. * * @returns Parsed binary number as a decimal one */ declare function binary(): Parser; /** * Parses an octal number prefixed with `0o` or `0O`, e.g. `0o420`, `0O420`. * * @returns Parsed octal number as a decimal one */ declare function octal(): Parser; /** * Parses a positive whole number without leading zeros, e.g. `0`, `7`, `420`. * * @returns Parsed whole number */ declare function whole(): Parser; /** * Parses an integer number with an optional minus sign, e.g. `0`, `-7`, `420`. * * @returns Parsed integer number */ declare function integer(): Parser; /** * Parses a float number with an optional minus sign, e.g. `0.25`, `-7.90`, `4.20`. * * Note: It doesn't handle floats with exponent parts. * * @returns Parsed float number */ declare function float(): Parser; /** * Ensures that one of the characters in the given string matches the current character. * * @param chars - A string of characters that current character should match * * @returns Current character */ declare function oneOf(chars: string): Parser; /** * Parses a string that matches a provided `re` regular expression. Returns the matched string, or * fails with an `expected` message. * * The regular expression must obey two simple rules: * * - It *does* use `g` flag. Flags like u and i are allowed and can be added if needed. * - It *doesn't* use `^` and `$` to match at the beginning or at the end of the text. * * If `g` flag is missing, it will be automatically injected. It's still better to always provide it * to avoid small performance penalty and clearly document the intention. * * @param rs - Regular expression * @param expected - Error message if the regular expression does not match input * * @returns Matched string */ declare function regexp(rs: RegExp, expected: string): Parser; /** * Simply returns the unparsed input as a string. Never fails. * * @returns Rest of the input as a string */ declare function rest(): SucceedingParser; /** * Parses an *ASCII* string. For parsing Unicode strings, consider using `ustring`. * * @param match - String to parse * * @returns Parsed string */ declare function string(match: string): Parser; /** * Parses a Unicode string. For parsing ASCII-only strings, consider using `string`. * * @param match - String to parse * * @returns Parsed string */ declare function ustring(match: string): Parser; /** * Parses whitespace, either a single character or consecutive ones. * * @returns Matched whitespace character(s) */ declare function whitespace(): Parser; export { Grammar, GrammarInit, GrammarType, ParserError, any, binary, defer, eof, eol, float, grammar, hex, integer, letter, letters, noneOf, nothing, octal, oneOf, regexp, rest, run, string, tryRun, ustring, whitespace, whole };