更新 2
|**|
原始问题:如果我不需要回溯,我可以避免使用 Ragel's吗?
()*
更新的答案:是的,如果您不需要回溯,您可以编写一个简单的标记器。
更新 1
我意识到询问 XML 标记化是一个红鲱鱼,因为我所做的并不是特定于 XML。
结束更新
我有一个 Ragel 扫描器/标记器,它只是在文件中查找 FooBarEntity 元素,例如:
<ABC >
<XYZ >
<FooBarEntity>
<Example >Hello world</Example >
</FooBarEntity>
</XYZ >
<XYZ >
<FooBarEntity>
<Example >sdrastvui</Example >
</FooBarEntity>
</XYZ >
</ABC >
扫描仪版本:
%%{
machine simple_scanner;
action Emit {
emit data[(ts+14)..(te-15)].pack('c*')
}
foo = '<FooBarEntity>' any+ :>> '</FooBarEntity>';
main := |*
foo => Emit;
any;
*|;
}%%
非扫描版本(即使用()*
代替|**|
)
%%{
machine simple_tokenizer;
action MyTs {
my_ts = p
}
action MyTe {
my_te = p
}
action Emit {
emit data[my_ts...my_te].pack('c*')
my_ts = nil
my_te = nil
}
foo = '<FooBarEntity>' any+ >MyTs :>> '</FooBarEntity>' >MyTe %Emit;
main := ( foo | any+ )*;
}%%
我想通了这一点并在https://github.com/seamusabshere/ruby_ragel_examples上为它编写了测试
您可以在https://github.com/seamusabshere/ruby_ragel_examples/blob/master/lib/simple_scanner.rl和https://github.com/seamusabshere/ruby_ragel_examples/blob/master/lib/simple_tokenizer看到读取/缓冲代码.rl