Skip to content

Commit

Permalink
Clean up and error refactoring
Browse files Browse the repository at this point in the history
Removed individual class utree as functions

Switch from config_error to new carto_error which is source_location aware.

Other error refactoring and cleanup
  • Loading branch information
rundel committed Sep 7, 2012
1 parent df8184b commit 143e370
Show file tree
Hide file tree
Showing 25 changed files with 351 additions and 301 deletions.
2 changes: 1 addition & 1 deletion include/exception.hpp
Expand Up @@ -15,7 +15,7 @@
#include <boost/spirit/home/support/info.hpp>
#include <boost/spirit/include/qi.hpp>

#include <position_iterator.hpp>
#include <utility/position_iterator.hpp>

namespace carto {

Expand Down
8 changes: 1 addition & 7 deletions include/expression_eval.hpp
Expand Up @@ -9,7 +9,7 @@

#include <parse/expression_grammar.hpp>

#include <position_iterator.hpp>
#include <utility/position_iterator.hpp>

namespace carto {

Expand All @@ -24,12 +24,6 @@ struct expression {
annotations(annotations_),
env(env_) { }

template<class T>
T as(utree const& ut)
{
return detail::as<T>(ut);
}

inline expression_node_type get_node_type(utree const& ut)
{
return (expression_node_type) annotations[ut.tag()].second;
Expand Down
6 changes: 0 additions & 6 deletions include/generate/generate_filter.hpp
Expand Up @@ -32,12 +32,6 @@ struct filter_printer {
filter_printer(utree const& tree_, annotations_type const& annotations_,
style_env const& env_, mapnik::rule& rule_);

template<class T>
T as(utree const& ut)
{
return detail::as<T>(ut);
}

std::string print();

utree parse_var(utree const& ut);
Expand Down
20 changes: 4 additions & 16 deletions include/mml_parser.hpp
Expand Up @@ -18,7 +18,6 @@

namespace carto {

using mapnik::config_error;
namespace al = boost::algorithm;

struct mml_parser {
Expand All @@ -28,33 +27,22 @@ struct mml_parser {
std::string path;
std::vector< std::vector<std::string> > layer_selectors;

mml_parser(parse_tree const& pt, bool strict_ = false, std::string const& path_ = "./");

mml_parser(std::string const& in, bool strict_ = false, std::string const& path_ = "./");

template<class T>
T as(utree const& ut)
{
return detail::as<T>(ut);
}
mml_parser(parse_tree const& pt, std::string const& path_, bool strict_ = false);
mml_parser(std::string const& in, std::string const& path_, bool strict_ = false);
mml_parser(std::string const& filename, bool strict_ = false);

parse_tree get_parse_tree();

std::string get_path();

node_type get_node_type(utree const& ut);

source_location get_location(utree const& ut);

void key_error(std::string const& key, utree const& node);

void parse(mapnik::Map& map);
void parse_map(mapnik::Map& map);

void parse_stylesheet(mapnik::Map& map, utree const& node);

void parse_layer(mapnik::Map& map, utree const& node);


void parse_Datasource(mapnik::layer& lyr, utree const& node);

std::string ensure_relative_to_xml( boost::optional<std::string> opt_path );
Expand Down
60 changes: 20 additions & 40 deletions include/mss_parser.hpp
Expand Up @@ -13,6 +13,7 @@
#include <utility/utree.hpp>
#include <utility/environment.hpp>

#include <boost/utility.hpp>
#include <boost/variant.hpp>
#include <boost/unordered_map.hpp>

Expand All @@ -22,31 +23,27 @@

namespace carto {

struct mss_parser {
struct mss_parser : private boost::noncopyable {

private:
parse_tree tree;
bool strict;
std::string path;

boost::unordered_map<std::size_t, std::string> fontset_names;
mapnik::expression_grammar<std::string::const_iterator> expr_grammar;

public:
mss_parser(parse_tree const& pt, std::string const& path_, bool strict_ = false);
mss_parser(std::string const& in, std::string const& path_, bool strict_ = false);
mss_parser(std::string const& filename, bool strict_ = false);

std::string const& get_fontset_name(std::size_t hash);

mss_parser(parse_tree const& pt, bool strict_ = false, std::string const& path_ = "./");

mss_parser(std::string const& in, bool strict_ = false, std::string const& path_ = "./");

template<class T>
T as(utree const& ut)
{
return detail::as<T>(ut);
}

parse_tree get_parse_tree();

std::string get_path();

int get_node_type(utree const& ut);


int get_node_type(utree const& ut);
source_location get_location(utree const& ut);

template<class symbolizer>
Expand Down Expand Up @@ -74,50 +71,33 @@ struct mss_parser {
return symbolizer();
}

mapnik::transform_type create_transform(std::string const& str);
mapnik::transform_type create_transform(std::string const& str, utree const& node);

void key_error(std::string const& key, utree const& node);

utree eval_var(utree const& node, style_env const& env);
utree parse_value(utree const& node, style_env const& env);
void parse_variable(utree const& node, style_env& env);

void parse(mapnik::Map& map, style_env& env);
void parse_stylesheet(mapnik::Map& map, style_env& env);

void parse_map_style(mapnik::Map& map, utree const& node, style_env& env);
void parse_style(mapnik::Map& map, utree const& node, style_env const& parent_env,
mapnik::rule const& parent_rule = mapnik::rule(), std::string const& parent_name = "");

void parse_filter(mapnik::Map& map, utree const& node, style_env const& env, mapnik::rule& rule);

utree eval_var(utree const& node, style_env const& env);

utree parse_value(utree const& node, style_env const& env);

void parse_attribute(mapnik::Map& map, utree const& node, style_env const& env, mapnik::rule& rule);

bool parse_polygon(mapnik::rule& rule, std::string const& key, utree const& value, style_env const& env);

bool parse_line(mapnik::rule& rule, std::string const& key, utree const& value, style_env const& env);

bool parse_marker(mapnik::rule& rule, std::string const& key, utree const& value, style_env const& env);

bool parse_point(mapnik::rule& rule, std::string const& key, utree const& value, style_env const& env);

bool parse_line_pattern(mapnik::rule& rule, std::string const& key, utree const& value, style_env const& env);

bool parse_polygon_pattern(mapnik::rule& rule, std::string const& key, utree const& value, style_env const& env);

bool parse_raster(mapnik::rule& rule, std::string const& key, utree const& value, style_env const& env);

bool parse_building(mapnik::rule& rule, std::string const& key, utree const& value, style_env const& env);

bool parse_text(mapnik::Map& map, mapnik::rule& rule, std::string const& key, utree const& value, style_env const& env);

bool parse_shield(mapnik::rule& rule, std::string const& key, utree const& value, style_env const& env);

void parse_variable(utree const& node, style_env& env);

void parse_map_style(mapnik::Map& map, utree const& node, style_env& env);

bool parse_shield(mapnik::rule& rule, std::string const& key, utree const& value, style_env const& env);
};

mss_parser load_mss(std::string filename, bool strict);

}
#endif
2 changes: 1 addition & 1 deletion include/parse/annotator.hpp
Expand Up @@ -16,7 +16,7 @@
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

#include <position_iterator.hpp>
#include <utility/position_iterator.hpp>

namespace carto {

Expand Down
2 changes: 1 addition & 1 deletion include/parse/error_handler.hpp
Expand Up @@ -10,7 +10,7 @@
#define ERROR_HANDLER_H

#include <exception.hpp>
#include <position_iterator.hpp>
#include <utility/position_iterator.hpp>

namespace carto {

Expand Down
2 changes: 1 addition & 1 deletion include/parse/filter_grammar.hpp
Expand Up @@ -9,7 +9,7 @@
#include <parse/error_handler.hpp>
#include <parse/annotator.hpp>

#include <position_iterator.hpp>
#include <utility/position_iterator.hpp>

namespace carto {

Expand Down
11 changes: 5 additions & 6 deletions include/parse/parse_tree.hpp
Expand Up @@ -8,8 +8,8 @@
#ifndef PARSE_TREE_H
#define PARSE_TREE_H

#include <mapnik/config_error.hpp>
#include <position_iterator.hpp>
#include <utility/carto_error.hpp>
#include <utility/position_iterator.hpp>

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_utree.hpp>
Expand All @@ -21,7 +21,6 @@
namespace carto {

using boost::spirit::utree;
using mapnik::config_error;

class parse_tree {

Expand Down Expand Up @@ -98,9 +97,9 @@ parse_tree build_parse_tree(std::string const& in, std::string const& path = "./
end(in.end());

bool r = qi::phrase_parse(it, end, p, boost::spirit::ascii::space, pt.ast());
if (!r) {
throw config_error("Parser failed!");
}
if (!r)
throw carto_error("Parser failed!");

return pt;
}

Expand Down
54 changes: 54 additions & 0 deletions include/utility/carto_error.hpp
@@ -0,0 +1,54 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2011 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 CARTO_ERROR_HPP
#define CARTO_ERROR_HPP

#include <utility/source_location.hpp>

namespace carto {

class carto_error : public std::exception
{
public:
carto_error(std::string const& what);
carto_error(std::string const& what, source_location const& loc);
carto_error(std::string const& what, source_location const& loc, std::string const& filename);
virtual ~carto_error() throw() {}

virtual const char * what() const throw();

void append_context(std::string const& ctx);
void set_location(source_location const& node);
void set_filename(std::string const& node);
protected:
mutable std::string what_;
mutable source_location loc_;
mutable std::string file_;
mutable std::string msg_;
};

void warn(carto_error const& err);

}

#endif // CARTO_ERROR_HPP
10 changes: 2 additions & 8 deletions include/utility/carto_functions.hpp
Expand Up @@ -13,8 +13,8 @@ namespace carto {

inline double clamp(double val);

struct hsl {

struct hsl
{
double h,s,l,a;
unsigned tag;

Expand All @@ -23,12 +23,6 @@ struct hsl {
double hue(double h, double m1, double m2);

utree to_rgb() ;

template<class T>
T as(utree const& ut)
{
return detail::as<T>(ut);
}
};

utree test(utree const& rgb);
Expand Down
Expand Up @@ -8,35 +8,15 @@

#ifndef MAPNIK_POSITION_ITERATOR_H
#define MAPNIK_POSITION_ITERATOR_H

#include <sstream>
#include <vector>


#include <boost/detail/iterator.hpp>
#include <boost/iterator/iterator_adaptor.hpp>
#include <boost/range/iterator_range.hpp>

#include <utility/source_location.hpp>

namespace carto {

struct source_location {

int line;
int column;

source_location (int l, int c);

source_location ();

bool valid();

std::string get_string();

bool operator==(source_location const& other) const;


};



template <typename Iterator, int TabLength = 2>
class position_iterator
: public boost::iterator_adaptor< position_iterator<Iterator>,
Expand Down Expand Up @@ -118,15 +98,16 @@ class position_iterator
source_location loc;
typename boost::detail::iterator_traits<Iterator>::value_type prev;
};

template<class Iterator>
inline source_location get_location (Iterator const& i) {
source_location loc(-1, -1);
return loc;
inline source_location get_location(Iterator const& i)
{
return source_location(-1, -1);
}

template<class Iterator>
inline source_location get_location (position_iterator<Iterator> const& i) {
inline source_location get_location(position_iterator<Iterator> const& i)
{
return i.location();
}

Expand Down

0 comments on commit 143e370

Please sign in to comment.