Search code examples
ragel

Ragel Parse Key Value Pairs Without delimiter for EOF


So I have a simple string that I was hoping to run through a ragel state machine.

key1=value1; key2="value2"; key3=value3

Here is a simplified version of my ragel

# Key Value Parts
name = ( token+ )  %on_name ;
value = ( ascii+ -- (" " | ";" | "," | "\r" | "\n" | "\\" | "\"" ) )  %on_value ;
pair = ( name "=" (value | "\"" value "\"") "; " ) ; ## ISSUE WITH FORCING ;
string  = ( pair )+ ;

# MACHINE
main := string >begin_parser @end_parser ;

The issue I'm having is that I'll never have a semicolon after the last key/value pair, so I'd like it to be optional, but when I do that the state machine finds several patches for the value. Is there some sort of syntax where I can say pair has to end with a (";" | *eof*)?

I did change, my main line to this, but it seems like a hack and doesn't really work with some of the other things I'd like to do with this state machine.

main := string >begin_parser @end_parser $/on_value;

Solution

  • I was too wrapped up in Ragel syntax and wasn't think about how I was processing. Instead of trying to add an optional semi-colon onto the end I should have force one on the front after it had already process a key value pair.

    pair = ( name "=" (value | "\"" value "\"") ) ;
    string  = pair ( "; " pair )* ;