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 aFlipped (<?>).
#chainl1Rec Source
chainl1Rec :: forall m s a. MonadRec m => ParserT s m a -> ParserT s m (a -> a -> a) -> ParserT s m aParse phrases delimited by a left-associative operator, requiring at least one match.
Stack-safe version of chainl1 at the expense of a MonadRec constraint.
#chainr1Rec Source
chainr1Rec :: forall m s a. MonadRec m => ParserT s m a -> ParserT s m (a -> a -> a) -> ParserT s m aParse phrases delimited by a right-associative operator, requiring at least one match.
Stack-safe version of chainr1 at the expense of a MonadRec constraint.
#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.
#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 (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 UnitFail if the parser succeeds.
Will never consume 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.
#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.
#skipMany1Rec Source
skipMany1Rec :: forall s a m. MonadRec m => ParserT s m a -> ParserT s m UnitSkip 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 UnitSkip 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 aIf 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 aIf 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 aProvide 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 aProvide 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
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))