How to look ahead

Jason Garber jason.gar... at gmail.com
Tue Feb 12 20:07:36 UTC 2008


Solved the issue by digging in a little deeper and understanding the
value of p for each type of user action.  I wasn't able to move p
backwards properly in a leaving action; it had to be finishing.  There
was also some nondeterminism getting in the way.

formerly: -  dim = ( digit+ >A %{ STORE(x) } (" x " | "x") digit+ >A
%*{ STORE(y) } ) >X ;
now     :+  dim = ( digit+ >A %{ STORE(x) } (" x " @{ ASET(space,
true)} | "x") digit @{ fhold; } ) >X ;

So the dim pattern matches multiple times on a phrase like 1x2x3.

Thanks for your willingness to help!

Jason


On Jan 29, 11:06 pm, Adrian Thurston <thurs... at cs.queensu.ca> wrote:
> Hey Jason,
>
> Generally I do lookahead by moving p backwards. This should work even in
> scanner actions (requires a recent version of Ragel). Can you give some
> code that doesn't do what you expect? I don't understand exactly what
> you're trying to do.
>
> Adrian
>
> Jason Garber wrote:
> > Hello Ragel users,
> > I'm working on SuperRedCloth (a Ragel implementation of RedCloth that
> > why the lucky stiff started awhile back).  I've run into some trouble
> > and could use some help.
>
> > In the inline scanner, we want the dimension rule to parse "1 x 2."
> > and then the formatter outputs it as "1 × 2."  As it was:
>
> > dim = ( digit+ >A %{ STORE(x) } " x " digit+ >A %{ STORE(y) } ) >X ;
>
> > (where A registers the start of the string to capture and STORE
> > captures it.  See
> >http://code.whytheluckystiff.net/redcloth/browser/branches/superredcl...
> > if you need more context.)
>
> > But, since it was in a scanner and it had to entertain the possibility
> > that the pattern was starting over (I assume), the STORE(y) action
> > would never happen, so you'd get "1 × ."
>
> > Assuming you can't have leaving actions at the end of a pattern in a
> > scanner, I considered (with the help of Johan Sørensen) looking ahead
> > for a digit but not consuming it, so with "1 x 2." the "1 x" is handed
> > to the formatter and the rest is handled by the default pattern (not
> > stored).  This gives the additional advantage of being able to parse
> > something like "1 x 2 x 3 x 4."
>
> > The question is, how to look ahead?  I've tried all kinds of things
> > with fhold, p--, etc., but it seems I'm at the limit of my Ragel
> > comprehension.  Or is there a better way than look-ahead?
>
> > Thanks in advance!



More information about the ragel-users mailing list