/
Repetition.purs
71 lines (56 loc) · 2.86 KB
/
Repetition.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module Text.Parsing.String.Repetition
( until
, least
, exact
, greedy
, many
, many1
, most
, range
) where
import Prelude
import Data.Foldable (foldMap)
import Data.String.CodeUnits (singleton)
import Data.Tuple (Tuple(..))
import Data.Tuple as T
import Text.Parsing.Parser (ParserT)
import Text.Parsing.Array.Repetition as R
-- Helper function to convert an array of characters to a string.
fromArray :: Array Char -> String
fromArray = foldMap singleton
-- | Consumes the current parse input with a parser `p` until the result of a parser `q` is successful.
-- | Does not consume the remaining parse input with the successful result of `q`.
until :: forall m a b. Monad m => ParserT a m Char -> ParserT a m b -> ParserT a m String
until p q = fromArray <$> R.until p q
-- | Consumes the current parse input with a parser `p`, with `n` repetitions of `p`.
-- | Does not check if the remaining parse input can be moreover parsed with `p`.
least :: forall a m. Monad m => Int -> ParserT a m Char -> ParserT a m String
least n p = fromArray <$> R.least n p
-- | Consumes the current parse input with a parser `p`, with `n` repetitions of `p`.
-- | Fails if the remaining parse input can be moreover be parsed with `p`.
exact :: forall a m. Monad m => Int -> ParserT a m Char -> ParserT a m String
exact n p = fromArray <$> R.exact n p
-- | Consumes the current input with a parser `p` as many times as successful.
-- | Produces a pair of the number of successful repetitions of `p`, and the accumulated result.
-- | Not guaranteed to be stack-safe for large input.
greedy :: forall a m. Monad m => ParserT a m Char -> ParserT a m (Tuple Int String)
greedy p = do
x <- R.greedy p
y <- pure $ T.fst x
z <- pure $ fromArray $ T.snd x
pure $ Tuple y z
-- | Consumes the current input with a parser `p` as many times as successful.
-- | Produces the accumulated result, without the guarantee of being stack-safe for large input.
many :: forall a m. Monad m => ParserT a m Char -> ParserT a m String
many p = fromArray <$> R.many p
-- | Consumes the current input with a parser `p` as many times as successful, with at least once occurrence of `p`.
-- | Produces the accumulated result, without the guarantee of being stack-safe for large input.
many1 :: forall a m. Monad m => ParserT a m Char -> ParserT a m String
many1 p = fromArray <$> R.many1 p
-- | Consumes the current parse input with a parser `p`, with `m` greedy repetitions of `p`.
-- | Fails if `m` is greater than the constraint `n` passed to the function.
most :: forall a m. Monad m => Int -> ParserT a m Char -> ParserT a m String
most n p = fromArray <$> R.most n p
-- | Consumes the current parse input with a parser `p`, with at *least* `min` and at *most* `max >= min` repetitions of `p`.
range :: forall a m. Monad m => Int -> Int -> ParserT a m Char -> ParserT a m String
range min max p = fromArray <$> R.range min max p