使用 Spirit x3,如何控制在每个不同的输入上调用哪个解析器?
Using Spirit x3, how to control which parser is called on each different input?
我正在使用 boost spirit x3,但有一点不清楚。我有一个文件,其中包含非常不同且重复的行。前几行可能是注释。接下来的 1000 行可能是坐标,接下来的 1000 行可能是 int 的列表,等等...
我的问题是如何识别该行并知道该行使用哪个解析器。例如,这里有两个解析器函数....
template <typename Iterator>
bool parse_ints(Iterator first, Iterator last, std::vector<int>& v)
{
using x3::int_;
using x3::phrase_parse;
using x3::_attr;
using ascii::space;
auto push_back = [&](auto& ctx){ v.push_back(_attr(ctx)); };
bool r = phrase_parse(first, last,
(
int_[push_back]
>> *(',' >> int_[push_back])
)
,
space);
if (first != last)
return false;
return r;
}
template <typename Iterator>
bool parse_doubles(Iterator first, Iterator last, std::vector<double>& v)
{
using x3::double_;
using x3::phrase_parse;
using x3::_attr;
using ascii::space;
auto push_back = [&](auto& ctx){ v.push_back(_attr(ctx)); };
bool r = phrase_parse(first, last,
(
double_[push_back]
>> *(',' >> double_[push_back])
)
,
space);
if (first != last) // fail if we did not get a full match
return false;
return r;
}
这是输入的方式(istringstream 的大小为数百 MB)。在 while 循环中,我想知道要调用哪个解析器。
int main()
{
istringstream str(my.data());
while (getline(str, line)) {
// based on the format of 'line', how does one know which
// parser to call?
}
}
您的语法可以描述整个输入。所以你可以说
auto input = *headerComment
>> points
>> edges;
您可以简单定义的地方
auto headerComment = '#' >> *(char_ - eol) >> eol;
例如
auto points = skip(blank) [
*(point >> eol)
];
auto edges = skip(blank) [
*(edge >> eol)
];
point = int_ >> int_ >> int_; // assuming x y z
edge = int_ >> int_; // assuming from to
这假设输入格式是明确的。 IE。如果点是 x y
则它不会工作,因为它无法与边区分开来。
我正在使用 boost spirit x3,但有一点不清楚。我有一个文件,其中包含非常不同且重复的行。前几行可能是注释。接下来的 1000 行可能是坐标,接下来的 1000 行可能是 int 的列表,等等...
我的问题是如何识别该行并知道该行使用哪个解析器。例如,这里有两个解析器函数....
template <typename Iterator>
bool parse_ints(Iterator first, Iterator last, std::vector<int>& v)
{
using x3::int_;
using x3::phrase_parse;
using x3::_attr;
using ascii::space;
auto push_back = [&](auto& ctx){ v.push_back(_attr(ctx)); };
bool r = phrase_parse(first, last,
(
int_[push_back]
>> *(',' >> int_[push_back])
)
,
space);
if (first != last)
return false;
return r;
}
template <typename Iterator>
bool parse_doubles(Iterator first, Iterator last, std::vector<double>& v)
{
using x3::double_;
using x3::phrase_parse;
using x3::_attr;
using ascii::space;
auto push_back = [&](auto& ctx){ v.push_back(_attr(ctx)); };
bool r = phrase_parse(first, last,
(
double_[push_back]
>> *(',' >> double_[push_back])
)
,
space);
if (first != last) // fail if we did not get a full match
return false;
return r;
}
这是输入的方式(istringstream 的大小为数百 MB)。在 while 循环中,我想知道要调用哪个解析器。
int main()
{
istringstream str(my.data());
while (getline(str, line)) {
// based on the format of 'line', how does one know which
// parser to call?
}
}
您的语法可以描述整个输入。所以你可以说
auto input = *headerComment
>> points
>> edges;
您可以简单定义的地方
auto headerComment = '#' >> *(char_ - eol) >> eol;
例如
auto points = skip(blank) [
*(point >> eol)
];
auto edges = skip(blank) [
*(edge >> eol)
];
point = int_ >> int_ >> int_; // assuming x y z
edge = int_ >> int_; // assuming from to
这假设输入格式是明确的。 IE。如果点是 x y
则它不会工作,因为它无法与边区分开来。