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

overpass_api/statements/ternary_operator.cc

114 of 238 instrumented lines run by the suite (47.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 "../data/tag_store.h"
20#include "../data/utils.h"
21#include "ternary_operator.h"
24void Ternary_Evaluator::add_statement(Statement* statement, std::string text)
25130871 q{
26130871 q Evaluator* tag_value_ = dynamic_cast< Evaluator* >(statement);
27130871 q if (!tag_value_)
280 substatement_error(get_name(), statement);
29130871 q else if (!condition)
3043671 q condition = tag_value_;
3187271 q else if (!lhs)
3243671 q {
3343671 q lhs = tag_value_;
3443671 q return_type_ = tag_value_->return_type();
3543671 q }
3643671 q else if (!rhs)
3743671 q rhs = tag_value_;
380 else
390 add_static_error(get_name() + " must have exactly three evaluator substatements.");
40130871 q}
43struct Any_Return_Type_Checker : Statement::Return_Type_Checker
4543671 q Any_Return_Type_Checker() {}
4743671 q virtual bool eval_required() const { return true; }
4843671 q virtual bool matches(Statement::Eval_Return_Type eval_type) const { return true; }
490 virtual std::string expectation() const { return "any nonempty type"; }
50};
53void Ternary_Evaluator::add_substatements(Statement* result, const std::string& operator_name,
54 const Token_Node_Ptr& tree_it, Statement::QL_Context tree_context,
55 Statement::Factory& stmt_factory, Error_Output* error_output)
5644072 q{
5744072 q if (result)
5844072 q {
5944072 q if (tree_it.rhs()->token != ":")
6141 q if (error_output)
6241 q error_output->add_parse_error(
6341 q "Operator \"?\" must have a \":\" operator as right-hand-side argument", tree_it->line_col.first);
6441 q return;
6743671 q Statement* condition = stmt_factory.create_evaluator(
6843671 q tree_it.lhs(), tree_context, Statement::Single_Return_Type_Checker(Statement::string));
6943671 q if (condition)
7043671 q result->add_statement(condition, "");
710 else if (error_output)
720 error_output->add_parse_error(
730 "Operator \"?\" needs a left-hand-side argument", tree_it->line_col.first);
7543671 q Statement::Eval_Return_Type rhs_expected = Statement::string;
7643671 q Statement* lhs = stmt_factory.create_evaluator(
7743671 q tree_it.rhs().lhs(), tree_context, Any_Return_Type_Checker());
7843671 q if (lhs)
7943671 q {
8043671 q result->add_statement(lhs, "");
8143671 q Evaluator* lhs_eval = (Evaluator*)lhs;
8243671 q if (lhs_eval)
8343671 q rhs_expected = lhs_eval->return_type();
8443671 q }
850 else if (error_output)
860 error_output->add_parse_error(
870 "Operator \":\" of operator \"?\" needs a left-hand-side argument", tree_it->line_col.first);
8943671 q Statement* rhs = stmt_factory.create_evaluator(
9043671 q tree_it.rhs().rhs(), tree_context, Statement::Single_Return_Type_Checker(rhs_expected));
9143671 q if (rhs)
9243671 q result->add_statement(rhs, "");
930 else if (error_output)
940 error_output->add_parse_error(
950 "Operator \":\" of operator \"?\" needs a right-hand-side argument"
960 " with the same return type as the left-hand-side argument", tree_it->line_col.first);
9743671 q }
9844072 q}
101Eval_Task* Ternary_Evaluator::get_string_task(Prepare_Task_Context& context, const std::string* key)
10311369 q Eval_Task* cond_task = condition ? condition->get_string_task(context, key) : 0;
10411369 q Eval_Task* lhs_task = lhs ? lhs->get_string_task(context, key) : 0;
10511369 q Eval_Task* rhs_task = rhs ? rhs->get_string_task(context, key) : 0;
10611369 q return new Ternary_Eval_Task(cond_task, lhs_task, rhs_task);
110std::string Ternary_Eval_Task::eval(const std::string* key) const
1122217 q if (!condition)
1130 return "0";
1142217 q if (string_represents_boolean_true(condition->eval(key)))
11544 q return lhs ? lhs->eval(key) : "";
1161815 q return rhs ? rhs->eval(key) : "";
120std::string Ternary_Eval_Task::eval(const Element_With_Context< Node_Skeleton >& data, const std::string* key) const
1212181517 q{
1222181517 q if (!condition)
1230 return "0";
1242181517 q if (string_represents_boolean_true(condition->eval(data, key)))
1251612810 q return lhs ? lhs->eval(data, key) : "";
12656879 q return rhs ? rhs->eval(data, key) : "";
1272181517 q}
130std::string Ternary_Eval_Task::eval(const Element_With_Context< Attic< Node_Skeleton > >& data, const std::string* key) const
132991 q if (!condition)
1330 return "0";
134991 q if (string_represents_boolean_true(condition->eval(data, key)))
135991 q return lhs ? lhs->eval(data, key) : "";
1360 return rhs ? rhs->eval(data, key) : "";
140std::string Ternary_Eval_Task::eval(const Element_With_Context< Way_Skeleton >& data, const std::string* key) const
14113135023 q{
14213135023 q if (!condition)
1430 return "0";
14413135023 q if (string_represents_boolean_true(condition->eval(data, key)))
145333888 q return lhs ? lhs->eval(data, key) : "";
1469796218 q return rhs ? rhs->eval(data, key) : "";
14713135023 q}
150std::string Ternary_Eval_Task::eval(const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const
152641 q if (!condition)
1530 return "0";
154641 q if (string_represents_boolean_true(condition->eval(data, key)))
1550 return lhs ? lhs->eval(data, key) : "";
156641 q return rhs ? rhs->eval(data, key) : "";
160std::string Ternary_Eval_Task::eval(const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const
1617911315 q{
1627911315 q if (!condition)
1630 return "0";
1647911315 q if (string_represents_boolean_true(condition->eval(data, key)))
16548055 q return lhs ? lhs->eval(data, key) : "";
1667430810 q return rhs ? rhs->eval(data, key) : "";
1677911315 q}
170std::string Ternary_Eval_Task::eval(const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const
17132152 q{
17232152 q if (!condition)
1730 return "0";
17432152 q if (string_represents_boolean_true(condition->eval(data, key)))
1750 return lhs ? lhs->eval(data, key) : "";
17632152 q return rhs ? rhs->eval(data, key) : "";
17732152 q}
180std::string Ternary_Eval_Task::eval(const Element_With_Context< Area_Skeleton >& data, const std::string* key) const
18252 q if (!condition)
1830 return "0";
18452 q if (string_represents_boolean_true(condition->eval(data, key)))
18521 q return lhs ? lhs->eval(data, key) : "";
18631 q return rhs ? rhs->eval(data, key) : "";
190std::string Ternary_Eval_Task::eval(const Element_With_Context< Derived_Skeleton >& data, const std::string* key) const
191195655 q{
192195655 q if (!condition)
1930 return "0";
194195655 q if (string_represents_boolean_true(condition->eval(data, key)))
19521 q return lhs ? lhs->eval(data, key) : "";
196195635 q return rhs ? rhs->eval(data, key) : "";
197195655 q}
200std::string Ternary_Eval_Task::eval(uint pos, const Element_With_Context< Way_Skeleton >& data, const std::string* key) const
2010{
2020 if (!condition)
2030 return "0";
2040 if (string_represents_boolean_true(condition->eval(pos, data, key)))
2050 return lhs ? lhs->eval(pos, data, key) : "";
2060 return rhs ? rhs->eval(pos, data, key) : "";
2070}
210std::string Ternary_Eval_Task::eval(uint pos, const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const
2110{
2120 if (!condition)
2130 return "0";
2140 if (string_represents_boolean_true(condition->eval(pos, data, key)))
2150 return lhs ? lhs->eval(pos, data, key) : "";
2160 return rhs ? rhs->eval(pos, data, key) : "";
2170}
220std::string Ternary_Eval_Task::eval(uint pos, const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const
2210{
2220 if (!condition)
2230 return "0";
2240 if (string_represents_boolean_true(condition->eval(pos, data, key)))
2250 return lhs ? lhs->eval(pos, data, key) : "";
2260 return rhs ? rhs->eval(pos, data, key) : "";
2270}
230std::string Ternary_Eval_Task::eval(uint pos, const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const
2310{
2320 if (!condition)
2330 return "0";
2340 if (string_represents_boolean_true(condition->eval(pos, data, key)))
2350 return lhs ? lhs->eval(pos, data, key) : "";
2360 return rhs ? rhs->eval(pos, data, key) : "";
2370}
240Eval_Geometry_Task* Ternary_Evaluator::get_geometry_task(Prepare_Task_Context& context)
2410{
2420 Eval_Task* cond_task = condition ? condition->get_string_task(context, 0) : 0;
2430 Eval_Geometry_Task* lhs_task = lhs ? lhs->get_geometry_task(context) : 0;
2440 Eval_Geometry_Task* rhs_task = rhs ? rhs->get_geometry_task(context) : 0;
2450 return new Ternary_Eval_Geometry_Task(cond_task, lhs_task, rhs_task);
2460}
249Opaque_Geometry* Ternary_Eval_Geometry_Task::eval() const
2500{
2510 if (!condition)
2520 return 0;
2530 if (string_represents_boolean_true(condition->eval(0)))
2540 return lhs ? lhs->eval() : 0;
2550 return rhs ? rhs->eval() : 0;
2560}
259Opaque_Geometry* Ternary_Eval_Geometry_Task::eval(const Element_With_Context< Node_Skeleton >& data) const
2600{
2610 if (!condition)
2620 return 0;
2630 if (string_represents_boolean_true(condition->eval(data, 0)))
2640 return lhs ? lhs->eval(data) : 0;
2650 return rhs ? rhs->eval(data) : 0;
2660}
269Opaque_Geometry* Ternary_Eval_Geometry_Task::eval(const Element_With_Context< Attic< Node_Skeleton > >& data) const
2700{
2710 if (!condition)
2720 return 0;
2730 if (string_represents_boolean_true(condition->eval(data, 0)))
2740 return lhs ? lhs->eval(data) : 0;
2750 return rhs ? rhs->eval(data) : 0;
2760}
279Opaque_Geometry* Ternary_Eval_Geometry_Task::eval(const Element_With_Context< Way_Skeleton >& data) const
2800{
2810 if (!condition)
2820 return 0;
2830 if (string_represents_boolean_true(condition->eval(data, 0)))
2840 return lhs ? lhs->eval(data) : 0;
2850 return rhs ? rhs->eval(data) : 0;
2860}
289Opaque_Geometry* Ternary_Eval_Geometry_Task::eval(const Element_With_Context< Attic< Way_Skeleton > >& data) const
2900{
2910 if (!condition)
2920 return 0;
2930 if (string_represents_boolean_true(condition->eval(data, 0)))
2940 return lhs ? lhs->eval(data) : 0;
2950 return rhs ? rhs->eval(data) : 0;
2960}
299Opaque_Geometry* Ternary_Eval_Geometry_Task::eval(const Element_With_Context< Relation_Skeleton >& data) const
3000{
3010 if (!condition)
3020 return 0;
3030 if (string_represents_boolean_true(condition->eval(data, 0)))
3040 return lhs ? lhs->eval(data) : 0;
3050 return rhs ? rhs->eval(data) : 0;
3060}
309Opaque_Geometry* Ternary_Eval_Geometry_Task::eval(const Element_With_Context< Attic< Relation_Skeleton > >& data) const
3100{
3110 if (!condition)
3120 return 0;
3130 if (string_represents_boolean_true(condition->eval(data, 0)))
3140 return lhs ? lhs->eval(data) : 0;
3150 return rhs ? rhs->eval(data) : 0;
3160}
319Opaque_Geometry* Ternary_Eval_Geometry_Task::eval(const Element_With_Context< Area_Skeleton >& data) const
3200{
3210 if (!condition)
3220 return 0;
3230 if (string_represents_boolean_true(condition->eval(data, 0)))
3240 return lhs ? lhs->eval(data) : 0;
3250 return rhs ? rhs->eval(data) : 0;
3260}
329Opaque_Geometry* Ternary_Eval_Geometry_Task::eval(const Element_With_Context< Derived_Skeleton >& data) const
3300{
3310 if (!condition)
3320 return 0;
3330 if (string_represents_boolean_true(condition->eval(data, 0)))
3340 return lhs ? lhs->eval(data) : 0;
3350 return rhs ? rhs->eval(data) : 0;
3360}
339Requested_Context Ternary_Evaluator::request_context() const
34111369 q if (condition)
34211369 q {
34311369 q Requested_Context result = condition->request_context();
34411369 q if (lhs)
34511369 q result.add(lhs->request_context());
34611369 q if (rhs)
34711369 q result.add(rhs->request_context());
34811369 q return result;
34911369 q }
3510 return Requested_Context();
355Operator_Stmt_Maker< Ternary_Evaluator > Ternary_Evaluator::statement_maker;
356Operator_Eval_Maker< Ternary_Evaluator > Ternary_Evaluator::evaluator_maker;