microparsec/combinators

Funcs

func `<?>`[T](parser: Parser[T]; expected: string): Parser[T] {...}{.inline.}

Build a Parser that behaves as parser, but whenever parser fails, it replaces expect error messages with expected. As such, this function effectively names a parser, in case failure occurs.

This is normally used at the end of a set alternatives where we want to return an error message in terms of a higher level construct rather than returning all possible characters.

Note: In the future, this might become a template, so that functions such as satisfy won't need a expected parameter for performance reasons.

  Source Edit
func choice[T](parsers: openArray[Parser[T]]): Parser[T] {...}{.inline.}

A Parser that tries to apply the actions in a sequence in order, until one of them succeeds. Returns the value of the succeeding action.

Note: This might use varargs in the future, but this proves to be more convenient. Furthermore, the current behavior is undefined for an empty sequence of parsers. In the future, this will be based in an Alternative empty.

  Source Edit
func sepBy1[S, T](parser: Parser[T]; separator: Parser[S]): Parser[seq[T]] {...}{.
    inline.}

Create a Parser that applies one or more occurrences of parser, separated by separator. Returns a sequence of the values returned by parser.

Note: Currently, sepBy is defined in function of this function. This might change in the future, if the implementation can be made simpler some other way.

  Source Edit
func manyTill[S, T](parser: Parser[T]; endparser: Parser[S]): Parser[seq[T]] {...}{.
    inline.}

A Parser that applies an action zero or more times until another action succeeds, and returns the sequence of values returned by the first action.

Note: Error messages are not good enough yet, but the current implementation is comparable to Attoparsec's.

  Source Edit
func skipMany[T](parser: Parser[T]): Parser[void] {...}{.inline.}
Build a Parser that skips zero or more instances of an action.   Source Edit
func count[T](n: int; parser: Parser[T]): Parser[seq[T]] {...}{.inline.}

A Parser that applies the given action repeatedly, returning every result.

Note: This short circuits in case of errors.

  Source Edit

Templates

template attempt[T](parser: Parser[T]): Parser[T]

Create a Parser that attempts a parse, and if it fails, rewind the input so that no input appears to have been consumed.

This function is called try in Parsec, but this conflicts with the try keyword in Nim.

This combinator is provided for compatibility with Parsec. We follow Attoparsec's implementation, which always backtracks on failure.

Note: We mean to deprecate this function once we're past 0.1 or so.

  Source Edit
template option[T](x: T; parser: Parser[T]): Parser[T]
A Parser that tries to apply an action. If it fails without consuming input, it returns the value x, otherwise the value returned by the action.   Source Edit
template many1[T](parser: Parser[T]): Parser[seq[T]]
Build a Parser that applies another Parser one or more times. Returns a sequence of the parsed values.   Source Edit
template sepBy[S; T](parser: Parser[T]; separator: Parser[S]): Parser[seq[T]]

A Parser that applies zero or more occurrences of parser, separated by separator. Returns a sequence of the values returned by parser.

Note: Attoparsec's implementation seems to be too complicated, (unnecessarily?) testing for a first occurrence. Maybe there's a performance reason, I don't know. Ours might be simpler, but there might be a catch somewhere. In any case, tests pass. Be advised, and report any issue you might find!

  Source Edit
template skipMany1[T](parser: Parser[T]): Parser[void]
Build a Parser that skips one or more instances of an action.   Source Edit
template optional[T](parser: Parser[T]): Parser[void]
Create a Parser that tries to apply parser. It might consume input if parser is successful and consumes input. And due to backtracking, it never fails. The result of parser is discarded.   Source Edit
template between[R; S; T](open: Parser[R]; parser: Parser[T]; close: Parser[S]): Parser[
    T]

Create a Parser that parses open, followed by parser and then close, returning the value given by parser.

Note: This function is experimental.

  Source Edit
template eof(state: ParseState): ParseResult[void]
A Parser that only succeeds at the end of the input. This is not a primitive parser but it is defined using notFollowedBy.   Source Edit