假設我們想要解析像這樣的遞歸塊。當「skip_comments_tag
」以塊爲前綴時,我們會以遞歸方式跳過此塊中的所有註釋(/*...*/
)。精神:無法在其規則定義中使用x3 :: skip(skipper)[某些遞歸規則]
{
{}
{
skip_comments_tag{
{} /*comments*/
{ /*comments*/ }
}
}
}
很容易想出一個遞歸解析器,如Coliru。
namespace Parser {
auto const ruleComment = x3::lit("/*") >> *(x3::char_ - "*/") >> "*/" | x3::space;
x3::rule<struct SBlockId> const ruleBlock;
auto const ruleBlock_def = x3::lit('{') >> *(ruleBlock | "skip_comments_tag" >> x3::skip(ruleComment)[ruleBlock]) >> '}';
BOOST_SPIRIT_DEFINE(ruleBlock)
}
不過,這並不編譯(當parse
函數被調用),因爲它會(在x3::skip_directive
由x3::make_context
)產生無限的上下文。 x3::no_case
和x3::with
也有這個問題,因爲它們全部在執行中使用x3::make_context
。
問題:
- 有總有更好的方式來寫解析器這種 問題,以避免這樣的編譯錯誤,又如何?
- 或者
x3::make_context
執行被認爲是這類問題的缺陷?