[ragel-users] recursion with ragel

Gaspard Bucher gaspard at teti.ch
Tue Dec 2 12:55:48 UTC 2008


> Hi list !
>
> I am sure this question has been answered a thousand times, but I
> could not find the list archives (all pages from google point to
> "group not found").
>
> Anyway: I am implementing a json like parser and want to use recursion:
>
> This is what I want to parse:
> "{ url:'foo' opts:{ bar:4.5 }}"
>
> To cope with the hash in the hash, I want to open a new parser after each key:
>
> "{ url:" ===> new parser with "'foo' opts:{ bar:4.5 }}", returns
> number of eaten chars
>
> I then want to update the position of *p. This is the code:
>
> action hash_value {
>  p++;
>  p += tmp_val.from_string(p);
>  tmp_h.set(key, tmp_val);
>  key = "";
> }
>
> My problem: closing '}' is not parsed correctly and then "@hash" is
> never executed.
>
> Regex:
>  ...
>  hash      = '{' (ws* string ':' >hash_value)* ws* '}';
>  main     := ws* (string @string | number @number | hash @hash);
>
> Many thanks for your help !
>
> Gaspard
>

I just found the solution: add an 'fhold' in the "hash_value" action.
My understanding is that >hash_value is executed "before" the pointer
is advanced to the next char. So I need to hold it back so that the
pointer is not advanced twice (since I do p++).

Gaspard




More information about the ragel-users mailing list