Module

Text.Parsing.Parser.Combinators

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

Combinators in other packages

Many variations of well-known monadic and applicative combinators used for parsing are defined in other PureScript packages. It’s awkward to re-export them because their names overlap so much, so we list them here.

Data.Array

Data.Array.NonEmpty

Data.List

Data.List.NonEmpty

  • See the many1 combinator below.

Data.List.Lazy

Combinators in this package

A parser combinator is a function which takes some parsers as arguments and returns a new parser.

The many combinator applied to parser p :: Parser s a will return a parser many p :: Parser s (Array a) which will repeat the parser p as many times as possible. If p never consumes input when it fails then many p will always succeed but may return an empty array.

The replicateA n combinator applied to parser p :: Parser s a will return a parser replicateA n p :: Parser s (Array a) which will repeat parser p exactly n times. replicateA n p will only succeed if it can match parser p exactly n consecutive times.

#(<?>) Source

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

#(<??>) Source

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

#(<~?>) 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 (<?>).

#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 ")")

#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

#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

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

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

#chainlRec Source

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

Parse phrases delimited by a left-associative operator.

Stack-safe version of chainl at the expense of a 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

#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

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

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

#chainrRec Source

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

Parse phrases delimited by a right-associative operator.

Stack-safe version of chainr at the expense of a 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.

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

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

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

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

#endByRec Source

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

Parse phrases delimited and terminated by a separator.

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

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

Match one or more times.

Stack-safe version of many1 at the expense of a 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 at least one phrase until the terminator phrase matches.

#many1TillRec Source

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

Parse at least one phrase until the terminator phrase matches.

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

#many1TillRec_ Source

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

Parse many phrases until the terminator phrase matches, requiring at least one match. Returns the list of phrases and the terminator phrase.

Stack-safe version of many1Till_ at the expense of a MonadRec constraint.

#many1Till_ Source

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

Parse many phrases until the terminator phrase matches, requiring at least one match. Returns the list of phrases and the terminator phrase.

#manyTill Source

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

Parse many phrases until the terminator phrase matches.

#manyTillRec Source

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

Parse many phrases until the terminator phrase matches.

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

#manyTillRec_ Source

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

Parse many phrases until the terminator phrase matches. Returns the list of phrases and the terminator phrase.

Stack-safe version of manyTill_ at the expense of a MonadRec constraint.

#manyTill_ Source

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

Parse many phrases until the terminator phrase matches. Returns the list of phrases and the terminator phrase.

Use the manyTill_ combinator to do non-greedy repetition of a pattern p, like we would in Regex by writing p*?. To repeat pattern p non-greedily, write manyTill_ p q where q is the entire rest of the parser.

For example, this parse fails because many repeats the pattern letter greedily.

runParser "aab" do
  a <- many letter
  b <- char 'b'
  pure (Tuple a b)
(ParseError "Expected 'b'" (Position { line: 1, column: 4 }))

To repeat pattern letter non-greedily, use manyTill_.

runParser "aab" do
  Tuple a b <- manyTill_ letter do
    char 'b'
  pure (Tuple a b)
(Tuple ('a' : 'a' : Nil) 'b')

#notFollowedBy Source

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

Fail if the parser succeeds.

Will never consume input.

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

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

#optional Source

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

Optionally parse something, failing quietly.

To optionally parse p and never fail: optional (try p).

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

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

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

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

#sepByRec Source

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

Parse phrases delimited by a separator.

Stack-safe version of sepBy at the expense of a 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.

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

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

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

#sepEndByRec Source

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

Parse phrases delimited and optionally terminated by a separator.

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

#skipMany Source

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

Skip many instances of a phrase.

#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

Skip at least one instance of a phrase.

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

#skipManyRec Source

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

Skip many instances of a phrase.

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

#try Source

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

If the parser fails then backtrack the input stream to the unconsumed state.

One use for this combinator is to ensure that the right parser of an alternative will always be tried when the left parser fails.

>>> runParser "ac" ((char 'a' *> char 'b') <|> (char 'a' *> char 'c'))
Left (ParseError "Expected 'b'" (Position { line: 1, column: 2 }))
>>> runParser "ac" (try (char 'a' *> char 'b') <|> (char 'a' *> char 'c'))
Right 'c'

#tryRethrow Source

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

If the parser fails then backtrack the input stream to the unconsumed state.

Like try, but will relocate the error to the try point.

>>> runParser "ac" (try (char 'a' *> char 'b'))
Left (ParseError "Expected 'b'" (Position { line: 1, column: 2 }))
>>> runParser "ac" (tryRethrow (char 'a' *> char 'b'))
Left (ParseError "Expected 'b'" (Position { line: 1, column: 1 }))

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

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

Re-exports from Control.Plus

#alt Source

alt :: forall f a. Alt f => f a -> f a -> f a

#empty Source

empty :: forall f a. Plus f => f a

#(<|>) Source

Operator alias for Control.Alt.alt (left-associative / precedence 3)

Re-exports from Data.Unfoldable

#replicateA Source

replicateA :: forall m f a. Applicative m => Unfoldable f => Traversable f => Int -> m a -> m (f a)

Perform an Applicative action n times, and accumulate all the results.

> replicateA 5 (randomInt 1 10) :: Effect (Array Int)
[1,3,2,7,5]

Re-exports from Data.Unfoldable1

#replicate1A Source

replicate1A :: forall m f a. Apply m => Unfoldable1 f => Traversable1 f => Int -> m a -> m (f a)

Perform an Apply action n times (at least once, so values n less than 1 will be treated as 1), and accumulate the results.

> replicate1A 2 (randomInt 1 10) :: Effect (NEL.NonEmptyList Int)
(NonEmptyList (NonEmpty 8 (2 : Nil)))
> replicate1A 0 (randomInt 1 10) :: Effect (NEL.NonEmptyList Int)
(NonEmptyList (NonEmpty 4 Nil))