Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions parsec.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ test-suite parsec.
else
build-depends: semigroups

test-suite parsec-issue121
default-language: Haskell2010
type: exitcode-stdio-1.0
main-is: issue121.hs
hs-source-dirs: test
build-depends: base, parsec, containers

test-suite parsec-issue127
default-language: Haskell2010
type: exitcode-stdio-1.0
Expand Down
7 changes: 6 additions & 1 deletion src/Text/Parsec/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,12 @@ showErrorMessages msgOr msgUnknown msgExpecting msgUnExpected msgEndOfInput msgs
(unExpect,msgs2) = span ((UnExpect "") ==) msgs1
(expect,messages) = span ((Expect "") ==) msgs2

showExpect = showMany msgExpecting expect
cleanedExpect | containsEofInSysUnExpect = filter (\msg -> messageString msg /= msgEndOfInput) expect
| otherwise = expect
where
containsEofInSysUnExpect = (null unExpect && not (null sysUnExpect)) && null (messageString $ head sysUnExpect)

showExpect = showMany msgExpecting cleanedExpect
showUnExpect = showMany msgUnExpected unExpect
showSysUnExpect | not (null unExpect) ||
null sysUnExpect = ""
Expand Down
37 changes: 37 additions & 0 deletions test/issue121.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Main (main) where

import Data.Map (fromList)
import Text.Parsec

sepBy1Try p sep = do
x <- p
xs <- many (try $ sep *> p)
return (x : xs)

key =
try (string "byr")
<|> try (string "cid")
<|> try (string "eyr")
<|> try (string "ecl")
<|> try (string "hgt")
<|> try (string "hcl")
<|> try (string "iyr")
<|> try (string "pid")

passportKV = try $ do
k <- key
char ':'
v <- many1 (try alphaNum <|> try (char '#'))
return (k, v)

passportLine = sepBy1Try passportKV space

passport = do
lines <- sepBy1Try passportLine endOfLine
return $ fromList [kv | line <- lines, kv <- line]

passports = sepBy1 passport (try (endOfLine *> endOfLine *> pure ()) <|> (endOfLine *> eof *> pure ()))

main = do
let raw = "byr:2001 iyr:2011\necl:brn\npid:487702556 hcl:#602927\nhgt:167cm eyr:2026\n"
print $ parse passports "(poop)" raw