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

overpass_api/statements/ternary_operator.cc

121 of 238 instrumented lines run by the suite (50.8%) 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)
25138471 q{
26138471 q Evaluator* tag_value_ = dynamic_cast< Evaluator* >(statement);
27138471 q if (!tag_value_)
280 substatement_error(get_name(), statement);
29138471 q else if (!condition)
3046471 q condition = tag_value_;
3192070 q else if (!lhs)
3246070 q {
3346070 q lhs = tag_value_;
3446070 q return_type_ = tag_value_->return_type();
3546070 q }
3646070 q else if (!rhs)
3746070 q rhs = tag_value_;
380 else
390 add_static_error(get_name() + " must have exactly three evaluator substatements.");
40138471 q}
43struct Any_Return_Type_Checker : Statement::Return_Type_Checker
4546471 q Any_Return_Type_Checker() {}
4746070 q virtual bool eval_required() const { return true; }
4846070 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)
5646872 q{
5746872 q if (result)
5846872 q {
5946872 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;
6746471 q Statement* condition = stmt_factory.create_evaluator(
6846471 q tree_it.lhs(), tree_context, Statement::Single_Return_Type_Checker(Statement::string));
6946471 q if (condition)
7046471 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);
7546471 q Statement::Eval_Return_Type rhs_expected = Statement::string;
7646471 q Statement* lhs = stmt_factory.create_evaluator(
7746471 q tree_it.rhs().lhs(), tree_context, Any_Return_Type_Checker());
7846471 q if (lhs)
7946070 q {
8046070 q result->add_statement(lhs, "");
8146070 q Evaluator* lhs_eval = (Evaluator*)lhs;
8246070 q if (lhs_eval)
8346070 q rhs_expected = lhs_eval->return_type();
8446070 q }
8541 q else if (error_output)
8641 q error_output->add_parse_error(
8741 q "Operator \":\" of operator \"?\" needs a left-hand-side argument", tree_it->line_col.first);
8946471 q Statement* rhs = stmt_factory.create_evaluator(
9046471 q tree_it.rhs().rhs(), tree_context, Statement::Single_Return_Type_Checker(rhs_expected));
9146471 q if (rhs)
9246070 q result->add_statement(rhs, "");
9341 q else if (error_output)
9441 q error_output->add_parse_error(
9541 q "Operator \":\" of operator \"?\" needs a right-hand-side argument"
9641 q " with the same return type as the left-hand-side argument", tree_it->line_col.first);
9746471 q }
9846872 q}
101Eval_Task* Ternary_Evaluator::get_string_task(Prepare_Task_Context& context, const std::string* key)
10311869 q Eval_Task* cond_task = condition ? condition->get_string_task(context, key) : 0;
10411869 q Eval_Task* lhs_task = lhs ? lhs->get_string_task(context, key) : 0;
10511869 q Eval_Task* rhs_task = rhs ? rhs->get_string_task(context, key) : 0;
10611869 q return new Ternary_Eval_Task(cond_task, lhs_task, rhs_task);
110std::string Ternary_Eval_Task::eval(const std::string* key) const
1122016 q if (!condition)
1130 return "0";
1142016 q if (string_represents_boolean_true(condition->eval(key)))
11544 q return lhs ? lhs->eval(key) : "";
1161613 q return rhs ? rhs->eval(key) : "";
120std::string Ternary_Eval_Task::eval(const Element_With_Context< Node_Skeleton >& data, const std::string* key) const
121116143814 q{
122116143814 q if (!condition)
1230 return "0";
124116143814 q if (string_represents_boolean_true(condition->eval(data, key)))
1255107189 q return lhs ? lhs->eval(data, key) : "";
1266507208 q return rhs ? rhs->eval(data, key) : "";
127116143814 q}
130std::string Ternary_Eval_Task::eval(const Element_With_Context< Attic< Node_Skeleton > >& data, const std::string* key) const
13125123 q{
13225123 q if (!condition)
1330 return "0";
13425123 q if (string_represents_boolean_true(condition->eval(data, key)))
13525123 q return lhs ? lhs->eval(data, key) : "";
1360 return rhs ? rhs->eval(data, key) : "";
13725123 q}
140std::string Ternary_Eval_Task::eval(const Element_With_Context< Way_Skeleton >& data, const std::string* key) const
14129871718 q{
14229871718 q if (!condition)
1430 return "0";
14429871718 q if (string_represents_boolean_true(condition->eval(data, key)))
1451311858 q return lhs ? lhs->eval(data, key) : "";
14616753216 q return rhs ? rhs->eval(data, key) : "";
14729871718 q}
150std::string Ternary_Eval_Task::eval(const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const
15184722 q{
15284722 q if (!condition)
1530 return "0";
15484722 q if (string_represents_boolean_true(condition->eval(data, key)))
1550 return lhs ? lhs->eval(data, key) : "";
15684722 q return rhs ? rhs->eval(data, key) : "";
15784722 q}
160std::string Ternary_Eval_Task::eval(const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const
16118147216 q{
16218147216 q if (!condition)
1630 return "0";
16418147216 q if (string_represents_boolean_true(condition->eval(data, key)))
1656608 q return lhs ? lhs->eval(data, key) : "";
16618081212 q return rhs ? rhs->eval(data, key) : "";
16718147216 q}
170std::string Ternary_Eval_Task::eval(const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const
17231 q if (!condition)
1730 return "0";
17431 q if (string_represents_boolean_true(condition->eval(data, key)))
1750 return lhs ? lhs->eval(data, key) : "";
17631 q return rhs ? rhs->eval(data, key) : "";
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)))
18511 q return lhs ? lhs->eval(data, key) : "";
18642 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
19191167 q{
19291167 q if (!condition)
1930 return "0";
19491167 q if (string_represents_boolean_true(condition->eval(data, key)))
19511 q return lhs ? lhs->eval(data, key) : "";
19691157 q return rhs ? rhs->eval(data, key) : "";
19791167 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
34111869 q if (condition)
34211869 q {
34311869 q Requested_Context result = condition->request_context();
34411869 q if (lhs)
34511869 q result.add(lhs->request_context());
34611869 q if (rhs)
34711869 q result.add(rhs->request_context());
34811869 q return result;
34911869 q }
3510 return Requested_Context();
355Operator_Stmt_Maker< Ternary_Evaluator > Ternary_Evaluator::statement_maker;
356Operator_Eval_Maker< Ternary_Evaluator > Ternary_Evaluator::evaluator_maker;