diff --git a/include/mapnik/json/feature_generator_grammar.hpp b/include/mapnik/json/feature_generator_grammar.hpp index c456b6339..c970903c5 100644 --- a/include/mapnik/json/feature_generator_grammar.hpp +++ b/include/mapnik/json/feature_generator_grammar.hpp @@ -24,23 +24,13 @@ #define MAPNIK_JSON_FEATURE_GENERATOR_GRAMMAR_HPP // mapnik -#include -#include #include #include #include -#include +#include // boost #include -#include -#include -#include -#include -#include -#include -#include -#include namespace boost { namespace spirit { namespace traits { @@ -101,73 +91,15 @@ struct get_id } }; -struct make_properties_range -{ - using properties_range_type = boost::iterator_range; - - template - struct result { using type = properties_range_type; }; - - properties_range_type operator() (mapnik::feature_impl const& f) const - { - return boost::make_iterator_range(f.begin(),f.end()); - } -}; - -struct utf8 -{ - template - struct result { using type = std::string; }; - - std::string operator() (mapnik::value_unicode_string const& ustr) const - { - std::string result; - to_utf8(ustr,result); - return result; - } -}; - template -struct escaped_string - : karma::grammar -{ - escaped_string(); - karma::rule esc_str; - karma::symbols esc_char; -}; - -struct extract_string -{ - using result_type = std::tuple; - - result_type operator() (mapnik::value const& val) const - { - bool need_quotes = val.is(); - return std::make_tuple(val.to_string(), need_quotes); - } -}; - -template -struct feature_generator_grammar: +struct feature_generator_grammar : karma::grammar { - using pair_type = std::tuple; - using range_type = make_properties_range::properties_range_type; - feature_generator_grammar(); karma::rule feature; multi_geometry_generator_grammar geometry; - escaped_string escaped_string_; - karma::rule properties; - karma::rule pair; - karma::rule()> value; - karma::rule value_null_; - karma::rule ustring; - typename karma::int_generator int__; + properties_generator_grammar properties; boost::phoenix::function id_; - boost::phoenix::function extract_string_; - boost::phoenix::function utf8_; - std::string quote_; }; }} diff --git a/include/mapnik/json/feature_generator_grammar_impl.hpp b/include/mapnik/json/feature_generator_grammar_impl.hpp index 1d1d3c907..45156fe1d 100644 --- a/include/mapnik/json/feature_generator_grammar_impl.hpp +++ b/include/mapnik/json/feature_generator_grammar_impl.hpp @@ -29,50 +29,14 @@ namespace mapnik { namespace json { -namespace karma = boost::spirit::karma; - -template -escaped_string::escaped_string() - : escaped_string::base_type(esc_str) -{ - karma::lit_type lit; - karma::_r1_type _r1; - karma::hex_type hex; - karma::right_align_type right_align; - karma::print_type kprint; - esc_char.add - ('"', "\\\"") - ('\\', "\\\\") - ('\b', "\\b") - ('\f', "\\f") - ('\n', "\\n") - ('\r', "\\r") - ('\t', "\\t") - ; - - esc_str = lit(_r1) - << *(esc_char - | kprint - | "\\u" << right_align(4,lit('0'))[hex]) - << lit(_r1) - ; -} - template feature_generator_grammar::feature_generator_grammar() - : feature_generator_grammar::base_type(feature), - quote_("\"") + : feature_generator_grammar::base_type(feature) { boost::spirit::karma::lit_type lit; boost::spirit::karma::uint_type uint_; - boost::spirit::karma::bool_type bool_; - boost::spirit::karma::double_type double_; boost::spirit::karma::_val_type _val; boost::spirit::karma::_1_type _1; - boost::spirit::karma::string_type kstring; - boost::spirit::karma::eps_type eps; - - using boost::phoenix::at_c; feature = lit("{\"type\":\"Feature\",\"id\":") << uint_[_1 = id_(_val)] @@ -80,30 +44,6 @@ feature_generator_grammar::feature_generator_grammar() << lit(",\"properties\":") << properties << lit('}') ; - - properties = lit('{') - << -(pair % lit(',')) - << lit('}') - ; - - pair = lit('"') - << kstring[_1 = boost::phoenix::at_c<0>(_val)] << lit('"') - << lit(':') - << value[_1 = extract_string_(at_c<1>(_val))] - ; - - value = eps(at_c<1>(_val)) << escaped_string_(quote_.c_str())[_1 = at_c<0>(_val)] - | - kstring[_1 = at_c<0>(_val)] - ; - - // FIXME http://boost-spirit.com/home/articles/karma-examples/creating-your-own-generator-component-for-spirit-karma/ - //value = (value_null_| bool_ | int__ | double_ | ustring)//[_1 = value_base_(_r1)] - // ; - //value_null_ = kstring[_1 = "null"] - // ; - //ustring = escaped_string_(quote_.c_str())[_1 = utf8_(_val)] - // ; } }} diff --git a/include/mapnik/json/properties_generator_grammar.hpp b/include/mapnik/json/properties_generator_grammar.hpp new file mode 100644 index 000000000..b634e4540 --- /dev/null +++ b/include/mapnik/json/properties_generator_grammar.hpp @@ -0,0 +1,95 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2014 Artem Pavlenko + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + *****************************************************************************/ + +#ifndef MAPNIK_JSON_PROPERTIES_GENERATOR_GRAMMAR_HPP +#define MAPNIK_JSON_PROPERTIES_GENERATOR_GRAMMAR_HPP + +#include +#include +#include + +// boost +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace mapnik { namespace json { + +template +struct escaped_string + : karma::grammar +{ + escaped_string(); + karma::rule esc_str; + karma::symbols esc_char; +}; + +struct extract_string +{ + using result_type = std::tuple; + + result_type operator() (mapnik::value const& val) const + { + bool need_quotes = val.is(); + return std::make_tuple(val.to_string(), need_quotes); + } +}; + +struct utf8 +{ + using result_type = std::string; + std::string operator() (mapnik::value_unicode_string const& ustr) const + { + std::string result; + to_utf8(ustr,result); + return result; + } +}; + +template +struct properties_generator_grammar : karma::grammar + +{ + using pair_type = std::tuple; + properties_generator_grammar(); + // rules + karma::rule properties; + karma::rule pair; + karma::rule()> value; + karma::rule value_null_; + karma::rule ustring; + escaped_string escaped_string_; + typename karma::int_generator int__; + boost::phoenix::function extract_string_; + boost::phoenix::function utf8_; + std::string quote_; +}; + +}} + +#endif //MAPNIK_JSON_PROPERTIES_GENERATOR_GRAMMAR_HPP diff --git a/include/mapnik/json/properties_generator_grammar_impl.hpp b/include/mapnik/json/properties_generator_grammar_impl.hpp new file mode 100644 index 000000000..8171284c8 --- /dev/null +++ b/include/mapnik/json/properties_generator_grammar_impl.hpp @@ -0,0 +1,96 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2014 Artem Pavlenko + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + *****************************************************************************/ + + +#include + +namespace mapnik { namespace json { + +namespace karma = boost::spirit::karma; + +template +escaped_string::escaped_string() + : escaped_string::base_type(esc_str) +{ + karma::lit_type lit; + karma::_r1_type _r1; + karma::hex_type hex; + karma::right_align_type right_align; + karma::print_type kprint; + + esc_char.add + ('"', "\\\"") + ('\\', "\\\\") + ('\b', "\\b") + ('\f', "\\f") + ('\n', "\\n") + ('\r', "\\r") + ('\t', "\\t") + ; + + esc_str = lit(_r1) + << *(esc_char + | kprint + | "\\u" << right_align(4,lit('0'))[hex]) + << lit(_r1) + ; +} + +template +properties_generator_grammar::properties_generator_grammar() + : properties_generator_grammar::base_type(properties), + quote_("\"") +{ + + boost::spirit::karma::lit_type lit; + boost::spirit::karma::_val_type _val; + boost::spirit::karma::_1_type _1; + boost::spirit::karma::string_type kstring; + boost::spirit::karma::eps_type eps; + using boost::phoenix::at_c; + + properties = lit('{') + << -(pair % lit(',')) + << lit('}') + ; + + pair = lit('"') + << kstring[_1 = boost::phoenix::at_c<0>(_val)] << lit('"') + << lit(':') + << value[_1 = extract_string_(at_c<1>(_val))] + ; + + value = eps(at_c<1>(_val)) << escaped_string_(quote_.c_str())[_1 = at_c<0>(_val)] + | + kstring[_1 = at_c<0>(_val)] + ; + + // FIXME http://boost-spirit.com/home/articles/karma-examples/creating-your-own-generator-component-for-spirit-karma/ + //value = (value_null_| bool_ | int__ | double_ | ustring)//[_1 = value_base_(_r1)] + // ; + //value_null_ = kstring[_1 = "null"] + // ; + //ustring = escaped_string_(quote_.c_str())[_1 = utf8_(_val)] + // ; +} + +}} diff --git a/src/json/mapnik_json_generator_grammar.cpp b/src/json/mapnik_json_generator_grammar.cpp index bc7bc5f37..67d673599 100644 --- a/src/json/mapnik_json_generator_grammar.cpp +++ b/src/json/mapnik_json_generator_grammar.cpp @@ -24,9 +24,12 @@ #include #include #include +#include #include using sink_type = std::back_insert_iterator; + +template struct mapnik::json::properties_generator_grammar; template struct mapnik::json::feature_generator_grammar; template struct mapnik::json::geometry_generator_grammar; template struct mapnik::json::multi_geometry_generator_grammar;