Sprout.Weed はじめました

やっとこさはじめました。

[ソース]

#define SPROUT_CONFIG_SUPPORT_TEMPORARY_CONTAINER_ITERATION
#include <sprout/weed.hpp>
#include <sprout/string.hpp>
#include <boost/mpl/print.hpp>
#include <boost/mpl/int.hpp>


int
main(){
    namespace weed = sprout::weed;
    using weed::int_;

    // 21:53:49 というような構文を解析
    constexpr auto parser = int_ >> ':' >> int_ >> ':' >> int_;
    
    static constexpr auto time = sprout::to_string(__TIME__);
    static constexpr auto result = weed::parse(time.begin(), time.end(), parser);
    
    // result.success() で解析が成功したかのチェック
    static_assert(result.success(), "failed parse");
    
    // result.attr() で解析結果を取得
    // 今回のパーサでは sprout::array<long long int, 3> が返ってくる
    // パーサによっては sprout::tuple が返ってくる場合もあるので注意
    static constexpr sprout::array<long long int, 3> result_attr = result.attr();
    static constexpr auto hour    = result_attr[0];
    static constexpr auto minutes = result_attr[1];
    static constexpr auto second  = result_attr[2];

    // mpl::print を使用してコンパイル時に出力したり
    namespace mpl = boost::mpl;
    typedef mpl::print<mpl::int_<hour   >>::type hourT;
    typedef mpl::print<mpl::int_<minutes>>::type minutesT;
    typedef mpl::print<mpl::int_<second >>::type secondT;
    
    return 0;
}

っと実際に使ってみるとこんな感じですね。
Boost.Spirit.Qi を使ったことがある人なら簡単なパーサはすぐに書けると思います。
複雑な構文じゃなければ特に問題ないんじゃないかなーと。
文字列をパースするにはどうするのがいいんじゃろうか。

[コンパイラ]

  • g++ (GCC) 4.7.0 20111210 (experimental)