[ragel-users] Ragel for JSON

Younès HAFRI voidptrptr at gmail.com
Fri Sep 4 23:05:17 UTC 2009


Hi List,
I started this week a small project to learn how to use "Ragel" on something
useful.

The idea is to parse JSON (www.json.org) data-interchange format. I've
posted my solution below with the corresponding graphviz FSM.
Lexing is OK and I can parse eveything correctly (except JSON comments ... I
don't care about them).

My question is for Ragel GURUS : is there a better way to do it ? Could I
parse JSON faster than that ?

Any hint, tricks to improve speed will be very welcome ;)

N.B: the beauty of JSON is that you need exactly one character lookahead to
parse everything.
But I don't know if my Ragel code is doing a good job on that.

Thanks in advance
Ferret

======================================================
   myspace        = [ \n\r\t];
    endspace       = any - myspace;
    end            = [\]},\[{}\":tfn] | myspace;

    exp            = ([eE] >number_float . [+\-]? . digit+) $append . (end
>number_break)?;
    float          = ('.' >number_float  . digit+) $append . (exp | end
>number_break)?;

    number         = (('-' >number_signed)? $append) . (('0' | ([1-9] .
digit*)) >number_unsigned $append . (float | exp | end >number_break)?);

    unicode        = ((0 .. 0xd777) | (0xe000 .. 0x10ffff));
    string         = '"' . (
                   start: (
                           (unicode - [\"\\]) $append -> start |
                           '"' (any >string_break)? -> final |
                           '\\' >append -> unquote
                           ),
                   unquote: ([\"\\/bfnrt] >append -> start |
                             'u' $append . (xdigit{4} $append) -> start
                             ),
                   final: empty
                   );

     blank        = myspace+ (endspace >break)?;
     true         = 'true'   (any >true_break)?;
     false        = 'false'  (any >false_break)?;
     nil          = 'null'   (any >null_break)?;
     object_start = '{'      (any >left_brace_break)?;
     object_end   = '}'      (any >right_brace_break)?;
     array_start  = '['      (any >left_bracket_break)?;
     array_end    = ']'      (any >right_bracket_break)?;
     comma        = ','      (any >comma_break)?;
     colon        = ':'      (any >colon_break )?;
     eoi          = 0 >eoi_break;

     main := (
              object_start |
              object_end   |
              array_start  |
              array_end    |
              colon        |
              comma        |
              string       |
              number       |
              true         |
              false        |
              nil          |
              blank        |
              eoi
              ) >clear;
}%%

======================================================
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.colm.net/pipermail/ragel-users/attachments/20090905/58fbe147/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: json_ragel_fsm.png.gz
Type: application/x-gzip
Size: 501981 bytes
Desc: not available
URL: <http://www.colm.net/pipermail/ragel-users/attachments/20090905/58fbe147/attachment-0001.bin>


More information about the ragel-users mailing list