forked from haskell/parsec
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[haskell#121] Fixed duplicating of end of input in unexpected and exp…
…ecting sections
- Loading branch information
Showing
3 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |