Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

"unexpected end of input, expecting end of input" #121

Open
samuela opened this issue Dec 23, 2020 · 4 comments
Open

"unexpected end of input, expecting end of input" #121

samuela opened this issue Dec 23, 2020 · 4 comments

Comments

@samuela
Copy link

samuela commented Dec 23, 2020

Weird error message coming from parsec...

#!/usr/bin/env stack
-- stack runghc --package parsec

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 (endOfLine *> ((try endOfLine *> pure ()) <|> eof))

main = do
  raw <- readFile "input.txt"
  print $ parse passports "(poop)" raw

and here's the input file:
input.txt

Here's what I get:

❯ ./day4.hs
Left "(poop)" (line 1134, column 1):
unexpected end of input
expecting new-line, end of input, "byr", "cid", "eyr", "ecl", "hgt", "hcl", "iyr" or "pid"

which seems... highly counter-intuitive!

@samuela
Copy link
Author

samuela commented Dec 23, 2020

Here's another way to trigger this:

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

which gets rid of the new-line option:

❯ ./day4.hs
Left "(poop)" (line 1134, column 1):
unexpected end of input
expecting end of input, "byr", "cid", "eyr", "ecl", "hgt", "hcl", "iyr" or "pid"

DK318 added a commit to DK318/parsec that referenced this issue Jan 4, 2022
DK318 added a commit to DK318/parsec that referenced this issue Jan 4, 2022
@phadej
Copy link
Collaborator

phadej commented Jan 4, 2022

Read http://blog.ezyang.com/2014/05/parsec-try-a-or-b-considered-harmful/

By trying too much you make parsecs life harder.

@phadej
Copy link
Collaborator

phadej commented Jan 4, 2022

In particular, removing try from sepBy1Try seems to fix the problem and not break the parser.

@samuela
Copy link
Author

samuela commented Jan 5, 2022

That's fair, but it feels like this error message falls into the category of things that "should never happen".

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants