4
votes

I am refactoring a typing system (type model) I have in place that uses spirit for string-serialization. I am using the compile-time modeling construct of type-traits.

template<>
type_traits<int4_type>
{
    typedef boost::spirit::qi::int_parser<boost::int32_t> string_parser;
}  

template<>
type_traits<string_type>
{
    typedef boost::spirit::ascii::string string_parser;
}

In this example I show primitive parsers, but I expect to put in rules as well.

The int4 type works, but that is because of (home/qi/numeric/int.hpp +27):

namespace tag
    {
        template <typename T, unsigned Radix, unsigned MinDigits
                , int MaxDigits> 
        struct int_parser {};
    }

    namespace qi
    {
        ///////////////////////////////////////////////////////////////////////
        // This one is the class that the user can instantiate directly in 
        // order to create a customized int parser
        template <typename T = int, unsigned Radix = 10, unsigned MinDigits = 1
                , int MaxDigits = -1>
        struct int_parser
          : spirit::terminal<tag::int_parser<T, Radix, MinDigits, MaxDigits> > 
        {};
    }

the string typedef doesn't work, and neither does eps. I couldn't figure out the why's of refering to the string parser. However, In the case of eps it boils down to:

#define BOOST_SPIRIT_TERMINAL(name)                                             \
    namespace tag { struct name {};  }                                          \
    typedef boost::proto::terminal<tag::name>::type name##_type;                \
    name##_type const name = {{}};                                              \
    inline void silence_unused_warnings__##name() { (void) name; }              \
    /***/

Which means I can't typedef it, it is a proto terminal construct, or put opaquely, a const global definition.

My question : How do I typedef a rule, grammar, primitive parser ?

Note : I have already started working on giving all my "types" a functor encapsulating a rule and then making that a type trait.

1

1 Answers

1
votes

I'm not sure it will work in all case but what I did to typedef my Skipper was to use BOOST_TYPEOF :

typedef BOOST_TYPEOF(boost::spirit::ascii::space - (boost::spirit::qi::eol | boost::spirit::qi::eoi)) SkipperT;

so your example would be

typedef BOOST_TYPEOF(boost::spirit::ascii::string) string_parser;

There is probably a better / more elegant way but it currently work for what I needed it.