[prev in list] [next in list] [prev in thread] [next in thread] 

List:       haskell-beginners
Subject:    [Haskell-beginners] lookAhead
From:       Dennis Raddle <dennis.raddle () gmail ! com>
Date:       2016-03-16 2:17:58
Message-ID: CAKxLvoqJY13ZY18KFpxkXY=7o8BEB8-++M=hYjiABuLg0s-96A () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


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

[Attachment #5 (text/html)]

<div dir="ltr">I&#39;m working with &quot;lookAhead&quot; in \
Parsec.<div><br></div><div>My goal is to simplify error messages and make them more \
relevant. I&#39;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.</div><div><br></div><div>1;%2|&gt;</div><div><br></div><div>&lt;%4|</div><div \
><br></div><div>&lt;&lt;5:4</div><div><br></div><div>&lt;&lt;5:4a</div><div><br></div><div>etc.</div><div><br></div><div>My \
> first parser worked, but would give error messages \
> like</div><div><br></div><div>&quot;Unexpected &#39;;&#39;, expected digit, \
> &#39;;&#39;, &#39;%&#39;, &#39;&lt;&lt;&#39;, &#39;|&#39;, \
> ....</div><div><br></div><div>etc., basically giving me no clue what the parser was \
> thinking.</div><div><br></div><div>The first challenge is that it&#39;s hard to \
> identify the particular type of mark from the first few characters. For instance, a \
> mark like this &quot;1;%2|&gt;&quot; --- what gives it away is the &quot;&gt;&quot; \
> at the very end. That means it&#39;s a &quot;right-facing warp&quot; and that \
> defines what to expect in the other characters. So it would be great if my parser \
> first identified this as a &quot;right-facing warp&quot; and then when parsing the \
> internals it can give messages that make sense in \
> context.</div><div><br></div><div>So I tried this, with \
> lookAhead</div><div><br></div><div>rightWarp = do</div><div>   try (lookAhead (do \
> many1 (noneOf &quot;&lt;&gt;&quot;)</div><div>                                      \
> char &#39;&gt;&#39;</div><div>                                                \
> eof))</div><div>   ... parse internals ...</div><div>   char \
> &#39;&gt;&#39;</div><div>   eof</div><div><br></div><div>The problem is that I want \
> to put a &lt;?&gt; message in here somewhere so that if this whole \
> &#39;rightWarp&#39; parser fails, it will say &quot;expected right warp&quot;.   \
> 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 \
> &#39;eof&#39; inside the lookAhead. If I put a &lt;?&gt; message anywhere else, the \
> parser will just say &quot;expected end of input&quot;. On the other hand, if there \
> is no &#39;&gt;&#39; character, then the failure point will be &quot;char \
> &#39;&gt;&#39;&quot; and the message will be &quot;expected &gt;&quot;.  \
> </div><div><br></div><div>What I was hoping was that there was some single place I \
> could put a &lt;?&gt; message that would say &quot;expected right warp&quot; no \
> matter where inside rightWarp the failure point \
> occurs.</div><div><br></div><div>Any \
> ideas?</div><div><br></div><div>Mike</div><div><br></div><div><br></div><div><br></div><div><br></div></div>
> 



_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic