Question

Here is my code so far: http://hpaste.org/86353 . I'm building w/ GHC on Windows.

The code above produces the following output on my test file:

parse error at (line 3, column 5):
unexpected " "
expecting "{", "if" or identifier

Here is my test input:

{ if 9 < 3      then
{
a(); b() c()
d()
}else if 2 < 1{if 3 > 2 { }}}

I have not been able to find out why the space after the expression frustrates parsec so much. If I remove the semi-colon after either a() or b() then parsec will complain about the first letter of the next call (ie: b or c). If I add the semi-colon back in, parsec complains about the space. No spaces between any calls, semi-colons after a() and b() but no semi-colon after c() causes the program to parse correctly. Same test except with a semi-colon after c() causes "unexpected \n".

Any hints?

Was it helpful?

Solution

The main problem is that you want newlines to matter, but your lexer treats them as whitespace. This means that all newlines get silently consumed after each token. Unfortunately, there is no easy way around this: In Parsec, is there a way to prevent lexeme from consuming newlines?.

This is certainly poor design on the part of the library. Which is a little odd because, in general, Parsec is one of the best-designed libraries I've ever used.

Another little problem is that your separator (oneOf ";\n") does not allow any spaces following it. If you change it to oneOf ";\n" >> skipMany (oneOf " \t")), you will be able to parse something like "{ a(); b(); c(); }` correctly. Unfortunately, this does not help with the earlier problem of significant newlines.

Ultimately, it might just be your best bet to write your own lexing routines for striping tailing whitespace. This will also be a good exercise in learning more about Parsec :).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top