[ragel-users] Parsing a template language

Adrian Thurston thurston at complang.org
Fri Jul 30 05:43:07 UTC 2010


In Ruby 'p' is an integer that is used as an index into 'data'. You can 
modify it as you can in C.

-Adrian

On 10-07-29 07:41 AM, Laslavic, Alex wrote:
>
> Keep in mind you can always muck with the location of 'p' in your input
> string. There are ragel psudo functions ( fhold, fexec ) that do this
> for you, or you can always just do:
>
> p = p - 2
>
> in your action code. The above is in C, actually, i'm not sure how to do
> that with ruby. Decrementing 'p' in ruby probably won't back it up along
> the string, but maybe 'fhold' or 'fexec' can help you.
>
> I have used the ( any* -- token ) trick to advance to the token, and
> then modify 'p' to get it back to the beginning of the token.
>
> Perhaps other in this list know a cleaner way to do this, but its what
> I've found that works.
>
> -----Original Message-----
> From: ragel-users-bounces at complang.org on behalf of Tobias Lütke
> Sent: Thu 7/29/2010 12:40 AM
> To: ragel-users at complang.org
> Subject: Re: [ragel-users] Parsing a template language
>
> Thanks Alex,
>
> I modified the code to your clever example. This almost works, however
> after running the any* scanner p will be advanced all the way to the
> end of {{, so the other rule will not match the tag correctly. Here is
> my current machine:
>
>
> machine parser;
>
> action start { tokstart = p; }
> action on_tag { results << [:tag, data[tokstart..p]] }
> action on_static { results << [:static, data[tokstart..p]] }
>
> tag = '{{' lower+ '}}' >start @on_tag;
> html = (any* -- '{{') >start @on_static;
> EOF = 0;
>
> main := |*
> tag;
> html;
> EOF;
> *|;
>
> Regards
> -- tobi
>
>
>
> On Tue, Jul 27, 2010 at 9:52 PM, Laslavic, Alex
> <Alex.Laslavic at turner.com> wrote:
>  > I'm actually working on a similar sounding task.
>  >
>  > Try the strong subtraction operator
>  > Untested:
>  >
>  > main := |*
>  >   '[[' lower+ ']]' => action
>  >   ( any* -- '[[' ) => action
>  > *|;
>  >
>  >
>  > ( any* -- '[[' ) will match the longest possible string that doesn't have
>  > '[[' as a substring.
>  >
>  > -----Original Message-----
>  > From: ragel-users-bounces at complang.org on behalf of Tobias Lütke
>  > Sent: Tue 7/27/2010 6:54 PM
>  > To: ragel-users at complang.org
>  > Subject: Re: [ragel-users] Parsing a template language
>  >
>  > Depends on the answers in this thread I suppose :-)
>  >
>  >
>  >
>  > On Tue, Jul 27, 2010 at 3:42 AM, Magnus Holm <judofyr at gmail.com> wrote:
>  >> (A little off-topic, but whatever:
>  >>
>  >> So Liquid will finally get a proper parser? :-))
>  >>
>  >> // Magnus Holm
>  >>
>  >>
>  >>
>  >> On Tue, Jul 27, 2010 at 03:15, Tobias Lütke <tobi at leetsoft.com> wrote:
>  >>> I've been working on a parser for simple template language. I'm using
>  >>> Ragel.
>  >>>
>  >>> The requirements are modest. I'm trying to find [[tags]] that can be
>  >>> embedded anywhere in the input string.
>  >>>
>  >>> I'm trying to parse a simple template language, something that can
>  >>> have tags such as {{foo}} embedded within HTML. I tried several
>  >>> approaches to parse this but had to resort to using a Ragel scanner
>  >>> and use the inefficient approach of only matching a single character
>  >>> as a "catch all". I feel this is the wrong way to go about this. I'm
>  >>> essentially abusing the longest-match bias of the scanner to implement
>  >>> my default rule ( it can only be 1 char long, so it should always be
>  >>> the last resort ).
>  >>>
>  >>> %%{
>  >>>
>  >>>  machine parser;
>  >>>
>  >>>  action start      { tokstart = p; }
>  >>>  action on_tag      { results << [:tag, data[tokstart..p]] }
>  >>>  action on_static  { results << [:static, data[p..p]] }
>  >>>
>  >>>  tag  = ('[[' lower+ ']]') >start @on_tag;
>  >>>
>  >>>  main := |*
>  >>>    tag;
>  >>>    any      => on_static;
>  >>>  *|;
>  >>>
>  >>> }%%
>  >>>
>  >>> ( actions written in ruby, but should be easy to understand ).
>  >>>
>  >>> How would you go about writing a parser for such a simple language? Is
>  >>> Ragel maybe not the right tool? It seems you have to fight Ragel tooth
>  >>> and nails if the syntax is unpredictable such as this.
>  >>>
>  >>>
>  >>> Regards
>  >>> -- tobi
>  >>>
>  >>> _______________________________________________
>  >>> ragel-users mailing list
>  >>> ragel-users at complang.org
>  >>> http://www.complang.org/mailman/listinfo/ragel-users
>  >>>
>  >>
>  >> _______________________________________________
>  >> ragel-users mailing list
>  >> ragel-users at complang.org
>  >> http://www.complang.org/mailman/listinfo/ragel-users
>  >>
>  >
>  > _______________________________________________
>  > ragel-users mailing list
>  > ragel-users at complang.org
>  > http://www.complang.org/mailman/listinfo/ragel-users
>  >
>  >
>  > _______________________________________________
>  > ragel-users mailing list
>  > ragel-users at complang.org
>  > http://www.complang.org/mailman/listinfo/ragel-users
>  >
>  >
>
> _______________________________________________
> ragel-users mailing list
> ragel-users at complang.org
> http://www.complang.org/mailman/listinfo/ragel-users
>
>
>
> _______________________________________________
> ragel-users mailing list
> ragel-users at complang.org
> http://www.complang.org/mailman/listinfo/ragel-users

_______________________________________________
ragel-users mailing list
ragel-users at complang.org
http://www.complang.org/mailman/listinfo/ragel-users



More information about the ragel-users mailing list