Safe Haskell | Safe-Inferred |
---|
The core functionality of a Parcom parser: defining and running parsers, lifting, getting tokens and characters from a stream, and the most basic primitive parsers and combinators that cannot easily be expressed in terms of other parsers and combinators.
- data ParcomError = ParcomError {}
- data SourcePosition = SourcePosition {}
- data ParcomT s t m a
- parseT :: (Stream s t, Token t, Monad m) => ParcomT s t m a -> String -> s -> m (Either ParcomError a)
- type Parcom s t a = ParcomT s t Identity a
- parse :: (Stream s t, Token t) => Parcom s t a -> String -> s -> Either ParcomError a
- peek :: (Monad m, Stream s t) => ParcomT s t m t
- next :: (Monad m, Stream s t, Token t) => ParcomT s t m t
- atEnd :: (Monad m, Stream s t) => ParcomT s t m Bool
- try :: Monad m => ParcomT s t m a -> ParcomT s t m a
- handle :: Monad m => ParcomT s t m a -> (ParcomError -> ParcomT s t m b) -> (a -> ParcomT s t m b) -> ParcomT s t m b
- handleB :: Monad m => ParcomT s t m a -> (ParcomError -> ParcomT s t m b) -> (a -> ParcomT s t m b) -> ParcomT s t m b
- notFollowedBy :: (Monad m, Stream s t) => ParcomT s t m a -> ParcomT s t m ()
- (<?>) :: (Monad m, Stream s t) => ParcomT s t m a -> String -> ParcomT s t m a
- (<|>) :: Alternative f => forall a. f a -> f a -> f a
- empty :: Alternative f => forall a. f a
- class Stream s t | s -> t
- class Token t
- class Listish s t | s -> t
- class Textish s
- peekChar :: (Monad m, Stream s t, Token t, Textish s) => ParcomT s t m Char
- nextChar :: (Monad m, Stream s t, Token t, Textish s) => ParcomT s t m Char
Documentation
data ParcomError Source
A parser error.
ParcomError | |
|
data SourcePosition Source
Represents a position in a source file. Both lines and columns are 1-based.
Parcom as a monad transformer. You can access the underlying monad stack using the usual lifting techniques.
MonadTrans (ParcomT s t) | ParcomT enables lifting by implementing |
Monad m => Monad (ParcomT s t m) | Parcom is a monad. Obviously. Since the Parcom monad handles both failure
through |
Monad m => Functor (ParcomT s t m) | |
Monad m => Applicative (ParcomT s t m) | |
Monad m => Alternative (ParcomT s t m) |
parseT :: (Stream s t, Token t, Monad m) => ParcomT s t m a -> String -> s -> m (Either ParcomError a)Source
Run a parcom transformer and return the result
parse :: (Stream s t, Token t) => Parcom s t a -> String -> s -> Either ParcomError aSource
Run a pure parcom parser and return the result
peek :: (Monad m, Stream s t) => ParcomT s t m tSource
Gets the next token from the stream without consuming it. Fails at end-of-input.
next :: (Monad m, Stream s t, Token t) => ParcomT s t m tSource
Gets the next token from the stream and consumes it. Fails at end-of-input.
atEnd :: (Monad m, Stream s t) => ParcomT s t m BoolSource
Checks whether end-of-input has been reached.
try :: Monad m => ParcomT s t m a -> ParcomT s t m aSource
Backtracking modifier; restores the parser state to the previous situation if the wrapped parser fails.
handle :: Monad m => ParcomT s t m a -> (ParcomError -> ParcomT s t m b) -> (a -> ParcomT s t m b) -> ParcomT s t m bSource
Wrap a raw parser to allow handling success and failure. The error and success handlers take the error or parsed value, respectively, and return a parser that should be applied in the error or success case, respectively. No backtracking is performed.
handleB :: Monad m => ParcomT s t m a -> (ParcomError -> ParcomT s t m b) -> (a -> ParcomT s t m b) -> ParcomT s t m bSource
Same as handle
, but backtrack on error (that is, if the raw parser
fails, any input it has consumed is restored.
notFollowedBy :: (Monad m, Stream s t) => ParcomT s t m a -> ParcomT s t m ()Source
Succeeds iff the given parser fails
(<?>) :: (Monad m, Stream s t) => ParcomT s t m a -> String -> ParcomT s t m aSource
Tags a parser with a human-readable description of the expected entity, generating an Expected {entity} type error message on failure.
(<|>) :: Alternative f => forall a. f a -> f a -> f a
An associative binary operation
empty :: Alternative f => forall a. f a
The identity of <|>
class Stream s t | s -> tSource
Typeclass for types that are suitable as source streams. Note that
implementing just Stream
gives you only a small subset of Parcom's
features; if you want to implement your own Stream
instances, you will
most likely also want to implement Token
for the corresponding token type,
Listish
to enable parsers that need to convert to or from lists of tokens,
and Textish
to enable parsers that work on Unicode text.
This typeclass is pretty much required to do anything useful with Parcom;
it is needed for Parcom to detect line endings so that parser errors will
report the correct source positions. If you need to parse streams that do
not support any meaningful concept of lines, consider implementing a dummy
instance, like so:
instance Token Foobar where
isLineDelimiter _ = False
This will treat the entire input as a single line.
Enables parsing on a per-character basis rather than per-token. For stream
types where the token type is Char
already, this is trivial, but for other
streams (e.g., bytestrings), some extra processing is required to perform a
conversion to Unicode.