Module

Text.Parsing.Parser.Combinators

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.many1 = Data.(Array|List).some
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

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.

#(<?>)

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

#asErrorMessage

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

Flipped (<?>).

#(<??>)

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

#between

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

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

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

Optionally parse something, failing quietly.

#optionMaybe

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

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

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

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.

#sepBy

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 ","

#sepBy1

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.

#sepEndBy

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.

#sepEndBy1

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.

#endBy1

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.

#endBy

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.

#chainr

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

#chainl

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.

#chainl1

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.

#chainl1'

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

#chainr1

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.

#chainr1'

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

#choice

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

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

Skip many instances of a phrase.

#skipMany1

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

Skip at least one instance of a phrase.

#notFollowedBy

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

Fail if the specified parser matches.

#manyTill

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.

#many1Till

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.

Modules