如何使用qi创建通用解析器?
我正在尝试使用qi创建通用解析器元素,因为我不幸(必须支持MSVC)无法使用X3. 这个想法是要有一个模板化的结构:
I am attempting to create generic parser-elements using qi as I unfortunately (MSVC must be supported) can not use X3. The idea is to have a templated struct:
template<class T> struct parse_type;
我可以这样使用:
template<class T> T from_string(std::string const& s)
{
T res;
parse_type<T> t;
...
if (phrase_parse(...,parse_type<T>(),...,t))
}
或像这样专门化
template<class T,class Alloc>
struct parse_type<std::vector<T,Alloc>>
{
// Parse a vector using rule '[' >> parse_type<T> % ',' > ']';
}
主要目的是允许轻松解析例如std :: tuple,boost :: optional和boost :: variant(由于qi的贪婪特性,最后一个不能自动生成.)
The primary purpose is to allow for easy parsing of e.g. std::tuple, boost::optional and boost::variant (The last one can not be automatic due to the greedy nature of qi).
我希望您能收到有关如何处理此问题的反馈.当前,我的结构基于qi :: grammar,但是X3不支持语法,并且MSVC编译它时我想使用X3,而且对于必须提供船长我也有些不自在. 一种替代方法是在parse_type中具有一个静态函数,该函数返回适当的规则.我正在考虑这是否是一种更清洁的方法?
I would appreciate feedback as to how approach this. Currently I base my struct on qi::grammar, but grammar is not supported in X3 and I would like to use X3 when MSVC compiles this, and I am also a little bit uncomfortable with having to provide the skipper. An alternative would be to have a static function in parse_type that returns the appropriate rule. I am considering if this is a cleaner approach?
任何反馈将不胜感激.
Update2:用在运行时失败的可编译示例替换了代码片段.这是代码:
Update2: Replaced code-snippet with compilable example that fails at runtime. Here is the code:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <string>
#include <string>
#include <iostream>
#include <iostream>
// Support to simplify
using iter = std::string::const_iterator;
void print(std::vector<int> const& v)
{
std::cout << '[';
for (auto i: v) std::cout << i << ',';
std::cout << "]";
}
namespace qi = boost::spirit::qi;
// My rule factory - quite useless if you do not specialise
template<class T> struct ps_rule;
// An example of using the factory
template<class T>
T from_string(std::string const& s)
{
T result;
iter first { std::begin(s) };
auto rule = ps_rule<T>::get();
phrase_parse(first,std::end(s),rule,qi::space,result);
return result;
}
// Specialising rule for int
template<>
struct ps_rule<int>
{
static qi::rule<iter,int()> get() { return qi::int_; }
};
// ... and for std::vector (where the elements must have rules)
template<class T,class Alloc>
struct ps_rule<std::vector<T,Alloc>>
{
static qi::rule<iter,std::vector<T,Alloc>()> get()
{
qi::rule<iter,std::vector<T,Alloc>()> res;
res.name("Vector");
res =
qi::lit('{')
>> ps_rule<T>::get() % ','
>> '}';
return res;
}
};
int main()
{
// This one works like a charm.
std::cout << ((from_string<int>("100") == 100) ? "OK\n":"Failed\n");
std::vector<int> v {1,2,3,4,5,6};
// This one fails
std::cout << ((from_string<std::vector<int>>("{1,2,3,4,5,6}") == v) ? "OK\n":"Failed\n");
}
代码在boost/function_template.hpp第766行中失败:
The code fails in boost/function_template.hpp line 766:
result_type operator()(BOOST_FUNCTION_PARMS) const
{
if (this->empty())
boost::throw_exception(bad_function_call());
return get_vtable()->invoker
(this->functor BOOST_FUNCTION_COMMA BOOST_FUNCTION_ARGS);
}
此代码是boost :: function4中的成员函数 ,boost :: fusion :: vector0>& ,boost :: spirit :: unused_type const&> 问题是get_vtable返回一个无效的指针.
This code is a member function in boost::function4 ,boost::fusion::vector0 > & ,boost::spirit::unused_type const&> and the problem is that get_vtable returns an invalid pointer.
您的主要问题是qi::rule
的副本构造函数引用了原始规则,在您的情况下,该规则是局部变量.避免此问题的一种方法是使用qi::rule
的copy
成员函数,但这需要稍微更改ps_rule
的专业化类型的返回类型.
Your main problem is that the copy constructor for qi::rule
takes a reference to the original rule, which in your case is a local variable. One way you can avoid this problem is by using qi::rule
's copy
member function but this requires changing slightly the return type of your specialization of ps_rule
.
static typename boost::proto::terminal<qi::rule<iter,std::vector<T,Alloc>()>>::type get()
{
//[...] (same as before)
return res.copy();
}
执行此操作后,即使ps_rule<int>
似乎可以单独工作,也会出现相同的问题.您可以执行类似的操作,但是在这种情况下不需要该规则,最好使用类似以下的命令(即使从性能的角度来看):
Once you do that, the same problem arises with your ps_rule<int>
even though it seemed to work in isolation. You could do something analogous but in this case the rule is not required, it would be better (even from a performance point of view) to just use something like:
static qi::int_type get() { return qi::int_; }
#include <boost/spirit/include/qi.hpp>
#include <string>
#include <iostream>
// Support to simplify
using iter = std::string::const_iterator;
void print(std::vector<int> const& v)
{
std::cout << '[';
for (auto i: v) std::cout << i << ',';
std::cout << "]";
}
namespace qi = boost::spirit::qi;
// My rule factory - quite useless if you do not specialise
template<class T> struct ps_rule;
// An example of using the factory
template<class T>
T from_string(std::string const& s)
{
T result;
iter first { std::begin(s) };
auto rule = ps_rule<T>::get();
qi::phrase_parse(first,std::end(s),rule,qi::space,result);
return result;
}
// Specialising rule for int
template<>
struct ps_rule<int>
{
static qi::int_type get() { return qi::int_; }
};
// ... and for std::vector (where the elements must have rules)
template<class T,class Alloc>
struct ps_rule<std::vector<T,Alloc>>
{
static typename boost::proto::terminal<qi::rule<iter,std::vector<T,Alloc>()>>::type get()
{
qi::rule<iter,std::vector<T,Alloc>()> res;
res.name("Vector");
res =
qi::lit('{')
>> ps_rule<T>::get() % ','
>> '}';
return res.copy();
}
};
int main()
{
// This one works like a charm.
std::cout << ((from_string<int>("100") == 100) ? "OK\n":"Failed\n");
std::vector<int> v {1,2,3,4,5,6};
std::cout << ((from_string<std::vector<int>>("{1,2,3,4,5,6}") == v) ? "OK\n":"Failed\n");
std::vector<std::vector<int> > vv {{1,2,3},{4,5,6}};
std::cout << ((from_string<std::vector<std::vector<int>>>("{{1,2,3},{4,5,6}}") == vv) ? "OK\n":"Failed\n");
}
PS:如果使用这里是一个示例.
PS: You can save lots of specializations if you use Spirit's own machinery to create parsers automatically in your primary template. Here is an example.