Options
All
  • Public
  • Public/Protected
  • All
Menu

Index

Type aliases

ParseFunction

ParseFunction<T>: (source: Source) => ParseResult<T> | ParseError

Type parameters

  • T

Type declaration

Functions

Const constant

  • constant<T>(value: T): Parser<T>
  • A parser that always succeeds and returns a constant value

    constant('hello').parseToEnd('') // 'hello'

    Type parameters

    • T

    Parameters

    • value: T

      Value to return

    Returns Parser<T>

Const error

  • error<T>(message: string): Parser<T>
  • Generate an error

    const result = error('Error message').parseToEnd('')
    result instanceof ParseError // true
    result.message // 'Error message'

    Type parameters

    • T

    Parameters

    • message: string

      Error message

    Returns Parser<T>

Const join

  • join(parser: Parser<string[]>, separator?: string): Parser<string>
  • Join together an array of strings

    const threeAs = repeat(text('a', 3))
    threeAs.parseToEnd('aaa') // ['a', 'a', 'a']
    
    const joined = join(threeAs)
    joined.parseToEnd('aaa') // 'aaa'

    Parameters

    • parser: Parser<string[]>

      Parser which returns a set of string

    • Default value separator: string = ""

      What the elements should be separated with

    Returns Parser<string>

Const lazy

  • Lazily evaluate a parser. Useful for rules which are self-referential

    let digit: Parser<string> = error('Not yet defined')
    const parser = lazy(() => digit)
    digit = regexp(/d/y)
    parser.matches('1') // true

    Type parameters

    • T

    Parameters

    Returns Parser<T>

Const list

  • Parses a list of elements separated by a common separator

    const digit = regexp(/d/y)
    const parser = list(digit, text(','))
    parser.parseToEnd('1,2,3') // [1, 2, 3]

    Type parameters

    • T

    • U

    Parameters

    • elementParser: Parser<T>

      Parser for each element of the list

    • separatorParser: Parser<U>

      Parser for the separator in the list

    Returns Parser<T[]>

Const maybe

  • Attempts to parse using parser. Returns null if parser fails

    const parser = maybe(text('a'))
    parser.matches('a') // true
    parser.matches('') // true
    parser.parseToEnd('a') // 'a'
    parser.parseToEnd('') // null

    Type parameters

    • T

    Parameters

    • parser: Parser<T>

      Parser to execute

    Returns Parser<T | null>

Const maybeWithDefault

  • maybeWithDefault<T>(parser: Parser<T>, defaultValue: T): Parser<T>
  • Attempts to parse using parser. Returns a default value if it fails

    const parser = maybeWithDefault(text('a'), 'b')
    parser.parseToEnd('a') // 'a'
    parser.parseToEnd('') // 'b'

    Type parameters

    • T

    Parameters

    • parser: Parser<T>

      Parser to execute

    • defaultValue: T

      Default value to return if parser fails

    Returns Parser<T>

Const not

  • not<T>(parser: Parser<T>, message?: undefined | string): Parser<null>
  • Returns an error if the given parser succeeds

    const parser = not(regexp(/d/y))
    parser.matches('1') // false
    parser.parseToEnd('a') // null

    Type parameters

    • T

    Parameters

    • parser: Parser<T>

      Parser which should fail

    • Optional message: undefined | string

      Error message, if it succeeds

    Returns Parser<null>

Const oneOrMore

  • oneOrMore<T>(parser: Parser<T>, message?: undefined | string): Parser<T[]>
  • Match a parser one or more times

    const parser = oneOrMore(regexp(/\d/y))
    parser.parseToEnd('') // ParseError
    parser.parseToEnd('123') // ['1', '2', '3']

    Type parameters

    • T

    Parameters

    • parser: Parser<T>

      Parser to use

    • Optional message: undefined | string

      Error message, if it fails

    Returns Parser<T[]>

Const pair

  • Parse a pair of items and return them as a tuple

    const keyValue = pair(key, value)

    Type parameters

    • T

    • U

    Parameters

    • firstParser: Parser<T>

      Parse the first element

    • secondParser: Parser<U>

      Parse the second element

    Returns Parser<[T, U]>

Const regexp

  • regexp(regexp: RegExp, message?: undefined | string): Parser<string>
  • Match against a regular expression. Regular expressions must be sticky (ex: /\d/y)

    Regular expression to match with

    Parameters

    • regexp: RegExp
    • Optional message: undefined | string

      Error message, if it fails

    Returns Parser<string>

Const repeat

  • repeat<T>(parser: Parser<T>, count: number, message?: undefined | string): Parser<T[]>
  • Repeat a parser a set number of times

    const parser = repeat(regexp(/d/y), 4)
    parser.matches('1234') // true
    parser.parseToEnd('1234') // ['1', '2', '3', '4']

    Type parameters

    • T

    Parameters

    • parser: Parser<T>

      Parser to execute

    • count: number

      Number of times to execute it

    • Optional message: undefined | string

      Error message, if it fails

    Returns Parser<T[]>

Const text

  • text(text: string, message?: undefined | string): Parser<string>
  • Match a literal string

    String to match

    Parameters

    • text: string
    • Optional message: undefined | string

      Error message, if it fails

    Returns Parser<string>

Const zeroOrMore

  • Attempt to match a parser zero or more times

    const parser = zeroOrMore(regexp(/\d/y))
    parser.parseToEnd('') // []
    parser.parseToEnd('123') // ['1', '2', '3']

    Type parameters

    • T

    Parameters

    • parser: Parser<T>

      Parser to use

    Returns Parser<T[]>

Generated using TypeDoc