| Copyright | (C) 2016-2026 David M. Johnson |
|---|---|
| License | BSD3-style (see the file LICENSE) |
| Maintainer | David M. Johnson <code@dmj.io> |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
Miso.Util.Parser
Contents
Description
Overview
Miso.Util.Parser is an internal parser combinator library that operates over a token stream produced by Miso.Util.Lexer. It is used by miso's JSON pipeline (Miso.JSON.Parser) and the client-side router (Miso.Router). It is not designed for general application use, but is exposed for downstream code that needs to build custom parsers.
Core types
newtypeParserTr token m a = Parser {runParserT:: r -> token -> m (a, token) } typeParsertoken a =ParserT() [token] [] a
ParserT is a monad transformer parameterised by:
r— a read-only environment (accessible viaaskParser)token— the input stream type (typically[t])m— the result monad; using[]gives non-deterministic/backtracking parsinga— the parsed result
The convenience alias fixes Parser token ar = () and m = [],
which gives a standard backtracking parser over a [token] stream.
Primitive combinators
anyToken::ParserTr [a] [] a -- consume any single tokensatisfy:: (a -> Bool) ->ParserTr [a] [] a -- consume if predicate holdstoken_:: Eq t => t ->Parsert t -- match a specific tokenpeek::Parsera a -- look ahead without consumingendOfInput::Parsera () -- succeed only at end of streamallTokens::ParserTr a [] a -- return the entire remaining streammodifyTokens:: (t -> t) ->ParserTr t [] () -- transform the token streamaskParser::ParserTr token [] r -- read the environmenterrorOut:: e ->ParserTr e [] () -- inject a custom error token
Error type
dataParseErrora token = UnexpectedParse [token] -- input remained after a successful parse | LexicalErrorLexerError-- upstream lex failure | Ambiguous [(a, [token])] -- multiple distinct parses | NoParses token -- no parse succeeded | EmptyStream -- input was empty
Running a parser
parse::Parsertoken a -> [token] -> Either (ParseErrora token) a
parse returns Right only when exactly one parse consumes all input.
Ambiguous or partial parses produce a Left error.
See also
- Miso.Util.Lexer — produces the token stream consumed here
- Miso.JSON.Parser — JSON parser built on this module
- Miso.Router — URI/route parser built on this module
- Miso.Util —
sepBy,oneOfused alongside parsers
Synopsis
- type Parser token a = ParserT () [token] [] a
- newtype ParserT r token (m :: Type -> Type) a = Parser {
- runParserT :: r -> token -> m (a, token)
- data ParseError a token
- = UnexpectedParse [token]
- | LexicalError LexerError
- | Ambiguous [(a, [token])]
- | NoParses token
- | EmptyStream
- parse :: Parser token a -> [token] -> Either (ParseError a token) a
- anyToken :: ParserT r [a] [] a
- satisfy :: (a -> Bool) -> ParserT r [a] [] a
- peek :: Parser a a
- token_ :: Eq token => token -> Parser token token
- errorOut :: errorToken -> ParserT r errorToken [] ()
- allTokens :: ParserT r a [] a
- modifyTokens :: (t -> t) -> ParserT r t [] ()
- askParser :: ParserT r token [] r
- endOfInput :: Parser a ()
Types
type Parser token a = ParserT () [token] [] a Source #
Convenience synonym when defining parser combinators
newtype ParserT r token (m :: Type -> Type) a Source #
Core type for parsing
Constructors
| Parser | |
Fields
| |
Instances
| Alternative (ParserT r token []) Source # | |
| Applicative (ParserT r token []) Source # | |
Defined in Miso.Util.Parser Methods pure :: a -> ParserT r token [] a # (<*>) :: ParserT r token [] (a -> b) -> ParserT r token [] a -> ParserT r token [] b # liftA2 :: (a -> b -> c) -> ParserT r token [] a -> ParserT r token [] b -> ParserT r token [] c # (*>) :: ParserT r token [] a -> ParserT r token [] b -> ParserT r token [] b # (<*) :: ParserT r token [] a -> ParserT r token [] b -> ParserT r token [] a # | |
| Functor (ParserT r token []) Source # | |
| Monad (ParserT r token []) Source # | |
| MonadPlus (ParserT r token []) Source # | |
| MonadFail (ParserT r token []) Source # | |
data ParseError a token Source #
A type for expressing failure during parsing.
Constructors
| UnexpectedParse [token] | |
| LexicalError LexerError | |
| Ambiguous [(a, [token])] | |
| NoParses token | |
| EmptyStream |
Instances
| (Show a, Show token) => Show (ParseError a token) Source # | |
Defined in Miso.Util.Parser Methods showsPrec :: Int -> ParseError a token -> ShowS # show :: ParseError a token -> String # showList :: [ParseError a token] -> ShowS # | |
| (Eq a, Eq token) => Eq (ParseError a token) Source # | |
Defined in Miso.Util.Parser Methods (==) :: ParseError a token -> ParseError a token -> Bool # (/=) :: ParseError a token -> ParseError a token -> Bool # | |
Combinators
Arguments
| :: Parser token a | Parser to run |
| -> [token] | Input token stream |
| -> Either (ParseError a token) a |
Executes a parser against a series of tokens.
Arguments
| :: (a -> Bool) | Predicate; the next token is consumed only if this returns |
| -> ParserT r [a] [] a |
Succeeds for any token for which the predicate f returns True.
Returns the parsed token.
Succeeds if the next token in the stream matches the given one. Returns the parsed token.
Arguments
| :: errorToken | Error token injected as the current stream (useful for error propagation) |
| -> ParserT r errorToken [] () |
Parser combinator that always fails
Modifies tokens
endOfInput :: Parser a () Source #
Parser combinator that only succeeds if there are no more tokens.