[Haskell-beginners] lookAhead
Dennis Raddle
dennis.raddle at gmail.com
Wed Mar 16 02:17:58 UTC 2016
I'm working with "lookAhead" in Parsec.
My goal is to simplify error messages and make them more relevant. I'm
parsing strings that represent musical score text markings that convey
nuances of playback expression. I worked long and hard to devise a concise
syntax that has sufficient expressive power --- must be concise because
there is little room in these score for text markings, especially if there
are a lot of them. The result is that it looks a little cryptic.
1;%2|>
<%4|
<<5:4
<<5:4a
etc.
My first parser worked, but would give error messages like
"Unexpected ';', expected digit, ';', '%', '<<', '|', ....
etc., basically giving me no clue what the parser was thinking.
The first challenge is that it's hard to identify the particular type of
mark from the first few characters. For instance, a mark like this "1;%2|>"
--- what gives it away is the ">" at the very end. That means it's a
"right-facing warp" and that defines what to expect in the other
characters. So it would be great if my parser first identified this as a
"right-facing warp" and then when parsing the internals it can give
messages that make sense in context.
So I tried this, with lookAhead
rightWarp = do
try (lookAhead (do many1 (noneOf "<>")
char '>'
eof))
... parse internals ...
char '>'
eof
The problem is that I want to put a <?> message in here somewhere so that
if this whole 'rightWarp' parser fails, it will say "expected right warp".
But I have found with experiment that the parser considers the point of
failure to be in different places inside the lookAhead. For instance if I
give an input string that has too many characters at the end, the failure
point will be the 'eof' inside the lookAhead. If I put a <?> message
anywhere else, the parser will just say "expected end of input". On the
other hand, if there is no '>' character, then the failure point will be
"char '>'" and the message will be "expected >".
What I was hoping was that there was some single place I could put a <?>
message that would say "expected right warp" no matter where inside
rightWarp the failure point occurs.
Any ideas?
Mike
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160315/2af3989a/attachment.html>
More information about the Beginners
mailing list