2012-05-22 7 views
5

Boost :: Spiritでは、Boost::Bindにバインドされた関数からexpectation_failureをどのようにトリガーできますか?ブーストスピリットの関数からexpectation_failureを投げる方法は?

背景:複雑なエントリを含む大きなファイルを解析します。エントリが前のエントリと矛盾するとき、私は失敗し、expectation_failure(適切な解析位置情報を含む)を投げたい。エントリを解析するとき、そのエントリが前に見たものと矛盾しているかどうかを判断する関数をバインドします。

私はポイントを示す小さなおもちゃの例を作りました。ここで私は単純にintが10で割り切れないときexpectation_failureをスローするようにしたい:

#include <iostream> 
#include <iomanip> 
#include <boost/spirit/include/qi.hpp> 
#include <boost/bind.hpp> 
#include <boost/spirit/include/classic_position_iterator.hpp> 
namespace qi = boost::spirit::qi; 
namespace classic = boost::spirit::classic; 

void checkNum(int const& i) { 
    if (i % 10 != 0) // >> How to throw proper expectation_failure? << 
    std::cerr << "ERROR: Number check failed" << std::endl; 
} 

template <typename Iterator, typename Skipper> 
struct MyGrammar : qi::grammar<Iterator, int(), Skipper> { 
    MyGrammar() : MyGrammar::base_type(start) { 
    start %= qi::eps > qi::int_[boost::bind(&checkNum, _1)]; 
    } 
    qi::rule<Iterator, int(), Skipper> start; 
}; 

template<class PosIter> 
std::string errorMsg(PosIter const& iter) { 
    const classic::file_position_base<std::string>& pos = iter.get_position(); 
    std::stringstream msg; 
    msg << "parse error at file " << pos.file 
     << " line " << pos.line << " column " << pos.column << std::endl 
     << "'" << iter.get_currentline() << "'" << std::endl 
     << std::setw(pos.column) << " " << "^- here"; 
    return msg.str(); 
} 

int main() { 
    std::string in = "11"; 
    typedef std::string::const_iterator Iter; 
    typedef classic::position_iterator2<Iter> PosIter; 
    MyGrammar<PosIter, qi::space_type> grm; 
    int i; 
    PosIter it(in.begin(), in.end(), "<string>"); 
    PosIter end; 
    try { 
    qi::phrase_parse(it, end, grm, qi::space, i); 
    if (it != end) 
     throw std::runtime_error(errorMsg(it)); 
    } catch(const qi::expectation_failure<PosIter>& e) { 
    throw std::runtime_error(errorMsg(e.first)); 
    } 
    return 0; 
} 

expectation_failureを投げることは、私が10で割り切れない整数で、このようなエラーメッセージが表示されますことを意味する:

parse error at file <string> line 1 column 2 
'11' 
    ^- here 
+0

int_の代わりに別のルールを作成できますか?条件が満たされたときにのみ整数に一致しますか?私はスピリットについてよく分かりませんが、AXのr_boolに似たルールがあると仮定して述語をラップしますが、これはかなり一般的な状況です。 –

+0

私は残念なことに、このようなものが必要になると思います:http://boost-spirit.com/home/articles/qi-example/creating-your-own-parser-component-for-spirit-qi/ – Frank

+0

申し訳ありませんそれを見て、それは非常にユーザーフレンドリーです。だからあなたはAXが必要です:-) –

答えて

5

私は確信していませんが、私は_passフェニックスのプレースホルダを使用して解析に失敗すると思います。このようなものは、でなければなりません。後半とにかく

bool myfunc(int i) {return i%10 == 0;} 

... 
_int [ _pass = phoenix::bind(myfunc,_1)] 
+0

はい、それは動作します。ありがとう! – Frank

+1

@Frank _passをfalseに設定すると、現在のルールのみが停止されますが、パーザー全体が他のすべてのルール(別名文法)で停止されないことに注意してください。 –

0

年:あなたは絶対に例外をスローしたいとon_errorはそれをキャッチしたい場合

は、エラーハンドラon_errorがキャッチ何もしないので、qi名前空間からexpectation_exceptionをスローする必要がありますelse。

これは、セマンティックアクションまたはカスタムパーサーの実装に適用される場合があります。

Exceptionqi::expactation_exception何もないです
boost::throw_exception(Exception(first, last, component.what(context))); 

は次のようになります。

セマンティックアクションのように手元にコンポーネントがない場合は、component.what(..)の代わりに独自のqi::infoオブジェクトを指定する必要があります。

on_errorによって保護されたコンテキスト内のどこからでも投げることができます。

関連する問題