add missing generic_json_grammar_x3

This commit is contained in:
artemp 2016-11-21 11:15:21 +01:00
parent 7d77835080
commit 3d629bf76d
2 changed files with 215 additions and 0 deletions

View file

@ -0,0 +1,82 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2015 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_GENERIC_JSON_GRAMMAR_X3_HPP
#define MAPNIK_JSON_GENERIC_JSON_GRAMMAR_X3_HPP
#include <mapnik/value/types.hpp>
#include <mapnik/util/variant.hpp>
#pragma GCC diagnostic push
#include <mapnik/warning_ignore.hpp>
#include <boost/spirit/home/x3.hpp>
#pragma GCC diagnostic pop
#include <vector>
namespace mapnik { namespace json {
namespace x3 = boost::spirit::x3;
struct json_value;
using json_array = std::vector<json_value>;
using json_object_element = std::pair<std::string, json_value>;
using json_object = std::vector<json_object_element>;
using json_value_base = mapnik::util::variant<value_null,
value_bool,
value_integer,
value_double,
std::string,
json_array,
json_object>;
struct json_value : json_value_base
{
#if __cpp_inheriting_constructors >= 200802
using json_value_base::json_value_base;
#else
json_value() = default;
template <typename T>
json_value(T && val)
: json_value_base(std::forward<T>(val)) {}
#endif
};
namespace grammar {
using generic_json_grammar_type = x3::rule<class generic_json_tag, json_value>;
using generic_json_key_value_type = x3::rule<class json_object_element_tag, json_object_element>;
BOOST_SPIRIT_DECLARE(generic_json_grammar_type);
BOOST_SPIRIT_DECLARE(generic_json_key_value_type);
}
grammar::generic_json_grammar_type const& generic_json_grammar();
grammar::generic_json_key_value_type const& generic_json_key_value();
}}
#endif // MAPNIK_JSON_GENERIC_JSON_GRAMMAR_X3_HPP

View file

@ -0,0 +1,133 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2016 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_GENERIC_JSON_GRAMMAR_X3_DEF_HPP
#define MAPNIK_JSON_GENERIC_JSON_GRAMMAR_X3_DEF_HPP
#include <boost/fusion/include/std_pair.hpp>
#include <mapnik/json/generic_json_grammar_x3.hpp>
#include <mapnik/json/unicode_string_grammar_x3_def.hpp>
namespace mapnik { namespace json { namespace grammar {
namespace x3 = boost::spirit::x3;
auto make_null = [] (auto const& ctx)
{
_val(ctx) = mapnik::value_null{};
};
auto make_true = [] (auto const& ctx)
{
_val(ctx) = true;
};
auto make_false = [] (auto const& ctx)
{
_val(ctx) = false;
};
auto assign = [](auto const& ctx)
{
_val(ctx) = _attr(ctx);
};
auto assign_key = [](auto const& ctx)
{
std::get<0>(_val(ctx)) = _attr(ctx);
};
auto assign_value = [](auto const& ctx)
{
std::get<1>(_val(ctx)) = _attr(ctx);
};
using x3::lit;
using x3::string;
// exported rules
// start
generic_json_grammar_type const value("JSON Value");
generic_json_key_value_type const key_value("JSON Object element");
// rules
x3::rule<class json_object_tag, json_object> object("JSON Object");
x3::rule<class json_array_tag, json_array> array("JSON Array");
x3::rule<class json_number_tag, json_value> number("JSON Number");
auto const json_double = x3::real_parser<value_double, x3::strict_real_policies<value_double>>();
auto const json_integer = x3::int_parser<value_integer, 10, 1, -1>();
// import unicode string rule
namespace { auto const& json_string = mapnik::json::unicode_string_grammar(); }
// generic json types
auto const value_def = object | array | json_string | number
;
auto const key_value_def = json_string[assign_key] > lit(':') > value[assign_value]
;
auto const object_def = lit('{')
> -(key_value % lit(','))
> lit('}')
;
auto const array_def = lit('[')
> -(value % lit(','))
> lit(']')
;
auto const number_def = json_double[assign]
| json_integer[assign]
| lit("true") [make_true]
| lit ("false") [make_false]
| lit("null")[make_null]
;
#pragma GCC diagnostic push
#include <mapnik/warning_ignore.hpp>
BOOST_SPIRIT_DEFINE(
value,
object,
key_value,
array,
number
);
#pragma GCC diagnostic pop
}}}
namespace mapnik { namespace json {
grammar::generic_json_grammar_type const& generic_json_grammar()
{
return grammar::value;
}
grammar::generic_json_key_value_type const& generic_json_key_value()
{
return grammar::key_value;
}
}}
#endif // MAPNIK_JSON_GENERIC_JSON_GRAMMAR_X3_DEF_HPP