提升精神语法eol
Boost Spirit grammar eol
我正在尝试解析以下形式的文件:
// comment bla bla
[sectionname]
key = value
key2=value2
// comment
key = value
[anothersection]
...
使用以下代码。不幸的是,它报告最后一个eol是一个错误,尽管最后的所有eol都应该被接受:(*qi::eol>-(sectionGrammar>*(+qi::iol>sectionGrama))>*qi:(eol),
此外,我真的不知道如何在不使用下一个键值对所需的eol的情况下正确解析注释,这也是我没有在Skipper中放入的原因(只有ascii::blank)。
我遇到的最后一个问题是,我不知道如何在不复制的情况下将节添加到boost::ptr_vector中。
这是我的代码:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_multi_pass.hpp>
#include <boost/spirit/include/classic_position_iterator.hpp> // for more detailed error information
#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/bind.hpp>
#include <boost/spirit/home/phoenix/core/argument.hpp>
#include <boost/foreach.hpp>
#include "txt.hpp"
// Only use in global namespace!
BOOST_FUSION_ADAPT_STRUCT(
wc3lib::map::Txt::Section,
(wc3lib::string, name)
(wc3lib::map::Txt::Pairs, entries)
)
namespace wc3lib
{
namespace map
{
namespace client
{
using namespace boost::spirit;
//using namespace boost::spirit::qi;
using qi::double_;
using qi::phrase_parse;
using standard::space;
using boost::phoenix::ref;
//typedef BOOST_TYPEOF(space | lit("//") >> *(standard::char_ - qi::eol) >> qi::eol) SkipperType;
/*
* Doesn't skip eols since value pairs are separated linewise which therefore can be specified easier in the rules
*/
template<typename Iterator>
struct CommentSkipper : public qi::grammar<Iterator> {
qi::rule<Iterator> skip;
CommentSkipper() : CommentSkipper::base_type(skip, "PL/0")
{
skip = ascii::blank | lit("//") >> *(standard::char_ - qi::eol) >> qi::eol;
}
};
template <typename Iterator, typename Skipper = CommentSkipper<Iterator> >
struct KeyValueSquence : qi::grammar<Iterator, Txt::Pairs(), Skipper>
{
//Txt::Pairs::value_type
qi::rule<Iterator, Txt::Pairs(), Skipper> query; // NOTE first rule used as parameter for base_type does always need the skipper type of the grammar
qi::rule<Iterator, std::pair<string, string>(), Skipper> pair;
qi::rule<Iterator, string()> key, value;
KeyValueSquence() : KeyValueSquence::base_type(query)
{
query = pair > *(pair); // use only > for backtracking
pair = +qi::eol > key > lit('=') > -value; // -('=' >> value)
key = standard::char_("a-zA-Z_") > *standard::char_("a-zA-Z_0-9");
value = +(standard::char_ - qi::eol); // values can be empty or all characters except eol which indicates the and of the value
}
};
template <typename Iterator, typename Skipper = CommentSkipper<Iterator> >
struct SectionRule : qi::grammar<Iterator, Txt::Section(), Skipper>
{
qi::rule<Iterator, Txt::Section(), Skipper> query;
qi::rule<Iterator, string()> name;
qi::rule<Iterator, Txt::Pairs(), Skipper> entries;
KeyValueSquence<Iterator, Skipper> keyValueSequence;
SectionRule() : SectionRule::base_type(query)
{
query = name > -entries;
name = lit('[') > standard::char_("a-zA-Z_") > *standard::char_("a-zA-Z_0-9") > lit(']');
entries = keyValueSequence;
}
};
template <typename Iterator>
bool parse(Iterator first, Iterator last, Txt::Sections §ions)
{
SectionRule<Iterator> sectionGrammar;
CommentSkipper<Iterator> commentSkipper;
std::vector<Txt::Section> tmpSections;
bool r = boost::spirit::qi::phrase_parse(
first,
last,
(*qi::eol > -(sectionGrammar > *(+qi::eol > sectionGrammar)) > *qi::eol),
// comment skipper
commentSkipper,
tmpSections //sections store into "sections"!
);
if (first != last) // fail if we did not get a full match
{
return false;
}
// TODO temporary workaround, add sections directly from heap to vector
BOOST_FOREACH(std::vector<Txt::Section>::const_reference ref, tmpSections) {
std::auto_ptr<Txt::Section> s(new Txt::Section());
s->name = ref.name;
s->entries = ref.entries;
sections.push_back(s);
}
return r;
}
}
</code>
来自注释
// use only > for backtracking
我觉得你理解错了。>
实际上将阻止回溯超过该点,因为它强制下一个令牌。
为了展示一些技术,我模拟了假定缺失的标题:
// #include "txt.hpp"
// minimal mockup
namespace wc3lib {
using std::string;
namespace map { namespace Txt {
typedef std::map<string, string> Pairs;
struct Section
{
string name;
Pairs entries;
};
typedef std::vector<Section> Sections;
} }
}
现在,我已经"修复"了你的代码,以展示如何进行
- 调试
- 错误处理
- 期望值(我选择了"非严格"解决方案,因为我没有时间仔细观察期望值的意义)
在Coliru现场观看
票据
请不要在命名空间范围内
using namespace
。相反,使用方便的名称空间别名:namespace qi = boost::spirit::qi; namespace ascii = boost::spirit::ascii;
请不要(不要)使用
auto_ptr<>
。它容易出错、过时、不推荐使用、不灵活:/如果必须在堆上有节,请改用unique_ptr
(为什么?)。- 期望流的末尾以及
eol
终止您的线路 命名时要小心。
query
被双重用作规则名称- 您是在解析节、查询还是
KeyValueSquence
- 考虑使用CCD_ 8而不是CCD_ 9等
我确实觉得,如果更仔细地命名,就不会出现一些混乱。
考虑将语法合并为一个语法,除非
- 真正的语法要复杂得多(在这个级别上,这似乎无关紧要,因为你仍然可以将这三个语法分组为一个子语法)
- 你绝对需要把语法分成不同的翻译单元
事不宜迟:
#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted.hpp>
#include <boost/foreach.hpp>
#include <map>
// #include "txt.hpp"
// minimal mockup
namespace wc3lib {
using std::string;
namespace map { namespace Txt {
typedef std::map<string, string> Pairs;
struct Section
{
string name;
Pairs entries;
};
typedef std::vector<Section> Sections;
} }
}
// Only use in global namespace!
BOOST_FUSION_ADAPT_STRUCT(
wc3lib::map::Txt::Section,
(wc3lib::string, name)
(wc3lib::map::Txt::Pairs, entries)
)
namespace wc3lib { namespace map { namespace client {
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
/*
* Doesn't skip eols since value pairs are separated linewise which therefore can be specified easier in the rules
*/
template<typename Iterator>
struct CommentSkipper : public qi::grammar<Iterator> {
qi::rule<Iterator> skip;
CommentSkipper() : CommentSkipper::base_type(skip, "PL/0")
{
using namespace qi;
skip = ascii::blank | (lit("//") >> *(standard::char_ - eol) >> eol);
BOOST_SPIRIT_DEBUG_NODES((skip));
}
};
template <typename Iterator, typename Skipper = CommentSkipper<Iterator> >
struct KeyValueSquence : qi::grammar<Iterator, Txt::Pairs(), Skipper>
{
qi::rule<Iterator, Txt::Pairs(), Skipper> pairs; // NOTE first rule used as parameter for base_type does always need the skipper type of the grammar
qi::rule<Iterator, std::pair<string, string>(), Skipper> pair;
qi::rule<Iterator, string()> key, value;
KeyValueSquence() : KeyValueSquence::base_type(pairs)
{
using namespace qi;
pairs = +pair; // use only > for backtracking
// these had a problem with backtracking (failing the rule at the end of a section)
pair = +eol > key > lit('=') > value; // -('=' >> value)
key = standard::char_("a-zA-Z_") > *standard::char_("a-zA-Z_0-9");
// using this removes that problem:
pair = +eol >> key >> lit('=') >> value; // -('=' >> value)
key = standard::char_("a-zA-Z_") >> *standard::char_("a-zA-Z_0-9");
value = *(standard::char_ - (eol|eoi)); // values can be empty or all characters except eol which indicates the end of the value
BOOST_SPIRIT_DEBUG_NODES((pairs)(pair)(key)(value));
}
};
template <typename Iterator, typename Skipper = CommentSkipper<Iterator> >
struct SectionRule : qi::grammar<Iterator, Txt::Section(), Skipper>
{
qi::rule<Iterator, Txt::Section(), Skipper> query;
qi::rule<Iterator, string()> name;
KeyValueSquence<Iterator, Skipper> keyValueSequence;
SectionRule() : SectionRule::base_type(query)
{
using namespace qi;
name = lit('[') > standard::char_("a-zA-Z_") > *standard::char_("a-zA-Z_0-9") > lit(']');
query = name > -keyValueSequence;
BOOST_SPIRIT_DEBUG_NODES((query)(name));
}
};
template <typename Iterator>
bool parse(Iterator first, Iterator last, Txt::Sections §ions)
{
SectionRule<Iterator> sectionGrammar;
CommentSkipper<Iterator> commentSkipper;
std::vector<Txt::Section> tmpSections;
try
{
bool r = qi::phrase_parse(
first, last,
(sectionGrammar % +qi::eol) >> *qi::eol > qi::eoi,
// comment skipper
commentSkipper,
tmpSections //sections store into "sections"!
);
if (first != last) // fail if we did not get a full match
{
std::cerr << "DEBUG: Unparsed: '" << std::string(first,last) << "n";
return false;
}
// TODO temporary workaround, add sections directly from heap to vector
sections = tmpSections;
return r;
} catch(qi::expectation_failure<Iterator> const& e)
{
std::cerr << "Unexpected: " << e.what() << " at '" << std::string(e.first,e.last) << "n";
return false;
}
}
} } }
int main()
{
std::cin.unsetf(std::ios::skipws);
boost::spirit::istream_iterator f(std::cin), l;
wc3lib::map::Txt::Sections parsed;
bool ok = wc3lib::map::client::parse(f, l, parsed);
if (ok)
{
std::cout << "Parsed " << parsed.size() << " sectionsn";
for(auto& section : parsed)
{
std::cout << "section [" << section.name << "] has " << section.entries.size() << " pairsn";
}
}
}
哪个打印
Parsed 2 sections
section [sectionname] has 3 pairs
section [anothersection] has 1 pairs
在解析器的调试跟踪之后/与之一起:
<query>
<try>// comment bla blan[</try>
<skip>
<try>// comment bla blan[</try>
<success>[sectionname]nkey = </success>
<attributes>[]</attributes>
</skip>
<skip>
<try>[sectionname]nkey = </try>
<fail/>
</skip>
<name>
<try>[sectionname]nkey = </try>
<success>nkey = valuenkey2=va</success>
<attributes>[[s, e, c, t, i, o, n, n, a, m, e]]</attributes>
</name>
<pairs>
<try>nkey = valuenkey2=va</try>
<pair>
<try>nkey = valuenkey2=va</try>
<skip>
<try>nkey = valuenkey2=va</try>
<fail/>
</skip>
<skip>
<try>key = valuenkey2=val</try>
<fail/>
</skip>
<skip>
<try>key = valuenkey2=val</try>
<fail/>
</skip>
<key>
<try>key = valuenkey2=val</try>
<success> = valuenkey2=value2</success>
<attributes>[[k, e, y]]</attributes>
</key>
<skip>
<try> = valuenkey2=value2</try>
<success>= valuenkey2=value2n</success>
<attributes>[]</attributes>
</skip>
<skip>
<try>= valuenkey2=value2n</try>
<fail/>
</skip>
<skip>
<try> valuenkey2=value2nn</try>
<success>valuenkey2=value2nnn</success>
<attributes>[]</attributes>
</skip>
<skip>
<try>valuenkey2=value2nnn</try>
<fail/>
</skip>
<value>
<try>valuenkey2=value2nnn</try>
<success>nkey2=value2nnn// co</success>
<attributes>[[v, a, l, u, e]]</attributes>
</value>
<success>nkey2=value2nnn// co</success>
<attributes>[[[k, e, y], [v, a, l, u, e]]]</attributes>
</pair>
<pair>
<try>nkey2=value2nnn// co</try>
<skip>
<try>nkey2=value2nnn// co</try>
<fail/>
</skip>
<skip>
<try>key2=value2nnn// com</try>
<fail/>
</skip>
<skip>
<try>key2=value2nnn// com</try>
<fail/>
</skip>
<key>
<try>key2=value2nnn// com</try>
<success>=value2nnn// comment</success>
<attributes>[[k, e, y, 2]]</attributes>
</key>
<skip>
<try>=value2nnn// comment</try>
<fail/>
</skip>
<skip>
<try>value2nnn// commentn</try>
<fail/>
</skip>
<value>
<try>value2nnn// commentn</try>
<success>nnn// commentnkey3 =</success>
<attributes>[[v, a, l, u, e, 2]]</attributes>
</value>
<success>nnn// commentnkey3 =</success>
<attributes>[[[k, e, y, 2], [v, a, l, u, e, 2]]]</attributes>
</pair>
<pair>
<try>nnn// commentnkey3 =</try>
<skip>
<try>nnn// commentnkey3 =</try>
<fail/>
</skip>
<skip>
<try>nn// commentnkey3 = </try>
<fail/>
</skip>
<skip>
<try>n// commentnkey3 = v</try>
<fail/>
</skip>
<skip>
<try>// commentnkey3 = va</try>
<success>key3 = value3nn[anot</success>
<attributes>[]</attributes>
</skip>
<skip>
<try>key3 = value3nn[anot</try>
<fail/>
</skip>
<skip>
<try>key3 = value3nn[anot</try>
<fail/>
</skip>
<key>
<try>key3 = value3nn[anot</try>
<success> = value3nn[anothers</success>
<attributes>[[k, e, y, 3]]</attributes>
</key>
<skip>
<try> = value3nn[anothers</try>
<success>= value3nn[anotherse</success>
<attributes>[]</attributes>
</skip>
<skip>
<try>= value3nn[anotherse</try>
<fail/>
</skip>
<skip>
<try> value3nn[anothersec</try>
<success>value3nn[anothersect</success>
<attributes>[]</attributes>
</skip>
<skip>
<try>value3nn[anothersect</try>
<fail/>
</skip>
<value>
<try>value3nn[anothersect</try>
<success>nn[anothersection]nk</success>
<attributes>[[v, a, l, u, e, 3]]</attributes>
</value>
<success>nn[anothersection]nk</success>
<attributes>[[[k, e, y, 3], [v, a, l, u, e, 3]]]</attributes>
</pair>
<pair>
<try>nn[anothersection]nk</try>
<skip>
<try>nn[anothersection]nk</try>
<fail/>
</skip>
<skip>
<try>n[anothersection]nke</try>
<fail/>
</skip>
<skip>
<try>[anothersection]nkey</try>
<fail/>
</skip>
<skip>
<try>[anothersection]nkey</try>
<fail/>
</skip>
<key>
<try>[anothersection]nkey</try>
<fail/>
</key>
<fail/>
</pair>
<success>nn[anothersection]nk</success>
<attributes>[[[[k, e, y], [v, a, l, u, e]], [[k, e, y, 2], [v, a, l, u, e, 2]], [[k, e, y, 3], [v, a, l, u, e, 3]]]]</attributes>
</pairs>
<success>nn[anothersection]nk</success>
<attributes>[[[s, e, c, t, i, o, n, n, a, m, e], [[[k, e, y], [v, a, l, u, e]], [[k, e, y, 2], [v, a, l, u, e, 2]], [[k, e, y, 3], [v, a, l, u, e, 3]]]]]</attributes>
</query>
<skip>
<try>nn[anothersection]nk</try>
<fail/>
</skip>
<skip>
<try>n[anothersection]nke</try>
<fail/>
</skip>
<skip>
<try>[anothersection]nkey</try>
<fail/>
</skip>
<query>
<try>[anothersection]nkey</try>
<skip>
<try>[anothersection]nkey</try>
<fail/>
</skip>
<name>
<try>[anothersection]nkey</try>
<success>nkey = valuen</success>
<attributes>[[a, n, o, t, h, e, r, s, e, c, t, i, o, n]]</attributes>
</name>
<pairs>
<try>nkey = valuen</try>
<pair>
<try>nkey = valuen</try>
<skip>
<try>nkey = valuen</try>
<fail/>
</skip>
<skip>
<try>key = valuen</try>
<fail/>
</skip>
<skip>
<try>key = valuen</try>
<fail/>
</skip>
<key>
<try>key = valuen</try>
<success> = valuen</success>
<attributes>[[k, e, y]]</attributes>
</key>
<skip>
<try> = valuen</try>
<success>= valuen</success>
<attributes>[]</attributes>
</skip>
<skip>
<try>= valuen</try>
<fail/>
</skip>
<skip>
<try> valuen</try>
<success>valuen</success>
<attributes>[]</attributes>
</skip>
<skip>
<try>valuen</try>
<fail/>
</skip>
<value>
<try>valuen</try>
<success>n</success>
<attributes>[[v, a, l, u, e]]</attributes>
</value>
<success>n</success>
<attributes>[[[k, e, y], [v, a, l, u, e]]]</attributes>
</pair>
<pair>
<try>n</try>
<skip>
<try>n</try>
<fail/>
</skip>
<key>
<try></try>
<fail/>
</key>
<fail/>
</pair>
<success>n</success>
<attributes>[[[[k, e, y], [v, a, l, u, e]]]]</attributes>
</pairs>
<success>n</success>
<attributes>[[[a, n, o, t, h, e, r, s, e, c, t, i, o, n], [[[k, e, y], [v, a, l, u, e]]]]]</attributes>
</query>
<skip>
<try>n</try>
<fail/>
</skip>
<query>
<try></try>
<name>
<try></try>
<fail/>
</name>
<fail/>
</query>
<skip>
<try>n</try>
<fail/>
</skip>
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 助记符和指向成员语法的指针
- 有人能分解一下这个c++模板的语法吗
- C++避免重复声明的语法是什么
- QMetaObject invokeMethod的基于函数指针的语法
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 单独定义模板化嵌套类方法的正确语法
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 为什么我会收到错误 C2143 语法错误:缺少"*"之前的';'?
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 使用基类指针调用基类的值构造函数的语法是什么?
- 很好的语法来获取对向量/数组数据的大小引用?
- C++语法运算符功能?
- C++使用 rand 定义函数语法
- 什么文件可以修改 atom 的C++语法?
- 创建模板嵌套类实例的语法?
- C++语法差异:二维和一维数组(指针算术)
- 提升精神语法eol