convert image_options grammar to boost::spirit::x3
This commit is contained in:
parent
4f9e97d955
commit
b57f034d01
1 changed files with 25 additions and 30 deletions
|
@ -24,49 +24,44 @@
|
|||
|
||||
#pragma GCC diagnostic push
|
||||
#include <mapnik/warning_ignore.hpp>
|
||||
#include <boost/spirit/include/qi.hpp>
|
||||
#include <boost/spirit/home/x3.hpp>
|
||||
#include <boost/fusion/include/std_pair.hpp>
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
namespace mapnik { namespace detail {
|
||||
namespace mapnik { namespace grammar {
|
||||
|
||||
namespace qi = boost::spirit::qi;
|
||||
namespace x3 = boost::spirit::x3;
|
||||
|
||||
template <typename Iterator>
|
||||
struct image_options_grammar
|
||||
: qi::grammar<Iterator, image_options_map(), boost::spirit::ascii::space_type>
|
||||
{
|
||||
using pair_type = std::pair<std::string, boost::optional<std::string>>;
|
||||
image_options_grammar()
|
||||
: image_options_grammar::base_type(start)
|
||||
{
|
||||
qi::lit_type lit;
|
||||
qi::char_type char_;
|
||||
start = pair >> *(lit(':') >> pair)
|
||||
;
|
||||
pair = key >> -('=' >> value)
|
||||
;
|
||||
key = char_("a-zA-Z_") >> *char_("a-zA-Z_0-9\\.\\-")
|
||||
;
|
||||
value = +char_("a-zA-Z_0-9\\.\\-")
|
||||
;
|
||||
}
|
||||
using x3::lit;
|
||||
using x3::ascii::char_;
|
||||
using pair_type = std::pair<std::string, boost::optional<std::string>>;
|
||||
|
||||
qi::rule<Iterator, image_options_map(), boost::spirit::ascii::space_type> start;
|
||||
qi::rule<Iterator, pair_type(), boost::spirit::ascii::space_type> pair;
|
||||
qi::rule<Iterator, std::string(), boost::spirit::ascii::space_type> key, value;
|
||||
};
|
||||
x3::rule<class image_options, image_options_map> const image_options("image options");
|
||||
x3::rule<class key_value, pair_type> const key_value("key_value");
|
||||
x3::rule<class key, std::string> const key("key");
|
||||
x3::rule<class value, std::string> const value("value");
|
||||
|
||||
} // ns detail
|
||||
auto const key_def = char_("a-zA-Z_") > *char_("a-zA-Z_0-9\\.\\-");
|
||||
auto const value_def = +char_("a-zA-Z_0-9\\.\\-");
|
||||
auto const key_value_def = key > -('=' > value);
|
||||
auto const image_options_def = key_value % lit(':');
|
||||
|
||||
BOOST_SPIRIT_DEFINE(key);
|
||||
BOOST_SPIRIT_DEFINE(value);
|
||||
BOOST_SPIRIT_DEFINE(key_value);
|
||||
BOOST_SPIRIT_DEFINE(image_options);
|
||||
|
||||
} // grammar
|
||||
|
||||
image_options_map parse_image_options(std::string const& str)
|
||||
{
|
||||
auto const begin = str.begin();
|
||||
auto const end = str.end();
|
||||
boost::spirit::ascii::space_type space;
|
||||
mapnik::detail::image_options_grammar<std::string::const_iterator> g;
|
||||
using boost::spirit::x3::space;
|
||||
using mapnik::grammar::image_options;
|
||||
//mapnik::detail::image_options_grammar<std::string::const_iterator> g;
|
||||
image_options_map options;
|
||||
bool success = boost::spirit::qi::phrase_parse(begin, end, g, space, options);
|
||||
bool success = boost::spirit::x3::phrase_parse(begin, end, image_options, space, options);
|
||||
if (!success)
|
||||
{
|
||||
throw std::runtime_error("Can't parse image options: " + str);
|
||||
|
|
Loading…
Add table
Reference in a new issue