Module
Text.Parsing.Parser
- Package
- purescript-parsing
- Repository
- purescript-contrib/purescript-parsing
#ParseError Source
data ParseError
A parsing error, consisting of a message and position information.
Constructors
Instances
Show ParseError
Eq ParseError
Ord ParseError
(Monad m) => MonadThrow ParseError (ParserT s m)
(Monad m) => MonadError ParseError (ParserT s m)
#ParseState Source
data ParseState s
Contains the remaining input and current position.
Constructors
Instances
(Monad m) => MonadState (ParseState s) (ParserT s m)
#ParserT Source
newtype ParserT :: Type -> (Type -> Type) -> Type -> Type
newtype ParserT s m a
The Parser monad transformer.
The first type argument is the stream type. Typically, this is either String
,
or some sort of token stream.
Constructors
ParserT (ExceptT ParseError (StateT (ParseState s) m) a)
Instances
Newtype (ParserT s m a) _
Lazy (ParserT s m a)
(Monad m, Semigroup a) => Semigroup (ParserT s m a)
(Monad m, Monoid a) => Monoid (ParserT s m a)
(Functor m) => Functor (ParserT s m)
(Monad m) => Apply (ParserT s m)
(Monad m) => Applicative (ParserT s m)
(Monad m) => Bind (ParserT s m)
(Monad m) => Monad (ParserT s m)
(MonadRec m) => MonadRec (ParserT s m)
(Monad m) => MonadState (ParseState s) (ParserT s m)
(Monad m) => MonadThrow ParseError (ParserT s m)
(Monad m) => MonadError ParseError (ParserT s m)
(Monad m) => Alt (ParserT s m)
(Monad m) => Plus (ParserT s m)
(Monad m) => Alternative (ParserT s m)
(Monad m) => MonadZero (ParserT s m)
(Monad m) => MonadPlus (ParserT s m)
MonadTrans (ParserT s)
#runParser Source
runParser :: forall s a. s -> Parser s a -> Either ParseError a
Apply a parser, keeping only the parsed result.
#runParserT Source
runParserT :: forall m s a. Monad m => s -> ParserT s m a -> m (Either ParseError a)
Apply a parser, keeping only the parsed result.
#mapParserT Source
mapParserT :: forall b n s a m. (m (Tuple (Either ParseError a) (ParseState s)) -> n (Tuple (Either ParseError b) (ParseState s))) -> ParserT s m a -> ParserT s n b
Change the underlying monad action and data type in a ParserT monad action.
#failWithPosition Source
failWithPosition :: forall m s a. Monad m => String -> Position -> ParserT s m a
Fail with a message and a position.
#region Source
region :: forall m s a. Monad m => (ParseError -> ParseError) -> ParserT s m a -> ParserT s m a
Contextualize parsing failures inside a region. If a parsing failure
occurs, then the ParseError
will be transformed by each containing
region
as the parser backs out the call stack.
The alternative
Alt
instance provides thealt
combinator<|>
.The expression
p_left <|> p_right
will first try thep_left
parser and if that fails and consumes no input then it will try thep_right
parser.While we are parsing down the
p_left
branch we may reach a point where we know this is the correct branch, but we cannot parse further. At that point we want to fail the entire parse instead of trying thep_right
branch.For example, consider this
fileParser
which can parse either an HTML file that begins with<html>
or a shell script file that begins with#!
.If we read a file from disk and run this
fileParser
on it and thestring "<html>"
parser succeeds, then we know that the first branch is the correct branch. Even if theparseTheRestOfTheHtml
parser fails we don’t want to try the other branch.To control the point at which we commit to the
p_left
branch use thetry
combinator and thelookAhead
combinator and theconsume
function.The
alt
combinator works this way because it gives us good localized error messages while also allowing an efficient implementation.