@@ -32,12 +32,13 @@ import Prelude
3232import Control.Alt ((<|>))
3333import Control.Lazy (fix )
3434import Control.Monad.Rec.Class (Step (..), tailRecM )
35-
3635import Data.Either (Either (..))
3736import 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
3940import Data.Maybe (Maybe (..))
40-
41+ import Data.NonEmpty ((:|))
4142import Text.Parsing.StringParser (Parser (..), fail )
4243
4344-- | Read ahead without consuming input.
@@ -52,8 +53,8 @@ many :: forall a. Parser a -> Parser (List a)
5253many = manyRec
5354
5455-- | 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
5758
5859-- | Provide an error message in case of failure.
5960withError :: forall a . Parser a -> String -> Parser a
@@ -79,32 +80,32 @@ optionMaybe p = option Nothing (Just <$> p)
7980
8081-- | Parse zero or more separated values.
8182sepBy :: forall a sep . Parser a -> Parser sep -> Parser (List a )
82- sepBy p sep = sepBy1 p sep <|> pure Nil
83+ sepBy p sep = map NEL .toList ( sepBy1 p sep) <|> pure Nil
8384
8485-- | Parse one or more separated values.
85- sepBy1 :: forall a sep . Parser a -> Parser sep -> Parser (List a )
86+ sepBy1 :: forall a sep . Parser a -> Parser sep -> Parser (NonEmptyList a )
8687sepBy1 p sep = do
8788 a <- p
8889 as <- many $ sep *> p
89- pure (Cons a as)
90+ pure (cons' a as)
9091
9192-- | Parse zero or more separated values, optionally ending with a separator.
9293sepEndBy :: 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
9495
9596-- | 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 )
9798sepEndBy1 p sep = do
9899 a <- p
99100 (do _ <- sep
100101 as <- sepEndBy p sep
101- pure (Cons a as)) <|> pure (singleton a)
102+ pure (cons' a as)) <|> pure (NEL . singleton a)
102103
103- -- | Parse zero or more separated values, ending with a separator.
104- endBy1 :: forall a sep . Parser a -> Parser sep -> Parser (List a )
104+ -- | Parse one or more separated values, ending with a separator.
105+ endBy1 :: forall a sep . Parser a -> Parser sep -> Parser (NonEmptyList a )
105106endBy1 p sep = many1 $ p <* sep
106107
107- -- | Parse one or more separated values, ending with a separator.
108+ -- | Parse zero or more separated values, ending with a separator.
108109endBy :: forall a sep . Parser a -> Parser sep -> Parser (List a )
109110endBy p sep = many $ p <* sep
110111
@@ -146,18 +147,21 @@ choice = foldl (<|>) (fail "Nothing to parse")
146147
147148-- | Parse values until a terminator.
148149manyTill :: 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)
150151
151152-- | 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 )
153154many1Till p end = do
154155 x <- p
155156 tailRecM inner (pure x)
156157 where
157158 ending acc = do
158159 _ <- end
159- pure $ Done (reverse acc)
160+ pure $ Done (NEL . reverse acc)
160161 continue acc = do
161162 c <- p
162- pure $ Loop (Cons c acc)
163+ pure $ Loop (NEL .cons c acc)
163164 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