Overpass API regression suite coverage (Nova Scotia) / overpass_api/statements/statement.cc

overpass_api/statements/statement.cc

194 of 272 instrumented lines run by the suite (71.3%) 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 "evaluator.h"
20#include "osm_script.h"
21#include "statement.h"
23#include <iomanip>
24#include <iostream>
25#include <sstream>
26#include <string>
29std::map< std::string, Statement::Statement_Maker* >& Statement::maker_by_name()
30275278612 q{
31275278612 q static std::map< std::string, Statement::Statement_Maker* > makers;
32275278612 q return makers;
33275278612 q}
36std::map< std::string, Statement::Criterion_Maker* >& Statement::maker_by_ql_criterion()
3764920612 q{
3864920612 q static std::map< std::string, Statement::Criterion_Maker* > makers;
3964920612 q return makers;
4064920612 q}
43std::map< std::string, std::vector< Statement::Evaluator_Maker* > >& Statement::maker_by_token()
4470592612 q{
4570592612 q static std::map< std::string, std::vector< Statement::Evaluator_Maker* > > makers;
4670592612 q return makers;
4770592612 q}
50std::map< std::string, std::vector< Statement::Evaluator_Maker* > >& Statement::maker_by_func_name()
51134544612 q{
52134544612 q static std::map< std::string, std::vector< Statement::Evaluator_Maker* > > makers;
53134544612 q return makers;
54134544612 q}
57void Statement::eval_attributes_array(std::string element, std::map< std::string, std::string >& attributes,
58 const std::map< std::string, std::string >& input)
5933898612 q{
60175361612 q for (std::map< std::string, std::string >::const_iterator it = input.begin(); it != input.end(); ++it)
61141463610 q {
62141463610 q std::map< std::string, std::string >::iterator ait(attributes.find(it->first));
63141463610 q if (ait != attributes.end())
64141463610 q ait->second = it->second;
650 else
660 {
670 std::ostringstream temp;
680 temp<<"Unknown attribute \""<<it->first<<"\" in element \""<<element<<"\".";
690 add_static_error(temp.str());
700 }
71141463610 q }
7233898612 q}
74void Statement::assure_no_text(std::string text, std::string name)
759462608 q{
769629608 q for (unsigned int i(0); i < text.size(); ++i)
771677 q {
781677 q if (!isspace(text[i]))
790 {
800 std::ostringstream temp;
810 temp<<"Element \""<<name<<"\" must not contain text.";
820 add_static_error(temp.str());
830 break;
840 }
851677 q }
869462608 q}
88void Statement::substatement_error(std::string parent, Statement* child)
890{
900 std::ostringstream temp;
910 temp<<"Element \""<<child->get_name()<<"\" cannot be subelement of element \""<<parent<<"\".";
920 add_static_error(temp.str());
930}
95void Statement::add_statement(Statement* statement, std::string text)
960{
970 assure_no_text(text, this->get_name());
980 substatement_error(get_name(), statement);
990}
101void Statement::add_final_text(std::string text)
103497 q assure_no_text(text, this->get_name());
106void Statement::display_full()
1070{
108 //display_verbatim(get_source(startpos, endpos - startpos));
1090}
111void Statement::display_starttag()
1120{
113 //display_verbatim(get_source(startpos, tagendpos - startpos));
1140}
117Statement::Factory::~Factory()
1192448612 q for (std::vector< Statement* >::const_iterator it = created_statements.begin();
12036351612 q it != created_statements.end(); ++it)
12133903612 q delete *it;
125Statement* Statement::Factory::create_statement
126 (std::string element, int line_number, const std::map< std::string, std::string >& attributes)
12717687612 q{
12817687612 q Statement* statement = 0;
13017687612 q std::map< std::string, Statement::Statement_Maker* >::iterator maker_it =
13117687612 q Statement::maker_by_name().find(element);
13317687612 q if (maker_it != Statement::maker_by_name().end())
13417687612 q statement = maker_it->second->create_statement(line_number, attributes, global_settings);
13617687612 q if (statement)
13717687612 q created_statements.push_back(statement);
1380 else
1390 {
1400 std::ostringstream temp;
1410 temp<<"Unknown tag \""<<element<<"\" in line "<<line_number<<'.';
1420 if (error_output_)
1430 error_output_->add_static_error(temp.str(), line_number);
1440 }
14617687612 q return statement;
14717687612 q}
150std::string Statement::eval_to_string(Statement::Eval_Return_Type eval_type)
15281 q if (eval_type == Statement::string)
15341 q return "string";
15441 q else if (eval_type == Statement::container)
1550 return "container";
15641 q else if (eval_type == Statement::geometry)
15741 q return "geometry";
1590 return "non-evaluator";
163Statement* stmt_from_tree_node(const Token_Node_Ptr& tree_it,
164 Statement::QL_Context tree_context, const Statement::Return_Type_Checker& eval_type,
165 const std::vector< Statement::Evaluator_Maker* >& makers, Statement::Factory& stmt_factory,
166 Parsed_Query& global_settings, Error_Output* error_output)
16713236210 q{
16813236210 q Statement* statement = 0;
17013236210 q std::vector< Statement::Evaluator_Maker* >::const_iterator maker_it = makers.begin();
17226640210 q while (!statement && maker_it != makers.end())
17313404210 q {
17413404210 q statement = (*maker_it)->create_evaluator(tree_it, tree_context, stmt_factory, global_settings, error_output);
17513404210 q ++maker_it;
17613404210 q }
17713352210 q while (maker_it != makers.end())
17811627 q {
17911627 q Statement* bis = (*maker_it)->create_evaluator(tree_it, tree_context, stmt_factory, global_settings, error_output);
18011627 q if (bis)
1810 {
1820 if (error_output)
1830 error_output->add_static_error(std::string("Function name \"") + tree_it.lhs()->token
1840 + "\" has ambiguous resolutions to \"" + statement->get_name() + "\" and \"" + bis->get_name() + "\".",
1850 tree_it->line_col.first);
1860 delete bis;
1870 delete statement;
1880 statement = 0;
1890 }
19011627 q ++maker_it;
19111627 q }
19313236210 q if (statement && eval_type.eval_required())
19411224207 q {
19511224207 q Evaluator* eval = dynamic_cast< Evaluator* >(statement);
19611224207 q if (!eval)
1970 {
1980 if (error_output)
1990 error_output->add_static_error(std::string("Evaluator expected, but function \"") + tree_it.lhs()->token
2000 + "\" is not an evaluator.", tree_it->line_col.first);
2010 delete statement;
2020 statement = 0;
2030 }
20411224207 q else if (!eval_type.matches(eval->return_type()))
20641 q if (error_output)
20741 q error_output->add_static_error(std::string("Evaluator for ") + eval_type.expectation()
20841 q + " expected, but function \"" + statement->get_name() + "\" is an evaluator for "
20941 q + Statement::eval_to_string(eval->return_type()) + ".", tree_it->line_col.first);
21041 q delete statement;
21141 q statement = 0;
21311224207 q }
21513236210 q return statement;
21613236210 q}
219Statement* Statement::Factory::create_evaluator(
220 const Token_Node_Ptr& tree_it, Statement::QL_Context tree_context,
221 const Statement::Return_Type_Checker& eval_type)
22215560210 q{
22315560210 q Statement* statement = 0;
22515560210 q if (tree_it->token == "(")
2267460201 q {
2277460201 q if (tree_it->lhs)
2285108200 q {
2295108200 q std::map< std::string, std::vector< Statement::Evaluator_Maker* > >::iterator all_it =
2305108200 q Statement::maker_by_func_name().find(tree_it.lhs()->token);
2315108200 q if (all_it != Statement::maker_by_func_name().end())
2325108200 q statement = stmt_from_tree_node(tree_it, tree_context, eval_type,
2335108200 q all_it->second, *this, global_settings, Statement::error_output);
2340 else
2350 error_output->add_parse_error(std::string("Function \"") + tree_it.lhs()->token + "\" not known",
2360 tree_it->line_col.first);
2375108200 q }
2382352108 q else if (tree_it->rhs)
2392352108 q return create_evaluator(tree_it.rhs(), tree_context, eval_type);
2400 else
2410 {
2420 Statement::error_output->add_static_error("Empty parentheses cannot be evaluated.", tree_it->line_col.first);
2430 return 0;
2440 }
2457460201 q }
2468100186 q else if (tree_it->token == ".")
24797663 q {
24897663 q if (tree_it->lhs && tree_it->rhs && tree_it.rhs()->token == "(" && tree_it.rhs()->lhs)
24996461 q {
25096461 q std::map< std::string, std::vector< Statement::Evaluator_Maker* > >::iterator all_it =
25196461 q Statement::maker_by_func_name().find(tree_it.rhs().lhs()->token);
25296461 q if (all_it != Statement::maker_by_func_name().end())
25396461 q statement = stmt_from_tree_node(tree_it, tree_context, eval_type,
25496461 q all_it->second, *this, global_settings, Statement::error_output);
2550 else
2560 error_output->add_parse_error(std::string("Function \"") + tree_it.rhs().lhs()->token + "\" not known",
2570 tree_it->line_col.first);
25896461 q }
259123 q else
260123 q {
261123 q std::map< std::string, std::vector< Statement::Evaluator_Maker* > >::iterator all_it =
262123 q Statement::maker_by_token().find(tree_it->token);
263123 q if (all_it != Statement::maker_by_token().end())
264123 q statement = stmt_from_tree_node(tree_it, tree_context, eval_type,
265123 q all_it->second, *this, global_settings, Statement::error_output);
266123 q }
26797663 q }
2687124186 q else if (tree_it->token == "")
2690 error_output->add_parse_error("Evaluator expected, but empty token found.", tree_it->line_col.first);
2707124186 q else
2717124186 q {
2727124186 q std::map< std::string, std::vector< Statement::Evaluator_Maker* > >::iterator all_it =
2737124186 q Statement::maker_by_token().find(tree_it->token);
2747124186 q if (all_it != Statement::maker_by_token().end())
2754696176 q statement = stmt_from_tree_node(tree_it, tree_context, eval_type,
2764696176 q all_it->second, *this, global_settings, Statement::error_output);
2787124186 q if (!statement)
2792456160 q {
2802456160 q all_it = Statement::maker_by_token().find("");
2812456160 q if (all_it != Statement::maker_by_token().end())
2822456160 q statement = stmt_from_tree_node(tree_it, tree_context, eval_type,
2832456160 q all_it->second, *this, global_settings, Statement::error_output);
2852456160 q if (statement)
2862420155 q ;
287367 q else if (tree_it->lhs)
288327 q {
289327 q if (tree_it->rhs)
290123 q error_output->add_parse_error(
291123 q std::string("\"") + tree_it->token + "\" cannot be used as binary operator", tree_it->line_col.first);
292204 q else
293204 q error_output->add_parse_error(
294204 q std::string("\"") + tree_it->token + "\" needs a right hand side argument", tree_it->line_col.first);
295327 q }
29641 q else if (tree_it->rhs)
2970 {
2980 error_output->add_parse_error(
2990 std::string("\"") + tree_it->token + "\" cannot be used as unary operator", tree_it->line_col.first);
3000 return create_evaluator(tree_it.rhs(), tree_context, eval_type);
3010 }
30241 q else
30341 q error_output->add_parse_error(std::string("Token \"") + tree_it->token
30441 q + "\" has not been recognized as statement", tree_it->line_col.first);
3052456160 q }
3067124186 q }
30813208210 q if (statement)
30913144207 q created_statements.push_back(statement);
31113208210 q return statement;
31215560210 q}
315Statement* Statement::Factory::create_criterion(const Token_Node_Ptr& tree_it,
316 const std::string& type, bool& can_standalone, const std::string& into)
3183084497 q Token_Node_Ptr criterion = find_leftmost_token(tree_it);
3193084497 q uint line_nr = criterion->line_col.first;
3203084497 q std::string criterion_s = criterion->token;
3223084497 q if (!criterion_s.empty()
3233084497 q && (isdigit(criterion_s[0])
3243084497 q || (criterion_s[0] == '-' && criterion_s.size() > 1 && isdigit(criterion_s[1]))))
325896215 q criterion_s = (tree_it->token == "," ? "bbox" : "id");
3273084497 q std::map< std::string, Criterion_Maker* >::iterator it = Statement::maker_by_ql_criterion().find(criterion_s);
3283084497 q if (it != Statement::maker_by_ql_criterion().end() && it->second)
3293084497 q {
3303084497 q can_standalone = it->second->can_standalone(type);
3313084497 q Statement* result = it->second->create_criterion(tree_it, type, into, *this, global_settings, error_output);
3323084497 q if (result)
3333072495 q created_statements.push_back(result);
3343084497 q return result;
3353084497 q }
3370 if (error_output)
3380 error_output->add_parse_error("Unknown query clause", line_nr);
3390 return 0;
343Error_Output* Statement::error_output = 0;
346void Statement::add_static_error(std::string error)
34893 q if (error_output)
34993 q error_output->add_static_error(error, line_number);
353void Statement::add_static_remark(std::string remark)
3540{
3550 if (error_output)
3560 error_output->add_static_remark(remark, line_number);
3570}
360void Statement::runtime_error(std::string error) const
36266 q if (error_output)
36366 q error_output->runtime_error(error);
367void Statement::runtime_remark(std::string error) const
3680{
3690 if (error_output)
3700 error_output->runtime_remark(error);
3710}
374std::map< std::string, std::string > convert_c_pairs(const char** attr)
3752008 q{
3762008 q std::map< std::string, std::string > result;
3775168 q for (int i = 0; attr[i]; i+=2)
3783167 q result[attr[i]] = attr[i+1];
3792008 q return result;
3802008 q}
383void Output_Statement::transfer_output(Resource_Manager& rman, Set& into) const
3854187589 q rman.swap_set(output, into);
3864187589 q into.clear();
390void Output_Statement::transfer_output(Resource_Manager& rman, Diff_Set& into) const
3921616 q rman.swap_diff_set(output, into);
3931616 q into.clear();