-
Notifications
You must be signed in to change notification settings - Fork 50
Fix sepBy
and sepEndBy
#114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
It seems like parseTest "a,a,a," (cons "a" (cons "a" (cons' "a" Nil))) $ do -- existing
as <- string "a" `endBy1` string ","
eof
pure as
parseTest "a,a,a" (cons "a" (cons' "a" Nil)) $ do -- new
as <- string "a" `endBy1` string ","
_ <- string "a"
pure as
parseTest "a,a,a" (Cons "a" (Cons "a" Nil)) $ do -- new
as <- string "a" `endBy` string ","
_ <- string "a"
pure as Or this is the intended behaviour of the combinators? |
And I could be wrong, but do parseTest "ab" "ab" do
optional (string "a" *> string "x")
string "ab" fails with |
@robertdp Thank you for pointing these issues out. I have just checked the implementation of Haskell's Parsec library and found they cannot parse your new test cases either. Maybe my latest commit on Btw, although adding |
@yzyzsun Sorry for thinking aloud in your PR. I only just discovered this problem caused by my PR, so I've been digging into this lib and comparing it with Parsec to try and understand what I was missing. This definitely needs input from the maintainers (and people who understand and use the lib) but I would suggest undoing all the changes from my PR, including removing the use of -- Original combinator
sepEndBy1 :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
sepEndBy1 p sep = do
a <- p
(do _ <- sep
as <- sepEndBy p sep
pure (a : as)) <|> pure (singleton a)
-- NonEmpty wrapper
sepEndBy1 :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (NonEmptyList a)
sepEndBy1 p sep =
C.sepEndBy1 p sep >>=
case _ of
Cons x xs -> pure (NEL.cons' x xs)
_ -> unsafeCrashWith "expected non-empty list"
|
@robertdp Never mind about that! Actually I agree with your suggestion since it can easily bring everything back to order. |
Thanks for kicking this off @yzyzsun! |
Description of the change
This pull request fixed #113.
Checklist: