以下是一个完全独立的示例。问题似乎是第84-89行 - 如果这些行已注释掉,则示例将进行编译。我正在尝试解析的是文件的每一行,有五个以冒号分隔的项目,最后三个项目是可选的。单个函数使用boost::filesystem::file
,使用boost.interprocess
吸收它并解析它。
我想要解析的内容示例:
a:1
a:2:c
a:3::d
a:4:::e
a:4:c:d:e
结果应存储在vector<file_line>
中,而file_line
是一个包含五个成员的结构,最后三个是可选的。这是代码和错误:
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma warning(disable : 4512) // assignment operator could not be generated
# pragma warning(disable : 4127) // conditional expression is constant
# pragma warning(disable : 4244) // 'initializing' : conversion from 'int' to 'char', possible loss of data
#endif
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/home/qi.hpp>
#include <boost/spirit/home/qi/string.hpp>
#include <boost/spirit/home/karma.hpp>
#include <boost/spirit/home/karma/binary.hpp>
#include <boost/spirit/home/phoenix.hpp>
#include <boost/spirit/home/phoenix/bind.hpp>
#include <boost/spirit/home/phoenix/core.hpp>
#include <boost/spirit/home/phoenix/operator.hpp>
#include <boost/spirit/home/phoenix/statement/sequence.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/filesystem/operations.hpp>
#include <string>
// This struct and fusion adapter is for parsing file servers in colon-newline format.
struct file_line
{
std::string a;
unsigned short b;
boost::optional<std::string> c;
boost::optional<std::string> d;
boost::optional<std::string> e;
};
BOOST_FUSION_ADAPT_STRUCT(
file_line,
(std::string, a)
(unsigned short, b)
(boost::optional<std::string>, c)
(boost::optional<std::string>, d)
(boost::optional<std::string>, e)
)
void
import_proxies_colon_newline(const boost::filesystem::path& file)
{
using namespace boost::spirit;
using qi::parse;
using qi::char_;
using qi::eol;
using qi::eoi;
using qi::lit;
using qi::ushort_;
// <word>:<ushort>:[word]:[word]:[word]
if(boost::filesystem::exists(file) && 0 != boost::filesystem::file_size(file))
{
// Use Boost.Interprocess for fast sucking in of the file. It works great, and provides the bidirectional
// iterators that we need for spirit.
boost::interprocess::file_mapping mapping(file.file_string().c_str(), boost::interprocess::read_only);
boost::interprocess::mapped_region mapped_rgn(mapping, boost::interprocess::read_only);
const char* beg = reinterpret_cast<char*>(mapped_rgn.get_address());
char const* const end = beg + mapped_rgn.get_size();
// And parse the data, putting the results into a vector of pairs of strings.
std::vector<file_line> output;
parse(beg, end,
// Begin grammar
(
*(
*eol
>> +(char_ - (':' | eol)
>> ':' >> ushort_
>> -(':'
>> *(char_ - (':' | eol))
>> (eol |
-(':'
>> *(char_ - (':' | eol))
// This doesn't work. Uncomment it, won't compile. No idea why. It's the same
// as above.
>> (eol |
-(':'
>>
+(char_ - eol)
)
)
)
)
)
>> *eol
)
)
// End grammar, begin output data
,output
);
}
}
由于问题限制在30,000个字符以内,我只会在这里显示前几个字符。该示例应尝试在您的计算机上编译和生成相同的内容。
1>C:\devel\dependencies\boost\boost-1_44\include\boost/spirit/home/support/container.hpp(101): error C2955: 'boost::Container' : use of class template requires template argument list
1> C:\devel\dependencies\boost\boost-1_44\include\boost/concept_check.hpp(602) : see declaration of 'boost::Container'
1> C:\devel\dependencies\boost\boost-1_44\include\boost/spirit/home/qi/operator/kleene.hpp(65) : see reference to class template instantiation 'boost::spirit::traits::container_value<Container>' being compiled
1> with
1> [
1> Container=char
1> ]
1> C:\devel\dependencies\boost\boost-1_44\include\boost/spirit/home/qi/detail/fail_function.hpp(38) : see reference to function template instantiation 'bool boost::spirit::qi::kleene<Subject>::parse<Iterator,Context,Skipper,Attribute>(Iterator &,const Iterator &,Context &,const Skipper &,Attribute &) const' being compiled
1> with
1> [
1> Subject=boost::spirit::qi::difference<boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::char_,boost::spirit::char_encoding::standard>>,boost::spirit::qi::alternative<boost::fusion::cons<boost::spirit::qi::literal_char<boost::spirit::char_encoding::standard,true,false>,boost::fusion::cons<boost::spirit::qi::eol_parser,boost::fusion::nil>>>>,
1> Iterator=const char *,
1> Context=const boost::fusion::unused_type,
1> Skipper=boost::fusion::unused_type,
1> Attribute=char
1> ]
...剪断 ...
1>C:\devel\dependencies\boost\boost-1_44\include\boost/spirit/home/support/container.hpp(102): fatal error C1903: unable to recover from previous error(s); stopping compilation
答案 0 :(得分:2)
我已经在Spirit邮件列表上回答了,但是为了完整起见,让我在这里发布。
你的榜样远非微不足道。我没有理由在代码中留下进程间,文件系统或Karma引用。这只会让每个愿意帮助的人更难以诊断事情。此外,你在某处有一个不匹配的括号。我假设您错过了关闭+(char_ - (':' | eol)
。
好的,让我们仔细看看。这是你的(简化)语法。它不再有用了,但在属性方面它应该与原来的行为相同:
*(+char_ >> -(*char_ >> (eol | -(*char_ >> (eol | -(':' >> +char_))))))
该语法的公开(传播属性)是:
vector<
tuple<
std::vector<char>,
optional<
tuple<
std::vector<char>,
variant<
char,
optional<
tuple<
std::vector<char>,
variant<
char,
optional<
std::vector<char>
>
>
>
>
>
>
>
>
>
属性兼容性规则可以做很多,但是他们无法确定将std :: string映射到variant<char, vector<char> >
。而且,我相信你自己不再理解你的语法了,为什么在这种情况下你希望Spirit能够正确理解你的语法?
我建议你首先将事物简化为规则,从而简化语法。这不仅使其更容易理解,而且允许您告诉Spirit您希望从语法的哪个子部分返回什么属性。例如:
rule<char const*, std::string()> e1 = +~char_(":\r\n");
rule<char const*, std::string()> e2 = *~char_(":\r\n");
rule<char const*, std::string()> e3 = +~char_("\r\n");
rule<char const*, ushort()> u = ':' >> ushort_;
rule<char const*, file_line()> fline =
*eol >> e1 >> u
>> -(':' >> e2 >> (eol | -(':' >> e2 >> (eol | -(':' >> e3))))) >> *eol;
使整个语法更具可读性:
*fline
好吗,对吧?
如果你进一步思考,你会发现,写作
foo >> (eol | -bar) >> *eol
相当于:
foo >> -bar >> *eol
进一步简化了它:
rule<char const*, file_line()> f =
*eol >> e1 >> u >> -(':' >> e2 >> -(':' >> e2 >> -(':' >> e3) ) ) >> *eol;
你现在可以看到你的语法产生至少5个子属性,而你的file_list只有4个成员。您需要相应地调整file_list结构。
以上编译现在(Boost SVN trunk),但它无法产生正确的结果。如果我使用"a:4:c:d:e"
提供,我会得到结果:output[0].a == "a"
,output[0].b == 4
和output[0].c == "cde"
。让我们来分析一下为什么会这样。
同样,属性兼容性规则只能完成部分工作。在这种情况下,file_list::a
会映射到e1
,file_list::b
映射到u
,而file_list::c
会映射到整个表达式的其余部分。实际上,这是你所期望的,因为可选项将序列分成3个元素。你的属性是“扁平的”,而语法则不是。
有两种解决方案:a)更改属性以匹配语法结构:
struct file_line
{
std::string a;
unsigned short b;
boost::optional<
fusion::vector<
std::string,
boost::optional<
fusion::vector<std::string, boost::optional<std::string> >
>
>
> c;
};
或b)使用语义动作来设置属性的元素(这就是我要做的)。