2

コンパイル時に指定した要素数を解析したい。repeat()[]ディレクティブを試しました。次のコードは私のケースを示しています。

 using namespace x3;
 std::tuple<float, float, float> tup;
 std::string str{"0.3 0.2 0.1"};
 auto ret = parse(std::begin(str), std::end(str), repeat(3)[ float_ >> (' ' | eol) ] , tup); 

コンパイラ エラー メッセージ:

error: static assertion failed: Expecting a single element fusion sequence
             static_assert(traits::has_size<Attribute, 1>::value

私がそれを書き出すとうまくいきます:

parse(std::begin(str), std::end(str), float_ >> ' ' >> float_ >> ' ' >> float_ ] , tup);

しかし、多数の要素があると混乱します。

「 repeat」ディレクティブを使用して文法を短縮する方法はありますか?

4

1 に答える 1

1

ここでわかるように、 の合成属性は でx3::repeat(3)[x3::float_]あり、属性vector<float>と一致しません (基本的にサイズ 3 の融合シーケンス)。合成された属性は、渡す値に依存しないことに注意してください。

必要なものを取得するには、別のディレクティブが必要です。型は、渡す値によって異なります。次に、このディレクティブは、サブジェクトが N 回繰り返されるシーケンスを生成します (単に作業を「委譲」するだけx3::sequenceで、属性の伝播に関してすべてが正しく機能することが保証されます)。これが機能する方法として、少なくとも 2 つの方法が考えられrepeat<N>[parser]ますrepeat(integral_constant<int,N>)[parser]。以下のコードでは、 を使用して 2 番目のアプローチを選択しましたboost::hana::integral_constant。これにより、以下を使用できます。

custom::repeat(3_c)[ x3::float_ >> (' ' | x3::eol | x3::eoi) ]

完全なコード (WandBox で実行)

custom_repeat.hpp

#include <type_traits>
#include <boost/spirit/home/x3.hpp> 

namespace custom
{
    struct repeat_gen
    {
        template <int Size>
        struct repeat_gen_lvl1
        {

            //using overloads with integral constants to avoid needing to partially specialize a function

            //this actually builds the sequence of parsers
            template <typename Parser,int N>
            auto generate_sequence(Parser const& parser, std::integral_constant<int,N>) const
            {
                return generate_sequence(parser,std::integral_constant<int,N-1>{}) >> parser;
            }

            template <typename Parser>
            auto generate_sequence(Parser const parser,std::integral_constant<int,1>) const
            {
                return parser;
            }

            template<typename Subject>
            auto operator[](Subject const& subject) const
            {
                //here the actual sequence is generated
                return generate_sequence(boost::spirit::x3::as_parser(subject), std::integral_constant<int,Size>{});
            }
        };

        template <typename IntConstant>
        repeat_gen_lvl1<int(IntConstant::value)>
        operator()(IntConstant) const
        {
            //returns an object that know the size of the wanted sequence and has an operator[] that will capture the subject
            return {};
        }

        template <typename IntegerType, typename Enable=std::enable_if_t<std::is_integral<IntegerType>::value> >
        auto operator()(IntegerType n) const
        {
            return boost::spirit::x3::repeat(n);
        }
    };

    //this object's only purpose is having an operator()
    auto const repeat = repeat_gen{};
}

main.cpp

#include <iostream>

#include <boost/spirit/home/x3.hpp>

#include <boost/fusion/include/std_tuple.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/fusion/include/as_vector.hpp>
#include <boost/fusion/include/is_sequence.hpp>

#include <boost/hana/integral_constant.hpp>

#include <boost/mpl/int.hpp>

#include "custom_repeat.hpp"

namespace x3 = boost::spirit::x3;

using namespace boost::hana::literals;

template <typename T>
void print_attr(const std::vector<T>& vec)
{
    std::cout << "Vector: ";
    for(const auto& elem : vec)
        std::cout << "[" << elem << "]";
}

template <typename Sequence, typename Enable=std::enable_if_t<boost::fusion::traits::is_sequence<Sequence>::value> >
void print_attr(const Sequence& seq)
{
    std::cout << "Sequence: " << boost::fusion::as_vector(seq);
}

template <typename Attr,typename Parser>
void parse(const std::string& str, const Parser& parser)
{
    Attr attr;
    std::string::const_iterator iter = std::begin(str), end = std::end(str);
    bool ret = x3::parse(iter, end, parser, attr);

    if(ret && (iter==end))
    {
        std::cout << "Success.\n";
        print_attr(attr);
        std::cout << std::endl;
    }
    else
    {
        std::cout << "Something failed. Unparsed: ->|" << std::string(iter,end) << "|<-" << std::endl;
    }
}

struct float_holder
{
    float val;
};

BOOST_FUSION_ADAPT_STRUCT(float_holder,val);

int main()
{
    boost::mpl::int_<2> two;
    std::integral_constant<int,1> one;

    parse<std::tuple<float,float,float> >("0.3 0.2 0.1", custom::repeat(3_c)[ x3::float_ >> (' ' | x3::eol | x3::eoi) ] );
    parse<std::pair<float,float> >("0.2 0.1", custom::repeat(two)[ x3::float_ >> (' ' | x3::eol | x3::eoi) ] );
    parse<float_holder>("0.1", custom::repeat(one)[ x3::float_ >> (' ' | x3::eol | x3::eoi) ] );

    parse<std::vector<float> >("0.3 0.2 0.1", custom::repeat(3)[ x3::float_ >> (' ' | x3::eol | x3::eoi) ] );

}
于 2016-08-20T14:54:53.520 に答える