Skip to content

Spirit X3

Artem Pavlenko edited this page Jun 11, 2014 · 2 revisions

###Adapting to work with Boost.Spirit X3

template <typename ... Types>
struct ast_variant : mapbox::util::variant<Types...>
{
    typedef  mapbox::util::variant<Types...> base_type;
    typedef boost::mpl::list<Types...> types;
    using base_type::base_type;
};

Example parsing

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

// stl
#include <string>
// variant
#include "/Users/artem/Projects/variant/variant.hpp"

// adapt mapbox::util::variant to be used with spirit X3
template <typename ... Types>
struct ast_variant : mapbox::util::variant<Types...>
{
    typedef  mapbox::util::variant<Types...> base_type;
    typedef boost::mpl::list<Types...> types;
    using base_type::base_type;
};

//namespace boost { namespace spirit { namespace x3 { namespace traits {
//template <typename... Types>
//    struct is_variant<ast_variant<Types...> >
//    : mpl::true_ {};
//}}}}

namespace mapnik {

typedef ast_variant<int,double,std::string> expression_type;

namespace x3 = boost::spirit::x3;

namespace expression_grammar {

using x3::strict_real_policies;
using x3::real_parser;
using x3::lit;
using x3::double_;
using x3::int_;
using x3::char_;
using x3::no_skip;

const real_parser<double, strict_real_policies<double> > strict_double;
x3::rule<class expression, expression_type> const expression_rule("expression");
x3::rule<class string_def, std::string> const string_rule("string");

auto const expression_rule_def = strict_double | int_ | string_rule ;
auto const string_rule_def = lit('"') >> no_skip[*(char_ - lit('"'))] >> lit('"') ;

BOOST_SPIRIT_DEFINE(
    expression_rule=expression_rule_def,
    string_rule = string_rule_def
    );

auto const expression = expression_rule;

}}
Clone this wiki locally