// Generated by typings // Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/555ea54770b8576162e761e344de969d6e960869/parsimmon/index.d.ts declare module "parsimmon" { // Type definitions for Parsimmon 1.0 // Project: https://github.com/jneen/parsimmon // Definitions by: Bart van der Schoor // Mizunashi Mana // Boris Cherny // Benny van Reeven // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped export type Action = (input: string, i: number) => Result; /** * **NOTE:** You probably will never need to use this function. Most parsing * can be accomplished using `Parsimmon.regexp` and combination with * `Parsimmon.seq` and `Parsimmon.alt`. * * You can add a primitive parser (similar to the included ones) by using * `Parsimmon(fn)`. This is an example of how to create a parser that matches * any character except the one provided: * * ```javascript * function notChar(char) { * return Parsimmon(function(input, i) { * if (input.charAt(i) !== char) { * return Parsimmon.makeSuccess(i + 1, input.charAt(i)); * } * return Parsimmon.makeFailure(i, 'anything different than "' + char + '"'); * }); * } * ``` * * This parser can then be used and composed the same way all the existing * ones are used and composed, for example: * * ```javascript * var parser = * Parsimmon.seq( * Parsimmon.string('a'), * notChar('b').times(5) * ); * parser.parse('accccc'); * //=> {status: true, value: ['a', ['c', 'c', 'c', 'c', 'c']]} * ``` */ export function Parsimmon(fn: Action): Parser; export type StreamType = string; export interface Index { /** zero-based character offset */ offset: number; /** one-based line offset */ line: number; /** one-based column offset */ column: number; } export interface Mark { start: Index; end: Index; value: T; } export type Result = Success | Failure; export interface Success extends ResultInterface { status: true; value: T; } export interface Failure extends ResultInterface { status: false; expected: string[]; index: Index; value: undefined; } export interface ResultInterface { status: boolean; index: Index | number; value?: T; furthest: number; expected: string[]; } export interface Parser { _: Action; /** * parse the string */ parse(input: string): Result; /** * Like parser.parse(input) but either returns the parsed value or throws * an error on failure. The error object contains additional properties * about the error. */ tryParse(input: string): T; /** * returns a new parser which tries parser, and if it fails uses otherParser. */ or(otherParser: Parser): Parser; /** * returns a new parser which tries parser, and on success calls the given function * with the result of the parse, which is expected to return another parser, which * will be tried next */ chain(next: (result: T) => Parser): Parser; /** * returns a new parser which tries parser, and on success calls the given function * with the result of the parse, which is expected to return another parser. */ then(call: (result: T) => Parser): Parser; /** * expects anotherParser to follow parser, and yields the result of anotherParser. * NB: the result of parser here is ignored. */ // tslint:disable-next-line:unified-signatures then(anotherParser: Parser): Parser; thru(f: ((parser: Parser) => T)): Parser; /** * transforms the output of parser with the given function. */ map(call: (result: T) => U): Parser; /** * returns a new parser with the same behavior, but which yields aResult. */ result(aResult: U): Parser; /** * returns a new parser that returns the fallback value if the first parser failed. */ fallback(fallbackValue: U): Parser; /** * expects otherParser after parser, but preserves the yield value of parser. */ skip(otherParser: Parser): Parser; /** * expects parser zero or more times, and yields an array of the results. */ many(): Parser; /** * expects parser exactly n times, and yields an array of the results. */ times(n: number): Parser; /** * expects parser between min and max times, and yields an array of the results. */ // tslint:disable-next-line:unified-signatures times(min: number, max: number): Parser; /** * expects parser at most n times. Yields an array of the results. */ atMost(n: number): Parser; /** * expects parser at least n times. Yields an array of the results. */ atLeast(n: number): Parser; /** * returns a new parser whose failure message is the passed description. */ mark(): Parser>; /** * Returns a new parser whose failure message is description. * For example, string('x').desc('the letter x') will indicate that 'the letter x' was expected. */ desc(description: string): Parser; } /** * Alias of `Parsimmon(fn)` for backwards compatibility. */ export function Parser(fn: (input: string, i: number) => Result): Parser; /** * To be used inside of Parsimmon(fn). Generates an object describing how * far the successful parse went (index), and what value it created doing * so. See documentation for Parsimmon(fn). */ export function makeSuccess(index: number, value: T): Success; /** * To be used inside of Parsimmon(fn). Generates an object describing how * far the unsuccessful parse went (index), and what kind of syntax it * expected to see (expectation). See documentation for Parsimmon(fn). */ export function makeFailure(furthest: number, expectation: string): Failure; /** * Returns true if obj is a Parsimmon parser, otherwise false. */ export function isParser(obj: any): boolean; /** * is a parser that expects to find "my-string", and will yield the same. */ export function string(string: string): Parser; /** * Returns a parser that looks for exactly one character from string, and yields that character. */ export function oneOf(string: string): Parser; /** * Returns a parser that looks for exactly one character NOT from string, and yields that character. */ export function noneOf(string: string): Parser; /** * Returns a parser that looks for a match to the regexp and yields the given match group * (defaulting to the entire match). The regexp will always match starting at the current * parse location. The regexp may only use the following flags: imu. Any other flag will * result in an error being thrown. */ export function regexp(myregex: RegExp, group?: number): Parser; // export function createLanguage(any: any): any; /** * This was the original name for Parsimmon.regexp, but now it is just an alias. */ export function regex(myregex: RegExp, group?: number): Parser; /** * Returns a parser that doesn't consume any of the string, and yields result. */ export function succeed(result: U): Parser; /** * This is an alias for Parsimmon.succeed(result). */ export function of(result: U): Parser; /** * accepts a variable number of parsers that it expects to find in order, yielding an array of the results. */ export function seq(p1: Parser): Parser<[T]>; export function seq(p1: Parser, p2: Parser): Parser<[T, U]>; export function seq(p1: Parser, p2: Parser, p3: Parser): Parser<[T, U, V]>; export function seq(p1: Parser, p2: Parser, p3: Parser, p4: Parser): Parser<[T, U, V, W]>; export function seq(p1: Parser, p2: Parser, p3: Parser, p4: Parser, p5: Parser): Parser<[T, U, V, W, X]>; export function seq(p1: Parser, p2: Parser, p3: Parser, p4: Parser, p5: Parser, p6: Parser): Parser<[T, U, V, W, X, Y]>; export function seq(p1: Parser, p2: Parser, p3: Parser, p4: Parser, p5: Parser, p6: Parser, p7: Parser): Parser<[T, U, V, W, X, Y, Z]>; export function seq(...parsers: Array>): Parser; export function seq(...parsers: Array>): Parser; /** * Takes the string passed to parser.parse(string) and the error returned from * parser.parse(string) and turns it into a human readable error message string. * Note that there are certainly better ways to format errors, so feel free to write your own. */ export function formatError(string: string, error: Result): string; /** * Matches all parsers sequentially, and passes their results as the arguments to a function. * Similar to calling Parsimmon.seq and then .map, but the values are not put in an array. */ export function seqMap(p1: Parser, cb: (a1: T) => U): Parser; export function seqMap(p1: Parser, p2: Parser, cb: (a1: T, a2: U) => V): Parser; export function seqMap(p1: Parser, p2: Parser, p3: Parser, cb: (a1: T, a2: U, a3: V) => W): Parser; export function seqMap(p1: Parser, p2: Parser, p3: Parser, p4: Parser, cb: (a1: T, a2: U, a3: V, a4: W) => X): Parser; export function seqMap(p1: Parser, p2: Parser, p3: Parser, p4: Parser, p5: Parser, cb: (a1: T, a2: U, a3: V, a4: W, a5: X) => Y): Parser; export function seqMap(p1: Parser, p2: Parser, p3: Parser, p4: Parser, p5: Parser, p6: Parser, cb: (a1: T, a2: U, a3: V, a4: W, a5: X, a6: Y) => Z): Parser; export function seqMap(p1: Parser, p2: Parser, p3: Parser, p4: Parser, p5: Parser, p6: Parser, p7: Parser, cb: (a1: T, a2: U, a3: V, a4: W, a5: X, a6: Y, a7: Z) => A): Parser; export function seqMap(p1: Parser, p2: Parser, p3: Parser, p4: Parser, p5: Parser, p6: Parser, p7: Parser, p8: Parser, cb: (a1: T, a2: U, a3: V, a4: W, a5: X, a6: Y, a7: Z, a8: A) => B): Parser; export type SuccessFunctionType = (index: number, result: U) => Result; export type FailureFunctionType = (index: number, msg: string) => Result; export type ParseFunctionType = (stream: StreamType, index: number) => Result; /** * allows to add custom primitive parsers. */ export function custom(parsingFunction: (success: SuccessFunctionType, failure: FailureFunctionType) => ParseFunctionType): Parser; /** * accepts a variable number of parsers, and yields the value of the first one that succeeds, * backtracking in between. */ export function alt(...parsers: Array>): Parser; export function alt(...parsers: Array>): Parser; /** * Accepts two parsers, and expects zero or more matches for content, separated by separator, yielding an array. */ export function sepBy(content: Parser, separator: Parser): Parser; /** * This is the same as Parsimmon.sepBy, but matches the content parser at least once. */ export function sepBy1(content: Parser, separator: Parser): Parser; /** * accepts a function that returns a parser, which is evaluated the first time the parser is used. * This is useful for referencing parsers that haven't yet been defined. */ export function lazy(f: () => Parser): Parser; export function lazy(description: string, f: () => Parser): Parser; /** * fail paring with a message */ export function fail(message: string): Parser; /** * is equivalent to Parsimmon.regex(/[a-z]/i) */ export let letter: Parser; /** * is equivalent to Parsimmon.regex(/[a-z]*`/i) */ export let letters: Parser; /** * is equivalent to Parsimmon.regex(/[0-9]/) */ export let digit: Parser; /** * is equivalent to Parsimmon.regex(/[0-9]*`/) */ export let digits: Parser; /** * is equivalent to Parsimmon.regex(/\s+/) */ export let whitespace: Parser; /** * is equivalent to Parsimmon.regex(/\s*`/) */ export let optWhitespace: Parser; /** * consumes and yields the next character of the stream. */ export let any: Parser; /** * consumes and yields the entire remainder of the stream. */ export let all: Parser; /** * expects the end of the stream. */ export let eof: Parser; /** * is a parser that yields the current index of the parse. */ export let index: Parser; /** * Returns a parser that yield a single character if it passes the predicate */ export function test(predicate: (char: string) => boolean): Parser; /** * Returns a parser yield a string containing all the next characters that pass the predicate */ export function takeWhile(predicate: (char: string) => boolean): Parser; }