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) 486744602 q std::string into = "_"; 496744602 q if (token.good() && *token == "->") 525992578 q clear_until_after(token, error_output, "."); 545992578 q into = get_identifier_token(token, error_output, "Variable"); 59std::string probe_from(Tokenizer_Wrapper& token, Error_Output* error_output) 62468106 q if (token.good() && *token == ".") 66452102 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) 75636135 q std::vector< TStatement* > substatements; 76636135 q clear_until_after(token, error_output, "(", "{", false); 780 return substatements; 79636135 q std::string closing_token = (*token == "(" ? ")" : "}"); 811016135 q while (token.good() && *token != closing_token) 8338069 q std::pair< uint, uint > pos_before = token.line_col(); 8438069 q TStatement* substatement = parse_statement< TStatement > 8538069 q (stmt_factory, parsed_query, token, error_output, depth+1); 8738069 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) 10442092 q std::vector< TStatement* > substatements; 10542092 q clear_until_after(token, error_output, "("); 10642092 q if (token.good() && *token != ")") 10830069 q TStatement* substatement = parse_statement< TStatement > 10930069 q (stmt_factory, parsed_query, token, error_output, depth+1); 11130069 q substatements.push_back(substatement); 11842092 q if (token.good() && *token != ")") 12016840 q TStatement* substatement = parse_statement< TStatement > 12116840 q (stmt_factory, parsed_query, token, error_output, depth+1); 12316840 q substatements.push_back(substatement); 12416840 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); 13145292 q while (token.good() && *token != ")") 133323 q std::pair< uint, uint > pos_before = token.line_col(); 134323 q TStatement* substatement = parse_statement< TStatement > 135323 q (stmt_factory, parsed_query, token, error_output, depth+1); 137323 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) 15624859 q Token_Tree tree(token, error_output, parenthesis_expected); 16024859 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) 16932873 q std::map< std::string, std::string > attr; 17132873 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) 1789222 q std::map< std::string, std::string > attr; 1809222 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) 18720852 q std::map< std::string, std::string > attr; 19020852 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) 198576118 q std::map< std::string, std::string > attr; 203576118 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) 21126057 q std::map< std::string, std::string > attr; 21226057 q attr["maxnum"] = maxnum; 21526057 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) 2224410 q std::map< std::string, std::string > attr; 2234410 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) 230123 q std::map< std::string, std::string > attr; 231123 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, 24914308591 q std::map< std::string, std::string > attr; 25414308591 q attr["geometry"] = geometry; 26014308591 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) 26811629 q std::map< std::string, std::string > attr; 27111629 q attr["version"] = version; 27311629 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) 2817218 q std::map< std::string, std::string > attr; 2827218 q attr["from"] = (from == "" ? "_" : from); 2847218 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) 2923064520 q std::map< std::string, std::string > attr; 2953064520 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) 3062108331 q std::map< std::string, std::string > attr; 3082108331 q if (key_regex == haskv_plain) 315620126 q else if (regex == haskv_regex) 319246 q attr["regv"] = value; 320246 q attr["case"] = "ignore"; 3232108331 q attr["modv"] = (straight ? "" : "not"); 3242108331 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) 332684125 q std::map< std::string, std::string > attr; 335684125 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) 34335682 q std::map< std::string, std::string > attr; 34435682 q attr["from"] = (from == "" ? "_" : from); 34735682 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) 35516841 q std::map< std::string, std::string > attr; 35616841 q attr["from"] = (from == "" ? "_" : from); 36016841 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) 3686014 q std::map< std::string, std::string > attr; 3696014 q attr["from"] = (from == "" ? "_" : from); 3716014 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) 3808020 q std::map< std::string, std::string > attr; 3818020 q attr["from"] = (from == "" ? "_" : from); 3888020 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) 3981552311 q std::vector< std::string > result; 3991552311 q result.push_back(get_identifier_token(token, error_output, "Keyword")); 4001552311 q clear_until_after(token, error_output, ":", "]"); 4011552311 q result.push_back(get_text_token(token, error_output, "Value")); 4021552311 q if (result.front() == "out") 404448112 q Output_Handler_Parser* format_parser = 405448112 q Output_Handler_Parser::get_format_parser(result.back()); 4100 error_output->add_parse_error("Unknown output format: " + result.back(), token.line_col().first); 414448112 q parsed_query.set_output_handler(format_parser, &token, error_output); 415448112 q if (parsed_query.get_output_handler()) 417448112 q result.push_back("output-config"); 418448112 q result.push_back(parsed_query.get_output_handler()->dump_config()); 422448112 q clear_until_after(token, error_output, "]", true); 4241104259 q else if (result.front() == "diff" || result.front() == "adiff") 42630476 q clear_until_after(token, error_output, ",", "]", false); 4308020 q result.push_back(get_text_token(token, error_output, "Value")); 4318020 q clear_until_after(token, error_output, "]", true); 436800187 q else if (result.front() == "bbox") 4388020 q clear_until_after(token, error_output, ",", "]", false); 4428020 q result.back() += "," + get_text_token(token, error_output, "Value"); 4438020 q clear_until_after(token, error_output, ",", "]", false); 4488020 q result.back() += "," + get_text_token(token, error_output, "Value"); 4498020 q clear_until_after(token, error_output, ",", "]", false); 4548020 q result.back() += "," + get_text_token(token, error_output, "Value"); 4558020 q clear_until_after(token, error_output, "]"); 458720173 q else if (result.front() == "maxsize" || result.front() == "timeout") 460367 q while (token.good() && *token != "]") 4620 result.back() += (' ' + *token); 465367 q clear_until_after(token, error_output, "]"); 468684171 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) 47742092 q std::pair< uint, uint > line_col = token.line_col(); 47942092 q bool is_difference = false; 48042092 q std::vector< TStatement* > substatements = 48142092 q collect_substatements_and_probe< TStatement >(stmt_factory, parsed_query, token, error_output, 48242092 q is_difference, depth+1); 48342092 q std::string into = probe_into(token, error_output); 48442092 q clear_until_after(token, error_output, ";"); 48642092 q TStatement* statement = 0; 4889222 q statement = create_difference_statement< TStatement >(stmt_factory, into, line_col.first); 49032873 q statement = create_union_statement< TStatement >(stmt_factory, into, line_col.first); 49242092 q for (typename std::vector< TStatement* >::const_iterator it = substatements.begin(); 49392092 q it != substatements.end(); ++it) 49450069 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) 50312832 q std::pair< uint, uint > line_col = token.line_col(); 50612832 q std::string from = probe_from(token, error_output); 50712832 q std::string into = probe_into(token, error_output); 50812832 q std::vector< TStatement* > substatements = 50912832 q collect_substatements< TStatement >(stmt_factory, parsed_query, token, error_output, depth+1); 51312832 q TStatement* statement = create_for_statement< TStatement > 51412832 q (stmt_factory, "foreach", from, into, line_col.first); 51512832 q for (typename std::vector< TStatement* >::const_iterator it = substatements.begin(); 51627232 q it != substatements.end(); ++it) 51714426 q statement->add_statement(*it, ""); 522struct For_Stmt_Return_Type_Checker : Statement::Return_Type_Checker 5248020 q For_Stmt_Return_Type_Checker() {} 5268020 q virtual bool eval_required() const { return true; } 527 virtual bool matches(Statement::Eval_Return_Type eval_type) const 5288020 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) 5388020 q std::pair< uint, uint > line_col = token.line_col(); 5418020 q std::string from = probe_from(token, error_output); 5428020 q std::string into = probe_into(token, error_output); 5448020 q clear_until_after(token, error_output, "("); 5458020 q TStatement* condition = parse_value_tree< TStatement >(stmt_factory, token, error_output, true, 5468020 q Statement::elem_eval_possible, For_Stmt_Return_Type_Checker()); 5478020 q clear_until_after(token, error_output, ")"); 5488020 q std::vector< TStatement* > substatements = 5498020 q collect_substatements< TStatement >(stmt_factory, parsed_query, token, error_output, depth+1); 5538020 q TStatement* statement = create_for_statement< TStatement > 5548020 q (stmt_factory, "for", from, into, line_col.first); 5558020 q statement->add_statement(condition, ""); 5568020 q for (typename std::vector< TStatement* >::const_iterator it = substatements.begin(); 55710020 q it != substatements.end(); ++it) 558205 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) 56726057 q std::pair< uint, uint > line_col = token.line_col(); 57326057 q maxnum = get_text_token(token, error_output, "maximum loop number"); 57426057 q clear_until_after(token, error_output, ")"); 57626057 q std::string from = probe_from(token, error_output); 57726057 q std::string into = probe_into(token, error_output); 57826057 q std::vector< TStatement* > substatements = 57926057 q collect_substatements< TStatement >(stmt_factory, parsed_query, token, error_output, depth); 58326057 q TStatement* statement = create_complete_statement< TStatement > 58426057 q (stmt_factory, maxnum, from, into, line_col.first); 58526057 q for (typename std::vector< TStatement* >::const_iterator it = substatements.begin(); 58631257 q it != substatements.end(); ++it) 587529 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) 5964410 q std::pair< uint, uint > line_col = token.line_col(); 5994410 q clear_until_after(token, error_output, "("); 6004410 q TStatement* condition = parse_value_tree< TStatement >(stmt_factory, token, error_output, true, 6014410 q Statement::evaluator_expected, Statement::Single_Return_Type_Checker(Statement::string)); 6024410 q clear_until_after(token, error_output, ")"); 6034410 q std::vector< TStatement* > substatements = 6044410 q collect_substatements< TStatement >(stmt_factory, parsed_query, token, error_output, depth); 6054410 q std::vector< TStatement* > else_statements; 6064410 q if (token.good() && *token == "else") 609287 q collect_substatements< TStatement >(stmt_factory, parsed_query, token, error_output, depth) 610287 q .swap(else_statements); 6154410 q TStatement* statement = create_if_statement< TStatement >(stmt_factory, line_col.first); 6174410 q statement->add_statement(condition, ""); 6184410 q for (typename std::vector< TStatement* >::const_iterator it = substatements.begin(); 6196410 q it != substatements.end(); ++it) 620204 q statement->add_statement(*it, ""); 6214410 q if (!else_statements.empty()) 622123 q statement->add_statement(create_else_statement< TStatement >(stmt_factory, line_col.first), ""); 6234410 q for (typename std::vector< TStatement* >::const_iterator it = else_statements.begin(); 6245610 q it != else_statements.end(); ++it) 625123 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) 6519215 q statement->add_statement(*it, ""); 656template< class TStatement > 657TStatement* parse_timeline(typename TStatement::Factory& stmt_factory, 658 Tokenizer_Wrapper& token, Error_Output* error_output) 66011629 q std::pair< uint, uint > line_col = token.line_col(); 66311629 q clear_until_after(token, error_output, "("); 66411629 q std::string type = get_text_token(token, error_output, "OSM base type"); 66811629 q clear_until_after(token, error_output, ","); 66911629 q std::string ref = get_text_token(token, error_output, "OSM element id"); 67011629 q clear_until_after(token, error_output, ",", ")", false); 675205 q version = get_text_token(token, error_output, "Number"); 67711629 q clear_until_after(token, error_output, ")"); 67911629 q std::string into = probe_into(token, error_output); 68011629 q clear_until_after(token, error_output, ";"); 68211629 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) 6907218 q std::pair< uint, uint > line_col = token.line_col(); 6937218 q TStatement* condition = 0; 6966416 q clear_until_after(token, error_output, "("); 6996416 q clear_until_after(token, error_output, "delta"); 7006416 q clear_until_after(token, error_output, ":"); 7016416 q condition = parse_value_tree< TStatement >(stmt_factory, token, error_output, true, 7026416 q Statement::elem_eval_possible, Statement::Single_Return_Type_Checker(Statement::string)); 7046416 q clear_until_after(token, error_output, ")"); 7067218 q std::string into = probe_into(token, error_output); 7087218 q std::vector< TStatement* > substatements; 7097218 q if (*token == "(" || *token == "{") 711369 q collect_substatements< TStatement >(stmt_factory, parsed_query, token, error_output, depth) 712369 q .swap(substatements); 717369 q clear_until_after(token, error_output, ";"); 7197218 q TStatement* statement = create_compare_statement< TStatement >(stmt_factory, line_col.first, from, into); 7216416 q statement->add_statement(condition, ""); 7227218 q for (typename std::vector< TStatement* >::const_iterator it = substatements.begin(); 72311218 q it != substatements.end(); ++it) 724409 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) 73414308591 q TStatement* statement = 0; 73814308591 q std::string mode = "body"; 74114308591 q std::string geometry = "skeleton"; 74214308591 q std::string show_ids = "yes"; 74733364591 q while (token.good() && *token != ";") 75113004583 q else if (*token == "tags") 75312988583 q else if (*token == "skel") 75512908583 q else if (*token == "body") 75712892583 q else if (*token == "meta") 75912796583 q else if (*token == "quirks") 76112792583 q else if (*token == "count") 7716312580 q else if (*token == "center") 7736264578 q else if (*token == "noids") 7756224578 q else if (isdigit((*token)[0])) 78014435 q south = get_text_token(token, error_output, "Number"); 78114435 q clear_until_after(token, error_output, ","); 78214435 q west = get_text_token(token, error_output, "Number"); 78314435 q clear_until_after(token, error_output, ","); 78414435 q north = get_text_token(token, error_output, "Number"); 78514435 q clear_until_after(token, error_output, ","); 78614435 q east = get_text_token(token, error_output, "Number"); 78714435 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); 80014308591 q statement = create_print_statement< TStatement > 80114308591 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); 81214308591 q clear_until_after(token, error_output, ";"); 818struct No_Return_Type_Checker : Statement::Return_Type_Checker 8201944118 q No_Return_Type_Checker() {} 8221924118 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) 833576118 q std::vector< TStatement* > evaluators; 839576118 q type = get_identifier_token(token, error_output, "Element class name"); 841576118 q Token_Tree tree(token, error_output, false); 844576118 q Statement::QL_Context tree_context = (strategy == "convert" ? Statement::in_convert : Statement::generic); 845576118 q Token_Node_Ptr tree_it(tree, tree.tree[0].rhs); 8471944118 q while (tree_it->token == ",") 8511368106 q TStatement* stmt = stmt_factory.create_evaluator(tree_it.rhs(), tree_context, No_Return_Type_Checker()); 8531348105 q evaluators.push_back(stmt); 859576118 q TStatement* stmt = stmt_factory.create_evaluator(tree_it, tree_context, No_Return_Type_Checker()); 863576118 q std::reverse(evaluators.begin(),evaluators.end()); 866576118 q std::string into = probe_into(token, error_output); 868576118 q statement = create_make_statement< TStatement >(stmt_factory, strategy, from, into, type, token.line_col().first); 870576118 q for (typename std::vector< TStatement* >::const_iterator it = evaluators.begin(); 8712500118 q it != evaluators.end(); ++it) 8721924118 q statement->add_statement(*it, ""); 875576118 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) 8858020 q uint line_col = token.line_col().first; 8898020 q if (*token != ";" && *token != "->" && *token != "(") 8948020 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); 9218020 q std::string into = probe_into(token, error_output); 9228020 q clear_until_after(token, error_output, ";"); 9248020 q return create_localize_statement< TStatement >(stmt_factory, type, from, into, 9258020 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)) 9332112331 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) 9462108331 q if (clause.statement == "has-kv") 9471488265 q return create_has_kv_statement< TStatement > 9481488265 q (stmt_factory, clause.attributes[0], clause.attributes[1], haskv_plain, haskv_plain, 9491488265 q (clause.attributes[2] == ""), clause.line_col.first); 950620126 q else if (clause.statement == "has-kv_regex") 95140888 q return create_has_kv_statement< TStatement > 95240888 q (stmt_factory, clause.attributes[0], clause.attributes[1], haskv_regex, haskv_plain, 95340888 q (clause.attributes[2] == ""), clause.line_col.first); 95421247 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); 95819242 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); 96218841 q else if (clause.statement == "has-kv_keyregex") 96318841 q return create_has_kv_statement< TStatement > 96418841 q (stmt_factory, clause.attributes[0], clause.attributes[1], haskv_regex, haskv_regex, 96518841 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) 97635682 q std::string type = *token; 97735682 q uint line_col = token.line_col().first; 97935682 q std::string into = probe_into(token, error_output); 98035682 q clear_until_after(token, error_output, ";"); 9838020 q return create_recurse_statement< TStatement >(stmt_factory, "down", from, into, line_col); 9856416 q return create_recurse_statement< TStatement >(stmt_factory, "down-rel", from, into, line_col); 98710827 q return create_recurse_statement< TStatement >(stmt_factory, "up", from, into, line_col); 98910426 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) 99816841 q std::string type = *token; 99916841 q uint line_col = token.line_col().first; 10067218 q lat = get_text_token(token, error_output, "Number"); 10077218 q clear_until_after(token, error_output, ",", ")", false); 10117218 q lon = get_text_token(token, error_output, "Number"); 10127218 q clear_until_after(token, error_output, ")", false); 101616841 q std::string into = probe_into(token, error_output); 101716841 q clear_until_after(token, error_output, ";"); 101916841 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) 10276014 q uint line_col = token.line_col().first; 10306014 q std::string into = probe_into(token, error_output); 10316014 q clear_until_after(token, error_output, ";"); 10336014 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) 10424428593 q std::pair< uint, uint > query_line_col = token.line_col(); 10444428593 q std::vector< Statement_Text > clauses; 10454428593 q std::vector< Token_Tree > subtrees; 10469628593 q while (token.good() && (*token == "[" || *token == "(" || *token == ".")) 10522116331 q bool key_regex = (*token == "~"); 10562116331 q if (*token == "!") // [!key] as shortcut for [key !~ ".*"] 105910425 q std::string key = get_text_token(token, error_output, "Key"); 106010425 q clear_until_after(token, error_output, "]"); 106110425 q Statement_Text clause("has-kv_regex", token.line_col()); 106210425 q clause.attributes.push_back(key); 106310425 q clause.attributes.push_back(".*"); 106410425 q clause.attributes.push_back("!"); 10692012324 q std::string key = get_text_token(token, error_output, "Key"); 10702012324 q clear_until_after(token, error_output, "!", "~", "=", "!=", "]", false); 107713630 q clear_until_after(token, error_output, "~", "=", "]", false); 108226857 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); 108726857 q Statement_Text clause("has-kv", token.line_col()); 108826857 q clause.attributes.push_back(key); 108926857 q clause.attributes.push_back(""); 109026857 q clause.attributes.push_back(straight ? "" : "!"); 10941744301 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); 11031228234 q if (token.good() && *token == "]") 11071224234 q Statement_Text clause("has-kv", token.line_col()); 11081224234 q clause.attributes.push_back(key); 11091224234 q clause.attributes.push_back(get_text_token(token, error_output, "Value")); 11101224234 q if (clause.attributes.back() != "") 11121220233 q clause.attributes.push_back(straight ? "" : "!"); 11151224234 q clear_until_after(token, error_output, "]"); 112319242 q Statement_Text clause("has-kv_keyregex", token.line_col()); 112419242 q clause.attributes.push_back(key); 112519242 q clause.attributes.push_back(get_text_token(token, error_output, "Value")); 112619242 q clause.attributes.push_back(straight ? "" : "!"); 112719242 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); 114132469 q Statement_Text clause("has-kv_regex", token.line_col()); 114232469 q clause.attributes.push_back(key); 114332469 q clause.attributes.push_back(get_text_token(token, error_output, "Value")); 114432469 q clause.attributes.push_back(straight ? "" : "!"); 114532469 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); 11683084497 q subtrees.push_back(Token_Tree(token, error_output, true)); 11693084497 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); 11794428593 q std::string into = probe_into(token, error_output); 11824428593 q if (clauses.empty() && subtrees.empty()) 11864010 q if (parsed_query.get_global_bbox_limitation().valid()) 118888 q statement = create_query_statement< TStatement > 118988 q (stmt_factory, type, into, query_line_col.first); 11923210 q error_output->add_parse_error("An empty query is not allowed", token.line_col().first); 119735257 q statement = create_item_statement< TStatement >(stmt_factory, from, into, query_line_col.first); 120012027 q statement = create_query_statement< TStatement > 120112027 q (stmt_factory, type, into, query_line_col.first); 120212027 q TStatement* substatement = create_item_statement< TStatement >(stmt_factory, from, "_", query_line_col.first); 120312027 q statement->add_statement(substatement, ""); 12073916590 q else if (clauses.size() == 1 && from == "" && subtrees.empty()) 1209832185 q if (clauses.front().statement == "has-kv" 1210832185 q || clauses.front().statement == "has-kv_regex" 1211832185 q || clauses.front().statement == "has-kv_keyregex" 1212832185 q || clauses.front().statement == "has-kv_icase" 1213832185 q || clauses.front().statement == "has-kv_keyregex_icase") 1215832185 q statement = create_query_statement< TStatement > 1216832185 q (stmt_factory, type, into, query_line_col.first); 1217832185 q TStatement* substatement = create_query_substatement< TStatement > 1218832185 q (stmt_factory, token, error_output, clauses.front(), type, from, "_"); 1220832185 q statement->add_statement(substatement, ""); 12240 statement = create_query_substatement< TStatement > 12250 (stmt_factory, token, error_output, clauses.front(), type, from, into); 12283084523 q else if (clauses.empty() && from == "" && subtrees.size() == 1) 12311800363 q TStatement* filter = stmt_factory.create_criterion( 12321800363 q Token_Node_Ptr(subtrees[0], subtrees[0].tree[0].rhs), type, can_standalone, into); 1239820201 q statement = create_query_statement< TStatement >(stmt_factory, type, into, query_line_col.first); 1240820201 q statement->add_statement(filter, ""); 12461284271 q statement = create_query_statement< TStatement >(stmt_factory, type, into, query_line_col.first); 125221250 q TStatement* substatement = create_item_statement< TStatement > 125321250 q (stmt_factory, from, "_", query_line_col.first); 125521250 q statement->add_statement(substatement, ""); 12581284271 q for (std::vector< Statement_Text >::const_iterator it = clauses.begin(); 12611276201 q TStatement* substatement = create_query_substatement< TStatement > 12621276201 q (stmt_factory, token, error_output, *it, type, from, "_"); 12641276201 q statement->add_statement(substatement, ""); 12662568271 q for (typename std::vector< Token_Tree >::const_iterator it = subtrees.begin(); it != subtrees.end(); ++it) 12691284231 q TStatement* filter = stmt_factory.create_criterion( 12701284231 q Token_Node_Ptr(*it, it->tree[0].rhs), type, can_standalone, "_"); 12721276229 q statement->add_statement(filter, ""); 12754428593 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); 129642092 q return parse_union< TStatement >(stmt_factory, parsed_query, token, error_output, depth); 12988020 q return parse_for< TStatement >(stmt_factory, parsed_query, token, error_output, depth); 130012832 q return parse_foreach< TStatement >(stmt_factory, parsed_query, token, error_output, depth); 130120528600 q else if (*token == "complete") 130226057 q return parse_complete< TStatement >(stmt_factory, parsed_query, token, error_output, depth); 13044410 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); 130720164600 q else if (*token == "timeline") 130811629 q return parse_timeline< TStatement >(stmt_factory, token, error_output); 131120048600 q if (token.good() && *token == ".") 132120048600 q if (token.good() && *token == "out") 132214308591 q return parse_output< TStatement >(stmt_factory, from, token, error_output); 13235740598 q else if (token.good() && *token == "convert") 132430068 q return parse_make< TStatement >(stmt_factory, from, token, error_output, "convert"); 13255440598 q else if (token.good() && *token == "make") 132627659 q return parse_make< TStatement >(stmt_factory, from, token, error_output, "make"); 13275164595 q else if (token.good() && (*token == "<" || *token == "<<" || *token == ">" || *token == ">>")) 132835682 q return parse_full_recurse< TStatement >(stmt_factory, token, from, error_output); 13294808595 q else if (token.good() && *token == "is_in") 133016841 q return parse_coord_query< TStatement >(stmt_factory, token, from, error_output); 13314640593 q else if (token.good() && *token == "map_to_area") 13326014 q return parse_map_to_area< TStatement >(stmt_factory, token, from, error_output); 13334580593 q else if (token.good() && *token == "local") 13348020 q return parse_localize< TStatement >(stmt_factory, token, from, error_output); 13354500593 q else if (token.good() && *token == "compare") 13367218 q return parse_compare< TStatement >(stmt_factory, parsed_query, token, from, error_output, depth); 13394428593 q if (*token != "out" && from == "") 13443360526 q else if (type != "node" && type != "way" && type != "relation" 13453360526 q && type != "nwr" && type != "nw" && type != "wr" && type != "nr" 13463360526 q && type != "derived" && type != "area") 134941 q error_output->add_parse_error("Unknown type \"" + type + "\"", token.line_col().first); 13534428593 q if (token.good() && *token == ".") 13634428593 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) 13722416604 q std::istringstream in(xml_raw); 13752416604 q std::map< std::string, std::string > attr; 13763968604 q while (token.good() && *token == "[") 13781552311 q std::vector< std::string > kv = parse_setup(token, error_output, parsed_query); 13851088259 q else if (kv[0] == "diff" || kv[0] == "adiff") 138812832 q attr["augmented"] = "deletions"; 13982416604 q TStatement* base_statement = stmt_factory.create_statement 13992416604 q ("osm-script", token.line_col().first, attr); 14021244311 q clear_until_after(token, error_output, ";"); 140620276602 q std::pair< uint, uint > pos_before = token.line_col(); 140720276602 q TStatement* statement = parse_statement< TStatement >(stmt_factory, parsed_query, token, error_output, 0); 140920240601 q base_statement->add_statement(statement, ""); 14103611 q else if (token.line_col() == pos_before) 14142416604 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) 1421604604 q generic_parse_and_validate_map_ql< Statement > 1422604604 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) 1428604604 q Statement_Dump::Factory stmt_factory(stmt_factory_); 1429604604 q std::vector< Statement_Dump* > stmt_seq; 1430604604 q generic_parse_and_validate_map_ql< Statement_Dump >(stmt_factory, xml_raw, error_output, stmt_seq, parsed_query); 1431604604 q for (std::vector< Statement_Dump* >::const_iterator it = stmt_seq.begin(); 1434604604 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) 1442604604 q Statement_Dump::Factory stmt_factory(stmt_factory_); 1443604604 q std::vector< Statement_Dump* > stmt_seq; 1444604604 q generic_parse_and_validate_map_ql< Statement_Dump >(stmt_factory, xml_raw, error_output, stmt_seq, parsed_query); 1445604604 q for (std::vector< Statement_Dump* >::const_iterator it = stmt_seq.begin(); 1447604604 q std::cout<<(*it)->dump_compact_map_ql(stmt_factory_)<<'\n'; 1448604604 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) 1470604604 q Statement_Dump::Factory stmt_factory(stmt_factory_); 1471604604 q std::vector< Statement_Dump* > stmt_seq; 1472604604 q generic_parse_and_validate_map_ql< Statement_Dump >(stmt_factory, xml_raw, error_output, stmt_seq, parsed_query); 1473604604 q for (std::vector< Statement_Dump* >::const_iterator it = stmt_seq.begin(); 1475604604 q std::cout<<(*it)->dump_pretty_map_ql(stmt_factory_); 1476604604 q for (std::vector< Statement_Dump* >::iterator it = stmt_seq.begin();