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/>. 19#include "../../expat/map_ql_input.h" 20#include "../core/datatypes.h" 21#include "../core/parsed_query.h" 22#include "../dispatch/scripting_core.h" 23#include "../statements/osm_script.h" 24#include "../statements/statement.h" 25#include "../statements/statement_dump.h" 26#include "map_ql_parser.h" 27#include "output_handler_parser.h" 28#include "tokenizer_utils.h" 40//----------------------------------------------------------------------------- 42template< class TStatement > 43TStatement* parse_statement(typename TStatement::Factory& stmt_factory, Parsed_Query& parsed_query, 44 Tokenizer_Wrapper& token, Error_Output* error_output, int depth); 46std::string probe_into(Tokenizer_Wrapper& token, Error_Output* error_output) 487688682 q std::string into = "_"; 497688682 q if (token.good() && *token == "->") 526700649 q clear_until_after(token, error_output, "."); 546700649 q into = get_identifier_token(token, error_output, "Variable"); 59std::string probe_from(Tokenizer_Wrapper& token, Error_Output* error_output) 62524111 q if (token.good() && *token == ".") 66512109 q from = get_identifier_token(token, error_output, "Variable"); 71template< class TStatement > 72std::vector< TStatement* > collect_substatements(typename TStatement::Factory& stmt_factory, Parsed_Query& parsed_query, 73 Tokenizer_Wrapper& token, Error_Output* error_output, int depth) 75756151 q std::vector< TStatement* > substatements; 76756151 q clear_until_after(token, error_output, "(", "{", false); 780 return substatements; 79756151 q std::string closing_token = (*token == "(" ? ")" : "}"); 811240151 q while (token.good() && *token != closing_token) 8348479 q std::pair< uint, uint > pos_before = token.line_col(); 8448479 q TStatement* substatement = parse_statement< TStatement > 8548479 q (stmt_factory, parsed_query, token, error_output, depth+1); 8748479 q substatements.push_back(substatement); 880 else if (token.line_col() == pos_before) 97template< class TStatement > 98std::vector< TStatement* > collect_substatements_and_probe 99 (typename TStatement::Factory& stmt_factory, Parsed_Query& parsed_query, 100 Tokenizer_Wrapper& token, Error_Output* error_output, bool& is_difference, int depth) 104512114 q std::vector< TStatement* > substatements; 105512114 q clear_until_after(token, error_output, "("); 106512114 q if (token.good() && *token != ")") 10840494 q TStatement* substatement = parse_statement< TStatement > 10940494 q (stmt_factory, parsed_query, token, error_output, depth+1); 11140494 q substatements.push_back(substatement); 118512114 q if (token.good() && *token != ")") 12021650 q TStatement* substatement = parse_statement< TStatement > 12121650 q (stmt_factory, parsed_query, token, error_output, depth+1); 12321650 q substatements.push_back(substatement); 12421650 q if (is_difference && *token != ")") 1270 error_output->add_parse_error("difference always requires two operands", token.line_col().first); 1280 clear_until_after(token, error_output, ")", false); 131568114 q while (token.good() && *token != ")") 133566 q std::pair< uint, uint > pos_before = token.line_col(); 134566 q TStatement* substatement = parse_statement< TStatement > 135566 q (stmt_factory, parsed_query, token, error_output, depth+1); 137566 q substatements.push_back(substatement); 1380 else if (token.line_col() == pos_before) 148//----------------------------------------------------------------------------- 151template< class TStatement > 152TStatement* parse_value_tree(typename TStatement::Factory& stmt_factory, Tokenizer_Wrapper& token, 153 Error_Output* error_output, bool parenthesis_expected, 154 Statement::QL_Context tree_context, const Statement::Return_Type_Checker& eval_type) 15628468 q Token_Tree tree(token, error_output, parenthesis_expected); 16028468 q return stmt_factory.create_evaluator(Token_Node_Ptr(tree, tree.tree[0].rhs), tree_context, 165template< class TStatement > 166TStatement* create_union_statement(typename TStatement::Factory& stmt_factory, 167 std::string into, uint line_nr) 16942496 q std::map< std::string, std::string > attr; 17142496 q return stmt_factory.create_statement("union", line_nr, attr); 174template< class TStatement > 175TStatement* create_difference_statement(typename TStatement::Factory& stmt_factory, 176 std::string into, uint line_nr) 1788821 q std::map< std::string, std::string > attr; 1808821 q return stmt_factory.create_statement("difference", line_nr, attr); 183template< class TStatement > 184TStatement* create_for_statement(typename TStatement::Factory& stmt_factory, 185 std::string stmt_name, std::string from, std::string into, uint line_nr) 18719246 q std::map< std::string, std::string > attr; 19019246 q return stmt_factory.create_statement(stmt_name, line_nr, attr); 194template< class TStatement > 195TStatement* create_make_statement(typename TStatement::Factory& stmt_factory, 196 std::string strategy, std::string from, std::string into, std::string type, uint line_nr) 198632127 q std::map< std::string, std::string > attr; 203632127 q return stmt_factory.create_statement(strategy, line_nr, attr); 207template< class TStatement > 208TStatement* create_complete_statement(typename TStatement::Factory& stmt_factory, 209 std::string maxnum, std::string from, std::string into, uint line_nr) 21133269 q std::map< std::string, std::string > attr; 21233269 q attr["maxnum"] = maxnum; 21533269 q return stmt_factory.create_statement("complete", line_nr, attr); 219template< class TStatement > 220TStatement* create_if_statement(typename TStatement::Factory& stmt_factory, uint line_nr) 2227617 q std::map< std::string, std::string > attr; 2237617 q return stmt_factory.create_statement("if", line_nr, attr); 227template< class TStatement > 228TStatement* create_else_statement(typename TStatement::Factory& stmt_factory, uint line_nr) 230287 q std::map< std::string, std::string > attr; 231287 q return stmt_factory.create_statement("else", line_nr, attr); 235template< class TStatement > 236TStatement* create_retro_statement(typename TStatement::Factory& stmt_factory, uint line_nr) 2386015 q std::map< std::string, std::string > attr; 2396015 q return stmt_factory.create_statement("retro", line_nr, attr); 243template< class TStatement > 244TStatement* create_print_statement(typename TStatement::Factory& stmt_factory, 245 std::string from, std::string mode, std::string order, std::string limit, std::string geometry, std::string show_ids, 246 std::string south, std::string north, std::string west, std::string east, 24916032673 q std::map< std::string, std::string > attr; 25416032673 q attr["geometry"] = geometry; 26016032673 q return stmt_factory.create_statement("print", line_nr, attr); 264template< class TStatement > 265TStatement* create_timeline_statement(typename TStatement::Factory& stmt_factory, uint line_nr, 266 const std::string& type, const std::string& ref, const std::string& version, const std::string& into) 26814837 q std::map< std::string, std::string > attr; 27114837 q attr["version"] = version; 27314837 q return stmt_factory.create_statement("timeline", line_nr, attr); 277template< class TStatement > 278TStatement* create_compare_statement(typename TStatement::Factory& stmt_factory, uint line_nr, 279 const std::string& from, const std::string& into) 2818421 q std::map< std::string, std::string > attr; 2828421 q attr["from"] = (from == "" ? "_" : from); 2848421 q return stmt_factory.create_statement("compare", line_nr, attr); 288template< class TStatement > 289TStatement* create_query_statement(typename TStatement::Factory& stmt_factory, 290 std::string type, std::string into, uint line_nr) 2923409580 q std::map< std::string, std::string > attr; 2953409580 q return stmt_factory.create_statement("query", line_nr, attr); 299typedef enum { haskv_plain, haskv_regex, haskv_icase } haskv_type; 302template< class TStatement > 303TStatement* create_has_kv_statement(typename TStatement::Factory& stmt_factory, 304 std::string key, std::string value, haskv_type regex, haskv_type key_regex, bool straight, uint line_nr) 3062284374 q std::map< std::string, std::string > attr; 3082284374 q if (key_regex == haskv_plain) 315684138 q else if (regex == haskv_regex) 319246 q attr["regv"] = value; 320246 q attr["case"] = "ignore"; 3232284374 q attr["modv"] = (straight ? "" : "not"); 3242284374 q return stmt_factory.create_statement("has-kv", line_nr, attr); 328template< class TStatement > 329TStatement* create_item_statement(typename TStatement::Factory& stmt_factory, 330 std::string from, std::string into, uint line_nr) 332868159 q std::map< std::string, std::string > attr; 335868159 q return stmt_factory.create_statement("item", line_nr, attr); 339template< class TStatement > 340TStatement* create_recurse_statement(typename TStatement::Factory& stmt_factory, 341 std::string type, std::string from, std::string into, uint line_nr) 34337687 q std::map< std::string, std::string > attr; 34437687 q attr["from"] = (from == "" ? "_" : from); 34737687 q return stmt_factory.create_statement("recurse", line_nr, attr); 351template< class TStatement > 352TStatement* create_coord_query_statement(typename TStatement::Factory& stmt_factory, 353 std::string lat, std::string lon, std::string from, std::string into, uint line_nr) 35520050 q std::map< std::string, std::string > attr; 35620050 q attr["from"] = (from == "" ? "_" : from); 36020050 q return stmt_factory.create_statement("coord-query", line_nr, attr); 364template< class TStatement > 365TStatement* create_map_to_area_statement(typename TStatement::Factory& stmt_factory, 366 std::string from, std::string into, uint line_nr) 3687218 q std::map< std::string, std::string > attr; 3697218 q attr["from"] = (from == "" ? "_" : from); 3717218 q return stmt_factory.create_statement("map-to-area", line_nr, attr); 375template< class TStatement > 376TStatement* create_localize_statement(typename TStatement::Factory& stmt_factory, 377 std::string type, std::string from, std::string into, 378 std::string south, std::string north, std::string west, std::string east, uint line_nr) 3808019 q std::map< std::string, std::string > attr; 3818019 q attr["from"] = (from == "" ? "_" : from); 3888019 q return stmt_factory.create_statement("localize", line_nr, attr); 392//----------------------------------------------------------------------------- 394std::vector< std::string > parse_setup(Tokenizer_Wrapper& token, 395 Error_Output* error_output, Parsed_Query& parsed_query) 3981836352 q std::vector< std::string > result; 3991836352 q result.push_back(get_identifier_token(token, error_output, "Keyword")); 4001836352 q clear_until_after(token, error_output, ":", "]"); 4011836352 q result.push_back(get_text_token(token, error_output, "Value")); 4021836352 q if (result.front() == "out") 404516129 q Output_Handler_Parser* format_parser = 405516129 q Output_Handler_Parser::get_format_parser(result.back()); 4100 error_output->add_parse_error("Unknown output format: " + result.back(), token.line_col().first); 414516129 q parsed_query.set_output_handler(format_parser, &token, error_output); 415516129 q if (parsed_query.get_output_handler()) 417516129 q result.push_back("output-config"); 418516129 q result.push_back(parsed_query.get_output_handler()->dump_config()); 422516129 q clear_until_after(token, error_output, "]", true); 4241320298 q else if (result.front() == "diff" || result.front() == "adiff") 42632481 q clear_until_after(token, error_output, ",", "]", false); 4309223 q result.push_back(get_text_token(token, error_output, "Value")); 4319223 q clear_until_after(token, error_output, "]", true); 436996226 q else if (result.front() == "bbox") 4389223 q clear_until_after(token, error_output, ",", "]", false); 4429223 q result.back() += "," + get_text_token(token, error_output, "Value"); 4439223 q clear_until_after(token, error_output, ",", "]", false); 4489223 q result.back() += "," + get_text_token(token, error_output, "Value"); 4499223 q clear_until_after(token, error_output, ",", "]", false); 4549223 q result.back() += "," + get_text_token(token, error_output, "Value"); 4559223 q clear_until_after(token, error_output, "]"); 458904209 q else if (result.front() == "maxsize" || result.front() == "timeout") 46010423 q while (token.good() && *token != "]") 4620 result.back() += (' ' + *token); 46510423 q clear_until_after(token, error_output, "]"); 468800200 q clear_until_after(token, error_output, "]"); 473template< class TStatement > 474TStatement* parse_union(typename TStatement::Factory& stmt_factory, Parsed_Query& parsed_query, 475 Tokenizer_Wrapper& token, Error_Output* error_output, int depth) 477512114 q std::pair< uint, uint > line_col = token.line_col(); 480512114 q std::vector< TStatement* > substatements = 481512114 q collect_substatements_and_probe< TStatement >(stmt_factory, parsed_query, token, error_output, 483512114 q std::string into = probe_into(token, error_output); 484512114 q clear_until_after(token, error_output, ";"); 4888821 q statement = create_difference_statement< TStatement >(stmt_factory, into, line_col.first); 49042496 q statement = create_union_statement< TStatement >(stmt_factory, into, line_col.first); 492512114 q for (typename std::vector< TStatement* >::const_iterator it = substatements.begin(); 4931188114 q it != substatements.end(); ++it) 49467694 q statement->add_statement(*it, ""); 499template< class TStatement > 500TStatement* parse_foreach(typename TStatement::Factory& stmt_factory, Parsed_Query& parsed_query, 501 Tokenizer_Wrapper& token, Error_Output* error_output, int depth) 50311628 q std::pair< uint, uint > line_col = token.line_col(); 50611628 q std::string from = probe_from(token, error_output); 50711628 q std::string into = probe_into(token, error_output); 50811628 q std::vector< TStatement* > substatements = 50911628 q collect_substatements< TStatement >(stmt_factory, parsed_query, token, error_output, depth+1); 51311628 q TStatement* statement = create_for_statement< TStatement > 51411628 q (stmt_factory, "foreach", from, into, line_col.first); 51511628 q for (typename std::vector< TStatement* >::const_iterator it = substatements.begin(); 51629228 q it != substatements.end(); ++it) 51717627 q statement->add_statement(*it, ""); 522struct For_Stmt_Return_Type_Checker : Statement::Return_Type_Checker 5247619 q For_Stmt_Return_Type_Checker() {} 5267619 q virtual bool eval_required() const { return true; } 527 virtual bool matches(Statement::Eval_Return_Type eval_type) const 5287619 q { return eval_type == Statement::string || eval_type == Statement::container; } 529 virtual std::string expectation() const 5300 { return Statement::eval_to_string(Statement::string) + " or " + Statement::eval_to_string(Statement::container); } 534template< class TStatement > 535TStatement* parse_for(typename TStatement::Factory& stmt_factory, Parsed_Query& parsed_query, 536 Tokenizer_Wrapper& token, Error_Output* error_output, int depth) 5387619 q std::pair< uint, uint > line_col = token.line_col(); 5417619 q std::string from = probe_from(token, error_output); 5427619 q std::string into = probe_into(token, error_output); 5447619 q clear_until_after(token, error_output, "("); 5457619 q TStatement* condition = parse_value_tree< TStatement >(stmt_factory, token, error_output, true, 5467619 q Statement::elem_eval_possible, For_Stmt_Return_Type_Checker()); 5477619 q clear_until_after(token, error_output, ")"); 5487619 q std::vector< TStatement* > substatements = 5497619 q collect_substatements< TStatement >(stmt_factory, parsed_query, token, error_output, depth+1); 5537619 q TStatement* statement = create_for_statement< TStatement > 5547619 q (stmt_factory, "for", from, into, line_col.first); 5557619 q statement->add_statement(condition, ""); 5567619 q for (typename std::vector< TStatement* >::const_iterator it = substatements.begin(); 55710019 q it != substatements.end(); ++it) 558246 q statement->add_statement(*it, ""); 563template< class TStatement > 564TStatement* parse_complete(typename TStatement::Factory& stmt_factory, Parsed_Query& parsed_query, 565 Tokenizer_Wrapper& token, Error_Output* error_output, int depth) 56733269 q std::pair< uint, uint > line_col = token.line_col(); 57333269 q maxnum = get_text_token(token, error_output, "maximum loop number"); 57433269 q clear_until_after(token, error_output, ")"); 57633269 q std::string from = probe_from(token, error_output); 57733269 q std::string into = probe_into(token, error_output); 57833269 q std::vector< TStatement* > substatements = 57933269 q collect_substatements< TStatement >(stmt_factory, parsed_query, token, error_output, depth); 58333269 q TStatement* statement = create_complete_statement< TStatement > 58433269 q (stmt_factory, maxnum, from, into, line_col.first); 58533269 q for (typename std::vector< TStatement* >::const_iterator it = substatements.begin(); 58642469 q it != substatements.end(); ++it) 5879214 q statement->add_statement(*it, ""); 592template< class TStatement > 593TStatement* parse_if(typename TStatement::Factory& stmt_factory, Parsed_Query& parsed_query, 594 Tokenizer_Wrapper& token, Error_Output* error_output, int depth) 5967617 q std::pair< uint, uint > line_col = token.line_col(); 5997617 q clear_until_after(token, error_output, "("); 6007617 q TStatement* condition = parse_value_tree< TStatement >(stmt_factory, token, error_output, true, 6017617 q Statement::evaluator_expected, Statement::Single_Return_Type_Checker(Statement::string)); 6027617 q clear_until_after(token, error_output, ")"); 6037617 q std::vector< TStatement* > substatements = 6047617 q collect_substatements< TStatement >(stmt_factory, parsed_query, token, error_output, depth); 6057617 q std::vector< TStatement* > else_statements; 6067617 q if (token.good() && *token == "else") 6095212 q collect_substatements< TStatement >(stmt_factory, parsed_query, token, error_output, depth) 6157617 q TStatement* statement = create_if_statement< TStatement >(stmt_factory, line_col.first); 6177617 q statement->add_statement(condition, ""); 6187617 q for (typename std::vector< TStatement* >::const_iterator it = substatements.begin(); 61910017 q it != substatements.end(); ++it) 620246 q statement->add_statement(*it, ""); 6217617 q if (!else_statements.empty()) 622287 q statement->add_statement(create_else_statement< TStatement >(stmt_factory, line_col.first), ""); 6237617 q for (typename std::vector< TStatement* >::const_iterator it = else_statements.begin(); 62410417 q it != else_statements.end(); ++it) 625287 q statement->add_statement(*it, ""); 630template< class TStatement > 631TStatement* parse_retro(typename TStatement::Factory& stmt_factory, Parsed_Query& parsed_query, 632 Tokenizer_Wrapper& token, Error_Output* error_output, int depth) 6346015 q std::pair< uint, uint > line_col = token.line_col(); 6376015 q clear_until_after(token, error_output, "("); 6386015 q TStatement* condition = parse_value_tree< TStatement >(stmt_factory, token, error_output, true, 6396015 q Statement::evaluator_expected, Statement::Single_Return_Type_Checker(Statement::string)); 6406015 q clear_until_after(token, error_output, ")"); 6416015 q std::vector< TStatement* > substatements = 6426015 q collect_substatements< TStatement >(stmt_factory, parsed_query, token, error_output, depth); 6466015 q TStatement* statement = create_retro_statement< TStatement >(stmt_factory, line_col.first); 6486015 q statement->add_statement(condition, ""); 6496015 q for (typename std::vector< TStatement* >::const_iterator it = substatements.begin(); 65015215 q it != substatements.end(); ++it) 6519214 q statement->add_statement(*it, ""); 656template< class TStatement > 657TStatement* parse_timeline(typename TStatement::Factory& stmt_factory, 658 Tokenizer_Wrapper& token, Error_Output* error_output) 66014837 q std::pair< uint, uint > line_col = token.line_col(); 66314837 q clear_until_after(token, error_output, "("); 66414837 q std::string type = get_text_token(token, error_output, "OSM base type"); 66814837 q clear_until_after(token, error_output, ","); 66914837 q std::string ref = get_text_token(token, error_output, "OSM element id"); 67014837 q clear_until_after(token, error_output, ",", ")", false); 675246 q version = get_text_token(token, error_output, "Number"); 67714837 q clear_until_after(token, error_output, ")"); 67914837 q std::string into = probe_into(token, error_output); 68014837 q clear_until_after(token, error_output, ";"); 68214837 q return create_timeline_statement< TStatement >(stmt_factory, line_col.first, type, ref, version, into); 686template< class TStatement > 687TStatement* parse_compare(typename TStatement::Factory& stmt_factory, Parsed_Query& parsed_query, 688 Tokenizer_Wrapper& token, const std::string& from, Error_Output* error_output, int depth) 6908421 q std::pair< uint, uint > line_col = token.line_col(); 6938421 q TStatement* condition = 0; 6968020 q clear_until_after(token, error_output, "("); 6997218 q clear_until_after(token, error_output, "delta"); 7007218 q clear_until_after(token, error_output, ":"); 7017218 q condition = parse_value_tree< TStatement >(stmt_factory, token, error_output, true, 7027218 q Statement::elem_eval_possible, Statement::Single_Return_Type_Checker(Statement::string)); 7048020 q clear_until_after(token, error_output, ")"); 7068421 q std::string into = probe_into(token, error_output); 7088421 q std::vector< TStatement* > substatements; 7098421 q if (*token == "(" || *token == "{") 7114411 q collect_substatements< TStatement >(stmt_factory, parsed_query, token, error_output, depth) 7174010 q clear_until_after(token, error_output, ";"); 7198421 q TStatement* statement = create_compare_statement< TStatement >(stmt_factory, line_col.first, from, into); 7217218 q statement->add_statement(condition, ""); 7228421 q for (typename std::vector< TStatement* >::const_iterator it = substatements.begin(); 72313221 q it != substatements.end(); ++it) 7244811 q statement->add_statement(*it, ""); 730template< class TStatement > 731TStatement* parse_output(typename TStatement::Factory& stmt_factory, 732 const std::string& from, Tokenizer_Wrapper& token, Error_Output* error_output) 73416032673 q TStatement* statement = 0; 73816032673 q std::string mode = "body"; 74116032673 q std::string geometry = "skeleton"; 74216032673 q std::string show_ids = "yes"; 74737640673 q while (token.good() && *token != ";") 75114824657 q else if (*token == "tags") 75314800657 q else if (*token == "skel") 75514700657 q else if (*token == "body") 75714648657 q else if (*token == "meta") 75914488657 q else if (*token == "quirks") 76114484657 q else if (*token == "count") 7717140652 q else if (*token == "center") 7737072648 q else if (*token == "noids") 7757016648 q else if (isdigit((*token)[0])) 78019248 q south = get_text_token(token, error_output, "Number"); 78119248 q clear_until_after(token, error_output, ","); 78219248 q west = get_text_token(token, error_output, "Number"); 78319248 q clear_until_after(token, error_output, ","); 78419248 q north = get_text_token(token, error_output, "Number"); 78519248 q clear_until_after(token, error_output, ","); 78619248 q east = get_text_token(token, error_output, "Number"); 78719248 q clear_until_after(token, error_output, ")", false); 7920 error_output->add_parse_error 7930 (std::string("Invalid parameter for print: \"") + *token + "\"", token.line_col().first); 80016032673 q statement = create_print_statement< TStatement > 80116032673 q (stmt_factory, from == "" ? "_" : from, mode, order, limit, geometry, show_ids, 8080 error_output->add_parse_error("Garbage after output statement found.", 8090 token.line_col().first); 81216032673 q clear_until_after(token, error_output, ";"); 818struct No_Return_Type_Checker : Statement::Return_Type_Checker 8202216127 q No_Return_Type_Checker() {} 8222200127 q virtual bool eval_required() const { return false; } 8230 virtual bool matches(Statement::Eval_Return_Type eval_type) const { return false; } 8240 virtual std::string expectation() const { return Statement::eval_to_string(Statement::non_evaluator); } 828template< class TStatement > 829TStatement* parse_make(typename TStatement::Factory& stmt_factory, const std::string& from, 830 Tokenizer_Wrapper& token, Error_Output* error_output, const std::string& strategy) 833632127 q std::vector< TStatement* > evaluators; 839632127 q type = get_identifier_token(token, error_output, "Element class name"); 841632127 q Token_Tree tree(token, error_output, false); 844632127 q Statement::QL_Context tree_context = (strategy == "convert" ? Statement::in_convert : Statement::generic); 845632127 q Token_Node_Ptr tree_it(tree, tree.tree[0].rhs); 8472216127 q while (tree_it->token == ",") 8511584115 q TStatement* stmt = stmt_factory.create_evaluator(tree_it.rhs(), tree_context, No_Return_Type_Checker()); 8531568114 q evaluators.push_back(stmt); 859632127 q TStatement* stmt = stmt_factory.create_evaluator(tree_it, tree_context, No_Return_Type_Checker()); 863632127 q std::reverse(evaluators.begin(),evaluators.end()); 866632127 q std::string into = probe_into(token, error_output); 868632127 q statement = create_make_statement< TStatement >(stmt_factory, strategy, from, into, type, token.line_col().first); 870632127 q for (typename std::vector< TStatement* >::const_iterator it = evaluators.begin(); 8712832127 q it != evaluators.end(); ++it) 8722200127 q statement->add_statement(*it, ""); 875632127 q clear_until_after(token, error_output, ";"); 881template< class TStatement > 882TStatement* parse_localize(typename TStatement::Factory& stmt_factory, 883 Tokenizer_Wrapper& token, const std::string& from, Error_Output* error_output) 8858019 q uint line_col = token.line_col().first; 8898019 q if (*token != ";" && *token != "->" && *token != "(") 8948019 q std::string south, north, west, east; 8980 south = get_text_token(token, error_output, "Value"); 8990 clear_until_after(token, error_output, ",", ")", false); 9030 west = get_text_token(token, error_output, "Value"); 9040 clear_until_after(token, error_output, ",", ")", false); 9090 north = get_text_token(token, error_output, "Value"); 9100 clear_until_after(token, error_output, ",", ")", false); 9150 east = get_text_token(token, error_output, "Value"); 9160 clear_until_after(token, error_output, ",", ")", false); 9218019 q std::string into = probe_into(token, error_output); 9228019 q clear_until_after(token, error_output, ";"); 9248019 q return create_localize_statement< TStatement >(stmt_factory, type, from, into, 9258019 q south, north, west, east, line_col); 931 Statement_Text(std::string statement_ = "", 932 std::pair< uint, uint > line_col_ = std::make_pair(0, 0)) 9332284374 q : statement(statement_), line_col(line_col_) {} 935 std::string statement; 936 std::pair< uint, uint > line_col; 937 std::vector< std::string > attributes; 940template< class TStatement > 941TStatement* create_query_substatement 942 (typename TStatement::Factory& stmt_factory, 943 Tokenizer_Wrapper& token, Error_Output* error_output, 944 const Statement_Text& clause, std::string type, std::string from, std::string into) 9462284374 q if (clause.statement == "has-kv") 9471600298 q return create_has_kv_statement< TStatement > 9481600298 q (stmt_factory, clause.attributes[0], clause.attributes[1], haskv_plain, haskv_plain, 9491600298 q (clause.attributes[2] == ""), clause.line_col.first); 950684138 q else if (clause.statement == "has-kv_regex") 95146095 q return create_has_kv_statement< TStatement > 95246095 q (stmt_factory, clause.attributes[0], clause.attributes[1], haskv_regex, haskv_plain, 95346095 q (clause.attributes[2] == ""), clause.line_col.first); 95422450 q else if (clause.statement == "has-kv_icase") 955205 q return create_has_kv_statement< TStatement > 956205 q (stmt_factory, clause.attributes[0], clause.attributes[1], haskv_icase, haskv_plain, 957205 q (clause.attributes[2] == ""), clause.line_col.first); 95820445 q else if (clause.statement == "has-kv_keyregex_icase") 95941 q return create_has_kv_statement< TStatement > 96041 q (stmt_factory, clause.attributes[0], clause.attributes[1], haskv_icase, haskv_regex, 96141 q (clause.attributes[2] == ""), clause.line_col.first); 96220044 q else if (clause.statement == "has-kv_keyregex") 96320044 q return create_has_kv_statement< TStatement > 96420044 q (stmt_factory, clause.attributes[0], clause.attributes[1], haskv_regex, haskv_regex, 96520044 q (clause.attributes[2] == ""), clause.line_col.first); 9660 else if (clause.statement == "item") 9670 return create_item_statement< TStatement > 9680 (stmt_factory, clause.attributes[0], "_", clause.line_col.first); 972template< class TStatement > 973TStatement* parse_full_recurse(typename TStatement::Factory& stmt_factory, 974 Tokenizer_Wrapper& token, const std::string& from, Error_Output* error_output) 97637687 q std::string type = *token; 97737687 q uint line_col = token.line_col().first; 97937687 q std::string into = probe_into(token, error_output); 98037687 q clear_until_after(token, error_output, ";"); 98313632 q return create_recurse_statement< TStatement >(stmt_factory, "down", from, into, line_col); 9856817 q return create_recurse_statement< TStatement >(stmt_factory, "down-rel", from, into, line_col); 98710025 q return create_recurse_statement< TStatement >(stmt_factory, "up", from, into, line_col); 9897218 q return create_recurse_statement< TStatement >(stmt_factory, "up-rel", from, into, line_col); 994template< class TStatement > 995TStatement* parse_coord_query(typename TStatement::Factory& stmt_factory, 996 Tokenizer_Wrapper& token, const std::string& from, Error_Output* error_output) 99820050 q std::string type = *token; 99920050 q uint line_col = token.line_col().first; 10068421 q lat = get_text_token(token, error_output, "Number"); 10078421 q clear_until_after(token, error_output, ",", ")", false); 10118421 q lon = get_text_token(token, error_output, "Number"); 10128421 q clear_until_after(token, error_output, ")", false); 101620050 q std::string into = probe_into(token, error_output); 101720050 q clear_until_after(token, error_output, ";"); 101920050 q return create_coord_query_statement< TStatement >(stmt_factory, lat, lon, from, into, line_col); 1022template< class TStatement > 1023TStatement* parse_map_to_area(typename TStatement::Factory& stmt_factory, 1024 Tokenizer_Wrapper& token, const std::string& from, Error_Output* error_output) 10277218 q uint line_col = token.line_col().first; 10307218 q std::string into = probe_into(token, error_output); 10317218 q clear_until_after(token, error_output, ";"); 10337218 q return create_map_to_area_statement< TStatement >(stmt_factory, from, into, line_col); 1037template< class TStatement > 1038TStatement* parse_query(typename TStatement::Factory& stmt_factory, Parsed_Query& parsed_query, 1039 const std::string& type, const std::string& from, Tokenizer_Wrapper& token, 1040 Error_Output* error_output) 10425060673 q std::pair< uint, uint > query_line_col = token.line_col(); 10445060673 q std::vector< Statement_Text > clauses; 10455060673 q std::vector< Token_Tree > subtrees; 104610996673 q while (token.good() && (*token == "[" || *token == "(" || *token == ".")) 10522288374 q bool key_regex = (*token == "~"); 10562288374 q if (*token == "!") // [!key] as shortcut for [key !~ ".*"] 105910024 q std::string key = get_text_token(token, error_output, "Key"); 106010024 q clear_until_after(token, error_output, "]"); 106110024 q Statement_Text clause("has-kv_regex", token.line_col()); 106210024 q clause.attributes.push_back(key); 106310024 q clause.attributes.push_back(".*"); 106410024 q clause.attributes.push_back("!"); 10692188362 q std::string key = get_text_token(token, error_output, "Key"); 10702188362 q clear_until_after(token, error_output, "!", "~", "=", "!=", "]", false); 107715237 q clear_until_after(token, error_output, "~", "=", "]", false); 108227256 q if (key_regex && error_output) 10830 error_output->add_parse_error( 10840 "A regular expression for a key can only be combined with a regular expression as value criterion", 10850 token.line_col().first); 108727256 q Statement_Text clause("has-kv", token.line_col()); 108827256 q clause.attributes.push_back(key); 108927256 q clause.attributes.push_back(""); 109027256 q clause.attributes.push_back(straight ? "" : "!"); 10941916332 q else if (*token == "=" || *token == "!=") 10970 error_output->add_parse_error( 10980 "A regular expression for a key can only be combined with a regular expression as value criterion", 10990 token.line_col().first); 11031332261 q if (token.good() && *token == "]") 11071328260 q Statement_Text clause("has-kv", token.line_col()); 11081328260 q clause.attributes.push_back(key); 11091328260 q clause.attributes.push_back(get_text_token(token, error_output, "Value")); 11101328260 q if (clause.attributes.back() != "") 11121328260 q clause.attributes.push_back(straight ? "" : "!"); 11151328260 q clear_until_after(token, error_output, "]"); 112320445 q Statement_Text clause("has-kv_keyregex", token.line_col()); 112420445 q clause.attributes.push_back(key); 112520445 q clause.attributes.push_back(get_text_token(token, error_output, "Value")); 112620445 q clause.attributes.push_back(straight ? "" : "!"); 112720445 q clear_until_after(token, error_output, ",", "]", false); 113041 q clause.statement = "has-kv_keyregex_icase"; 113241 q clear_until_after(token, error_output, "i"); 113341 q clear_until_after(token, error_output, "]", false); 114138079 q Statement_Text clause("has-kv_regex", token.line_col()); 114238079 q clause.attributes.push_back(key); 114338079 q clause.attributes.push_back(get_text_token(token, error_output, "Value")); 114438079 q clause.attributes.push_back(straight ? "" : "!"); 114538079 q clear_until_after(token, error_output, ",", "]", false); 1148205 q clause.statement = "has-kv_icase"; 1150205 q clear_until_after(token, error_output, "i"); 1151205 q clear_until_after(token, error_output, "]", false); 11640 error_output->add_parse_error("':' or '.' expected.", token.line_col().first); 11683648570 q subtrees.push_back(Token_Tree(token, error_output, true)); 11693648570 q clear_until_after(token, error_output, ")"); 11730 Statement_Text clause("item", token.line_col()); 11740 clause.attributes.push_back(probe_from(token, error_output)); 11750 clauses.push_back(clause); 11795060673 q std::string into = probe_into(token, error_output); 11825060673 q if (clauses.empty() && subtrees.empty()) 11867619 q if (parsed_query.get_global_bbox_limitation().valid()) 11881717 q statement = create_query_statement< TStatement > 11891717 q (stmt_factory, type, into, query_line_col.first); 11925919 q error_output->add_parse_error("An empty query is not allowed", token.line_col().first); 119744072 q statement = create_item_statement< TStatement >(stmt_factory, from, into, query_line_col.first); 120019246 q statement = create_query_statement< TStatement > 120119246 q (stmt_factory, type, into, query_line_col.first); 120219246 q TStatement* substatement = create_item_statement< TStatement >(stmt_factory, from, "_", query_line_col.first); 120319246 q statement->add_statement(substatement, ""); 12074352666 q else if (clauses.size() == 1 && from == "" && subtrees.empty()) 1209772174 q if (clauses.front().statement == "has-kv" 1210772174 q || clauses.front().statement == "has-kv_regex" 1211772174 q || clauses.front().statement == "has-kv_keyregex" 1212772174 q || clauses.front().statement == "has-kv_icase" 1213772174 q || clauses.front().statement == "has-kv_keyregex_icase") 1215772174 q statement = create_query_statement< TStatement > 1216772174 q (stmt_factory, type, into, query_line_col.first); 1217772174 q TStatement* substatement = create_query_substatement< TStatement > 1218772174 q (stmt_factory, token, error_output, clauses.front(), type, from, "_"); 1220772174 q statement->add_statement(substatement, ""); 12240 statement = create_query_substatement< TStatement > 12250 (stmt_factory, token, error_output, clauses.front(), type, from, into); 12283580593 q else if (clauses.empty() && from == "" && subtrees.size() == 1) 12311996400 q TStatement* filter = stmt_factory.create_criterion( 12321996400 q Token_Node_Ptr(subtrees[0], subtrees[0].tree[0].rhs), type, can_standalone, into); 1239844191 q statement = create_query_statement< TStatement >(stmt_factory, type, into, query_line_col.first); 1240844191 q statement->add_statement(filter, ""); 12461584336 q statement = create_query_statement< TStatement >(stmt_factory, type, into, query_line_col.first); 125223654 q TStatement* substatement = create_item_statement< TStatement > 125323654 q (stmt_factory, from, "_", query_line_col.first); 125523654 q statement->add_statement(substatement, ""); 12581584336 q for (std::vector< Statement_Text >::const_iterator it = clauses.begin(); 12611512246 q TStatement* substatement = create_query_substatement< TStatement > 12621512246 q (stmt_factory, token, error_output, *it, type, from, "_"); 12641512246 q statement->add_statement(substatement, ""); 12663236336 q for (typename std::vector< Token_Tree >::const_iterator it = subtrees.begin(); it != subtrees.end(); ++it) 12691652290 q TStatement* filter = stmt_factory.create_criterion( 12701652290 q Token_Node_Ptr(*it, it->tree[0].rhs), type, can_standalone, "_"); 12721644288 q statement->add_statement(filter, ""); 12755060673 q clear_until_after(token, error_output, ";"); 1281template< class TStatement > 1282TStatement* parse_statement(typename TStatement::Factory& stmt_factory, Parsed_Query& parsed_query, 1283 Tokenizer_Wrapper& token, Error_Output* error_output, int depth) 12910 error_output->add_parse_error("Nesting of statements is limited to 1023 levels", token.line_col().first); 1296512114 q return parse_union< TStatement >(stmt_factory, parsed_query, token, error_output, depth); 12987619 q return parse_for< TStatement >(stmt_factory, parsed_query, token, error_output, depth); 130011628 q return parse_foreach< TStatement >(stmt_factory, parsed_query, token, error_output, depth); 130123152681 q else if (*token == "complete") 130233269 q return parse_complete< TStatement >(stmt_factory, parsed_query, token, error_output, depth); 13047617 q return parse_if< TStatement >(stmt_factory, parsed_query, token, error_output, depth); 13066015 q return parse_retro< TStatement >(stmt_factory, parsed_query, token, error_output, depth); 130722684681 q else if (*token == "timeline") 130814837 q return parse_timeline< TStatement >(stmt_factory, token, error_output); 131122536681 q if (token.good() && *token == ".") 132122536681 q if (token.good() && *token == "out") 132216032673 q return parse_output< TStatement >(stmt_factory, from, token, error_output); 13236504677 q else if (token.good() && *token == "convert") 132434079 q return parse_make< TStatement >(stmt_factory, from, token, error_output, "convert"); 13256164677 q else if (token.good() && *token == "make") 132629263 q return parse_make< TStatement >(stmt_factory, from, token, error_output, "make"); 13275872676 q else if (token.good() && (*token == "<" || *token == "<<" || *token == ">" || *token == ">>")) 132837687 q return parse_full_recurse< TStatement >(stmt_factory, token, from, error_output); 13295496676 q else if (token.good() && *token == "is_in") 133020050 q return parse_coord_query< TStatement >(stmt_factory, token, from, error_output); 13315296673 q else if (token.good() && *token == "map_to_area") 13327218 q return parse_map_to_area< TStatement >(stmt_factory, token, from, error_output); 13335224673 q else if (token.good() && *token == "local") 13348019 q return parse_localize< TStatement >(stmt_factory, token, from, error_output); 13355144673 q else if (token.good() && *token == "compare") 13368421 q return parse_compare< TStatement >(stmt_factory, parsed_query, token, from, error_output, depth); 13395060673 q if (*token != "out" && from == "") 13443692591 q else if (type != "node" && type != "way" && type != "relation" 13453692591 q && type != "nwr" && type != "nw" && type != "wr" && type != "nr" 13463692591 q && type != "derived" && type != "area") 134941 q error_output->add_parse_error("Unknown type \"" + type + "\"", token.line_col().first); 13535060673 q if (token.good() && *token == ".") 13635060673 q return parse_query< TStatement >(stmt_factory, parsed_query, type, from, token, error_output); 1367template< class TStatement > 1368void generic_parse_and_validate_map_ql 1369 (typename TStatement::Factory& stmt_factory, 1370 const std::string& xml_raw, Error_Output* error_output, std::vector< TStatement* >& stmt_seq, Parsed_Query& parsed_query) 13722736684 q std::istringstream in(xml_raw); 13752736684 q std::map< std::string, std::string > attr; 13764572684 q while (token.good() && *token == "[") 13781836352 q std::vector< std::string > kv = parse_setup(token, error_output, parsed_query); 13851308298 q else if (kv[0] == "diff" || kv[0] == "adiff") 138813233 q attr["augmented"] = "deletions"; 13982736684 q TStatement* base_statement = stmt_factory.create_statement 13992736684 q ("osm-script", token.line_col().first, attr); 14021408352 q clear_until_after(token, error_output, ";"); 140622696683 q std::pair< uint, uint > pos_before = token.line_col(); 140722696683 q TStatement* statement = parse_statement< TStatement >(stmt_factory, parsed_query, token, error_output, 0); 140922633682 q base_statement->add_statement(statement, ""); 14106320 q else if (token.line_col() == pos_before) 14142736684 q stmt_seq.push_back(base_statement); 1418void parse_and_validate_map_ql 1419 (Statement::Factory& stmt_factory, const std::string& xml_raw, Error_Output* error_output, Parsed_Query& parsed_query) 1421684684 q generic_parse_and_validate_map_ql< Statement > 1422684684 q (stmt_factory, xml_raw, error_output, *get_statement_stack(), parsed_query); 1425void parse_and_dump_xml_from_map_ql 1426 (Statement::Factory& stmt_factory_, const std::string& xml_raw, Error_Output* error_output, Parsed_Query& parsed_query) 1428684684 q Statement_Dump::Factory stmt_factory(stmt_factory_); 1429684684 q std::vector< Statement_Dump* > stmt_seq; 1430684684 q generic_parse_and_validate_map_ql< Statement_Dump >(stmt_factory, xml_raw, error_output, stmt_seq, parsed_query); 1431684684 q for (std::vector< Statement_Dump* >::const_iterator it = stmt_seq.begin(); 1434684684 q for (std::vector< Statement_Dump* >::iterator it = stmt_seq.begin(); 1439void parse_and_dump_compact_from_map_ql 1440 (Statement::Factory& stmt_factory_, const std::string& xml_raw, Error_Output* error_output, Parsed_Query& parsed_query) 1442684684 q Statement_Dump::Factory stmt_factory(stmt_factory_); 1443684684 q std::vector< Statement_Dump* > stmt_seq; 1444684684 q generic_parse_and_validate_map_ql< Statement_Dump >(stmt_factory, xml_raw, error_output, stmt_seq, parsed_query); 1445684684 q for (std::vector< Statement_Dump* >::const_iterator it = stmt_seq.begin(); 1447684684 q std::cout<<(*it)->dump_compact_map_ql(stmt_factory_)<<'\n'; 1448684684 q for (std::vector< Statement_Dump* >::iterator it = stmt_seq.begin(); 1453void parse_and_dump_bbox_from_map_ql 1454 (Statement::Factory& stmt_factory_, const std::string& xml_raw, Error_Output* error_output, Parsed_Query& parsed_query) 14560 Statement_Dump::Factory stmt_factory(stmt_factory_); 14570 std::vector< Statement_Dump* > stmt_seq; 14580 generic_parse_and_validate_map_ql< Statement_Dump >(stmt_factory, xml_raw, error_output, stmt_seq, parsed_query); 14590 for (std::vector< Statement_Dump* >::const_iterator it = stmt_seq.begin(); 14600 it != stmt_seq.end(); ++it) 14610 std::cout<<(*it)->dump_bbox_map_ql(stmt_factory_)<<'\n'; 14620 for (std::vector< Statement_Dump* >::iterator it = stmt_seq.begin(); 14630 it != stmt_seq.end(); ++it) 1467void parse_and_dump_pretty_from_map_ql 1468 (Statement::Factory& stmt_factory_, const std::string& xml_raw, Error_Output* error_output, Parsed_Query& parsed_query) 1470684684 q Statement_Dump::Factory stmt_factory(stmt_factory_); 1471684684 q std::vector< Statement_Dump* > stmt_seq; 1472684684 q generic_parse_and_validate_map_ql< Statement_Dump >(stmt_factory, xml_raw, error_output, stmt_seq, parsed_query); 1473684684 q for (std::vector< Statement_Dump* >::const_iterator it = stmt_seq.begin(); 1475684684 q std::cout<<(*it)->dump_pretty_map_ql(stmt_factory_); 1476684684 q for (std::vector< Statement_Dump* >::iterator it = stmt_seq.begin();