Skip to content

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

Description

@samuela

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions