[Haskell-cafe] Re: Proving my point

Philippa Cowderoy flippa at flippac.org
Fri May 16 16:25:01 EDT 2008


On Fri, 16 May 2008, Achim Schneider wrote:

> Andrew Coppin <andrewcoppin at btinternet.com> wrote:
> 
> > Wait... "unexpected end of input; expecting [...] end of input [...]"
> > 
> > That's just *wrong*...! ;-)
> > 
> > But don't despaire - show us your parser and what it's supposed to 
> > parse, and I'm sure somebody [maybe even me] will be able to tell you 
> > what's up.
> 
> This is what I came up with while simplifying the parser:
> 
> import Text.Parsec
> 
> identifier = do
>     whiteSpace
>     s <- many1 letter
>     whiteSpace
>     return s
> 
> whiteSpace = do 
>     eof <|> ((many $ choice [ char ' ', newline ]) >> return ())
> 
> main = do 
>     let syn = runParser (do
>         char '\\'
>         many1 identifier
>         char ':'
>         whiteSpace
>         identifier
>         whiteSpace
>         ) () "" "\\a b"
>     print syn
> 
> Admittedly, this is a quite degenerate case crouching in at least 10
> corners simultaneously. Anyway, I get
> 
> % ./test
> Left (line 1, column 5):
> unexpected end of input
> expecting end of input, letter or ":"
> 

Confusing, isn't it? It's almost the right message, too. I'm pretty sure 
the misbehaviour's because eof doesn't consume - see what happens if you 
put an error message on all of whiteSpace?

> 
> and if I change it to
> 
> whiteSpace = do 
>     (many eof >> return ()) 
>     <|> ((many $ choice [ char ' ', newline ]) >> return ())
> 
> Left (line 1, column 3):
> unexpected " "
> expecting letter, end of input or ":"
> 

Which is broken for your purposes, but that's because many always succeeds 
so the changed whiteSpace doesn't actually eat whitespace. 

> 
> Please, please don't ask me for the rationale of using eof like this,
> you would get the same answer as if you'd ask me why I cast a stone into
> the sea.
> 

As a matter of general practice I'd suggest including eof exactly once, as 
in:

topLevel = do {r <- realTopLevel; eof; return r}
realTopLevel = ... 

Which isn't to say that you haven't run into something confusing and 
possibly broken here, of course.

-- 
flippa at flippac.org

"The reason for this is simple yet profound. Equations of the form
x = x are completely useless. All interesting equations are of the
form x = y." -- John C. Baez


More information about the Haskell-Cafe mailing list