Skip to content

Commit

Permalink
[haskell#121] Fixed duplicating of end of input in unexpected and exp…
Browse files Browse the repository at this point in the history
…ecting sections
  • Loading branch information
DK318 committed Jan 4, 2022
1 parent 5d679ab commit c4c7a9a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
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
9 changes: 7 additions & 2 deletions 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 All @@ -214,4 +219,4 @@ showErrorMessages msgOr msgUnknown msgExpecting msgUnExpected msgEndOfInput msgs
separate _ [m] = m
separate sep (m:ms) = m ++ sep ++ separate sep ms

clean = nub . filter (not . null)
clean = nub . filter (not . null)
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

0 comments on commit c4c7a9a

Please # to comment.