看着这个例子 http://boost-spirit.com/home/articles/qi-example/parsing-a-list-of-key-value-pairs-using-spirit-qi/为了实现 Spirit 解析器,当我试图编写类似的东西时,有些东西让我感到困惑。
语法的属性模板参数(std::map<std::string, std::string>()
)和规则的签名模板参数(例如qi::rule<Iterator, std::string()> key, value
) 包含括号。
namespace qi = boost::spirit::qi;
template <typename Iterator>
struct keys_and_values
: qi::grammar<Iterator, std::map<std::string, std::string>()> // <- parentheses here
{
keys_and_values()
: keys_and_values::base_type(query)
{
query = pair >> *((qi::lit(';') | '&') >> pair);
pair = key >> -('=' >> value);
key = qi::char_("a-zA-Z_") >> *qi::char_("a-zA-Z_0-9");
value = +qi::char_("a-zA-Z_0-9");
}
qi::rule<Iterator, std::map<std::string, std::string>()> query; // <- parentheses here
qi::rule<Iterator, std::pair<std::string, std::string>()> pair; // <- parentheses here
qi::rule<Iterator, std::string()> key, value; // <- parentheses here
};
我以前从未见过这一点,并且在编写自己的版本时无意中忽略了这一点。这导致我的解析器无法生成正确的输出运行时间(输出映射为空)。
这些括号的目的是什么?我的猜测是,这使得该规则的属性成为该类型的对象。