@types/parsimmon

  • Version 1.10.9
  • Published
  • 27.2 kB
  • No dependencies
  • MIT license

Install

npm i @types/parsimmon
yarn add @types/parsimmon
pnpm add @types/parsimmon

Overview

TypeScript definitions for parsimmon

Index

Variables

variable all

const all: Parser<string>;
  • consumes and yields the entire remainder of the stream.

variable any

const any: Parser<string>;
  • consumes and yields the next character of the stream.

variable cr

const cr: Parser<string>;
  • Equivalent to Parsimmon.string("\r").

    This parser checks for the "carriage return" character, which is used as the line terminator for classic Mac OS 9 text files.

variable crlf

const crlf: Parser<string>;
  • Equivalent to Parsimmon.string("\r\n").

    This parser checks for the "carriage return" character followed by the "line feed" character, which is used as the line terminator for Windows text files and HTTP headers.

variable digit

const digit: Parser<string>;
  • is equivalent to Parsimmon.regex(/[0-9]/)

variable digits

const digits: Parser<string>;
  • is equivalent to Parsimmon.regex(/[0-9]*`/)

variable end

const end: Parser<string>;
  • Equivalent to Parsimmon.alt(Parsimmon.newline, Parsimmon.eof).

    This is the most general purpose "end of line" parser. It allows the "end of file" in addition to all three text file line endings from Parsimmon.newline. This is important because text files frequently do not have line terminators at the end ("trailing newline").

variable eof

const eof: Parser<undefined>;
  • expects the end of the stream.

variable index

const index: Parser<Index>;
  • is a parser that yields the current index of the parse.

variable letter

const letter: Parser<string>;
  • is equivalent to Parsimmon.regex(/[a-z]/i)

variable letters

const letters: Parser<string>;
  • is equivalent to Parsimmon.regex(/[a-z]*`/i)

variable lf

const lf: Parser<string>;
  • Equivalent to Parsimmon.string("\n").

    This parser checks for the "line feed" character, which is used as the line terminator for Linux and macOS text files.

variable newline

const newline: Parser<string>;
  • This flexible parser will match any kind of text file line ending.

variable optWhitespace

const optWhitespace: Parser<string>;
  • is equivalent to Parsimmon.regex(/\s*`/)

variable whitespace

const whitespace: Parser<string>;
  • is equivalent to Parsimmon.regex(/\s+/)

Functions

function alt

alt: {
<U>(...parsers: Parser<U>[]): Parser<U>;
(...parsers: Parser<any>[]): Parser<any>;
};
  • accepts a variable number of parsers, and yields the value of the first one that succeeds, backtracking in between.

function bitSeq

bitSeq: (alignments: number[]) => Parser<number[]>;
  • Returns a parser that yields a byte (as a number) that matches the given input; similar to Parsimmon.digit and Parsimmon.letter.

function bitSeqObj

bitSeqObj: <Key extends string>(
namedAlignments: Array<[Key, number] | number>
) => Parser<{ [K in Key]: number }>;
  • Works like Parsimmon.bitSeq except each item in the array is either a number of bits or pair (array with length = 2) of name and bits. The bits are parsed in order and put into an object based on the name supplied. If there's no name for the bits, it will be parsed but discarded from the returned value.

function byte

byte: (int: number) => Parser<number>;
  • Returns a parser that yields a byte (as a number) that matches the given input; similar to Parsimmon.digit and Parsimmon.letter.

function createLanguage

createLanguage: {
(rules: Rule): Language;
<TLanguageSpec>(rules: TypedRule<TLanguageSpec>): TypedLanguage<TLanguageSpec>;
};
  • Starting point for building a language parser in Parsimmon.

    For having the resulting language rules return typed parsers, e.g. Parser<Foo> instead of Parser<any>, pass a language specification as type parameter to this function. The language specification should be of the following form:

    {
    rule1: type;
    rule2: type;
    }

    For example:

    const language = Parsimmon.createLanguage<{
    expr: Expr;
    numberLiteral: number;
    stringLiteral: string;
    }>({
    expr: r => (some expression that yields Parser<Expr>),
    numberLiteral: r => (some expression that yields Parser<number>),
    stringLiteral: r => (some expression that yields Parser<string>)
    });

    Now both language and the parameter r that is passed into every parser rule will be of the following type:

    {
    expr: Parser<Expr>;
    numberLiteral: Parser<number>;
    stringLiteral: Parser<string>;
    }

    Another benefit is that both the rules parameter and the resulting language should match the properties defined in the language specification type, which means that the compiler checks that there are no missing or superfluous rules in the language definition, and that the rules you access on the resulting language do actually exist.

function custom

custom: <U>(
parsingFunction: (
success: SuccessFunctionType<U>,
failure: FailureFunctionType<U>
) => ParseFunctionType<U>
) => Parser<U>;
  • allows to add custom primitive parsers.

function empty

empty: () => Parser<never>;
  • Returns Parsimmon.fail("fantasy-land/empty").

function fail

fail: (message: string) => Parser<never>;
  • fail paring with a message

function formatError

formatError: <T>(string: string, error: Result<T>) => string;
  • 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.

function isParser

isParser: (obj: any) => obj is Parser<any>;
  • Returns true if obj is a Parsimmon parser, otherwise false.

function lazy

lazy: {
<U>(f: () => Parser<U>): Parser<U>;
<U>(description: string, f: () => Parser<U>): Parser<U>;
};
  • 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.

function lookahead

lookahead: (arg: string | RegExp | Parser<any>) => Parser<''>;
  • Parses using arg, but does not consume what it parses. Yields an empty string.

function makeFailure

makeFailure: (furthest: number, expectation: string | string[]) => FailureReply;
  • 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).

function makeSuccess

makeSuccess: <T>(index: number, value: T) => SuccessReply<T>;
  • 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).

function noneOf

noneOf: (string: string) => Parser<string>;
  • Returns a parser that looks for exactly one character NOT from string, and yields that character.

function notFollowedBy

notFollowedBy: (parser: Parser<any>) => Parser<null>;
  • Parses using parser, but does not consume what it parses. Yields null if the parser does not match the input. Otherwise it fails.

function of

of: <U>(result: U) => Parser<U>;
  • This is an alias for Parsimmon.succeed(result).

function oneOf

oneOf: (string: string) => Parser<string>;
  • Returns a parser that looks for exactly one character from string, and yields that character.

function Parser

Parser: <T>(fn: (input: string, i: number) => Parsimmon.Reply<T>) => Parser<T>;
  • Alias of Parsimmon(fn) for backwards compatibility.

function Parsimmon

Parsimmon: typeof Parsimmon;
  • **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:

    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:

    var parser =
    Parsimmon.seq(
    Parsimmon.string('a'),
    notChar('b').times(5)
    );
    parser.parse('accccc');
    //=> {status: true, value: ['a', ['c', 'c', 'c', 'c', 'c']]}

function range

range: (begin: string, end: string) => Parser<string>;
  • Parsers a single character in from begin to end, inclusive.

function regex

regex: (myregex: RegExp, group?: number) => Parser<string>;
  • This was the original name for Parsimmon.regexp, but now it is just an alias.

function regexp

regexp: (myregex: RegExp, group?: number) => Parser<string>;
  • 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.

function sepBy

sepBy: <T, U>(content: Parser<T>, separator: Parser<U>) => Parser<T[]>;
  • Accepts two parsers, and expects zero or more matches for content, separated by separator, yielding an array.

function sepBy1

sepBy1: <T, U>(content: Parser<T>, separator: Parser<U>) => Parser<[T, ...T[]]>;
  • This is the same as Parsimmon.sepBy, but matches the content parser at least once.

function seq

seq: {
<T>(p1: Parser<T>): Parser<[T]>;
<T, U>(p1: Parser<T>, p2: Parser<U>): Parser<[T, U]>;
<T, U, V>(p1: Parser<T>, p2: Parser<U>, p3: Parser<V>): Parser<[T, U, V]>;
<T, U, V, W>(p1: Parser<T>, p2: Parser<U>, p3: Parser<V>, p4: Parser<W>): Parser<
[T, U, V, W]
>;
<T, U, V, W, X>(
p1: Parser<T>,
p2: Parser<U>,
p3: Parser<V>,
p4: Parser<W>,
p5: Parser<X>
): Parser<[T, U, V, W, X]>;
<T, U, V, W, X, Y>(
p1: Parser<T>,
p2: Parser<U>,
p3: Parser<V>,
p4: Parser<W>,
p5: Parser<X>,
p6: Parser<Y>
): Parser<[T, U, V, W, X, Y]>;
<T, U, V, W, X, Y, Z>(
p1: Parser<T>,
p2: Parser<U>,
p3: Parser<V>,
p4: Parser<W>,
p5: Parser<X>,
p6: Parser<Y>,
p7: Parser<Z>
): Parser<[T, U, V, W, X, Y, Z]>;
<T>(...parsers: Parser<T>[]): Parser<T[]>;
<T extends any[]>(...parsers: T): Parser<UnParser<T>>;
};
  • accepts a variable number of parsers that it expects to find in order, yielding an array of the results.

function seqMap

seqMap: {
<T, U>(p1: Parser<T>, cb: (a1: T) => U): Parser<U>;
<T, U, V>(p1: Parser<T>, p2: Parser<U>, cb: (a1: T, a2: U) => V): Parser<V>;
<T, U, V, W>(
p1: Parser<T>,
p2: Parser<U>,
p3: Parser<V>,
cb: (a1: T, a2: U, a3: V) => W
): Parser<W>;
<T, U, V, W, X>(
p1: Parser<T>,
p2: Parser<U>,
p3: Parser<V>,
p4: Parser<W>,
cb: (a1: T, a2: U, a3: V, a4: W) => X
): Parser<X>;
<T, U, V, W, X, Y>(
p1: Parser<T>,
p2: Parser<U>,
p3: Parser<V>,
p4: Parser<W>,
p5: Parser<X>,
cb: (a1: T, a2: U, a3: V, a4: W, a5: X) => Y
): Parser<Y>;
<T, U, V, W, X, Y, Z>(
p1: Parser<T>,
p2: Parser<U>,
p3: Parser<V>,
p4: Parser<W>,
p5: Parser<X>,
p6: Parser<Y>,
cb: (a1: T, a2: U, a3: V, a4: W, a5: X, a6: Y) => Z
): Parser<Z>;
<T, U, V, W, X, Y, Z, A>(
p1: Parser<T>,
p2: Parser<U>,
p3: Parser<V>,
p4: Parser<W>,
p5: Parser<X>,
p6: Parser<Y>,
p7: Parser<Z>,
cb: (a1: T, a2: U, a3: V, a4: W, a5: X, a6: Y, a7: Z) => A
): Parser<A>;
<T, U, V, W, X, Y, Z, A, B>(
p1: Parser<T>,
p2: Parser<U>,
p3: Parser<V>,
p4: Parser<W>,
p5: Parser<X>,
p6: Parser<Y>,
p7: Parser<Z>,
p8: Parser<A>,
cb: (a1: T, a2: U, a3: V, a4: W, a5: X, a6: Y, a7: Z, a8: A) => B
): Parser<B>;
<T, U, V, W, X, Y, Z, A, B, C>(
p1: Parser<T>,
p2: Parser<U>,
p3: Parser<V>,
p4: Parser<W>,
p5: Parser<X>,
p6: Parser<Y>,
p7: Parser<Z>,
p8: Parser<A>,
p9: Parser<B>,
cb: (a1: T, a2: U, a3: V, a4: W, a5: X, a6: Y, a7: Z, a8: A, a9: B) => C
): Parser<C>;
<T, U, V, W, X, Y, Z, A, B, C, D>(
p1: Parser<T>,
p2: Parser<U>,
p3: Parser<V>,
p4: Parser<W>,
p5: Parser<X>,
p6: Parser<Y>,
p7: Parser<Z>,
p8: Parser<A>,
p9: Parser<B>,
p10: Parser<C>,
cb: (
a1: T,
a2: U,
a3: V,
a4: W,
a5: X,
a6: Y,
a7: Z,
a8: A,
a9: B,
a10: C
) => D
): Parser<D>;
};
  • 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.

function seqObj

seqObj: <T, Key extends keyof T = keyof T>(
...args: (Parser<any> | [Key, Parser<T[Key]>])[]
) => Parser<{ [K in Key]: T[K] }>;

    function string

    string: <T extends string>(string: T) => Parser<T>;
    • is a parser that expects to find "my-string", and will yield the same.

    function succeed

    succeed: <U>(result: U) => Parser<U>;
    • Returns a parser that doesn't consume any of the string, and yields result.

    function takeWhile

    takeWhile: (predicate: (char: string) => boolean) => Parser<string>;
    • Returns a parser yield a string containing all the next characters that pass the predicate

    function test

    test: (predicate: (char: string) => boolean) => Parser<string>;
    • Returns a parser that yield a single character if it passes the predicate

    Interfaces

    interface Failure

    interface Failure {}

      property expected

      expected: string[];

        property index

        index: Index;

          property status

          status: false;

            interface FailureReply

            interface FailureReply {}

              property expected

              expected: string[];

                property furthest

                furthest: number;

                  property index

                  index: -1;

                    property status

                    status: false;

                      property value

                      value: null;

                        interface Index

                        interface Index {}

                          property column

                          column: number;
                          • one-based column offset

                          property line

                          line: number;
                          • one-based line offset

                          property offset

                          offset: number;
                          • zero-based character offset

                          interface Language

                          interface Language {}

                            index signature

                            [key: string]: Parser<any>;

                              interface Mark

                              interface Mark<T> {}

                                property end

                                end: Index;

                                  property start

                                  start: Index;

                                    property value

                                    value: T;

                                      interface Node

                                      interface Node<Name extends string, T> extends Mark<T> {}

                                        property name

                                        name: Name;

                                          interface Parser

                                          interface Parser<T> {}

                                            method ap

                                            ap: <U>(otherParser: Parser<(t: T) => U>) => Parser<U>;
                                            • Takes parser which returns a function and applies it to the parsed value of otherParser.

                                            method assert

                                            assert: (condition: (result: T) => boolean, message: string) => Parser<T>;
                                            • Passes the result of parser to the function condition, which returns a boolean. If the the condition is false, returns a failed parse with the given message. Else it returns the original result of parser.

                                            method atLeast

                                            atLeast: (n: number) => Parser<T[]>;
                                            • expects parser at least n times. Yields an array of the results.

                                            method atMost

                                            atMost: (n: number) => Parser<T[]>;
                                            • expects parser at most n times. Yields an array of the results.

                                            method chain

                                            chain: <U>(next: (result: T) => Parser<U>) => Parser<U>;
                                            • 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

                                            method contramap

                                            contramap: <U>(fn: (input: T) => U) => Parser<U>;
                                            • Transforms the input of parser with the given function.

                                            method desc

                                            desc: (description: string | string[]) => Parser<T>;
                                            • 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.

                                            method empty

                                            empty: () => Parser<never>;
                                            • Returns Parsimmon.fail("fantasy-land/empty").

                                            method fallback

                                            fallback: <U>(fallbackValue: U) => Parser<T | U>;
                                            • returns a new parser that returns the fallback value if the first parser failed.

                                            method lookahead

                                            lookahead: (arg: string | RegExp | Parser<any>) => Parser<T>;
                                            • Returns a parser that looks for whatever arg wants to parse, but does not consume it. Yields the same result as parser. Equivalent to parser.skip(Parsimmon.lookahead(anotherParser)).

                                            method many

                                            many: () => Parser<T[]>;
                                            • expects parser zero or more times, and yields an array of the results.

                                            method map

                                            map: <U>(call: (result: T) => U) => Parser<U>;
                                            • transforms the output of parser with the given function.

                                            method mark

                                            mark: () => Parser<Mark<T>>;
                                            • Yields an object with start, value, and end keys, where value is the original value yielded by the parser, and start and end indicate the Index objects representing the range of the parse result.

                                            method node

                                            node: <Name extends string>(name: Name) => Parser<Node<Name, T>>;
                                            • Like mark(), but yields an object with an additional name key to use as an AST.

                                            method notFollowedBy

                                            notFollowedBy: (anotherParser: Parser<any>) => Parser<T>;
                                            • Returns a parser that looks for anything but whatever anotherParser wants to parse, and does not consume it. Yields the same result as parser. Equivalent to parser.skip(Parsimmon.notFollowedBy(anotherParser)).

                                            method of

                                            of: <U>(result: U) => Parser<U>;
                                            • Equivalent to Parsimmon.of(result).

                                            method or

                                            or: <U>(otherParser: Parser<U>) => Parser<T | U>;
                                            • returns a new parser which tries parser, and if it fails uses otherParser.

                                            method parse

                                            parse: (input: string) => Result<T>;
                                            • parse the string

                                            method promap

                                            promap: <U, V>(
                                            inputFn: (input: T) => U,
                                            outputFn: (output: U) => V
                                            ) => Parser<V>;
                                            • Transforms the input and output of parser with the given function.

                                            method result

                                            result: <U>(aResult: U) => Parser<U>;
                                            • returns a new parser with the same behavior, but which yields aResult.

                                            method sepBy

                                            sepBy: <U>(separator: Parser<U>) => Parser<T[]>;
                                            • Equivalent to Parsimmon.sepBy(parser, separator).

                                              Expects zero or more matches for parser, separated by the parser separator, yielding an array.

                                            method sepBy1

                                            sepBy1: <U>(separator: Parser<U>) => Parser<[T, ...T[]]>;
                                            • Equivalent to Parsimmon.sepBy(parser, separator).

                                              Expects one or more matches for parser, separated by the parser separator, yielding a non-empty array.

                                            method skip

                                            skip: <U>(otherParser: Parser<U>) => Parser<T>;
                                            • expects otherParser after parser, but preserves the yield value of parser.

                                            method then

                                            then: {
                                            <U>(call: (result: T) => Parser<U>): Parser<U>;
                                            <U>(anotherParser: Parser<U>): Parser<U>;
                                            };
                                            • 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.

                                            • expects anotherParser to follow parser, and yields the result of anotherParser. NB: the result of parser here is ignored.

                                            method thru

                                            thru: <U>(call: (wrapper: Parser<T>) => Parser<U>) => Parser<U>;
                                            • returns wrapper(this) from the parser. Useful for custom functions used to wrap your parsers, while keeping with Parsimmon chaining style.

                                            method tie

                                            tie: () => Parser<string>;
                                            • Equivalent to parser.tieWith("").

                                              Note: parser.tie() is usually used after Parsimmon.seq(...parsers) or parser.many().

                                            method tieWith

                                            tieWith: (join: string) => Parser<string>;
                                            • When called on a parser yielding an array of strings, yields all their strings concatenated with the separator. Asserts that its input is actually an array of strings.

                                            method times

                                            times: { (n: number): Parser<T[]>; (min: number, max: number): Parser<T[]> };
                                            • expects parser exactly n times, and yields an array of the results.

                                            • expects parser between min and max times, and yields an array of the results.

                                            method trim

                                            trim: <U>(anotherParser: Parser<U>) => Parser<T>;
                                            • expects anotherParser before and after parser, yielding the result of parser

                                            method tryParse

                                            tryParse: (input: string) => T;
                                            • 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.

                                            method wrap

                                            wrap: (before: Parser<any>, after: Parser<any>) => Parser<T>;
                                            • Expects the parser before before parser and after after parser.

                                            interface Rule

                                            interface Rule {}

                                              index signature

                                              [key: string]: (r: Language) => Parser<any>;

                                                interface Success

                                                interface Success<T> {}

                                                  property status

                                                  status: true;

                                                    property value

                                                    value: T;

                                                      interface SuccessReply

                                                      interface SuccessReply<T> {}

                                                        property expected

                                                        expected: string[];

                                                          property furthest

                                                          furthest: -1;

                                                            property index

                                                            index: number;

                                                              property status

                                                              status: true;

                                                                property value

                                                                value: T;

                                                                  Type Aliases

                                                                  type FailureFunctionType

                                                                  type FailureFunctionType<U> = (index: number, msg: string) => Reply<U>;

                                                                    type ParseFunctionType

                                                                    type ParseFunctionType<U> = (stream: StreamType, index: number) => Reply<U>;

                                                                      type Reply

                                                                      type Reply<T> = SuccessReply<T> | FailureReply;

                                                                        type Result

                                                                        type Result<T> = Success<T> | Failure;

                                                                          type StreamType

                                                                          type StreamType = string;

                                                                            type SuccessFunctionType

                                                                            type SuccessFunctionType<U> = (index: number, result: U) => Reply<U>;

                                                                              type TypedLanguage

                                                                              type TypedLanguage<TLanguageSpec> = {
                                                                              [P in keyof TLanguageSpec]: Parser<TLanguageSpec[P]>;
                                                                              };

                                                                                type TypedRule

                                                                                type TypedRule<TLanguageSpec> = {
                                                                                [P in keyof TLanguageSpec]: (
                                                                                r: TypedLanguage<TLanguageSpec>
                                                                                ) => Parser<TLanguageSpec[P]>;
                                                                                };

                                                                                  type UnParser

                                                                                  type UnParser<T> = T extends Parser<infer U> ? U : never;

                                                                                    Package Files (1)

                                                                                    Dependencies (0)

                                                                                    No dependencies.

                                                                                    Dev Dependencies (0)

                                                                                    No dev dependencies.

                                                                                    Peer Dependencies (0)

                                                                                    No peer dependencies.

                                                                                    Badge

                                                                                    To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                                                    You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@types/parsimmon.

                                                                                    • Markdown
                                                                                      [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@types/parsimmon)
                                                                                    • HTML
                                                                                      <a href="https://www.jsdocs.io/package/@types/parsimmon"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>