Module

Text.Parsing.Parser.Combinators

Package
purescript-parsing
Repository
purescript-contrib/purescript-parsing

Combinators for creating parsers.

Notes

A few of the known combinators from Parsec are missing in this module. That is because they have already been defined in other libraries.

Text.Parsec.many  = Data.(Array|List).many
Text.Parsec.(<|>) = Control.Alt.alt (<|>)

Because Strings are not Char Arrays in PureScript many and some on Char Parsers need to be used in conjunction with Data.String.CodeUnits.fromCharArray to achieve "Parsec-like" results.

Text.Parsec.many  (char 'x') <=> fromCharArray <$> Data.Array.many (char 'x')

Note that Data.(Array|List).(many|some) are not stack safe. If you need to parse large numbers of items then consider using Data.List.(manyRec|someRec) instead.

#withErrorMessage Source

withErrorMessage :: forall m s a. Monad m => ParserT s m a -> String -> ParserT s m a

Provide an error message in the case of failure.

#(<?>) Source

Operator alias for Text.Parsing.Parser.Combinators.withErrorMessage (left-associative / precedence 3)

#withLazyErrorMessage Source

withLazyErrorMessage :: forall m s a. Monad m => ParserT s m a -> (Unit -> String) -> ParserT s m a

Provide an error message in the case of failure, but lazily. This is handy in cases where constructing the error message is expensive, so it's preferable to defer it until an error actually happens.

parseBang :: Parser Char
parseBang = char '!' <~?> \_ -> "Expected a bang"

#(<~?>) Source

Operator alias for Text.Parsing.Parser.Combinators.withLazyErrorMessage (left-associative / precedence 3)

#asErrorMessage Source

asErrorMessage :: forall m s a. Monad m => String -> ParserT s m a -> ParserT s m a

Flipped (<?>).

#(<??>) Source

Operator alias for Text.Parsing.Parser.Combinators.asErrorMessage (left-associative / precedence 3)

#between Source

between :: forall m s a open close. Monad m => ParserT s m open -> ParserT s m close -> ParserT s m a -> ParserT s m a

Wrap a parser with opening and closing markers.

For example:

parens = between (string "(") (string ")")

#option Source

option :: forall m s a. Monad m => a -> ParserT s m a -> ParserT s m a

Provide a default result in the case where a parser fails without consuming input.

#optional Source

optional :: forall m s a. Monad m => ParserT s m a -> ParserT s m Unit

Optionally parse something, failing quietly.

#optionMaybe Source

optionMaybe :: forall m s a. Monad m => ParserT s m a -> ParserT s m (Maybe a)

pure Nothing in the case where a parser fails without consuming input.

#try Source

try :: forall m s a. Monad m => ParserT s m a -> ParserT s m a

In case of failure, reset the stream to the unconsumed state.

#tryRethrow Source

tryRethrow :: forall m s a. Monad m => ParserT s m a -> ParserT s m a

Like try, but will reannotate the error location to the try point.

#lookAhead Source

lookAhead :: forall s a m. Monad m => ParserT s m a -> ParserT s m a

Parse a phrase, without modifying the consumed state or stream position.

#many1 Source

many1 :: forall m s a. Monad m => ParserT s m a -> ParserT s m (NonEmptyList a)

Match one or more times.

#many1Rec Source

many1Rec :: forall m s a. MonadRec m => ParserT s m a -> ParserT s m (NonEmptyList a)

Stack-safe version of many1 at the expense of MonadRec constraint

#sepBy Source

sepBy :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)

Parse phrases delimited by a separator.

For example:

digit `sepBy` string ","

#sepByRec Source

sepByRec :: forall m s a sep. MonadRec m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)

Stack-safe version of sepBy at the expense of MonadRec constraint

#sepBy1 Source

sepBy1 :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (NonEmptyList a)

Parse phrases delimited by a separator, requiring at least one match.

#sepBy1Rec Source

sepBy1Rec :: forall m s a sep. MonadRec m => ParserT s m a -> ParserT s m sep -> ParserT s m (NonEmptyList a)

Stack-safe version of sepBy1 at the expense of MonadRec constraint

#sepEndBy Source

sepEndBy :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)

Parse phrases delimited and optionally terminated by a separator.

#sepEndByRec Source

sepEndByRec :: forall m s a sep. MonadRec m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)

Stack-safe version of sepEndBy at the expense of MonadRec constraint

#sepEndBy1 Source

sepEndBy1 :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (NonEmptyList a)

Parse phrases delimited and optionally terminated by a separator, requiring at least one match.

#sepEndBy1Rec Source

sepEndBy1Rec :: forall m s a sep. MonadRec m => ParserT s m a -> ParserT s m sep -> ParserT s m (NonEmptyList a)

Stack-safe version of sepEndBy1 at the expense of MonadRec constraint

#endBy1 Source

endBy1 :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (NonEmptyList a)

Parse phrases delimited and terminated by a separator, requiring at least one match.

#endBy1Rec Source

endBy1Rec :: forall m s a sep. MonadRec m => ParserT s m a -> ParserT s m sep -> ParserT s m (NonEmptyList a)

Stack-safe version of endBy1 at the expense of MonadRec constraint

#endBy Source

endBy :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)

Parse phrases delimited and terminated by a separator.

#endByRec Source

endByRec :: forall m s a sep. MonadRec m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)

Stack-safe version of endBy at the expense of MonadRec constraint

#chainr Source

chainr :: forall m s a. Monad m => ParserT s m a -> ParserT s m (a -> a -> a) -> a -> ParserT s m a

Parse phrases delimited by a right-associative operator.

For example:

chainr digit (string "+" $> add) 0

#chainrRec Source

chainrRec :: forall m s a. MonadRec m => ParserT s m a -> ParserT s m (a -> a -> a) -> a -> ParserT s m a

Stack-safe version of chainr at the expense of MonadRec constraint.

#chainl Source

chainl :: forall m s a. Monad m => ParserT s m a -> ParserT s m (a -> a -> a) -> a -> ParserT s m a

Parse phrases delimited by a left-associative operator.

For example:

chainr digit (string "+" $> add) 0

#chainlRec Source

chainlRec :: forall m s a. MonadRec m => ParserT s m a -> ParserT s m (a -> a -> a) -> a -> ParserT s m a

Stack-safe version of chainl at the expense of MonadRec constraint.

#chainl1 Source

chainl1 :: forall m s a. Monad m => ParserT s m a -> ParserT s m (a -> a -> a) -> ParserT s m a

Parse phrases delimited by a left-associative operator, requiring at least one match.

#chainl1Rec Source

chainl1Rec :: forall m s a. MonadRec m => ParserT s m a -> ParserT s m (a -> a -> a) -> ParserT s m a

Stack-safe version of chainl1 at the expense of MonadRec constraint.

#chainr1 Source

chainr1 :: forall m s a. Monad m => ParserT s m a -> ParserT s m (a -> a -> a) -> ParserT s m a

Parse phrases delimited by a right-associative operator, requiring at least one match.

#chainr1Rec Source

chainr1Rec :: forall m s a. MonadRec m => ParserT s m a -> ParserT s m (a -> a -> a) -> ParserT s m a

Stack-safe version of chainr1 at the expense of MonadRec constraint.

#choice Source

choice :: forall f m s a. Foldable f => Monad m => f (ParserT s m a) -> ParserT s m a

Parse one of a set of alternatives.

#skipMany Source

skipMany :: forall s a m. Monad m => ParserT s m a -> ParserT s m Unit

Skip many instances of a phrase.

#skipManyRec Source

skipManyRec :: forall s a m. MonadRec m => ParserT s m a -> ParserT s m Unit

Stack-safe version of skipMany at the expense of MonadRec constraint.

#skipMany1 Source

skipMany1 :: forall s a m. Monad m => ParserT s m a -> ParserT s m Unit

Skip at least one instance of a phrase.

#skipMany1Rec Source

skipMany1Rec :: forall s a m. MonadRec m => ParserT s m a -> ParserT s m Unit

Stack-safe version of skipMany1 at the expense of MonadRec constraint.

#notFollowedBy Source

notFollowedBy :: forall s a m. Monad m => ParserT s m a -> ParserT s m Unit

Fail if the specified parser matches.

#manyTill Source

manyTill :: forall s a m e. Monad m => ParserT s m a -> ParserT s m e -> ParserT s m (List a)

Parse several phrases until the specified terminator matches.

#manyTillRec Source

manyTillRec :: forall s a m e. MonadRec m => ParserT s m a -> ParserT s m e -> ParserT s m (List a)

Stack-safe version of manyTill at the expense of MonadRec constraint

#many1Till Source

many1Till :: forall s a m e. Monad m => ParserT s m a -> ParserT s m e -> ParserT s m (NonEmptyList a)

Parse several phrases until the specified terminator matches, requiring at least one match.

#many1TillRec Source

many1TillRec :: forall s a m e. MonadRec m => ParserT s m a -> ParserT s m e -> ParserT s m (NonEmptyList a)

Stack-safe version of many1Till at the expense of MonadRec constraint