使用 boost::spirit 读取空值
Read empty values with boost::spirit
我想将 CSV 文件读入结构中:
struct data
{
std::string a;
std::string b;
std::string c;
}
但是,我什至想读取空字符串以确保所有值都在正确的位置。
我将结构调整为 boost::fusion,因此以下工作:
// Our parser (using a custom skipper to skip comments and empty lines )
template <typename Iterator, typename skipper = comment_skipper<Iterator> >
struct google_parser : qi::grammar<Iterator, addressbook(), skipper>
{
google_parser() : google_parser::base_type(contacts, "contacts")
{
using qi::eol;
using qi::eps;
using qi::_1;
using qi::_val;
using qi::repeat;
using standard_wide::char_;
using phoenix::at_c;
using phoenix::val;
value = *(char_ - ',' - eol) [_val += _1];
// This works but only for small structs
entry %= value >> ',' >> value >> ',' >> value >> eol;
}
qi::rule<Iterator, std::string()> value;
qi::rule<Iterator, data()> entry;
};
不幸的是,repeat
将所有非空值存储在向量中,因此属性值可能会混合在一起(即,如果 b
的字段为空,它可能包含来自c
):
entry %= repeat(2)[ value >> ','] >> value >> eol;
我想使用类似于 repeat
的简短规则,因为我的结构在实践中有 60 个属性!不仅写 60 条规则很乏味,而且 Boost 似乎不喜欢长规则...
您只想确保您也解析了 "empty" 个字符串的值。
value = +(char_ - ',' - eol) | attr("(unspecified)");
entry = value >> ',' >> value >> ',' >> value >> eol;
查看演示:
//#define BOOST_SPIRIT_DEBUG
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
struct data {
std::string a;
std::string b;
std::string c;
};
BOOST_FUSION_ADAPT_STRUCT(data, (std::string, a)(std::string, b)(std::string, c))
template <typename Iterator, typename skipper = qi::blank_type>
struct google_parser : qi::grammar<Iterator, data(), skipper> {
google_parser() : google_parser::base_type(entry, "contacts") {
using namespace qi;
value = +(char_ - ',' - eol) | attr("(unspecified)");
entry = value >> ',' >> value >> ',' >> value >> eol;
BOOST_SPIRIT_DEBUG_NODES((value)(entry))
}
private:
qi::rule<Iterator, std::string()> value;
qi::rule<Iterator, data(), skipper> entry;
};
int main() {
using It = std::string::const_iterator;
google_parser<It> p;
for (std::string input : {
"something, awful, is\n",
"fine,,just\n",
"like something missing: ,,\n",
})
{
It f = input.begin(), l = input.end();
data parsed;
bool ok = qi::phrase_parse(f,l,p,qi::blank,parsed);
if (ok)
std::cout << "Parsed: '" << parsed.a << "', '" << parsed.b << "', '" << parsed.c << "'\n";
else
std::cout << "Parse failed\n";
if (f!=l)
std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
}
}
打印:
Parsed: 'something', 'awful', 'is'
Parsed: 'fine', '(unspecified)', 'just'
Parsed: 'like something missing: ', '(unspecified)', '(unspecified)'
但是,你有一个更大的问题。 qi::repeat(2) [ value ]
将解析为 2 个字符串的假设不起作用。
repeat
,像operator*
、operator+
和operator%
解析成一个容器属性。在这种情况下,容器属性(字符串)也将接收来自第二个 value
的输入:
Parsed: 'somethingawful', 'is', ''
Parsed: 'fine(unspecified)', 'just', ''
Parsed: 'like something missing: (unspecified)', '(unspecified)', ''
因为这不是您想要的,请重新考虑您的数据类型:
要么不调整结构,而是编写自定义特征来分配字段(参见 http://www.boost.org/doc/libs/1_57_0/libs/spirit/doc/html/spirit/advanced/customize.html)
更改结构以包含 std::string 的向量以匹配暴露的属性
或者创建一个自动解析器生成器:
auto_
方法:
如果你教 Qi 如何提取单个值,你可以使用像
这样的简单规则
entry = skip(skipper() | ',') [auto_] >> eol;
这样,Spirit 本身将为给定的 Fusion 序列生成正确数量的值提取!
这是一个快速但肮脏的方法:
CAVEAT Specializing for std::string
directly like this might not be the best idea (it might not always be appropriate and might interact badly with other parsers). However, by default create_parser<std::string>
is not defined (because, what would it do?) so I seized the opportunity for the purpose of this demonstration:
namespace boost { namespace spirit { namespace traits {
template <> struct create_parser<std::string> {
typedef proto::result_of::deep_copy<
BOOST_TYPEOF(
qi::lexeme [+(qi::char_ - ',' - qi::eol)] | qi::attr("(unspecified)")
)
>::type type;
static type call() {
return proto::deep_copy(
qi::lexeme [+(qi::char_ - ',' - qi::eol)] | qi::attr("(unspecified)")
);
}
};
}}}
再次查看演示输出:
Parsed: 'something', 'awful', 'is'
Parsed: 'fine', 'just', '(unspecified)'
Parsed: 'like something missing: ', '(unspecified)', '(unspecified)'
NOTE There was some advanced sorcery to get the skipper to work "just right" (see skip()[]
and lexeme[]
). Some general explanations can be found here: Boost spirit skipper issues
更新
容器方法
其中有一个微妙之处。实际上是两个。所以这是一个演示:
//#define BOOST_SPIRIT_DEBUG
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
struct data {
std::vector<std::string> parts;
};
BOOST_FUSION_ADAPT_STRUCT(data, (std::vector<std::string>, parts))
template <typename Iterator, typename skipper = qi::blank_type>
struct google_parser : qi::grammar<Iterator, data(), skipper> {
google_parser() : google_parser::base_type(entry, "contacts") {
using namespace qi;
qi::as<std::vector<std::string> > strings;
value = +(char_ - ',' - eol) | attr("(unspecified)");
entry = strings [ repeat(2) [ value >> ',' ] >> value ] >> eol;
BOOST_SPIRIT_DEBUG_NODES((value)(entry))
}
private:
qi::rule<Iterator, std::string()> value;
qi::rule<Iterator, data(), skipper> entry;
};
int main() {
using It = std::string::const_iterator;
google_parser<It> p;
for (std::string input : {
"something, awful, is\n",
"fine,,just\n",
"like something missing: ,,\n",
})
{
It f = input.begin(), l = input.end();
data parsed;
bool ok = qi::phrase_parse(f,l,p,qi::blank,parsed);
if (ok) {
std::cout << "Parsed: ";
for (auto& part : parsed.parts)
std::cout << "'" << part << "' ";
std::cout << "\n";
}
else
std::cout << "Parse failed\n";
if (f!=l)
std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
}
}
细微之处在于:
- 通过自动属性处理调整单元素序列遇到边缘情况:Spirit Qi attribute propagation issue with single-member struct
- 在这种特殊情况下,Spirit 需要亲力亲为才能将
repeat[...]>>value
视为/原子/合成单个容器。 as<T>
directive solves that here
我想将 CSV 文件读入结构中:
struct data
{
std::string a;
std::string b;
std::string c;
}
但是,我什至想读取空字符串以确保所有值都在正确的位置。 我将结构调整为 boost::fusion,因此以下工作:
// Our parser (using a custom skipper to skip comments and empty lines )
template <typename Iterator, typename skipper = comment_skipper<Iterator> >
struct google_parser : qi::grammar<Iterator, addressbook(), skipper>
{
google_parser() : google_parser::base_type(contacts, "contacts")
{
using qi::eol;
using qi::eps;
using qi::_1;
using qi::_val;
using qi::repeat;
using standard_wide::char_;
using phoenix::at_c;
using phoenix::val;
value = *(char_ - ',' - eol) [_val += _1];
// This works but only for small structs
entry %= value >> ',' >> value >> ',' >> value >> eol;
}
qi::rule<Iterator, std::string()> value;
qi::rule<Iterator, data()> entry;
};
不幸的是,repeat
将所有非空值存储在向量中,因此属性值可能会混合在一起(即,如果 b
的字段为空,它可能包含来自c
):
entry %= repeat(2)[ value >> ','] >> value >> eol;
我想使用类似于 repeat
的简短规则,因为我的结构在实践中有 60 个属性!不仅写 60 条规则很乏味,而且 Boost 似乎不喜欢长规则...
您只想确保您也解析了 "empty" 个字符串的值。
value = +(char_ - ',' - eol) | attr("(unspecified)");
entry = value >> ',' >> value >> ',' >> value >> eol;
查看演示:
//#define BOOST_SPIRIT_DEBUG
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
struct data {
std::string a;
std::string b;
std::string c;
};
BOOST_FUSION_ADAPT_STRUCT(data, (std::string, a)(std::string, b)(std::string, c))
template <typename Iterator, typename skipper = qi::blank_type>
struct google_parser : qi::grammar<Iterator, data(), skipper> {
google_parser() : google_parser::base_type(entry, "contacts") {
using namespace qi;
value = +(char_ - ',' - eol) | attr("(unspecified)");
entry = value >> ',' >> value >> ',' >> value >> eol;
BOOST_SPIRIT_DEBUG_NODES((value)(entry))
}
private:
qi::rule<Iterator, std::string()> value;
qi::rule<Iterator, data(), skipper> entry;
};
int main() {
using It = std::string::const_iterator;
google_parser<It> p;
for (std::string input : {
"something, awful, is\n",
"fine,,just\n",
"like something missing: ,,\n",
})
{
It f = input.begin(), l = input.end();
data parsed;
bool ok = qi::phrase_parse(f,l,p,qi::blank,parsed);
if (ok)
std::cout << "Parsed: '" << parsed.a << "', '" << parsed.b << "', '" << parsed.c << "'\n";
else
std::cout << "Parse failed\n";
if (f!=l)
std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
}
}
打印:
Parsed: 'something', 'awful', 'is'
Parsed: 'fine', '(unspecified)', 'just'
Parsed: 'like something missing: ', '(unspecified)', '(unspecified)'
但是,你有一个更大的问题。 qi::repeat(2) [ value ]
将解析为 2 个字符串的假设不起作用。
repeat
,像operator*
、operator+
和operator%
解析成一个容器属性。在这种情况下,容器属性(字符串)也将接收来自第二个 value
的输入:
Parsed: 'somethingawful', 'is', ''
Parsed: 'fine(unspecified)', 'just', ''
Parsed: 'like something missing: (unspecified)', '(unspecified)', ''
因为这不是您想要的,请重新考虑您的数据类型:
要么不调整结构,而是编写自定义特征来分配字段(参见 http://www.boost.org/doc/libs/1_57_0/libs/spirit/doc/html/spirit/advanced/customize.html)
更改结构以包含 std::string 的向量以匹配暴露的属性
或者创建一个自动解析器生成器:
auto_
方法:
如果你教 Qi 如何提取单个值,你可以使用像
这样的简单规则entry = skip(skipper() | ',') [auto_] >> eol;
这样,Spirit 本身将为给定的 Fusion 序列生成正确数量的值提取!
这是一个快速但肮脏的方法:
CAVEAT Specializing for
std::string
directly like this might not be the best idea (it might not always be appropriate and might interact badly with other parsers). However, by defaultcreate_parser<std::string>
is not defined (because, what would it do?) so I seized the opportunity for the purpose of this demonstration:
namespace boost { namespace spirit { namespace traits {
template <> struct create_parser<std::string> {
typedef proto::result_of::deep_copy<
BOOST_TYPEOF(
qi::lexeme [+(qi::char_ - ',' - qi::eol)] | qi::attr("(unspecified)")
)
>::type type;
static type call() {
return proto::deep_copy(
qi::lexeme [+(qi::char_ - ',' - qi::eol)] | qi::attr("(unspecified)")
);
}
};
}}}
再次查看演示输出:
Parsed: 'something', 'awful', 'is'
Parsed: 'fine', 'just', '(unspecified)'
Parsed: 'like something missing: ', '(unspecified)', '(unspecified)'
NOTE There was some advanced sorcery to get the skipper to work "just right" (see
skip()[]
andlexeme[]
). Some general explanations can be found here: Boost spirit skipper issues
更新
容器方法
其中有一个微妙之处。实际上是两个。所以这是一个演示:
//#define BOOST_SPIRIT_DEBUG
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
struct data {
std::vector<std::string> parts;
};
BOOST_FUSION_ADAPT_STRUCT(data, (std::vector<std::string>, parts))
template <typename Iterator, typename skipper = qi::blank_type>
struct google_parser : qi::grammar<Iterator, data(), skipper> {
google_parser() : google_parser::base_type(entry, "contacts") {
using namespace qi;
qi::as<std::vector<std::string> > strings;
value = +(char_ - ',' - eol) | attr("(unspecified)");
entry = strings [ repeat(2) [ value >> ',' ] >> value ] >> eol;
BOOST_SPIRIT_DEBUG_NODES((value)(entry))
}
private:
qi::rule<Iterator, std::string()> value;
qi::rule<Iterator, data(), skipper> entry;
};
int main() {
using It = std::string::const_iterator;
google_parser<It> p;
for (std::string input : {
"something, awful, is\n",
"fine,,just\n",
"like something missing: ,,\n",
})
{
It f = input.begin(), l = input.end();
data parsed;
bool ok = qi::phrase_parse(f,l,p,qi::blank,parsed);
if (ok) {
std::cout << "Parsed: ";
for (auto& part : parsed.parts)
std::cout << "'" << part << "' ";
std::cout << "\n";
}
else
std::cout << "Parse failed\n";
if (f!=l)
std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
}
}
细微之处在于:
- 通过自动属性处理调整单元素序列遇到边缘情况:Spirit Qi attribute propagation issue with single-member struct
- 在这种特殊情况下,Spirit 需要亲力亲为才能将
repeat[...]>>value
视为/原子/合成单个容器。as<T>
directive solves that here