@@ -32,12 +32,13 @@ import Prelude
32
32
import Control.Alt ((<|>))
33
33
import Control.Lazy (fix )
34
34
import Control.Monad.Rec.Class (Step (..), tailRecM )
35
-
36
35
import Data.Either (Either (..))
37
36
import Data.Foldable (class Foldable , foldl )
38
- import Data.List (List (..), singleton , manyRec , reverse )
37
+ import Data.List (List (..), manyRec )
38
+ import Data.List.NonEmpty (NonEmptyList (..))
39
+ import Data.List.NonEmpty as NEL
39
40
import Data.Maybe (Maybe (..))
40
-
41
+ import Data.NonEmpty ((:|))
41
42
import Text.Parsing.StringParser (Parser (..), fail )
42
43
43
44
-- | Read ahead without consuming input.
@@ -52,8 +53,8 @@ many :: forall a. Parser a -> Parser (List a)
52
53
many = manyRec
53
54
54
55
-- | Match one or more times.
55
- many1 :: forall a . Parser a -> Parser (List a )
56
- many1 p = Cons <$> p <*> many p
56
+ many1 :: forall a . Parser a -> Parser (NonEmptyList a )
57
+ many1 p = cons' <$> p <*> many p
57
58
58
59
-- | Provide an error message in case of failure.
59
60
withError :: forall a . Parser a -> String -> Parser a
@@ -90,18 +91,18 @@ sepBy1 p sep = do
90
91
91
92
-- | Parse zero or more separated values, optionally ending with a separator.
92
93
sepEndBy :: forall a sep . Parser a -> Parser sep -> Parser (List a )
93
- sepEndBy p sep = sepEndBy1 p sep <|> pure Nil
94
+ sepEndBy p sep = map NEL .toList ( sepEndBy1 p sep) <|> pure Nil
94
95
95
96
-- | Parse one or more separated values, optionally ending with a separator.
96
- sepEndBy1 :: forall a sep . Parser a -> Parser sep -> Parser (List a )
97
+ sepEndBy1 :: forall a sep . Parser a -> Parser sep -> Parser (NonEmptyList a )
97
98
sepEndBy1 p sep = do
98
99
a <- p
99
100
(do _ <- sep
100
101
as <- sepEndBy p sep
101
- pure (Cons a as)) <|> pure (singleton a)
102
+ pure (cons' a as)) <|> pure (NEL . singleton a)
102
103
103
104
-- | Parse zero or more separated values, ending with a separator.
104
- endBy1 :: forall a sep . Parser a -> Parser sep -> Parser (List a )
105
+ endBy1 :: forall a sep . Parser a -> Parser sep -> Parser (NonEmptyList a )
105
106
endBy1 p sep = many1 $ p <* sep
106
107
107
108
-- | Parse one or more separated values, ending with a separator.
@@ -146,18 +147,21 @@ choice = foldl (<|>) (fail "Nothing to parse")
146
147
147
148
-- | Parse values until a terminator.
148
149
manyTill :: forall a end . Parser a -> Parser end -> Parser (List a )
149
- manyTill p end = (end *> pure Nil ) <|> many1Till p end
150
+ manyTill p end = (end *> pure Nil ) <|> map NEL .toList ( many1Till p end)
150
151
151
152
-- | Parse values until the terminator matches, requiring at least one match.
152
- many1Till :: forall a end . Parser a -> Parser end -> Parser (List a )
153
+ many1Till :: forall a end . Parser a -> Parser end -> Parser (NonEmptyList a )
153
154
many1Till p end = do
154
155
x <- p
155
156
tailRecM inner (pure x)
156
157
where
157
158
ending acc = do
158
159
_ <- end
159
- pure $ Done (reverse acc)
160
+ pure $ Done (NEL . reverse acc)
160
161
continue acc = do
161
162
c <- p
162
- pure $ Loop (Cons c acc)
163
+ pure $ Loop (NEL .cons c acc)
163
164
inner acc = ending acc <|> continue acc
165
+
166
+ cons' :: forall a . a -> List a -> NonEmptyList a
167
+ cons' h t = NonEmptyList (h :| t)
0 commit comments