子规则有点过时。老实说,我甚至不知道《精灵V2》中还有这样的东西。
我建议使用常规的Spirit V2属性传播,这使内容一下子变得更可读:
dog_ = qi::lit("DOG") >> '|' >> "Name=" >> lit_ >> '|'
>> "Barks=" >> yesno_ >> '|'
>> "Has a Tail=" >> yesno_ >> '|'
>> "Address=" >> lit_
;
bird_ = qi::lit("BIRD") >> '|' >> "Name=" >> lit_ >> '|'
>> "Tweets=" >> yesno_ >> '|'
>> "Address=" >> lit_
;
start_ = dog_ | bird_;
我想象过
lit_
规则(作为
qi::lit_
不会响起任何铃声):
lit_ = qi::lexeme [ *~qi::char_('|') ];
当然,您需要调整属性类型,只要它们没有内置支持(如
boost::variant<Dog, Bird>
,
std::string
和
bool
它们都是在没有任何附加代码的情况下处理的):
BOOST_FUSION_ADAPT_STRUCT(Dog,
(std::string, Name)(bool, Barks)(bool, HasATail)(std::string, Address))
BOOST_FUSION_ADAPT_STRUCT(Bird,
(std::string, Name)(bool, Tweets)(std::string, Address))
现在,程序扩展到打印一些调试信息,输出为:
Live On Coliru
Matched: [DOG|Name=tim |Barks=Yes|Has a Tail=No|Address=3 infinite loop]
Matched: [BIRD|Name=poc |Tweets=Yes|Address=10 stack overflow street]
完整样本代码
//#define BOOST_SPIRIT_DEBUG
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_symbols.hpp>
static const char* YesNo(bool b) { return b?"Yes":"No"; }
struct Dog {
std::string Name;
bool Barks;
bool HasATail;
std::string Address;
friend std::ostream& operator <<(std::ostream& os, Dog const& o) {
return os << "[DOG|Name=" << o.Name << "|Barks=" << YesNo(o.Barks) << "|Has a Tail=" << YesNo(o.HasATail) << "|Address=" << o.Address << "]";
}
};
struct Bird {
std::string Name;
bool Tweets;
std::string Address;
friend std::ostream& operator <<(std::ostream& os, Bird const& o) {
return os << "[BIRD|Name=" << o.Name << "|Tweets=" << YesNo(o.Tweets) << "|Address=" << o.Address << "]";
}
};
typedef boost::variant<Dog, Bird> ZooAnimal;
BOOST_FUSION_ADAPT_STRUCT(Dog, (std::string, Name)(bool, Barks)(bool, HasATail)(std::string, Address))
BOOST_FUSION_ADAPT_STRUCT(Bird, (std::string, Name)(bool, Tweets)(std::string, Address))
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
template <typename Iterator>
struct ZooGrammar : public qi::grammar<Iterator, ZooAnimal(), ascii::space_type>
{
ZooGrammar() : ZooGrammar::base_type(start_)
{
using qi::_1;
yesno_.add("Yes", true)("Y", true)("No", false)("N", false);
dog_ = qi::lit("DOG") >> '|' >> "Name=" >> lit_ >> '|'
>> "Barks=" >> yesno_ >> '|'
>> "Has a Tail=" >> yesno_ >> '|'
>> "Address=" >> lit_
;
bird_ = qi::lit("BIRD") >> '|' >> "Name=" >> lit_ >> '|'
>> "Tweets=" >> yesno_ >> '|'
>> "Address=" >> lit_
;
start_ = dog_ | bird_;
lit_ = qi::lexeme [ *~qi::char_('|') ];
BOOST_SPIRIT_DEBUG_NODES((dog_)(bird_)(start_)(lit_))
}
private:
qi::rule<Iterator, ZooAnimal(), ascii::space_type> start_;
qi::rule<Iterator, std::string(), ascii::space_type> lit_;
qi::rule<Iterator, Dog(), ascii::space_type> dog_;
qi::rule<Iterator, Bird(), ascii::space_type> bird_;
qi::symbols<char, bool> yesno_;
};
int main()
{
typedef std::string::const_iterator iterator_type;
typedef ZooGrammar<iterator_type> grammar;
for (std::string const input : {
"DOG | Name=tim | Barks=Yes | Has a Tail=N | Address=3 infinite loop",
"BIRD| Name=poc | Tweets=Yes| Address=10 stack overflow street"
})
{
iterator_type start = input.begin();
iterator_type end = input.end();
grammar g;
ZooAnimal animal;
if(qi::phrase_parse(start, end, g, ascii::space, animal))
std::cout << "Matched: " << animal << "\n";
else
std::cout << "Parse failed\n";
if (start != end)
std::cout << "Remaining input: '" << std::string(start, end) << "'\n";
}
}