Options
All
  • Public
  • Public/Protected
  • All
Menu

Type parameters

  • T

Hierarchy

  • Parser

Index

Constructors

constructor

Properties

parse

parse: ParseFunction<T>

Defines how to parse source

Methods

and

  • Run a parser after this one, discarding this ones value

    text('a').and(text('b')).bind(value =>
      assert(value === 'b')
    )

    Type parameters

    • U

    Parameters

    • parser: Parser<U>

      Parser to run next

    Returns Parser<U>

bind

  • Captures the result of this parser and bind another parser

    const parser = regexp(/\d/y).bind(digit => text(`${digit}abc`))
    parser.matches('11abc') // true
    parser.matches('99abc') // true

    Type parameters

    • U

    Parameters

    • map: (value: T) => Parser<U>

      Map value to another parser

        • Parameters

          • value: T

          Returns Parser<U>

    Returns Parser<U>

concat

  • Concatenates using any toString-able parser

    const parser = text('a').concat(regexp(/\d/y))
    parser.parseToEnd('a1') // 'a1'

    Parameters

    • parser: Parser<{ toString: any }>

      Parser which parses a toString-able value

    Returns Parser<string>

map

  • map<U>(map: (t: T) => U): Parser<U>
  • Maps the result of a parser to a different value

    const parser = regexp(/\d/y).map(Number)
    parser.parseToEnd('1') // 1

    Type parameters

    • U

    Parameters

    • map: (t: T) => U

      Mapping function

        • (t: T): U
        • Parameters

          • t: T

          Returns U

    Returns Parser<U>

matches

  • matches(str: string): boolean
  • Returns true if the parser can parse the given string

    const parser = text('a')
    parser.matches('a') // true
    parser.matches('b') // false

    Parameters

    • str: string

      Input

    Returns boolean

matchesToEnd

  • matchesToEnd(str: string): boolean
  • Returns true if the parse can parse the given string to the end

    const parser = text('a')
    parser.matchesToEnd('a') // true
    parser.matchesToEnd('ab') // false

    Parameters

    • str: string

      Input

    Returns boolean

or

  • Attempts to match the alternative parser if this one fails. Returns the value of whichever was successful

    const parser = text('a').or(integer)
    parse.parseToEnd('a') // 'a'
    parse.parseToEnd('123') // 123

    Type parameters

    • U

    Parameters

    • parser: Parser<T | U>

      Alternative parser

    Returns Parser<T | U>

parseToEnd

  • Attempts to parse a string to the end. Fails if the parser does not parse the entire input

    const parser = text('a')
    parser.parseToEnd('a') // 'a'
    parse.parseToEnd('a1') instanceof ParseError // true

    Parameters

    • str: string

      Input string

    Returns T | ParseError

skip

  • Matches a parser but skips the result, returning the result of the previous parser.

    Type parameters

    • U

    Parameters

    • parser: Parser<U>

      Parser to match then skip the result

    Returns Parser<T>

Generated using TypeDoc