Right. I took "option" out for some reason. The following definitions have the desired behavior. I also added a base case to fromTo because otherwise the call to count would consume m tokens even if n-m < 0, which is probably not what we'd expect.<div>
<br></div><div><div>upTo :: Int -> GenParser tok st a -> GenParser tok st [a]</div><div>upTo n p</div><div> | n <= 0 = return []</div><div> | otherwise = option [] $ liftM2 (:) p (upTo (n-1) p)</div><div><br>
</div><div>fromTo :: Int -> Int -> GenParser tok st a -> GenParser tok st [a]</div><div>fromTo m n p</div><div> | n-m < 0 = return []</div><div> | otherwise = liftM2 (++) (count m p) (upTo (n-m) p)</div>
<div><br></div><div>Thanks!</div><div><br></div><br><div class="gmail_quote">On Mon, Oct 19, 2009 at 12:55 PM, Chaddaï Fouché <span dir="ltr"><<a href="mailto:chaddai.fouche@gmail.com">chaddai.fouche@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im">On Mon, Oct 19, 2009 at 6:22 PM, Ashish Agarwal <<a href="mailto:agarwal1975@gmail.com">agarwal1975@gmail.com</a>> wrote:<br>
> The semantics of (upTo n p) should be to parse at most n tokens, but if less<br>
> than n tokens are available that should still be a successful parse. And the<br>
> next token should be the first one upTo failed on.<br>
> I attempted to use the "try" parser in various locations but that doesn't<br>
> seem to help, or maybe I'm using it incorrectly.<br>
<br>
<br>
</div>First, you should probably use pattern matching rather than if for<br>
your base case/recursive case distinction, it's clearer (at least most<br>
Haskellers seems to think it is) :<br>
<br>
> upTo 0 p = return []<br>
<br>
second, option was conceived for these case where you want to try a<br>
parser and returns something if it fail :<br>
<br>
> upTo n p = option [] $ liftM2 (:) p (upTo (n-1) p)<br>
<br>
Even then, be careful of putting a try before any multi-token parser<br>
that could fail harmlessly during the upTo.<br>
<font color="#888888"><br>
--<br>
Jedaï<br>
</font></blockquote></div><br></div>