Module parsel

a parser combinator library for Lua

Functions

Parsel.parse (input, parser) Entry point to parsing a string
Parsel.map (base, mapFn) Map the result of a parser

Tables

Parsel.nullResult a result indicating an optional match returned as a placeholder when an optional match did not succeed

Base parsers

Parsel.literal (lit) Parse any string literal
Parsel.letter () Parse any alphabetic letter
Parsel.digit () Parse any digit
Parsel.char () Match any single character
Parsel.charExcept (char) Match anything but the specified literal single character
Parsel.newline () Match single newline char
Parsel.whitespace () Match whitespace Matches at least one tab, space, or newline and consumes it
Parsel.optionalWhitespace () Match optional whitespace Optionally matches and consumes spaces, tabs and newlines
Parsel.anyLiteral (...) Match any literal passed in, succeeds with the match
Parsel.untilLiteral (literal) Match the parsers string until the specified literal is found

Combinators

Parsel.any (...) Parse any combinators specified in the list
Parsel.either (p1, p2) Try first parser, and if that fails, try the second parser
Parsel.oneOrMore (p) Parse a combinator at least one time and until the parse fails
Parsel.seq (...) Parse all combinators in sequence
Parsel.zeroOrMore (p) Parse zero or more instances of combinators
Parsel.optional (p) Optionally parse a combinator, return Parsel.nullResult if not matched
Parsel.lazy (f) Returns a parser that lazily evaluates a function
Parsel.sepBy (p, delim) Match parsers delimited by successful parse of delim the result is a table containing just the parsed values (delimiter ignored) fails if parsing a delimiter then parser fails, or if the first parsing of p fails
Parsel.sepByAllowTrailing (p, delim) Match parsers delimited by successful parse of delim, but allow a trailing delimiter
Parsel.exclude (p, exclusionFunc) Fails a parser if it matches condition set by exclusionFunc


Functions

Parsel.parse (input, parser)
Entry point to parsing a string

Parameters:

  • input the string to match
  • parser to use on the string

Returns:

    the result returned by the parser
Parsel.map (base, mapFn)
Map the result of a parser

Parameters:

  • base parser to run and map the result of
  • mapFn function to apply to the result of the parser operation

Returns:

    a new parser function with a mapped result

Tables

Parsel.nullResult
a result indicating an optional match returned as a placeholder when an optional match did not succeed

Fields:

  • type always "NULL"

See also:

Base parsers

Parsel.literal (lit)
Parse any string literal

Parameters:

  • lit the literal to match

Returns:

    a parser function matching the literal
Parsel.letter ()
Parse any alphabetic letter

Returns:

    a parser function
Parsel.digit ()
Parse any digit

Returns:

    a parser function
Parsel.char ()
Match any single character

Returns:

    a parser function that matches any character
Parsel.charExcept (char)
Match anything but the specified literal single character

Parameters:

  • char character to exclude

Returns:

    a parser function that matches any character besides char
Parsel.newline ()
Match single newline char

Returns:

    a parser function that matches a newline character
Parsel.whitespace ()
Match whitespace Matches at least one tab, space, or newline and consumes it

Returns:

    a parser function
Parsel.optionalWhitespace ()
Match optional whitespace Optionally matches and consumes spaces, tabs and newlines

Returns:

    a parser function
Parsel.anyLiteral (...)
Match any literal passed in, succeeds with the match

Parameters:

  • ... a sequence of literals to try in order

Returns:

    a parser function that matches any of the literals
Parsel.untilLiteral (literal)
Match the parsers string until the specified literal is found

Parameters:

  • literal the literal to match until

Returns:

    a parser function matched until right before the literal is found or end of string

Usage:

    local untilEnd = parsel.untilLiteral('end')
    parsel.parse("if then end").result
     -- "if then "

Combinators

Parsel.any (...)
Parse any combinators specified in the list

Parameters:

  • ... a list of required parsers to parse in attempt in order

Returns:

    a combined parser function
Parsel.either (p1, p2)
Try first parser, and if that fails, try the second parser

Parameters:

  • p1 the first parser to try
  • p2 the second parser to try

Returns:

    a parser function
Parsel.oneOrMore (p)
Parse a combinator at least one time and until the parse fails

Parameters:

  • p the parser to attempt one or more times

Returns:

    a parser function
Parsel.seq (...)
Parse all combinators in sequence

Parameters:

  • ... a list of required parsers to parse in sequence

Returns:

    a combined parser function
Parsel.zeroOrMore (p)
Parse zero or more instances of combinators

Parameters:

  • p the parser to attempt zero or more times

Returns:

    a parser function
Parsel.optional (p)
Optionally parse a combinator, return Parsel.nullResult if not matched

Parameters:

  • p parser to attempt, if failed, nullResult will be returned

Returns:

    a parser function

See also:

Parsel.lazy (f)
Returns a parser that lazily evaluates a function

Parameters:

  • f func (must return a parser)

Returns:

    a parser function
Parsel.sepBy (p, delim)
Match parsers delimited by successful parse of delim the result is a table containing just the parsed values (delimiter ignored) fails if parsing a delimiter then parser fails, or if the first parsing of p fails

Parameters:

  • p the parser to match repeatedly
  • delim the parser to match as a delimiter

Returns:

    a parser function
Parsel.sepByAllowTrailing (p, delim)
Match parsers delimited by successful parse of delim, but allow a trailing delimiter

Parameters:

  • p the parser to match repeatedly
  • delim the parser to match as a delimiter

Returns:

    a parser function

See also:

Parsel.exclude (p, exclusionFunc)
Fails a parser if it matches condition set by exclusionFunc

Parameters:

  • p parser to wrap
  • exclusionFunc if this function returns true, the parser will fail

Returns:

    a parser function that parses p and fails if the exclusionFunc criteria is matched

Usage:

    local ignoreOdd = parsel.exclude(parsel.digit(), function(d)
      return tonumber(d)%2 == 1
    end)
    local evenString = parsel.oneOrMore(ignoreOdd)
    table.concat(parsel.parse("2468").result, "")
     -- 246
generated by LDoc 1.5.0