Skip to content

Commit 920e461

Browse files
committed
Add many1
1 parent 0fa2f5f commit 920e461

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "purescript-parsing-repetition",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"license": "MIT",
55
"repository": {
66
"type": "git",

src/Text/Parsing/Applicative/Repetition.purs

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Text.Parsing.Applicative.Repetition
44
, least
55
, greedy
66
, many
7+
, many1
78
, most
89
, range
910
) where
@@ -43,8 +44,13 @@ greedy p = R.greedy (pure <$> p)
4344
many :: forall m a f b. Monad m => Applicative f => Monoid (f b) => ParserT a m b -> ParserT a m (f b)
4445
many p = R.many (pure <$> p)
4546

47+
-- | Consumes the current input with a parser `p` as many times as successful, with at least one occurrence of `p`.
48+
-- | Produces the accumulated result, without the guarantee of being stack-safe for large input.
49+
many1 :: forall m a f b. Monad m => Applicative f => Monoid (f b) => ParserT a m b -> ParserT a m (f b)
50+
many1 p = R.many1 (pure <$> p)
51+
4652
-- | Consumes the current parse input with a parser `p`, with `m` greedy repetitions of `p`.
47-
-- | Fails if `m` is greater than the constraint `n` passed to the function.
53+
-- | Fails if `m` is greater than the constraint `n` passed to the function.
4854
most :: forall m a f b. Monad m => Applicative f => Monoid (f b) => Int -> ParserT a m b -> ParserT a m (f b)
4955
most n p = case n > 0 of
5056
false -> exact 0 p
@@ -53,7 +59,7 @@ most n p = case n > 0 of
5359
m <- pure $ T.fst w
5460
x <- pure $ T.snd w
5561
case m > n of
56-
true -> fail $ "Number of repetitions must be at most " <> show n <> "."
62+
true -> fail $ "Number of repetitions must be at most " <> show n <> "."
5763
false -> pure x
5864

5965
-- | Consumes the current parse input with a parser `p`, with at *least* `min` and at *most* `max >= min` repetitions of `p`.

src/Text/Parsing/Array/Repetition.purs

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Text.Parsing.Array.Repetition
44
, least
55
, greedy
66
, many
7+
, many1
78
, most
89
, range
910
) where
@@ -47,8 +48,16 @@ greedy p = do
4748
many :: forall m a b. Monad m => ParserT a m b -> ParserT a m (Array b)
4849
many p = A.fromFoldable <$> L.many p
4950

51+
-- | Consumes the current input with a parser `p` as many times as successful, with at least one occurrence of `p`.
52+
-- | Produces the accumulated result, without the guarantee of being stack-safe for large input.
53+
many1 :: forall m a b. Monad m => ParserT a m b -> ParserT a m (Array b)
54+
many1 p = do
55+
x <- pure <$> p
56+
y <- many p
57+
pure $ x <> y
58+
5059
-- | Consumes the current parse input with a parser `p`, with `m` greedy repetitions of `p`.
51-
-- | Fails if `m` is greater than the constraint `n` passed to the function.
60+
-- | Fails if `m` is greater than the constraint `n` passed to the function.
5261
most :: forall a m b. Monad m => Int -> ParserT a m b -> ParserT a m (Array b)
5362
most = R.most
5463

src/Text/Parsing/Monoid/Repetition.purs

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Text.Parsing.Monoid.Repetition
44
, least
55
, greedy
66
, many
7+
, many1
78
) where
89

910
import Prelude
@@ -63,10 +64,18 @@ greedy = \p -> many' 0 mempty p
6364
many' n acc = \p -> do
6465
x <- C.optionMaybe p
6566
case x of
66-
(Nothing) -> pure $ Tuple n acc
67+
(Nothing) -> pure $ Tuple n acc
6768
(Just y) -> many' (n + 1) (acc <> y) p
6869

6970
-- | Consumes the current input with a parser `p` as many times as successful.
7071
-- | Produces the accumulated result, without the guarantee of being stack-safe for large input.
7172
many :: forall a m b. Monad m => Monoid b => ParserT a m b -> ParserT a m b
7273
many p = T.snd <$> greedy p
74+
75+
-- | Consumes the current input with a parser `p` as many times as successful, with at least one occurrence of `p`.
76+
-- | Produces the accumulated result, without the guarantee of being stack-safe for large input.
77+
many1 :: forall a m b. Monad m => Monoid b => ParserT a m b -> ParserT a m b
78+
many1 p = do
79+
x <- p
80+
y <- many p
81+
pure $ x <> y

src/Text/Parsing/String/Repetition.purs

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Text.Parsing.String.Repetition
44
, exact
55
, greedy
66
, many
7+
, many1
78
, most
89
, range
910
) where
@@ -33,7 +34,7 @@ until p q = fromArray <$> R.until p q
3334
-- | Consumes the current parse input with a parser `p`, with `n` repetitions of `p`.
3435
-- | Does not check if the remaining parse input can be moreover parsed with `p`.
3536
least :: forall a m. Monad m => Int -> ParserT a m Char -> ParserT a m String
36-
least n p = fromArray <$> R.least n p
37+
least n p = fromArray <$> R.least n p
3738

3839
-- | Consumes the current parse input with a parser `p`, with `n` repetitions of `p`.
3940
-- | Fails if the remaining parse input can be moreover be parsed with `p`.
@@ -55,8 +56,13 @@ greedy p = do
5556
many :: forall a m. Monad m => ParserT a m Char -> ParserT a m String
5657
many p = fromArray <$> R.many p
5758

59+
-- | Consumes the current input with a parser `p` as many times as successful, with at least once occurrence of `p`.
60+
-- | Produces the accumulated result, without the guarantee of being stack-safe for large input.
61+
many1 :: forall a m. Monad m => ParserT a m Char -> ParserT a m String
62+
many1 p = fromArray <$> R.many1 p
63+
5864
-- | Consumes the current parse input with a parser `p`, with `m` greedy repetitions of `p`.
59-
-- | Fails if `m` is greater than the constraint `n` passed to the function.
65+
-- | Fails if `m` is greater than the constraint `n` passed to the function.
6066
most :: forall a m. Monad m => Int -> ParserT a m Char -> ParserT a m String
6167
most n p = fromArray <$> R.most n p
6268

0 commit comments

Comments
 (0)