我试图定义一个解析器,其中规则不是完全预定义的,即它们包含一个变量部分。这对于Spirit Qi来说没有问题,但由于X3的静态特性,我无法实现它。我尝试了与指令,不幸的是没有记录,但没有运气到目前为止。到目前为止,我发现的唯一示例是在lambda表达式中。
我构造了一个简单的示例来演示这个问题:解析分隔符作为参数给出的整数。#include <boost/spirit/home/x3.hpp>
#include <iostream>
namespace x3 = boost::spirit::x3;
namespace parsing {
x3::rule<struct parser> parser {"parser"};
//struct separator {};
char separator(',');
//auto parser_def = x3::int_ % x3::lit(x3::get<separator>(/* context */)); // candidate function template not viable: requires single argument 'context'
auto parser_def = x3::int_ % x3::lit(separator);
BOOST_SPIRIT_DEFINE(parser)
}
void parse(const std::string &data, const char separator) {
using namespace std;
//auto parser = x3::with<parsing::separator>(ref(separator)) [parsing::parser] >> x3::eoi;
auto parser = parsing::parser >> x3::eoi;
if (x3::parse(data.begin(), data.end(), parser))
cout << "Parse succeededn";
else
cout << "Parse failedn";
}
int main() {
parse("1 2 3", ' ');
parse("1,2,3", ',');
parse("1;2;3", ';');
}
我注释掉了我试图使用与指令的部分。
这是目前可能与X3?以前有人这样做过吗?
在看了更多的X3帖子后,我被sehe的这个答案所启发:https://stackoverflow.com/a/38303379/7110782
对x3::_pass的了解让我想到了这个解决方案:
#include <boost/spirit/home/x3.hpp>
#include <iostream>
namespace x3 = boost::spirit::x3;
namespace parsing {
x3::rule<struct parser> parser {"parser"};
struct separator {};
// only the separator which is currently in the context is allowed (passes)
auto isSeparator = [](auto& ctx){ x3::_pass(ctx) = x3::_attr(ctx) == x3::get<separator>(ctx); };
// at first match any char and then check whether it is the separator
auto parser_def = x3::int_ % x3::char_[isSeparator];
BOOST_SPIRIT_DEFINE(parser)
}
void parse(const std::string &data, const char separator) {
using namespace std;
auto parser = x3::with<parsing::separator>(ref(separator)) [parsing::parser] >> x3::eoi;
if (x3::parse(data.begin(), data.end(), parser))
cout << "Parse succeededn";
else
cout << "Parse failedn";
}
int main() {
// succeed
parse("1 2 3", ' ');
parse("1,2,3", ',');
parse("1;2;3", ';');
// fail
parse("1,2,3", ' ');
parse("1;2;3", ',');
}
下一步需要测试的是设置多个参数的可能性(可能通过级联x3::with<>)。
编辑:是的,通过级联x3::带<>设置多个参数似乎可以工作。例如:
auto parser = x3::with<parsing::separator>(ref(separator))[x3::with<parsing::separator2>(ref(separator2))[parsing::parser]] >> x3::eoi;
一个更简单的解决方案是,您可以使用一个函数获取一个字符并返回类型为x3::rule<struct parser>
的解析器值。
auto getParser(char sep)
{
return x3::int_ % x3::lit(sep);
}
完整代码(Godbolt:https://godbolt.org/z/ENxCTF)
#include <boost/spirit/home/x3.hpp>
#include <iostream>
namespace x3 = boost::spirit::x3;
namespace parsing {
x3::rule<struct parser> parser {"parser"};
//struct separator {};
char separator(',');
//auto parser_def = x3::int_ % x3::lit(x3::get<separator>(/* context */)); // candidate function template not viable: requires single argument 'context'
auto parser_def = x3::int_ % x3::lit(separator);
BOOST_SPIRIT_DEFINE(parser)
auto getParser(char sep)
{
return x3::int_ % x3::lit(sep);
}
}
void parse(const std::string &data, const char separator) {
using namespace std;
//auto parser = x3::with<parsing::separator>(ref(separator)) [parsing::parser] >> x3::eoi;
auto parser = parsing::getParser(separator) >> x3::eoi;
if (x3::parse(data.begin(), data.end(), parser))
cout << "Parse succeededn";
else
cout << "Parse failedn";
}
int main() {
parse("1 2 3", ' ');
parse("1,2,3", ',');
parse("1;2;3", ';');
}