Hi List,<div><br></div><div>I started this week a small project to learn how to use "Ragel" on something useful.</div><div><br></div><div>The idea is to parse JSON (<a href="http://www.json.org">www.json.org</a>) data-interchange format. <span class="Apple-style-span" style="font-family: Times; font-size: medium; "><span class="Apple-style-span" style="font-family: arial; font-size: small; ">I've posted my solution below with the corresponding graphviz FSM. </span></span></div>
<div>Lexing is OK and I can parse eveything correctly (except JSON comments ... I don't care about them).</div><div><br></div><div>My question is for Ragel GURUS : is there a better way to do it ? Could I parse JSON faster than that ?</div>
<div><br></div><div>Any hint, tricks to improve speed will be very welcome ;)</div><div><br></div><div>N.B: the beauty of JSON is that you need exactly one character lookahead to parse everything.</div><div>But I don't know if my Ragel code is doing a good job on that.</div>
<div><br></div><div>Thanks in advance</div><div>Ferret</div><div><br></div><div>======================================================</div><div><div> myspace = [ \n\r\t];</div><div> endspace = any - myspace;</div>
<div> end = [\]},\[{}\":tfn] | myspace;</div><div><br></div><div> exp = ([eE] >number_float . [+\-]? . digit+) $append . (end >number_break)?;</div><div> float = ('.' >number_float . digit+) $append . (exp | end >number_break)?;</div>
<div><br></div><div> number = (('-' >number_signed)? $append) . (('0' | ([1-9] . digit*)) >number_unsigned $append . (float | exp | end >number_break)?);</div><div><br></div><div> unicode = ((0 .. 0xd777) | (0xe000 .. 0x10ffff));</div>
<div> string = '"' . (</div><div> start: (</div><div> (unicode - [\"\\]) $append -> start |</div><div> '"' (any >string_break)? -> final |</div>
<div> '\\' >append -> unquote</div><div> ),</div><div> unquote: ([\"\\/bfnrt] >append -> start |</div><div> 'u' $append . (xdigit{4} $append) -> start</div>
<div> ),</div><div> final: empty</div><div> );</div><div><br></div><div> blank = myspace+ (endspace >break)?;</div><div> true = 'true' (any >true_break)?;</div>
<div> false = 'false' (any >false_break)?;</div><div> nil = 'null' (any >null_break)?;</div><div> object_start = '{' (any >left_brace_break)?;</div><div>
object_end = '}' (any >right_brace_break)?;</div><div> array_start = '[' (any >left_bracket_break)?;</div><div> array_end = ']' (any >right_bracket_break)?;</div>
<div> comma = ',' (any >comma_break)?;</div><div> colon = ':' (any >colon_break )?;</div><div> eoi = 0 >eoi_break;</div><div><br></div><div> main := (</div>
<div> object_start | </div><div> object_end |</div><div> array_start |</div><div> array_end |</div><div> colon |</div><div> comma |</div>
<div> string | </div><div> number | </div><div> true | </div><div> false | </div><div> nil | </div><div> blank |</div>
<div> eoi</div><div> ) >clear;</div><div>}%%</div><div><br></div><div>======================================================</div></div>