私のTreetopには、完璧主義者が1つだけのルール、または少なくとももっと美しいものでなければならないと信じている、この実用的なルールのペアがあります。
rule _
crap
/
" "*
end
rule crap
" "* "\\x0D\\x0A"* " "*
end
時々「\x0D\x0A」で終わるいくつかの式を解析しています。ええ、「\ r\n」ではなく「\x0D\x0A」です。ある時点で何かが二重に逃げました。長い話。
そのルールは機能しますが、それは醜くて気になります。私はこれを試しました:
rule _
" "* "\\x0D\\x0A"* " "*
/
" "*
end
これが原因
SyntaxError: (eval):1276:in `load_from_string': compile error
(eval):1161: class/module name must be CONSTANT
from /.../gems/treetop-1.4.9/lib/treetop/compiler/grammar_compiler.rb:42:in `load_from_string'
from /.../gems/treetop-1.4.9/lib/treetop/compiler/grammar_compiler.rb:35:in `load'
from /.../gems/treetop-1.4.9/lib/treetop/compiler/grammar_compiler.rb:32:in `open'
from /.../gems/treetop-1.4.9/lib/treetop/compiler/grammar_compiler.rb:32:in `load'
理想的には、実際に次のようなものを書きたいと思います。
rule _
(" " | "\\x0D\\x0A")*
end
しかし、それは機能しません。私たちがそれに取り組んでいる間、ルールごとに1つだけ*を持つことはできないことも発見しました。
rule _
" "*
/
"\n"*
end
これは""と一致しますが、\nにはなりません。