Skip to content

Commit 88e0822

Browse files
authored
Revert combinator changes from PR #102 (#116)
1 parent 825dfe8 commit 88e0822

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Breaking changes:
99
New features:
1010

1111
Bugfixes:
12+
- Revert combinator implementation changes from #102 (@robertdp, https://github.com/purescript-contrib/purescript-parsing/pull/116)
1213

1314
Other improvements:
1415

src/Text/Parsing/Parser/Combinators.purs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
-- |
88
-- | ```purescript
99
-- | Text.Parsec.many = Data.(Array|List).many
10-
-- | Text.Parsec.many1 = Data.(Array|List).some
1110
-- | Text.Parsec.(<|>) = Control.Alt.alt (<|>)
1211
-- | ```
1312
-- |
@@ -24,13 +23,15 @@
2423
module Text.Parsing.Parser.Combinators where
2524

2625
import Prelude
26+
2727
import Control.Monad.Except (runExceptT, ExceptT(..))
2828
import Control.Monad.State (StateT(..), runStateT)
2929
import Control.Plus (empty, (<|>))
3030
import Data.Either (Either(..))
3131
import Data.Foldable (class Foldable, foldl)
32-
import Data.List (List(..), (:), many)
33-
import Data.List.NonEmpty (NonEmptyList, cons', singleton)
32+
import Data.List (List(..), many, (:))
33+
import Data.List.NonEmpty (NonEmptyList)
34+
import Data.List.NonEmpty as NEL
3435
import Data.Maybe (Maybe(..))
3536
import Data.Newtype (unwrap)
3637
import Data.Tuple (Tuple(..))
@@ -92,6 +93,10 @@ lookAhead p = (ParserT <<< ExceptT <<< StateT) \s -> do
9293
Tuple e _ <- runStateT (runExceptT (unwrap p)) s
9394
pure (Tuple e s)
9495

96+
-- | Match one or more times.
97+
many1 :: forall m s a. Monad m => ParserT s m a -> ParserT s m (NonEmptyList a)
98+
many1 p = NEL.cons' <$> p <*> many p
99+
95100
-- | Parse phrases delimited by a separator.
96101
-- |
97102
-- | For example:
@@ -100,40 +105,30 @@ lookAhead p = (ParserT <<< ExceptT <<< StateT) \s -> do
100105
-- | digit `sepBy` string ","
101106
-- | ```
102107
sepBy :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
103-
sepBy p sep =
104-
(do a <- p
105-
as <- many $ sep *> p
106-
pure (a : as)) <|> pure Nil
108+
sepBy p sep = map NEL.toList (sepBy1 p sep) <|> pure Nil
107109

108110
-- | Parse phrases delimited by a separator, requiring at least one match.
109111
sepBy1 :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (NonEmptyList a)
110112
sepBy1 p sep = do
111113
a <- p
112114
as <- many $ sep *> p
113-
pure (cons' a as)
115+
pure (NEL.cons' a as)
114116

115117
-- | Parse phrases delimited and optionally terminated by a separator.
116118
sepEndBy :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
117-
sepEndBy p sep =
118-
(do a <- p
119-
as <- many $ sep *> p
120-
optional sep
121-
pure (a : as)) <|> pure Nil
119+
sepEndBy p sep = map NEL.toList (sepEndBy1 p sep) <|> pure Nil
122120

123121
-- | Parse phrases delimited and optionally terminated by a separator, requiring at least one match.
124122
sepEndBy1 :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (NonEmptyList a)
125123
sepEndBy1 p sep = do
126124
a <- p
127-
(do as <- many $ sep *> p
128-
optional sep
129-
pure (cons' a as)) <|> pure (singleton a)
125+
(do _ <- sep
126+
as <- sepEndBy p sep
127+
pure (NEL.cons' a as)) <|> pure (NEL.singleton a)
130128

131129
-- | Parse phrases delimited and terminated by a separator, requiring at least one match.
132130
endBy1 :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (NonEmptyList a)
133-
endBy1 p sep = do
134-
a <- p <* sep
135-
as <- many $ p <* sep
136-
pure (cons' a as)
131+
endBy1 p sep = many1 $ p <* sep
137132

138133
-- | Parse phrases delimited and terminated by a separator.
139134
endBy :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
@@ -201,12 +196,11 @@ manyTill p end = scan
201196
scan = (end $> Nil)
202197
<|> do x <- p
203198
xs <- scan
204-
pure (x:xs)
199+
pure (x : xs)
205200

206201
-- | Parse several phrases until the specified terminator matches, requiring at least one match.
207202
many1Till :: forall s a m e. Monad m => ParserT s m a -> ParserT s m e -> ParserT s m (NonEmptyList a)
208203
many1Till p end = do
209204
x <- p
210205
xs <- manyTill p end
211-
pure (cons' x xs)
212-
206+
pure (NEL.cons' x xs)

0 commit comments

Comments
 (0)