Module

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.

#Parser Source

type Parser s = ParserT s Identity

The Parser s monad, where s is the type of the input stream.

A synonym for the ParserT monad transformer applied to the Identity monad.

#runParser Source

runParser :: forall s a. s -> Parser s a -> Either ParseError a

Run 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

Instances

#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 ParseError

A parsing error, consisting of an error message and the position in the input stream at which the error occurred.

Constructors

Instances

#Position Source

newtype Position

Position represents the position of the parser in the input stream.

  • index is the position offset since the start of the input. Starts at 0.
  • line is the current line in the input. Starts at 1.
  • column is the column of the next character in the current line that will be parsed. Starts at 1.

Constructors

Instances

#initialPos Source

initialPos :: Position

The Position before any input has been parsed.

{ index: 0, line: 1, column: 1 }

#consume Source

consume :: forall s m. ParserT s m Unit

Set the consumed flag.

Setting the consumed flag means that we're committed to this parsing branch of an alternative (<|>), so that if this branch fails then we want to fail the entire parse instead of trying the other alternative.

#position Source

position :: forall s m. ParserT s m Position

Returns the current position in the stream.

#fail Source

fail :: forall m s a. String -> ParserT s m a

Fail with a message.

#failWithPosition Source

failWithPosition :: forall m s a. String -> Position -> ParserT s m a

Fail with a message and a position.

#region Source

region :: forall m s a. (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.

#ParseState Source

data ParseState s

The 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 a

Query 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.

#hoistParserT Source

hoistParserT :: forall s m n a. (m ~> n) -> ParserT s m a -> ParserT s n a

#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 b

Change the underlying monad action m and result data type a in a ParserT s m monad action.