遗憾的是,不可能以一般的方式实现你想要的(或者至少我不知道如何实现),但如果你愿意只使用 Spirit.Qi 的有限子集,下面的方法可能会起作用。
首先要知道的是,当您使用以下内容时:
int_ >> double_
您只需一个 Boost.Proto 表达式,它描述了多个终端以及它们之间的关系。该表达式本身并不“知道”有关如何解析一个 int 和一个 double 的任何信息。每当你使用parse
/phrase_parse
或将这些 Proto 表达式之一分配给rule
Spirit “编译”域(Qi 或 Karma)的表达式,并创建执行实际工作的解析器/生成器。
Here http://coliru.stacked-crooked.com/a/85da3fc9e8effbae您可以看到一个小示例,其中显示了 Proto 和编译后的 Qi 表达式的确切类型:
Raw proto type:
boost::proto::exprns_::expr<boost::proto::tagns_::tag::shift_right, boost::proto::argsns_::list2<boost::spirit::terminal<boost::spirit::tag::int_> const&, boost::spirit::terminal<boost::spirit::tag::double_> const&>, 2l>
"Pretty" proto type:
shift_right(
terminal(boost::spirit::tag::int_)
, terminal(boost::spirit::tag::double_)
)
Compiled Qi type:
boost::spirit::qi::sequence<boost::fusion::cons<boost::spirit::qi::any_int_parser<int, 10u, 1u, -1>, boost::fusion::cons<boost::spirit::qi::any_real_parser<double, boost::spirit::qi::real_policies<double> >, boost::fusion::nil_> > >
只要您有权访问原始表达式,您就可以使用 Proto 转换/语法将其转换为合适的 Karma 表达式。
在下面的示例中,我使用了以下转换:
Qi |Karma |Reason
------------|---------------|------
lexeme[expr]|verbatim[expr] | lexeme does not exist in Karma
omit[expr] |no_delimit[eps]| omit consumes an attribute in Karma
a >> b |a << b |
a > b |a << b | < does not exist in Karma
a - b |a | - does not exist in Karma
为了实现这种转换,您可以使用boost::proto::or_
得到类似的东西:
struct Grammar : proto::or_<
proto::when<Matcher1,Transform1>,
proto::when<Matcher2,Transform2>,
Matcher3,
Matcher4
>{};
我将尝试解释这是如何工作的。
MatcherN
在下面的例子中可以是:
-
proto::terminal<boost::spirit::tag::omit>
: 仅匹配该特定终端。
-
proto::terminal<proto::_>
:匹配之前未专门匹配的任何终端。
-
proto::subscript<proto::terminal<boost::spirit::tag::omit>,proto::_>
: 火柴omit[expr]
where expr
可以是任何东西。
-
proto::shift_right<ToKarma,ToKarma>
: 火柴expr1 >> expr2
where expr1
and expr2
必须递归地符合ToKarma
语法。
-
proto::nary_expr<proto::_,proto::vararg<ToKarma> >
:匹配任何 n 元(一元、二进制或实际上 n 元,如函数调用)a(b,c,d,e)
) 其中表达式的每个元素都符合 ToKarma 语法。
All the TransformN
在这个例子中是表达式构建器,这里有一些解释:
-
_make_terminal(boost::spirit::tag::lexeme())
: 建立一个proto::terminal<boost::spirit::tag::lexeme>
(注意,需要添加()
在标签之后,如果你忘记了它们,你会得到一个可怕的错误)。
-
_make_subscript(_make_terminal(tag::no_delimit()), _make_terminal(tag::eps()))
: 建立一个proto::subscript<proto::terminal<tag::no_delimit>, proto::terminal<tag::eps> >
,或等价于no_delimit[eps]
.
-
_make_shift_left(ToKarma(proto::_left), ToKarma(proto::_right))
: proto::_left
意思是取原始表达式的lhs。ToKarma(proto::_left)
意味着将 ToKarma 语法/变换递归地应用于原始表达式的 lhs。整体_make_shift_left
基本上构建transformed_lhs << transformed_rhs
.
A MatcherN
本身(不在内部proto::when
) 是使用对原始元素递归应用变换的结果作为元素来构建相同类型的表达式的简写。
完整样本(在 WandBox 上运行) http://melpon.org/wandbox/permlink/rcP9z8NzIhPPJhaW
#include <iostream>
#include <string>
#include <tuple>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/fusion/include/std_tuple.hpp>
namespace proto= boost::proto;
struct ToKarma: proto::or_<
//translation of directives
proto::when<proto::terminal<boost::spirit::tag::lexeme>, proto::_make_terminal(boost::spirit::tag::verbatim())>, //lexeme -> verbatim
proto::when<
proto::subscript<proto::terminal<boost::spirit::tag::omit>,proto::_>, //omit[expr] -> no_delimit[eps]
proto::_make_subscript(proto::_make_terminal(boost::spirit::tag::no_delimit()),proto::_make_terminal(boost::spirit::tag::eps()))
>,
proto::terminal<proto::_>, //if the expression is any other terminal leave it as is
//translation of operators
proto::when<proto::shift_right<ToKarma,ToKarma>, proto::_make_shift_left(ToKarma(proto::_left),ToKarma(proto::_right)) >, //changes '>>' into '<<'
proto::when<proto::greater<ToKarma,ToKarma>, proto::_make_shift_left(ToKarma(proto::_left),ToKarma(proto::_right)) >, //changes '>' into '<<'
proto::when<proto::minus<ToKarma,ToKarma>, ToKarma(proto::_left)>, //changes 'expr-whatever' into 'expr'
proto::nary_expr<proto::_,proto::vararg<ToKarma> > //if it's anything else leave it unchanged and recurse into the expression tree
>{};
template <typename ... Attr, typename Parser>
void test(const std::string& input, const Parser& parser)
{
std::cout << "Original: \"" << input << "\"\n";
std::tuple<Attr...> attr;
std::string::const_iterator iter = input.begin(), end = input.end();
bool result = boost::spirit::qi::phrase_parse(iter,end,parser,boost::spirit::qi::space,attr);
if(result && iter==end)
{
ToKarma to_karma;
std::cout << "Generated: \"" << boost::spirit::karma::format_delimited(to_karma(parser), boost::spirit::karma::space, attr) << '"' << std::endl;
}
else
{
std::cout << "Parsing failed. Unparsed: ->" << std::string(iter,end) << "<-" << std::endl;
}
}
int main(){
using namespace boost::spirit::qi;
test<std::string,double,std::string >("Xx 1.233 pseudo", lexeme[+(char_-' '-'\n')] >> double_ >> lexeme[+(char_-' '-'\n')]);
test<int,double>("foo 1 2.5", omit[lexeme[+alpha]] > int_ > double_);
}
PS:
绝对行不通的事情:
qi::rule
qi::grammar
qi::symbols
Karma 中不存在的东西:
qi::attr
qi::matches
qi::hold
- 排列解析器
^
- 顺序或解析器
||
Karma 中具有不同语义的事物: