Module

Text.Parsing.StringParser

This module defines the Parser type of string parsers, and its instances.

#Pos

type Pos = Int

A position in an input string.

#PosString

type PosString = { pos :: Pos, str :: String }

Strings are represented as a string with an index from the start of the string.

{ str: s, pos: n } is interpreted as the substring of s starting at index n.

This allows us to avoid repeatedly finding substrings every time we match a character.

#ParseError

type ParseError = { error :: String, pos :: Pos }

The type of parsing errors.

#Parser

newtype Parser a

A parser is represented as a function that, when successful, returns a result and the position where the parse finished or, when it fails, a ParserError with more information on where and why it failed. See also printParserError.

Constructors

Instances

#unParser

unParser :: forall a. Parser a -> PosString -> Either ParseError { result :: a, suffix :: PosString }

Run a parser, allowing the caller to define where to start within the input String and what to do with the unchanged output of the Parser. See runparser for more typical usages.

#runParser

runParser :: forall a. Parser a -> String -> Either ParseError a

Run a parser for an input string. See also printParserError and unParser for more flexible usages.

#printParserError

printParserError :: ParseError -> String

Prints a ParseError's the error message and the position of the error.

#fail

fail :: forall a. String -> Parser a

Fail with the specified message.

#try

try :: forall a. Parser a -> Parser a

In case of error, the default behavior is to backtrack if no input was consumed.

try p backtracks even if input was consumed.

Modules