我正在尝试使用以下语法解析测试结果的日志文件。
template<typename Iterator>
struct test_step_parse: qi::grammar<Iterator, TestResults(), ascii::space_type> {
test_step_parse() : test_step_parse::base_type(rqmts) {
using qi::lit;
using qi::lexeme;
using qi::omit;
using qi::int_;
using qi::eol;
using qi::char_;
test_step = lit("Test Step No.") >> int_;
pass_or_fail = lit("-") >> (string("Passed") | string("Failed"));
fdor_rqmts = -lit("OID")
>> lit("FDOR")
>> -lit("OID")
>> int_
>> *(-omit[char_(",/\\")] >> -(lit("FDOR") ^ lit("OID")) >> int_)
;
sard_rqmts = -lit("OID")
>> lit("SARD")
>> -lit("OID")
>> int_
>> *(-omit[char_(",/\\")] >> -(lit("SARD") ^ -lit("OID")) >> int_)
;
comment = lit("Comment:") >> lexeme[*(char_ - eol)] ;
rqmts = test_step
>> pass_or_fail
>> omit[*(char_ - lit("REQUIREMENT VERIFIED:"))]
>> lit("REQUIREMENT VERIFIED:")
>> (fdor_rqmts ^ sard_rqmts)
>> omit[+char_("=")]
>> -comment
;
BOOST_SPIRIT_DEBUG_NODE(test_step);
BOOST_SPIRIT_DEBUG_NODE(pass_or_fail);
BOOST_SPIRIT_DEBUG_NODE(fdor_rqmts);
BOOST_SPIRIT_DEBUG_NODE(sard_rqmts);
BOOST_SPIRIT_DEBUG_NODE(comment);
BOOST_SPIRIT_DEBUG_NODE(rqmts);
}
qi::rule<Iterator, TestResults(), ascii::space_type> rqmts;
qi::rule<Iterator, int(), ascii::space_type> test_step;
qi::rule<Iterator, std::string(), ascii::space_type> pass_or_fail;
qi::rule<Iterator, std::string(), ascii::space_type> comment;
qi::rule<Iterator, std::vector<int>(), ascii::space_type> fdor_rqmts;
qi::rule<Iterator, std::vector<int>(), ascii::space_type> sard_rqmts;
};
语法分析正常。但是,当尝试自动填充我的用户定义结构时,std::string 类型的规则“comment”的属性被作为 ascii 数字传递到向量中。
这是我的用户定义结构:
struct TestResults {
int test_step_no;
std::string pass_or_fail;
std::vector<int> FDOR;
std::vector<int> SARD;
std::string comment;
};
BOOST_FUSION_ADAPT_STRUCT(
atp::TestResults,
(int, test_step_no)
(std::string, pass_or_fail)
(std::vector<int>, FDOR)
(std::vector<int>, SARD)
(std::string, comment)
)
调试输出表明规则公开了正确的类型,但不知何故,它们没有正确放置在结构中。
任何想法我做错了什么?对任何滥用术语等表示抱歉。
使用 gcc 4.6.3 并提升 1-47。谢谢你。
更新:为了澄清,所以我期待解析以下内容:
std::string test_test =
"Test Step No. 953-Failed\n"
"==============================================================================\n"
"STEP 7.7:\n"
"Test step, etc.\n"
"===============================================================================\n"
"REQUIREMENT VERIFIED: FDOR 12345"
" SARD 12345, 12356\n"
"===============================================================================\n"
"Comment: Didn't work.\n"
;
使用调试器,似乎所有规则都正确地公开了它们的属性:
<rqmts>
<try>Test Step No. 953-Fa</try>
<test_step>
<try>Test Step No. 953-Fa</try>
<success>-Failed\n============</success>
<attributes>[953]</attributes>
</test_step>
<pass_or_fail>
<try>-Failed\n============</try>
<success>\n===================</success>
<attributes>[[F, a, i, l, e, d]]</attributes>
</pass_or_fail>
<fdor_rqmts>
<try> FDOR 12345 </try>
<success> </success>
<attributes>[[12345]]</attributes>
</fdor_rqmts>
<sard_rqmts>
<try> </try>
<success>\n===================</success>
<attributes>[[12345, 12356]]</attributes>
</sard_rqmts>
<comment>
<try>Comment: Didn't work</try>
<success>\n</success>
<attributes>[[D, i, d, n, ', t, , w, o, r, k, .]]</attributes>
</comment>
<success>\n</success>
<attributes>[[953, [F, a, i, l, e, d], [12345], [68, 105, 100, 110, 39, 116, 32, 119, 111, 114, 107, 46], []]]</attributes>
</rqmts>
所以一切似乎都在工作,除了“评论”属性(std :: string)被填充在“SARD”属性(vector)中,作为ascii数字的向量,即,
[68, 105, 100, 110, 39, 116, 32, 119, 111, 114, 107, 46] = "Didn't work."