我有很多具有共同的前缀和后缀规则:分解出精神的公用部分规则
rule = begin_stuff >> some >> other >> stuff >> end_stuff.
(其中begin_stuff
和end_stuff
从文字组成)
我希望能够到说
rule = wrapped(some >> other >> stuff);
我试过的东西沿着
线但我所得到的大部分是来自Qi的编译时间断言。
我该如何以这种方式重构Spirit规则?
我有很多具有共同的前缀和后缀规则:分解出精神的公用部分规则
rule = begin_stuff >> some >> other >> stuff >> end_stuff.
(其中begin_stuff
和end_stuff
从文字组成)
我希望能够到说
rule = wrapped(some >> other >> stuff);
我试过的东西沿着
线但我所得到的大部分是来自Qi的编译时间断言。
我该如何以这种方式重构Spirit规则?
我认为你需要使用Spirit Repository的Qi Confix Parser Derective。这正是你需要的。
我认为你正在寻找'subrules'(即Spirit V1/classic曾经有过)。现在已经过时了。
看一看
C++ 11 auto
和BOOST_AUTO
auto subexpression = int_ >> ',' >> double_;
qi::rule<It> rule = "A:" >> subexpression >> "Rest:" >> (subexpression % eol);
过去有问题与精神的规则使用auto
(尤其是与MSVC)(见Zero to 60 MPH in 2 seconds!和评论)但我已被告知(很快)is no longer an issue:
Yep. Anyway,FYI, it's fixed in Spirit-3. You can use auto all you want. Regards, -- Joel de Guzman
这里介绍的是概念的证明通过一个共同的子规则不同的“化合物”的规则,以允许在()
包装, []
或{}
:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
typedef std::string::const_iterator It;
template <typename R>
void test(const std::string& input, R const& rule)
{
It f(input.begin()), l(input.end());
bool ok = qi::phrase_parse(f,l,rule,qi::space);
std::cout << "'" << input << "'\tparse " << (ok?"success":"failure") << "\n";
}
int main()
{
typedef qi::rule<It, qi::space_type> common_rule;
typedef qi::rule<It, void(common_rule), qi::space_type> compound_rule;
common_rule common = qi::int_;
compound_rule
in_parens = qi::lit('(') >> qi::_r1 >> ')',
in_brackets = qi::lit('[') >> qi::_r1 >> ']',
in_braces = qi::lit('{') >> qi::_r1 >> '}';
test("{ 231 }" , in_braces (phx::ref(common)));
test("{ hello }", in_braces (phx::val("hello")));
test("(231)" , in_parens (phx::ref(common)));
test("(hello)", in_parens (phx::val("hello")));
test("[ 231 ]" , in_brackets(phx::ref(common)));
test("[ hello ]", in_brackets(phx::val("hello")));
}
输出:
'{ 231 }' parse success
'{ hello }' parse success
'(231)' parse success
'(hello)' parse success
'[ 231 ]' parse success
'[ hello ]' parse success
PS。注意以上是不是典型的Spirit语法。当“通用”规则会暴露不同的属性时,这种方式并不会奏效。
谢谢你能回答。你能完成它吗?最后一个字“也”告诉我,你有更多的话要说... – Arkadiy
@Arkadiy我不记得任何具体的我要说。除了我可能添加的关于“自动”的事情。干杯 – sehe
问题:我们如何从val(“hello”)到common_rule? – Arkadiy