miso
Copyright(C) 2016-2026 David M. Johnson
LicenseBSD3-style (see the file LICENSE)
MaintainerDavid M. Johnson <code@dmj.io>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellSafe-Inferred
LanguageHaskell2010

Miso.Util.Parser

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

newtype ParserT r token m a = Parser
  { runParserT :: r -> token -> m (a, token) }

type Parser token a = ParserT () [token] [] a

ParserT is a monad transformer parameterised by:

  • r — a read-only environment (accessible via askParser)
  • token — the input stream type (typically [t])
  • m — the result monad; using [] gives non-deterministic/backtracking parsing
  • a — the parsed result

The Parser token a convenience alias fixes r = () and m = [], which gives a standard backtracking parser over a [token] stream.

Primitive combinators

anyToken    :: ParserT r [a] [] a         -- consume any single token
satisfy     :: (a -> Bool) -> ParserT r [a] [] a  -- consume if predicate holds
token_      :: Eq t => t -> Parser t t    -- match a specific token
peek        :: Parser a a                 -- look ahead without consuming
endOfInput  :: Parser a ()                -- succeed only at end of stream
allTokens   :: ParserT r a [] a           -- return the entire remaining stream
modifyTokens :: (t -> t) -> ParserT r t [] ()  -- transform the token stream
askParser   :: ParserT r token [] r       -- read the environment
errorOut    :: e -> ParserT r e [] ()     -- inject a custom error token

Error type

data ParseError a token
  = UnexpectedParse [token]  -- input remained after a successful parse
  | LexicalError LexerError  -- upstream lex failure
  | Ambiguous [(a, [token])] -- multiple distinct parses
  | NoParses token           -- no parse succeeded
  | EmptyStream              -- input was empty

Running a parser

parse :: Parser token a -> [token] -> Either (ParseError a token) a

parse returns Right only when exactly one parse consumes all input. Ambiguous or partial parses produce a Left error.

See also

Synopsis

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

  • runParserT :: r -> token -> m (a, token)

    Run the parser given a read-only environment r and input token; returns zero or more (result, remaining-input) pairs in m

Instances

Instances details
Alternative (ParserT r token []) Source # 
Instance details

Defined in Miso.Util.Parser

Methods

empty :: ParserT r token [] a #

(<|>) :: ParserT r token [] a -> ParserT r token [] a -> ParserT r token [] a #

some :: ParserT r token [] a -> ParserT r token [] [a] #

many :: ParserT r token [] a -> ParserT r token [] [a] #

Applicative (ParserT r token []) Source # 
Instance details

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 # 
Instance details

Defined in Miso.Util.Parser

Methods

fmap :: (a -> b) -> ParserT r token [] a -> ParserT r token [] b #

(<$) :: a -> ParserT r token [] b -> ParserT r token [] a #

Monad (ParserT r token []) Source # 
Instance details

Defined in Miso.Util.Parser

Methods

(>>=) :: ParserT r token [] a -> (a -> ParserT r token [] b) -> ParserT r token [] b #

(>>) :: ParserT r token [] a -> ParserT r token [] b -> ParserT r token [] b #

return :: a -> ParserT r token [] a #

MonadPlus (ParserT r token []) Source # 
Instance details

Defined in Miso.Util.Parser

Methods

mzero :: ParserT r token [] a #

mplus :: ParserT r token [] a -> ParserT r token [] a -> ParserT r token [] a #

MonadFail (ParserT r token []) Source # 
Instance details

Defined in Miso.Util.Parser

Methods

fail :: String -> ParserT r token [] a #

data ParseError a token Source #

A type for expressing failure during parsing.

Constructors

UnexpectedParse [token] 
LexicalError LexerError 
Ambiguous [(a, [token])] 
NoParses token 
EmptyStream 

Instances

Instances details
(Show a, Show token) => Show (ParseError a token) Source # 
Instance details

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 # 
Instance details

Defined in Miso.Util.Parser

Methods

(==) :: ParseError a token -> ParseError a token -> Bool #

(/=) :: ParseError a token -> ParseError a token -> Bool #

Combinators

parse Source #

Arguments

:: Parser token a

Parser to run

-> [token]

Input token stream

-> Either (ParseError a token) a 

Executes a parser against a series of tokens.

anyToken :: ParserT r [a] [] a Source #

Match any token.

satisfy Source #

Arguments

:: (a -> Bool)

Predicate; the next token is consumed only if this returns True

-> ParserT r [a] [] a 

Succeeds for any token for which the predicate f returns True. Returns the parsed token.

peek :: Parser a a Source #

Views the next token without consuming input

token_ Source #

Arguments

:: Eq token 
=> token

Expected token value

-> Parser token token 

Succeeds if the next token in the stream matches the given one. Returns the parsed token.

errorOut Source #

Arguments

:: errorToken

Error token injected as the current stream (useful for error propagation)

-> ParserT r errorToken [] () 

Parser combinator that always fails

allTokens :: ParserT r a [] a Source #

Returns all input from a parser

modifyTokens Source #

Arguments

:: (t -> t)

Transform to apply to the current token stream

-> ParserT r t [] () 

Modifies tokens

askParser :: ParserT r token [] r Source #

Retrieves read-only state from a Parser

endOfInput :: Parser a () Source #

Parser combinator that only succeeds if there are no more tokens.