1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al. 3 * This file is part of Overpass_API. 5 * Overpass_API is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Affero General Public License as 7 * published by the Free Software Foundation, either version 3 of the 8 * License, or (at your option) any later version. 10 * Overpass_API is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 15 * You should have received a copy of the GNU Affero General Public License 16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>. 20#include "osm_script.h" 29std::map< std::string, Statement::Statement_Maker* >& Statement::maker_by_name() 31275278612 q static std::map< std::string, Statement::Statement_Maker* > makers; 36std::map< std::string, Statement::Criterion_Maker* >& Statement::maker_by_ql_criterion() 3864920612 q static std::map< std::string, Statement::Criterion_Maker* > makers; 43std::map< std::string, std::vector< Statement::Evaluator_Maker* > >& Statement::maker_by_token() 4570592612 q static std::map< std::string, std::vector< Statement::Evaluator_Maker* > > makers; 50std::map< std::string, std::vector< Statement::Evaluator_Maker* > >& Statement::maker_by_func_name() 52134544612 q static std::map< std::string, std::vector< Statement::Evaluator_Maker* > > makers; 57void Statement::eval_attributes_array(std::string element, std::map< std::string, std::string >& attributes, 58 const std::map< std::string, std::string >& input) 60175361612 q for (std::map< std::string, std::string >::const_iterator it = input.begin(); it != input.end(); ++it) 62141463610 q std::map< std::string, std::string >::iterator ait(attributes.find(it->first)); 63141463610 q if (ait != attributes.end()) 64141463610 q ait->second = it->second; 670 std::ostringstream temp; 680 temp<<"Unknown attribute \""<<it->first<<"\" in element \""<<element<<"\"."; 690 add_static_error(temp.str()); 74void Statement::assure_no_text(std::string text, std::string name) 769629608 q for (unsigned int i(0); i < text.size(); ++i) 781677 q if (!isspace(text[i])) 800 std::ostringstream temp; 810 temp<<"Element \""<<name<<"\" must not contain text."; 820 add_static_error(temp.str()); 88void Statement::substatement_error(std::string parent, Statement* child) 900 std::ostringstream temp; 910 temp<<"Element \""<<child->get_name()<<"\" cannot be subelement of element \""<<parent<<"\"."; 920 add_static_error(temp.str()); 95void Statement::add_statement(Statement* statement, std::string text) 970 assure_no_text(text, this->get_name()); 980 substatement_error(get_name(), statement); 101void Statement::add_final_text(std::string text) 103497 q assure_no_text(text, this->get_name()); 106void Statement::display_full() 108 //display_verbatim(get_source(startpos, endpos - startpos)); 111void Statement::display_starttag() 113 //display_verbatim(get_source(startpos, tagendpos - startpos)); 117Statement::Factory::~Factory() 1192448612 q for (std::vector< Statement* >::const_iterator it = created_statements.begin(); 12036351612 q it != created_statements.end(); ++it) 125Statement* Statement::Factory::create_statement 126 (std::string element, int line_number, const std::map< std::string, std::string >& attributes) 13017687612 q std::map< std::string, Statement::Statement_Maker* >::iterator maker_it = 13117687612 q Statement::maker_by_name().find(element); 13317687612 q if (maker_it != Statement::maker_by_name().end()) 13417687612 q statement = maker_it->second->create_statement(line_number, attributes, global_settings); 13717687612 q created_statements.push_back(statement); 1400 std::ostringstream temp; 1410 temp<<"Unknown tag \""<<element<<"\" in line "<<line_number<<'.'; 1430 error_output_->add_static_error(temp.str(), line_number); 150std::string Statement::eval_to_string(Statement::Eval_Return_Type eval_type) 15281 q if (eval_type == Statement::string) 15441 q else if (eval_type == Statement::container) 15641 q else if (eval_type == Statement::geometry) 1590 return "non-evaluator"; 163Statement* stmt_from_tree_node(const Token_Node_Ptr& tree_it, 164 Statement::QL_Context tree_context, const Statement::Return_Type_Checker& eval_type, 165 const std::vector< Statement::Evaluator_Maker* >& makers, Statement::Factory& stmt_factory, 166 Parsed_Query& global_settings, Error_Output* error_output) 17013236210 q std::vector< Statement::Evaluator_Maker* >::const_iterator maker_it = makers.begin(); 17226640210 q while (!statement && maker_it != makers.end()) 17413404210 q statement = (*maker_it)->create_evaluator(tree_it, tree_context, stmt_factory, global_settings, error_output); 17713352210 q while (maker_it != makers.end()) 17911627 q Statement* bis = (*maker_it)->create_evaluator(tree_it, tree_context, stmt_factory, global_settings, error_output); 1830 error_output->add_static_error(std::string("Function name \"") + tree_it.lhs()->token 1840 + "\" has ambiguous resolutions to \"" + statement->get_name() + "\" and \"" + bis->get_name() + "\".", 1850 tree_it->line_col.first); 19313236210 q if (statement && eval_type.eval_required()) 19511224207 q Evaluator* eval = dynamic_cast< Evaluator* >(statement); 1990 error_output->add_static_error(std::string("Evaluator expected, but function \"") + tree_it.lhs()->token 2000 + "\" is not an evaluator.", tree_it->line_col.first); 20411224207 q else if (!eval_type.matches(eval->return_type())) 20741 q error_output->add_static_error(std::string("Evaluator for ") + eval_type.expectation() 20841 q + " expected, but function \"" + statement->get_name() + "\" is an evaluator for " 20941 q + Statement::eval_to_string(eval->return_type()) + ".", tree_it->line_col.first); 219Statement* Statement::Factory::create_evaluator( 220 const Token_Node_Ptr& tree_it, Statement::QL_Context tree_context, 221 const Statement::Return_Type_Checker& eval_type) 22515560210 q if (tree_it->token == "(") 2295108200 q std::map< std::string, std::vector< Statement::Evaluator_Maker* > >::iterator all_it = 2305108200 q Statement::maker_by_func_name().find(tree_it.lhs()->token); 2315108200 q if (all_it != Statement::maker_by_func_name().end()) 2325108200 q statement = stmt_from_tree_node(tree_it, tree_context, eval_type, 2335108200 q all_it->second, *this, global_settings, Statement::error_output); 2350 error_output->add_parse_error(std::string("Function \"") + tree_it.lhs()->token + "\" not known", 2360 tree_it->line_col.first); 2392352108 q return create_evaluator(tree_it.rhs(), tree_context, eval_type); 2420 Statement::error_output->add_static_error("Empty parentheses cannot be evaluated.", tree_it->line_col.first); 2468100186 q else if (tree_it->token == ".") 24897663 q if (tree_it->lhs && tree_it->rhs && tree_it.rhs()->token == "(" && tree_it.rhs()->lhs) 25096461 q std::map< std::string, std::vector< Statement::Evaluator_Maker* > >::iterator all_it = 25196461 q Statement::maker_by_func_name().find(tree_it.rhs().lhs()->token); 25296461 q if (all_it != Statement::maker_by_func_name().end()) 25396461 q statement = stmt_from_tree_node(tree_it, tree_context, eval_type, 25496461 q all_it->second, *this, global_settings, Statement::error_output); 2560 error_output->add_parse_error(std::string("Function \"") + tree_it.rhs().lhs()->token + "\" not known", 2570 tree_it->line_col.first); 261123 q std::map< std::string, std::vector< Statement::Evaluator_Maker* > >::iterator all_it = 262123 q Statement::maker_by_token().find(tree_it->token); 263123 q if (all_it != Statement::maker_by_token().end()) 264123 q statement = stmt_from_tree_node(tree_it, tree_context, eval_type, 265123 q all_it->second, *this, global_settings, Statement::error_output); 2687124186 q else if (tree_it->token == "") 2690 error_output->add_parse_error("Evaluator expected, but empty token found.", tree_it->line_col.first); 2727124186 q std::map< std::string, std::vector< Statement::Evaluator_Maker* > >::iterator all_it = 2737124186 q Statement::maker_by_token().find(tree_it->token); 2747124186 q if (all_it != Statement::maker_by_token().end()) 2754696176 q statement = stmt_from_tree_node(tree_it, tree_context, eval_type, 2764696176 q all_it->second, *this, global_settings, Statement::error_output); 2802456160 q all_it = Statement::maker_by_token().find(""); 2812456160 q if (all_it != Statement::maker_by_token().end()) 2822456160 q statement = stmt_from_tree_node(tree_it, tree_context, eval_type, 2832456160 q all_it->second, *this, global_settings, Statement::error_output); 287367 q else if (tree_it->lhs) 290123 q error_output->add_parse_error( 291123 q std::string("\"") + tree_it->token + "\" cannot be used as binary operator", tree_it->line_col.first); 293204 q error_output->add_parse_error( 294204 q std::string("\"") + tree_it->token + "\" needs a right hand side argument", tree_it->line_col.first); 29641 q else if (tree_it->rhs) 2980 error_output->add_parse_error( 2990 std::string("\"") + tree_it->token + "\" cannot be used as unary operator", tree_it->line_col.first); 3000 return create_evaluator(tree_it.rhs(), tree_context, eval_type); 30341 q error_output->add_parse_error(std::string("Token \"") + tree_it->token 30441 q + "\" has not been recognized as statement", tree_it->line_col.first); 30913144207 q created_statements.push_back(statement); 315Statement* Statement::Factory::create_criterion(const Token_Node_Ptr& tree_it, 316 const std::string& type, bool& can_standalone, const std::string& into) 3183084497 q Token_Node_Ptr criterion = find_leftmost_token(tree_it); 3193084497 q uint line_nr = criterion->line_col.first; 3203084497 q std::string criterion_s = criterion->token; 3233084497 q && (isdigit(criterion_s[0]) 3243084497 q || (criterion_s[0] == '-' && criterion_s.size() > 1 && isdigit(criterion_s[1])))) 325896215 q criterion_s = (tree_it->token == "," ? "bbox" : "id"); 3273084497 q std::map< std::string, Criterion_Maker* >::iterator it = Statement::maker_by_ql_criterion().find(criterion_s); 3283084497 q if (it != Statement::maker_by_ql_criterion().end() && it->second) 3303084497 q can_standalone = it->second->can_standalone(type); 3313084497 q Statement* result = it->second->create_criterion(tree_it, type, into, *this, global_settings, error_output); 3333072495 q created_statements.push_back(result); 3380 error_output->add_parse_error("Unknown query clause", line_nr); 343Error_Output* Statement::error_output = 0; 346void Statement::add_static_error(std::string error) 34993 q error_output->add_static_error(error, line_number); 353void Statement::add_static_remark(std::string remark) 3560 error_output->add_static_remark(remark, line_number); 360void Statement::runtime_error(std::string error) const 36366 q error_output->runtime_error(error); 367void Statement::runtime_remark(std::string error) const 3700 error_output->runtime_remark(error); 374std::map< std::string, std::string > convert_c_pairs(const char** attr) 3762008 q std::map< std::string, std::string > result; 3775168 q for (int i = 0; attr[i]; i+=2) 3783167 q result[attr[i]] = attr[i+1]; 383void Output_Statement::transfer_output(Resource_Manager& rman, Set& into) const 3854187589 q rman.swap_set(output, into); 390void Output_Statement::transfer_output(Resource_Manager& rman, Diff_Set& into) const 3921616 q rman.swap_diff_set(output, into);