Boost.Spirit (2.3) でカスタム Parser クラスを作成しようとしましたが、うまくいきませんでした。コードは次のとおりです。
template <class Iter>
class crule : public boost::spirit::qi::parser<crule<Iter> >
{
rule<Iter> r_;
public:
crule(const rule<Iter>& r) : r_(r) {}
template <class T>
crule(const T& t) : r_(t) {}
template<class Ctx, class Skip>
bool parse(Iter& f, const Iter& l, Ctx& context, Skip& skip, typename rule<Iter>::template attribute<Ctx, Iter>::type& attr) const {
return r_.parse(f, l, context, skip, attr);
}
template <class Ctx>
boost::spirit::info what(Ctx& context) const {
return r_.what(context);
}
template <class Context, class It>
struct attribute {
typedef typename rule<Iter>::template attribute<Context, It>::type type;
};
};
そして、私はすべての要件を満たしていますが (少なくとも私は満たしていると思います) 、解析式でこのクラスを使用しようとするとエラーが発生します。
shell_grammar.h:134: error: no match for 'operator!' in '!shell_grammar<Iter>::token(boost::spirit::qi::rule<Iter, boost::fusion::unused_type, boost::fusion::unused_type, boost::fusion::unused_type>) [with Iter = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >](boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::fusion::unused_type, boost::fusion::unused_type, boost::fusion::unused_type>(((const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::fusion::unused_type, boost::fusion::unused_type, boost::fusion::unused_type>&)((const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::fusion::unused_type, boost::fusion::unused_type, boost::fusion::unused_type>*)(&((shell_grammar<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >*)this)->shell_grammar<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::reserved_words)))))'
shell_grammar.h:134: note: candidates are: operator!(bool) <built-in>
他のパーセット (例: ) の実装を調べようとしましたnot_predicate
が、それが機能する違いは何なのかわかりません。
モチベーション
私がそうする理由は、この質問に関連しています。独特の字句規則を持つ POSIX シェル言語を解析したい。特に、「スキッパーパーサー」は語彙素にも適用する必要がありますが、「句レベル」のスキッパーパーサーとは異なるものにする必要があります。これは、lexeme
ディレクティブが実行できないことであり、skip
プリスキップ (AFAIK) も行いません。これは私が必要としているものでもあります。だから私は関数を作成したい
something token(std::string);
トークンに一致するルールを返します。1 つの方法は、端末として機能する独自のrule
ラッパーを作成することです (rule
単独では参照セマンティクスに使用できないため)。別の方法は、新しいパーサーを作成し (これは の非端末になりますproto
)、それにシェルのトークン解析を実装することです。