[ragel-users] Parsing an indented structure

Adrian Thurston thurs... at cs.queensu.ca
Wed Sep 19 17:00:16 UTC 2007


Hi,

If you mean an offside rule (like python has) then what you want to do
is maintain a stack of indentation levels. At the beginning of every
line you count the spaces/tabs to see if you have more/less whitespace
than the head of the stack. If more you push and go in one level (you
could fcall at this point). If less you go out one level (fret). See the
python manual, it's got a pretty good description of the process.

Doing this requires that you have a grammar that can distinguish between
whitespace at the beginning of a line and whitespace elsewhere. This
means a line-oriented grammar. The top level of your callable machines
should look like:

line = check_indent stuff '\n';
process := line*;

-Adrian

Jonathan Stott wrote:
> Hi
> 
> I'm quite new to ragel, and I'm using it to parse some input files for
> a program, so I can have human readable files, but still parse them
> easily.
> 
> I started with a syntax like:
> 
> # some interesting conditions
> ina: 2.1
> ito: 0.4 #X et al, 2007
> 
> with various tokens accepted, and the double stored in an appropriate
> variable. I'd now like to extend the syntax to allow some more
> varaibles to be set, and so for organisations sake (and to seperate
> things into logical divisions, I'd like to be able to support a syntax
> like:
> 
> x: 10
> y: 15
> currents:
> ina: 2.1
> 
> This works okay. I have the main scanner fcall the 'current' scanner
> when the currents token is parsed, and it starts to parse current
> tokens, which is fine until I follow the currents section with another
> token, which then causes problems.  So I'd like to indent the current
> tokens by two spaces, like:
> y: 15
> currents:
>   ina: 1.01
>   ito: 1.2
> 
> But I'm unsure how how to handle the indent or snapping back from the
> indented scanner to the main scanner.  Advice and pointers would be
> appreciated.
> 
> Regards,
> Jonathan
> 
> --~--~---------~--~----~------------~-------~--~----~
> You received this message because you are subscribed to the Google Groups "ragel-users" group.
> To post to this group, send email to ragel-users at googlegroups.com
> To unsubscribe from this group, send email to ragel-users-unsubscribe at googlegroups.com
> For more options, visit this group at http://groups.google.com/group/ragel-users?hl=en
> -~----------~----~----~----~------~----~------~--~---
> 
> 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 252 bytes
Desc: OpenPGP digital signature
URL: <http://www.colm.net/pipermail/ragel-users/attachments/20070919/436923eb/attachment-0001.sig>


More information about the ragel-users mailing list