microparsec/types

Types

ParseState = ref object
  stream: Stream
  position, lastPosition: ParsePosition
A ParseState keeps track of the Stream and where we are at it.   Source Edit
ParseResult[T] = Result[T, ParseError]
A ParseResult of type T is a Result object with either a parsed object of type T or a ParseError.   Source Edit
Parser[T] = ParseState -> ParseResult[T]
A Parser for a type T is a function that receives a ParseState and gives back a ParseResult of type T.   Source Edit

Procs

proc `$`[T](res: ParseResult[T]): string {...}{.inline.}
Represent a ParseResult as a string. Mostly used to print errors.   Source Edit
proc peekChar(state: ParseState): char {...}{.inline, raises: [IOError, OSError],
    tags: [ReadIOEffect].}
Peeks a char from the ParseState state. Raises IOError if an error occurred. Returns '0' as an EOF marker.   Source Edit
proc readLastStr(state: ParseState; length: int): string {...}{.inline,
    raises: [IOError, OSError], tags: [ReadIOEffect].}
Reads a string of length length from the ParseState state ending at the current position. Raises IOError if an error occurred.   Source Edit
proc readStr(state: ParseState; length: int): string {...}{.inline,
    raises: [IOError, OSError], tags: [ReadIOEffect].}

Reads a string of length length from the ParseState state starting at the current position. Raises IOError if an error occurred.

Note: This function is experimental.

  Source Edit
proc debugParse[T](parser: Parser[T]; x: auto; withPosition = false): string {...}{.
    inline.}
  Source Edit

Funcs

func fail[T](unexpected: string; expected: openArray[string]; state: ParseState;
             message = ""): ParseResult[T] {...}{.inline.}
Stop parsing and report a ParseError.   Source Edit
func fail[T](res: ParseResult[auto]): ParseResult[T] {...}{.inline.}
Stop parsing and report the ParseError of a ParseResult.   Source Edit
func newParseState(s: Stream): ParseState {...}{.inline, raises: [], tags: [].}
Creates a new ParseState from the stream s.   Source Edit

Templates

template atEnd(state: ParseState): bool
Checks if more data can be read from state. Returns true if all data has been read.   Source Edit
template stepBack(state: ParseState)
Go a step back.   Source Edit
template getPosition(state: ParseState): int
Retrieves the current position in the ParseState state.   Source Edit
template readChar(state: ParseState): char
Reads a char from the ParseState state. Raises IOError if an error occurred. Returns '0' as an EOF marker.   Source Edit
template newParseState(s: string): ParseState
Creates a new ParseState from the string s.   Source Edit
template parse[T; ](parser: Parser[T]; x: auto): ParseResult[T]
Apply a Parser to x.   Source Edit
template parse[T](parser: Parser[T]; x: ParseState): ParseResult[T]
Apply a Parser to x.   Source Edit