Overpass API regression suite coverage (Nova Scotia) / overpass_api/frontend/map_ql_parser.cc

overpass_api/frontend/map_ql_parser.cc

1000 of 1112 instrumented lines run by the suite (89.9%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
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.
9 *
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.
14 *
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/>.
17 */
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"
30#include <cctype>
31#include <fstream>
32#include <iostream>
33#include <iterator>
34#include <queue>
35#include <set>
36#include <sstream>
37#include <vector>
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)
476744602 q{
486744602 q std::string into = "_";
496744602 q if (token.good() && *token == "->")
505992578 q {
515992578 q ++token;
525992578 q clear_until_after(token, error_output, ".");
535992578 q if (token.good())
545992578 q into = get_identifier_token(token, error_output, "Variable");
555992578 q }
566744602 q return into;
576744602 q}
59std::string probe_from(Tokenizer_Wrapper& token, Error_Output* error_output)
61468106 q std::string from = "_";
62468106 q if (token.good() && *token == ".")
63452102 q {
64452102 q ++token;
65452102 q if (token.good())
66452102 q from = get_identifier_token(token, error_output, "Variable");
67452102 q }
68468106 q return from;
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);
77636135 q if (!token.good())
780 return substatements;
79636135 q std::string closing_token = (*token == "(" ? ")" : "}");
80636135 q ++token;
811016135 q while (token.good() && *token != closing_token)
8238069 q {
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);
8638069 q if (substatement)
8738069 q substatements.push_back(substatement);
880 else if (token.line_col() == pos_before)
890 break;
9038069 q }
91636135 q if (token.good())
92636135 q ++token;
94636135 q return substatements;
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)
10242092 q is_difference = false;
10442092 q std::vector< TStatement* > substatements;
10542092 q clear_until_after(token, error_output, "(");
10642092 q if (token.good() && *token != ")")
10730069 q {
10830069 q TStatement* substatement = parse_statement< TStatement >
10930069 q (stmt_factory, parsed_query, token, error_output, depth+1);
11030069 q if (substatement)
11130069 q substatements.push_back(substatement);
11230069 q if (*token == "-")
1149222 q is_difference = true;
1159222 q ++token;
11730069 q }
11842092 q if (token.good() && *token != ")")
11916840 q {
12016840 q TStatement* substatement = parse_statement< TStatement >
12116840 q (stmt_factory, parsed_query, token, error_output, depth+1);
12216840 q if (substatement)
12316840 q substatements.push_back(substatement);
12416840 q if (is_difference && *token != ")")
1250 {
1260 if (error_output)
1270 error_output->add_parse_error("difference always requires two operands", token.line_col().first);
1280 clear_until_after(token, error_output, ")", false);
1290 }
13016840 q }
13145292 q while (token.good() && *token != ")")
132323 q {
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);
136323 q if (substatement)
137323 q substatements.push_back(substatement);
1380 else if (token.line_col() == pos_before)
1390 break;
140323 q }
14142092 q if (token.good())
14242092 q ++token;
14442092 q return substatements;
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);
15724859 q if (tree.tree.empty())
1580 return 0;
16024859 q return stmt_factory.create_evaluator(Token_Node_Ptr(tree, tree.tree[0].rhs), tree_context,
16124859 q eval_type);
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;
17032873 q attr["into"] = into;
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;
1799222 q attr["into"] = into;
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;
18820852 q attr["from"] = from;
18920852 q attr["into"] = into;
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;
199576118 q if (from != "")
20029667 q attr["from"] = from;
201576118 q attr["into"] = into;
202576118 q attr["type"] = type;
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;
21326057 q attr["from"] = from;
21426057 q attr["into"] = into;
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,
247 uint line_nr)
24814308591 q{
24914308591 q std::map< std::string, std::string > attr;
25014308591 q attr["from"] = from;
25114308591 q attr["mode"] = mode;
25214308591 q attr["order"] = order;
25314308591 q attr["limit"] = limit;
25414308591 q attr["geometry"] = geometry;
25514308591 q attr["ids"] = show_ids;
25614308591 q attr["s"] = south;
25714308591 q attr["n"] = north;
25814308591 q attr["w"] = west;
25914308591 q attr["e"] = east;
26014308591 q return stmt_factory.create_statement("print", line_nr, attr);
26114308591 q}
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;
26911629 q attr["type"] = type;
27011629 q attr["ref"] = ref;
27111629 q attr["version"] = version;
27211629 q attr["into"] = into;
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);
2837218 q attr["into"] = into;
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;
2933064520 q attr["type"] = type;
2943064520 q attr["into"] = into;
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)
3091916307 q attr["k"] = key;
31019242 q else
31119242 q attr["regk"] = key;
3132108331 q if (regex == haskv_plain)
3141488265 q attr["v"] = value;
315620126 q else if (regex == haskv_regex)
316596123 q attr["regv"] = value;
317246 q else
318246 q {
319246 q attr["regv"] = value;
320246 q attr["case"] = "ignore";
321246 q }
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;
333684125 q attr["from"] = from;
334684125 q attr["into"] = into;
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);
34535682 q attr["into"] = into;
34635682 q attr["type"] = type;
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);
35716841 q attr["into"] = into;
35816841 q attr["lat"] = lat;
35916841 q attr["lon"] = lon;
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);
3706014 q attr["into"] = into;
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);
3828020 q attr["into"] = into;
3838020 q attr["type"] = type;
3848020 q attr["s"] = south;
3858020 q attr["n"] = north;
3868020 q attr["w"] = west;
3878020 q attr["e"] = east;
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)
3971552311 q ++token;
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());
407448112 q if (!format_parser)
4080 {
4090 if (error_output)
4100 error_output->add_parse_error("Unknown output format: " + result.back(), token.line_col().first);
4110 }
412448112 q else
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")
42530476 q {
42630476 q clear_until_after(token, error_output, ",", "]", false);
42730476 q if (*token == ",")
4298020 q ++token;
4308020 q result.push_back(get_text_token(token, error_output, "Value"));
4318020 q clear_until_after(token, error_output, "]", true);
43322456 q else
43422456 q ++token;
43530476 q }
436800187 q else if (result.front() == "bbox")
4388020 q clear_until_after(token, error_output, ",", "]", false);
4398020 q if (*token == ",")
4418020 q ++token;
4428020 q result.back() += "," + get_text_token(token, error_output, "Value");
4438020 q clear_until_after(token, error_output, ",", "]", false);
4458020 q if (*token == ",")
4478020 q ++token;
4488020 q result.back() += "," + get_text_token(token, error_output, "Value");
4498020 q clear_until_after(token, error_output, ",", "]", false);
4518020 q if (*token == ",")
4538020 q ++token;
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")
459367 q {
460367 q while (token.good() && *token != "]")
4610 {
4620 result.back() += (' ' + *token);
4630 ++token;
4640 }
465367 q clear_until_after(token, error_output, "]");
466367 q }
467684171 q else
468684171 q clear_until_after(token, error_output, "]");
4691552311 q return result;
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;
48742092 q if (is_difference)
4889222 q statement = create_difference_statement< TStatement >(stmt_factory, into, line_col.first);
48932873 q else
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, "");
49542092 q return statement;
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();
50412832 q ++token;
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);
51012832 q if (*token == ";")
51141 q ++token;
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, "");
51812832 q return statement;
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); }
531};
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();
5398020 q ++token;
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);
5508020 q if (*token == ";")
5510 ++token;
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, "");
5598020 q return statement;
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();
56826057 q ++token;
56926057 q std::string maxnum;
57026057 q if (*token == "(")
57126057 q {
57226057 q ++token;
57326057 q maxnum = get_text_token(token, error_output, "maximum loop number");
57426057 q clear_until_after(token, error_output, ")");
57526057 q }
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);
58026057 q if (*token == ";")
5810 ++token;
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, "");
58826057 q return statement;
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();
5974410 q ++token;
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")
607287 q {
608287 q ++token;
609287 q collect_substatements< TStatement >(stmt_factory, parsed_query, token, error_output, depth)
610287 q .swap(else_statements);
611287 q }
6124410 q if (*token == ";")
6130 ++token;
6154410 q TStatement* statement = create_if_statement< TStatement >(stmt_factory, line_col.first);
6164410 q if (condition)
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, "");
6264410 q return statement;
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();
6356015 q ++token;
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);
6436015 q if (*token == ";")
6440 ++token;
6466015 q TStatement* statement = create_retro_statement< TStatement >(stmt_factory, line_col.first);
6476015 q if (condition)
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, "");
6526015 q return statement;
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();
66111629 q ++token;
66311629 q clear_until_after(token, error_output, "(");
66411629 q std::string type = get_text_token(token, error_output, "OSM base type");
66511629 q if (type == "rel")
6660 type = "relation";
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);
67111629 q std::string version;
67211629 q if (*token == ",")
673205 q {
674205 q ++token;
675205 q version = get_text_token(token, error_output, "Number");
676205 q }
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();
6917218 q ++token;
6937218 q TStatement* condition = 0;
6947218 q if (*token == "(")
6966416 q clear_until_after(token, error_output, "(");
6976416 q if (*token == "delta")
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 == "{")
710369 q {
711369 q collect_substatements< TStatement >(stmt_factory, parsed_query, token, error_output, depth)
712369 q .swap(substatements);
713369 q if (*token == ";")
7140 ++token;
715369 q }
716369 q else
717369 q clear_until_after(token, error_output, ";");
7197218 q TStatement* statement = create_compare_statement< TStatement >(stmt_factory, line_col.first, from, into);
7207218 q if (condition)
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, "");
7267218 q return statement;
730template< class TStatement >
731TStatement* parse_output(typename TStatement::Factory& stmt_factory,
732 const std::string& from, Tokenizer_Wrapper& token, Error_Output* error_output)
73314308591 q{
73414308591 q TStatement* statement = 0;
73514308591 q if (*token == "out")
73614308591 q {
73714308591 q ++token;
73814308591 q std::string mode = "body";
73914308591 q std::string order = "id";
74014308591 q std::string limit = "";
74114308591 q std::string geometry = "skeleton";
74214308591 q std::string show_ids = "yes";
74314308591 q std::string south = "";
74414308591 q std::string north = "";
74514308591 q std::string west = "";
74614308591 q std::string east = "";
74733364591 q while (token.good() && *token != ";")
74819056583 q {
74919056583 q if (*token == "ids")
7506052578 q mode = "ids_only";
75113004583 q else if (*token == "tags")
752164 q mode = "tags";
75312988583 q else if (*token == "skel")
7548020 q mode = "skeleton";
75512908583 q else if (*token == "body")
756164 q mode = "body";
75712892583 q else if (*token == "meta")
7589624 q mode = "meta";
75912796583 q else if (*token == "quirks")
76041 q mode = "quirks";
76112792583 q else if (*token == "count")
7626168579 q mode = "count";
7636624582 q else if (*token == "qt")
76415238 q order = "quadtile";
7656472582 q else if (*token == "asc")
76641 q order = "id";
7676468582 q else if (*token == "geom")
7689622 q geometry = "full";
7696372580 q else if (*token == "bb")
7706015 q geometry = "bounds";
7716312580 q else if (*token == "center")
7724812 q geometry = "center";
7736264578 q else if (*token == "noids")
7744010 q show_ids = "no";
7756224578 q else if (isdigit((*token)[0]))
7766080578 q limit = *token;
77714435 q else if (*token == "(")
77814435 q {
77914435 q ++token;
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);
78814435 q }
7890 else
7900 {
7910 if (error_output)
7920 error_output->add_parse_error
7930 (std::string("Invalid parameter for print: \"") + *token + "\"", token.line_col().first);
7940 }
79519056583 q ++token;
79619056583 q }
79814308591 q if (statement == 0)
79914308591 q {
80014308591 q statement = create_print_statement< TStatement >
80114308591 q (stmt_factory, from == "" ? "_" : from, mode, order, limit, geometry, show_ids,
80214308591 q south, north, west, east,
80314308591 q token.line_col().first);
80414308591 q }
8050 else
8060 {
8070 if (error_output)
8080 error_output->add_parse_error("Garbage after output statement found.",
8090 token.line_col().first);
8100 }
81114308591 q }
81214308591 q clear_until_after(token, error_output, ";");
81414308591 q return statement;
81514308591 q}
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); }
825};
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)
832576118 q TStatement* statement = 0;
833576118 q std::vector< TStatement* > evaluators;
834576118 q std::string type = "";
835576118 q if (*token == strategy)
837576118 q ++token;
838576118 q if (*token != ";")
839576118 q type = get_identifier_token(token, error_output, "Element class name");
841576118 q Token_Tree tree(token, error_output, false);
842576118 q if (tree.tree.size() > 1)
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 == ",")
8481368106 q {
8491368106 q if (tree_it->rhs)
8501368106 q {
8511368106 q TStatement* stmt = stmt_factory.create_evaluator(tree_it.rhs(), tree_context, No_Return_Type_Checker());
8521368106 q if (stmt)
8531348105 q evaluators.push_back(stmt);
8541368106 q }
8561368106 q tree_it = tree_it.lhs();
8571368106 q }
859576118 q TStatement* stmt = stmt_factory.create_evaluator(tree_it, tree_context, No_Return_Type_Checker());
860576118 q if (stmt)
861576118 q evaluators.push_back(stmt);
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, ";");
877576118 q return statement;
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;
8868020 q ++token;
8888020 q std::string type = "l";
8898020 q if (*token != ";" && *token != "->" && *token != "(")
8915213 q type = *token;
8925213 q ++token;
8948020 q std::string south, north, west, east;
8958020 q if (*token == "(")
8960 {
8970 ++token;
8980 south = get_text_token(token, error_output, "Value");
8990 clear_until_after(token, error_output, ",", ")", false);
9000 if (*token == ",")
9010 {
9020 ++token;
9030 west = get_text_token(token, error_output, "Value");
9040 clear_until_after(token, error_output, ",", ")", false);
9050 }
9060 if (*token == ",")
9070 {
9080 ++token;
9090 north = get_text_token(token, error_output, "Value");
9100 clear_until_after(token, error_output, ",", ")", false);
9110 }
9120 if (*token == ",")
9130 {
9140 ++token;
9150 east = get_text_token(token, error_output, "Value");
9160 clear_until_after(token, error_output, ",", ")", false);
9170 }
9180 ++token;
9190 }
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);
929struct Statement_Text
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;
938};
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);
9690 return 0;
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;
97835682 q ++token;
97935682 q std::string into = probe_into(token, error_output);
98035682 q clear_until_after(token, error_output, ";");
98235682 q if (type == ">")
9838020 q return create_recurse_statement< TStatement >(stmt_factory, "down", from, into, line_col);
98427666 q else if (type == ">>")
9856416 q return create_recurse_statement< TStatement >(stmt_factory, "down-rel", from, into, line_col);
98621251 q else if (type == "<")
98710827 q return create_recurse_statement< TStatement >(stmt_factory, "up", from, into, line_col);
98810426 q else if (type == "<<")
98910426 q return create_recurse_statement< TStatement >(stmt_factory, "up-rel", from, into, line_col);
9900 else
9910 return 0;
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;
100016841 q ++token;
100216841 q std::string lat, lon;
100316841 q if (*token == "(")
10057218 q ++token;
10067218 q lat = get_text_token(token, error_output, "Number");
10077218 q clear_until_after(token, error_output, ",", ")", false);
10087218 q if (*token == ",")
10107218 q ++token;
10117218 q lon = get_text_token(token, error_output, "Number");
10127218 q clear_until_after(token, error_output, ")", false);
10147218 q ++token;
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)
10266014 q std::string type = *token;
10276014 q uint line_col = token.line_col().first;
10286014 q ++token;
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 == "."))
10475200590 q {
10485200590 q if (*token == "[")
10492116331 q {
10502116331 q ++token;
10522116331 q bool key_regex = (*token == "~");
10532116331 q if (key_regex)
105419242 q ++token;
10562116331 q if (*token == "!") // [!key] as shortcut for [key !~ ".*"]
105810425 q ++token;
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("!");
106510425 q clauses.push_back(clause);
106610425 q continue;
10692012324 q std::string key = get_text_token(token, error_output, "Key");
10702012324 q clear_until_after(token, error_output, "!", "~", "=", "!=", "]", false);
10722012324 q bool straight = true;
10732012324 q if (*token == "!")
107513630 q straight = false;
107613630 q ++token;
107713630 q clear_until_after(token, error_output, "~", "=", "]", false);
10802012324 q if (*token == "]")
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 ? "" : "!");
109126857 q ++token;
109226857 q clauses.push_back(clause);
10941744301 q else if (*token == "=" || *token == "!=")
10951228234 q {
10961228234 q if (key_regex && error_output)
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);
11011228234 q straight = (*token == "=");
11021228234 q ++token;
11031228234 q if (token.good() && *token == "]")
110441 q ++token;
11051224234 q else
11061224234 q {
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() != "")
11111220233 q {
11121220233 q clause.attributes.push_back(straight ? "" : "!");
11131220233 q clauses.push_back(clause);
11141220233 q }
11151224234 q clear_until_after(token, error_output, "]");
11161224234 q }
11171228234 q }
1118516107 q else //if (*token == "~")
1120516107 q if (key_regex)
112219242 q ++token;
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);
112819242 q if (*token == ",")
113041 q clause.statement = "has-kv_keyregex_icase";
113141 q ++token;
113241 q clear_until_after(token, error_output, "i");
113341 q clear_until_after(token, error_output, "]", false);
113519242 q ++token;
113619242 q clauses.push_back(clause);
113832469 q else
114032469 q ++token;
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);
114632469 q if (*token == ",")
1148205 q clause.statement = "has-kv_icase";
1149205 q ++token;
1150205 q clear_until_after(token, error_output, "i");
1151205 q clear_until_after(token, error_output, "]", false);
115332469 q ++token;
115432469 q clauses.push_back(clause);
11572012324 q }
11583084497 q else if (*token == "(")
11593084497 q {
11603084497 q ++token;
11613084497 q if (!token.good())
11620 {
11630 if (error_output)
11640 error_output->add_parse_error("':' or '.' expected.", token.line_col().first);
11650 break;
11660 }
11683084497 q subtrees.push_back(Token_Tree(token, error_output, true));
11693084497 q clear_until_after(token, error_output, ")");
11703084497 q }
11710 else
11720 {
11730 Statement_Text clause("item", token.line_col());
11740 clause.attributes.push_back(probe_from(token, error_output));
11750 clauses.push_back(clause);
11760 }
11775200590 q }
11794428593 q std::string into = probe_into(token, error_output);
11814428593 q TStatement* statement = 0;
11824428593 q if (clauses.empty() && subtrees.empty())
118451290 q if (from == "")
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);
11913210 q else if (error_output)
11923210 q error_output->add_parse_error("An empty query is not allowed", token.line_col().first);
119447281 q else
119647281 q if (type == "")
119735257 q statement = create_item_statement< TStatement >(stmt_factory, from, into, query_line_col.first);
119812027 q else
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, "_");
1219832185 q if (substatement)
1220832185 q statement->add_statement(substatement, "");
12220 else
12230 {
12240 statement = create_query_substatement< TStatement >
12250 (stmt_factory, token, error_output, clauses.front(), type, from, into);
12260 }
12283084523 q else if (clauses.empty() && from == "" && subtrees.size() == 1)
12291800363 q {
12301800363 q bool can_standalone = false;
12311800363 q TStatement* filter = stmt_factory.create_criterion(
12321800363 q Token_Node_Ptr(subtrees[0], subtrees[0].tree[0].rhs), type, can_standalone, into);
12331800363 q if (filter)
12341796362 q {
12351796362 q if (can_standalone)
1236976203 q statement = filter;
1237820201 q else
1239820201 q statement = create_query_statement< TStatement >(stmt_factory, type, into, query_line_col.first);
1240820201 q statement->add_statement(filter, "");
12421796362 q }
12431800363 q }
12441284271 q else
12451284271 q {
12461284271 q statement = create_query_statement< TStatement >(stmt_factory, type, into, query_line_col.first);
12471284271 q if (!statement)
12480 return 0;
12501284271 q if (from != "")
125221250 q TStatement* substatement = create_item_statement< TStatement >
125321250 q (stmt_factory, from, "_", query_line_col.first);
125421250 q if (substatement)
125521250 q statement->add_statement(substatement, "");
12581284271 q for (std::vector< Statement_Text >::const_iterator it = clauses.begin();
12592560271 q it != clauses.end(); ++it)
12601276201 q {
12611276201 q TStatement* substatement = create_query_substatement< TStatement >
12621276201 q (stmt_factory, token, error_output, *it, type, from, "_");
12631276201 q if (substatement)
12641276201 q statement->add_statement(substatement, "");
12651276201 q }
12662568271 q for (typename std::vector< Token_Tree >::const_iterator it = subtrees.begin(); it != subtrees.end(); ++it)
12671284231 q {
12681284231 q bool can_standalone = false;
12691284231 q TStatement* filter = stmt_factory.create_criterion(
12701284231 q Token_Node_Ptr(*it, it->tree[0].rhs), type, can_standalone, "_");
12711284231 q if (filter)
12721276229 q statement->add_statement(filter, "");
12731284231 q }
12741284271 q }
12754428593 q clear_until_after(token, error_output, ";");
12774428593 q return statement;
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)
128421156602 q{
128521156602 q if (!token.good())
12860 return 0;
128821156602 q if (depth >= 1024)
12890 {
12900 if (error_output)
12910 error_output->add_parse_error("Nesting of statements is limited to 1023 levels", token.line_col().first);
12920 return 0;
12930 }
129521156602 q if (*token == "(")
129642092 q return parse_union< TStatement >(stmt_factory, parsed_query, token, error_output, depth);
129720736601 q else if (*token == "for")
12988020 q return parse_for< TStatement >(stmt_factory, parsed_query, token, error_output, depth);
129920656601 q else if (*token == "foreach")
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);
130320268600 q else if (*token == "if")
13044410 q return parse_if< TStatement >(stmt_factory, parsed_query, token, error_output, depth);
130520224600 q else if (*token == "retro")
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);
131020048600 q std::string from = "";
131120048600 q if (token.good() && *token == ".")
131215416578 q {
131315416578 q ++token;
131415416578 q if (token.good())
131515416578 q {
131615416578 q from = *token;
131715416578 q ++token;
131815416578 q }
131915416578 q }
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);
13384428593 q std::string type = "";
13394428593 q if (*token != "out" && from == "")
13404076593 q {
13414076593 q type = *token;
13424076593 q if (type == "rel")
1343716154 q type = "relation";
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")
134841 q if (error_output)
134941 q error_output->add_parse_error("Unknown type \"" + type + "\"", token.line_col().first);
13514076593 q ++token;
13524076593 q }
13534428593 q if (token.good() && *token == ".")
135533272 q ++token;
135633272 q if (token.good())
135833272 q from = *token;
135933272 q ++token;
13634428593 q return parse_query< TStatement >(stmt_factory, parsed_query, type, from, token, error_output);
136420048600 q}
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);
13732416604 q Tokenizer_Wrapper token(in);
13752416604 q std::map< std::string, std::string > attr;
13763968604 q while (token.good() && *token == "[")
13771552311 q {
13781552311 q std::vector< std::string > kv = parse_setup(token, error_output, parsed_query);
13791552311 q if (kv.size() < 2)
13800 continue;
13811552311 q if (kv[0] == "maxsize")
1382164 q kv[0] = "element-limit";
13831536311 q else if (kv[0] == "out")
1384448112 q kv[0] = "output";
13851088259 q else if (kv[0] == "diff" || kv[0] == "adiff")
138730476 q if (kv[0] == "adiff")
138812832 q attr["augmented"] = "deletions";
138930476 q if (kv.size() >= 3)
13908020 q attr["date"] = kv[2];
139130476 q kv[0] = "from";
13931552311 q attr[kv[0]] = kv[1];
13941552311 q if (kv.size() == 4)
1395448112 q attr[kv[2]] = kv[3];
13961552311 q }
13982416604 q TStatement* base_statement = stmt_factory.create_statement
13992416604 q ("osm-script", token.line_col().first, attr);
14012416604 q if (!attr.empty())
14021244311 q clear_until_after(token, error_output, ";");
140422692604 q while (token.good())
140520276602 q {
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);
140820276602 q if (statement)
140920240601 q base_statement->add_statement(statement, "");
14103611 q else if (token.line_col() == pos_before)
14110 break;
141220276602 q }
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();
14321208604 q it != stmt_seq.end(); ++it)
1433604604 q std::cout<<(*it)->dump_xml();
1434604604 q for (std::vector< Statement_Dump* >::iterator it = stmt_seq.begin();
14351208604 q it != stmt_seq.end(); ++it)
1436604604 q delete *it;
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();
14461208604 q it != stmt_seq.end(); ++it)
1447604604 q std::cout<<(*it)->dump_compact_map_ql(stmt_factory_)<<'\n';
1448604604 q for (std::vector< Statement_Dump* >::iterator it = stmt_seq.begin();
14491208604 q it != stmt_seq.end(); ++it)
1450604604 q delete *it;
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)
14640 delete *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();
14741208604 q it != stmt_seq.end(); ++it)
1475604604 q std::cout<<(*it)->dump_pretty_map_ql(stmt_factory_);
1476604604 q for (std::vector< Statement_Dump* >::iterator it = stmt_seq.begin();
14771208604 q it != stmt_seq.end(); ++it)
1478604604 q delete *it;