2016-01-28 10:28:44 -05:00
|
|
|
#ifndef SERVER_API_BASE_PARAMETERS_GRAMMAR_HPP
|
|
|
|
#define SERVER_API_BASE_PARAMETERS_GRAMMAR_HPP
|
|
|
|
|
|
|
|
#include "engine/api/base_parameters.hpp"
|
|
|
|
|
2016-03-04 17:17:57 -05:00
|
|
|
#include "engine/bearing.hpp"
|
2016-04-07 07:25:35 -04:00
|
|
|
#include "engine/hint.hpp"
|
|
|
|
#include "engine/polyline_compressor.hpp"
|
2016-01-28 10:28:44 -05:00
|
|
|
|
2016-04-08 05:38:07 -04:00
|
|
|
#include <boost/optional.hpp>
|
2016-04-07 07:25:35 -04:00
|
|
|
//#define BOOST_SPIRIT_DEBUG
|
|
|
|
#include <boost/spirit/include/qi.hpp>
|
2016-01-28 10:28:44 -05:00
|
|
|
|
2016-04-08 05:38:07 -04:00
|
|
|
#include <limits>
|
2016-04-08 08:05:39 -04:00
|
|
|
#include <string>
|
2016-01-28 10:28:44 -05:00
|
|
|
|
|
|
|
namespace osrm
|
|
|
|
{
|
|
|
|
namespace server
|
|
|
|
{
|
|
|
|
namespace api
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace qi = boost::spirit::qi;
|
2016-04-05 19:42:45 -04:00
|
|
|
|
2016-04-07 07:25:35 -04:00
|
|
|
template <typename T, char... Fmt> struct no_trailing_dot_policy : qi::real_policies<T>
|
|
|
|
{
|
|
|
|
template <typename Iterator> static bool parse_dot(Iterator &first, Iterator const &last)
|
|
|
|
{
|
2016-04-05 19:42:45 -04:00
|
|
|
if (first == last || *first != '.')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
static const constexpr char fmt[sizeof...(Fmt)] = {Fmt...};
|
|
|
|
|
2016-04-07 07:25:35 -04:00
|
|
|
if (first + sizeof(fmt) < last && std::equal(fmt, fmt + sizeof(fmt), first + 1u))
|
2016-04-05 19:42:45 -04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
++first;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-07 07:25:35 -04:00
|
|
|
template <typename Iterator> static bool parse_exp(Iterator &, const Iterator &)
|
|
|
|
{
|
2016-04-05 19:42:45 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Iterator, typename Attribute>
|
2016-04-07 07:25:35 -04:00
|
|
|
static bool parse_exp_n(Iterator &, const Iterator &, Attribute &)
|
|
|
|
{
|
2016-04-05 19:42:45 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Iterator, typename Attribute>
|
2016-04-07 07:25:35 -04:00
|
|
|
static bool parse_nan(Iterator &, const Iterator &, Attribute &)
|
|
|
|
{
|
2016-04-05 19:42:45 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Iterator, typename Attribute>
|
2016-04-07 07:25:35 -04:00
|
|
|
static bool parse_inf(Iterator &, const Iterator &, Attribute &)
|
|
|
|
{
|
2016-04-05 19:42:45 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-01-28 10:28:44 -05:00
|
|
|
struct BaseParametersGrammar : boost::spirit::qi::grammar<std::string::iterator>
|
|
|
|
{
|
|
|
|
using Iterator = std::string::iterator;
|
|
|
|
using RadiusesT = std::vector<boost::optional<double>>;
|
2016-04-05 19:42:45 -04:00
|
|
|
using json_policy = no_trailing_dot_policy<double, 'j', 's', 'o', 'n'>;
|
2016-01-28 10:28:44 -05:00
|
|
|
|
2016-03-03 08:26:13 -05:00
|
|
|
BaseParametersGrammar(qi::rule<Iterator> &root_rule_, engine::api::BaseParameters ¶meters_)
|
2016-03-02 19:48:30 -05:00
|
|
|
: BaseParametersGrammar::base_type(root_rule_), base_parameters(parameters_)
|
2016-01-28 10:28:44 -05:00
|
|
|
{
|
2016-03-03 08:26:13 -05:00
|
|
|
const auto add_bearing =
|
2016-04-07 07:25:35 -04:00
|
|
|
[this](boost::optional<boost::fusion::vector2<short, short>> bearing_range) {
|
|
|
|
boost::optional<engine::Bearing> bearing;
|
|
|
|
if (bearing_range)
|
|
|
|
{
|
|
|
|
bearing = engine::Bearing{boost::fusion::at_c<0>(*bearing_range),
|
|
|
|
boost::fusion::at_c<1>(*bearing_range)};
|
|
|
|
}
|
|
|
|
base_parameters.bearings.push_back(std::move(bearing));
|
|
|
|
};
|
|
|
|
const auto set_radiuses = [this](RadiusesT radiuses) {
|
2016-01-28 10:28:44 -05:00
|
|
|
base_parameters.radiuses = std::move(radiuses);
|
|
|
|
};
|
2016-04-07 07:25:35 -04:00
|
|
|
const auto add_hint = [this](const std::string &hint_string) {
|
2016-01-28 10:28:44 -05:00
|
|
|
if (hint_string.size() > 0)
|
|
|
|
{
|
|
|
|
base_parameters.hints.push_back(engine::Hint::FromBase64(hint_string));
|
|
|
|
}
|
|
|
|
};
|
2016-04-07 07:25:35 -04:00
|
|
|
const auto add_coordinate = [this](const boost::fusion::vector<double, double> &lonLat) {
|
2016-03-03 08:26:13 -05:00
|
|
|
base_parameters.coordinates.emplace_back(util::Coordinate(
|
|
|
|
util::FixedLongitude(boost::fusion::at_c<0>(lonLat) * COORDINATE_PRECISION),
|
|
|
|
util::FixedLatitude(boost::fusion::at_c<1>(lonLat) * COORDINATE_PRECISION)));
|
2016-03-02 19:48:30 -05:00
|
|
|
};
|
2016-04-07 07:25:35 -04:00
|
|
|
const auto polyline_to_coordinates = [this](const std::string &polyline) {
|
2016-03-02 19:48:30 -05:00
|
|
|
base_parameters.coordinates = engine::decodePolyline(polyline);
|
|
|
|
};
|
2016-01-28 10:28:44 -05:00
|
|
|
|
|
|
|
alpha_numeral = +qi::char_("a-zA-Z0-9");
|
2016-03-02 19:48:30 -05:00
|
|
|
polyline_chars = qi::char_("a-zA-Z0-9_.--[]{}@?|\\%~`^");
|
2016-04-06 10:47:16 -04:00
|
|
|
base64_char = qi::char_("a-zA-Z0-9--_=");
|
2016-01-28 10:28:44 -05:00
|
|
|
|
2016-04-08 05:38:07 -04:00
|
|
|
unlimited.add("unlimited", std::numeric_limits<double>::infinity());
|
|
|
|
|
2016-04-08 08:05:39 -04:00
|
|
|
radiuses_rule = qi::lit("radiuses=") > -(unlimited | qi::double_) % ";";
|
2016-03-03 08:26:13 -05:00
|
|
|
hints_rule =
|
2016-04-08 08:05:39 -04:00
|
|
|
qi::lit("hints=") >
|
2016-03-03 08:26:13 -05:00
|
|
|
qi::as_string[qi::repeat(engine::ENCODED_HINT_SIZE)[base64_char]][add_hint] % ";";
|
2016-01-28 10:28:44 -05:00
|
|
|
bearings_rule =
|
2016-04-08 08:05:39 -04:00
|
|
|
qi::lit("bearings=") > (-(qi::short_ > ',' > qi::short_))[add_bearing] % ";";
|
|
|
|
polyline_rule = qi::as_string[qi::lit("polyline(") > +polyline_chars > qi::lit(")")]
|
2016-04-07 07:25:35 -04:00
|
|
|
[polyline_to_coordinates];
|
2016-04-08 08:05:39 -04:00
|
|
|
location_rule = (double_ > qi::lit(',') > double_)[add_coordinate];
|
2016-03-02 19:48:30 -05:00
|
|
|
query_rule = (location_rule % ';') | polyline_rule;
|
|
|
|
|
2016-01-28 10:28:44 -05:00
|
|
|
base_rule = bearings_rule | radiuses_rule[set_radiuses] | hints_rule;
|
|
|
|
}
|
|
|
|
|
2016-03-03 08:26:13 -05:00
|
|
|
protected:
|
2016-01-28 10:28:44 -05:00
|
|
|
qi::rule<Iterator> base_rule;
|
2016-03-02 19:48:30 -05:00
|
|
|
qi::rule<Iterator> query_rule;
|
2016-01-28 10:28:44 -05:00
|
|
|
|
2016-03-03 08:26:13 -05:00
|
|
|
private:
|
2016-01-28 10:28:44 -05:00
|
|
|
engine::api::BaseParameters &base_parameters;
|
|
|
|
qi::rule<Iterator> bearings_rule;
|
|
|
|
qi::rule<Iterator> hints_rule;
|
2016-03-02 19:48:30 -05:00
|
|
|
qi::rule<Iterator> polyline_rule, location_rule;
|
2016-04-08 05:38:07 -04:00
|
|
|
qi::symbols<char, double> unlimited;
|
2016-01-28 10:28:44 -05:00
|
|
|
qi::rule<Iterator, RadiusesT()> radiuses_rule;
|
|
|
|
qi::rule<Iterator, unsigned char()> base64_char;
|
2016-03-02 19:48:30 -05:00
|
|
|
qi::rule<Iterator, std::string()> alpha_numeral, polyline_chars;
|
2016-04-05 19:42:45 -04:00
|
|
|
qi::real_parser<double, json_policy> double_;
|
2016-01-28 10:28:44 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|