Options
All
  • Public
  • Public/Protected
  • All
Menu

Index

References

ParseFunction

ParseFunction

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

Type parameters

  • T

Type declaration

ParseResult

ParseResult

ParseResult<T>:

Type parameters

  • T

constructor

  • new ParseResult(value: T, source: Source, line: number, column: number): ParseResult
  • Result of parsing

    Parameters

    • value: T

      Value from parsing

    • source: Source

      Source that value was parsed from

    • line: number

      Line at the end of the result

    • column: number

      Column at the end of the result

    Returns ParseResult

column

column: number

Column at the end of the result

line

line: number

Line at the end of the result

source

source: Source

Source that value was parsed from

value

value: T

Value from parsing

Parser

Parser

Parser<T>:

Type parameters

  • T

constructor

parse

parse: ParseFunction<T>

Defines how to parse source

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>

Source

Source

Source:

constructor

  • new Source(source: string, index: number, line?: number, column?: number): Source
  • Source for parsing

    Parameters

    • source: string

      Source input

    • index: number

      Where in the input to begin parsing

    • Default value line: number = 1

      Line which source[index] is on

    • Default value column: number = 1

      Column which source[index] is on

    Returns Source

column

column: number

Column which source[index] is on

index

index: number

Where in the input to begin parsing

line

line: number

Line which source[index] is on

source

source: string

Source input

getRemaining

  • getRemaining(): string

match

  • Attempt to match the source against a regular expression

    Parameters

    • regexp: RegExp

      Regular expression to match

    • Optional message: undefined | string

      Error message if it fails

    Returns ParseResult<string> | ParseError

text

  • Attempt to match the source against a literal string

    Parameters

    • text: string

      Text to match

    • Optional message: undefined | string

      Error message, if it fails

    Returns ParseResult<string> | ParseError

constant

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>

error

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>

join

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>

lazy

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>

list

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[]>

maybe

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>

maybeWithDefault

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>

not

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>

oneOrMore

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[]>

pair

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]>

regexp

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>

repeat

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[]>

text

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>

zeroOrMore

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