Parsing
- Package
- purescript-parsing
- Repository
- purescript-contrib/purescript-parsing
Types and operations for monadic parsing.
Combinators are in the Parsing.Combinators module.
Primitive parsers for String input streams are in the Parsing.String
module.
#runParser Source
runParser :: forall s a. s -> Parser s a -> Either ParseError aRun a parser on an input stream s and produce either an error or the
result a of the parser.
#ParserT Source
newtype ParserT :: Type -> (Type -> Type) -> Type -> Typenewtype ParserT s m a
The Parser s monad with a monad transformer parameter m.
Constructors
ParserT (forall r. Fn5 (ParseState s) ((Unit -> r) -> r) (m (Unit -> r) -> r) (Fn2 (ParseState s) ParseError r) (Fn2 (ParseState s) a r) r)
Instances
Lazy (ParserT s m a)(Semigroup a) => Semigroup (ParserT s m a)(Monoid a) => Monoid (ParserT s m a)Functor (ParserT s m)Apply (ParserT s m)Applicative (ParserT s m)Bind (ParserT s m)Monad (ParserT s m)MonadRec (ParserT s m)(MonadState t m) => MonadState t (ParserT s m)MonadThrow ParseError (ParserT s m)MonadError ParseError (ParserT s m)Alt (ParserT s m)Plus (ParserT s m)Alternative (ParserT s m)MonadPlus (ParserT s m)MonadTrans (ParserT s)
#runParserT Source
runParserT :: forall m s a. MonadRec m => s -> ParserT s m a -> m (Either ParseError a)runParser with a monad transfomer parameter m.
#runParserT' Source
runParserT' :: forall m s a. MonadRec m => ParseState s -> ParserT s m a -> m (Tuple (Either ParseError a) (ParseState s))Run a parser and produce either an error or the result of the parser along with the internal state of the parser when it finishes.
#ParseError Source
data ParseErrorA parsing error, consisting of an error message and the position in the input stream at which the error occurred.
Constructors
Instances
#Position Source
newtype PositionPosition represents the position of the parser in the input stream.
indexis the position offset since the start of the input. Starts at 0.lineis the current line in the input. Starts at 1.columnis the column of the next character in the current line that will be parsed. Starts at 1.
Constructors
Instances
#initialPos Source
initialPos :: PositionThe Position before any input has been parsed.
{ index: 0, line: 1, column: 1 }
#failWithPosition Source
failWithPosition :: forall m s a. String -> Position -> ParserT s m aFail with a message and a position.
#region Source
region :: forall m s a. (ParseError -> ParseError) -> ParserT s m a -> ParserT s m aContextualize 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.
#ParseState Source
data ParseState sThe internal state of the ParserT s m monad.
Contains the remaining input and current position and the consumed flag.
The consumed flag is used to implement the rule for alt that
- If the left parser fails without consuming any input, then backtrack and try the right parser.
- If the left parser fails and consumes input, then fail immediately.
Constructors
#stateParserT Source
stateParserT :: forall s m a. (ParseState s -> Tuple a (ParseState s)) -> ParserT s m aQuery and modify the ParserT internal state.
Like the state member of MonadState.
#getParserT Source
getParserT :: forall s m. ParserT s m (ParseState s)Query the ParserT internal state.
Like the get member of MonadState.
#mapParserT Source
mapParserT :: forall b n s a m. MonadRec m => Functor n => (m (Tuple (Either ParseError a) (ParseState s)) -> n (Tuple (Either ParseError b) (ParseState s))) -> ParserT s m a -> ParserT s n bChange the underlying monad action m and result data type a in
a ParserT s m monad action.
The alternative
Altinstance provides thealtcombinator<|>.The expression
p_left <|> p_rightwill first try thep_leftparser and if that fails and consumes no input then it will try thep_rightparser.While we are parsing down the
p_leftbranch 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_rightbranch.For example, consider this
fileParserwhich 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
fileParseron it and thestring "<html>"parser succeeds, then we know that the first branch is the correct branch, so we want to commit to the first branch. Even if theparseTheRestOfTheHtmlparser fails we don’t want to try the second branch.To control the point at which we commit to the
p_leftbranch use thetrycombinator and thelookAheadcombinator and theconsumefunction.The
altcombinator works this way because it gives us good localized error messages while also allowing an efficient implementation. See Parsec: Direct Style Monadic Parser Combinators For The Real World section 2.3 Backtracking.