There is an example in Ragel manual 6.5 Semantic conditions, which demonstrates how to write a grammar for variable size structures, using when clause.
action rec_num { i = 0; n = getnumber(); }
action test_len { i++ < n }
data_fields = (
’d’
[0-9]+ %rec_num
’:’
( [a-z] when test_len )*
)**;
It works fine for small structures, however for bigger structures it slows down, because parser tries to evaluate condition on every character.
What I am trying to do is to skip scanning and just copy data into the buffer, for a grammar like this (note any*):
action rec_num { i = 0; n = getnumber(); }
action test_len { i++ < n }
data_fields = (
’d’
[0-9]+ %rec_num
’:’
( any* when test_len )*
)**;
So I want to copy buffer of length n straight away without iteration. How can I do this without leaving parser context?
You probably need to take matters into your own hand. The ragel user guide mentions that you can alter the
fpc/pvariable within the machine so this should be safe enough. This assumes you're processing all your data in one chunk (ie, the data field won't be broken up)If data is chunked up, you could split the buffering out: