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() 31310986692 q static std::map< std::string, Statement::Statement_Maker* > makers; 36std::map< std::string, Statement::Criterion_Maker* >& Statement::maker_by_ql_criterion() 3873728692 q static std::map< std::string, Statement::Criterion_Maker* > makers; 43std::map< std::string, std::vector< Statement::Evaluator_Maker* > >& Statement::maker_by_token() 4578664692 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() 52151320692 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) 60196658692 q for (std::map< std::string, std::string >::const_iterator it = input.begin(); it != input.end(); ++it) 62159054690 q std::map< std::string, std::string >::iterator ait(attributes.find(it->first)); 63159054690 q if (ait != attributes.end()) 64159054690 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) 7610981689 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() 1192768692 q for (std::vector< Statement* >::const_iterator it = created_statements.begin(); 12040389692 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) 13019861692 q std::map< std::string, Statement::Statement_Maker* >::iterator maker_it = 13119861692 q Statement::maker_by_name().find(element); 13319861692 q if (maker_it != Statement::maker_by_name().end()) 13419861692 q statement = maker_it->second->create_statement(line_number, attributes, global_settings); 13719861692 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) 17014212227 q std::vector< Statement::Evaluator_Maker* >::const_iterator maker_it = makers.begin(); 17228660227 q while (!statement && maker_it != makers.end()) 17414448227 q statement = (*maker_it)->create_evaluator(tree_it, tree_context, stmt_factory, global_settings, error_output); 17714360227 q while (maker_it != makers.end()) 17914836 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); 19314212227 q if (statement && eval_type.eval_required()) 19511928225 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); 20411928225 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) 22516772227 q if (tree_it->token == "(") 2295456221 q std::map< std::string, std::vector< Statement::Evaluator_Maker* > >::iterator all_it = 2305456221 q Statement::maker_by_func_name().find(tree_it.lhs()->token); 2315456221 q if (all_it != Statement::maker_by_func_name().end()) 2325452221 q statement = stmt_from_tree_node(tree_it, tree_context, eval_type, 2335452221 q all_it->second, *this, global_settings, Statement::error_output); 23541 q error_output->add_parse_error(std::string("Function \"") + tree_it.lhs()->token + "\" not known", 23641 q tree_it->line_col.first); 2392572129 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); 2468744197 q else if (tree_it->token == ".") 248102067 q if (tree_it->lhs && tree_it->rhs && tree_it.rhs()->token == "(" && tree_it.rhs()->lhs) 250100464 q std::map< std::string, std::vector< Statement::Evaluator_Maker* > >::iterator all_it = 251100464 q Statement::maker_by_func_name().find(tree_it.rhs().lhs()->token); 252100464 q if (all_it != Statement::maker_by_func_name().end()) 253100464 q statement = stmt_from_tree_node(tree_it, tree_context, eval_type, 254100464 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); 261164 q std::map< std::string, std::vector< Statement::Evaluator_Maker* > >::iterator all_it = 262164 q Statement::maker_by_token().find(tree_it->token); 263164 q if (all_it != Statement::maker_by_token().end()) 264164 q statement = stmt_from_tree_node(tree_it, tree_context, eval_type, 265164 q all_it->second, *this, global_settings, Statement::error_output); 2687724197 q else if (tree_it->token == "") 26981 q error_output->add_parse_error("Evaluator expected, but empty token found.", tree_it->line_col.first); 2727716197 q std::map< std::string, std::vector< Statement::Evaluator_Maker* > >::iterator all_it = 2737716197 q Statement::maker_by_token().find(tree_it->token); 2747716197 q if (all_it != Statement::maker_by_token().end()) 2755204187 q statement = stmt_from_tree_node(tree_it, tree_context, eval_type, 2765204187 q all_it->second, *this, global_settings, Statement::error_output); 2802536173 q all_it = Statement::maker_by_token().find(""); 2812536173 q if (all_it != Statement::maker_by_token().end()) 2822536173 q statement = stmt_from_tree_node(tree_it, tree_context, eval_type, 2832536173 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); 293163 q error_output->add_parse_error( 294163 q std::string("\"") + tree_it->token + "\" needs a right hand side argument", tree_it->line_col.first); 29682 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); 30382 q error_output->add_parse_error(std::string("Token \"") + tree_it->token 30482 q + "\" has not been recognized as statement", tree_it->line_col.first); 30914124225 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) 3183648570 q Token_Node_Ptr criterion = find_leftmost_token(tree_it); 3193648570 q uint line_nr = criterion->line_col.first; 3203648570 q std::string criterion_s = criterion->token; 3233648570 q && (isdigit(criterion_s[0]) 3243648570 q || (criterion_s[0] == '-' && criterion_s.size() > 1 && isdigit(criterion_s[1])))) 3251092257 q criterion_s = (tree_it->token == "," ? "bbox" : "id"); 3273648570 q std::map< std::string, Criterion_Maker* >::iterator it = Statement::maker_by_ql_criterion().find(criterion_s); 3283648570 q if (it != Statement::maker_by_ql_criterion().end() && it->second) 3303648570 q can_standalone = it->second->can_standalone(type); 3313648570 q Statement* result = it->second->create_criterion(tree_it, type, into, *this, global_settings, error_output); 3333636567 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) 349124 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 36355 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 38527553668 q rman.swap_set(output, into); 390void Output_Statement::transfer_output(Resource_Manager& rman, Diff_Set& into) const 3922020 q rman.swap_diff_set(output, into);