1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al. 3 * This file is part of Overpass_API. 5 * Overpass_API is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Affero General Public License as 7 * published by the Free Software Foundation, either version 3 of the 8 * License, or (at your option) any later version. 10 * Overpass_API is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 15 * You should have received a copy of the GNU Affero General Public License 16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>. 19#include "../data/tag_store.h" 20#include "../data/utils.h" 21#include "ternary_operator.h" 24void Ternary_Evaluator::add_statement(Statement* statement, std::string text) 26138471 q Evaluator* tag_value_ = dynamic_cast< Evaluator* >(statement); 280 substatement_error(get_name(), statement); 29138471 q else if (!condition) 3046471 q condition = tag_value_; 3446070 q return_type_ = tag_value_->return_type(); 390 add_static_error(get_name() + " must have exactly three evaluator substatements."); 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"; } 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) 5946872 q if (tree_it.rhs()->token != ":") 6241 q error_output->add_parse_error( 6341 q "Operator \"?\" must have a \":\" operator as right-hand-side argument", tree_it->line_col.first); 6746471 q Statement* condition = stmt_factory.create_evaluator( 6846471 q tree_it.lhs(), tree_context, Statement::Single_Return_Type_Checker(Statement::string)); 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()); 8046070 q result->add_statement(lhs, ""); 8146070 q Evaluator* lhs_eval = (Evaluator*)lhs; 8346070 q rhs_expected = lhs_eval->return_type(); 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)); 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); 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 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 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) : ""; 130std::string Ternary_Eval_Task::eval(const Element_With_Context< Attic< Node_Skeleton > >& data, const std::string* key) const 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) : ""; 140std::string Ternary_Eval_Task::eval(const Element_With_Context< Way_Skeleton >& data, const std::string* key) const 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) : ""; 150std::string Ternary_Eval_Task::eval(const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const 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) : ""; 160std::string Ternary_Eval_Task::eval(const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const 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) : ""; 170std::string Ternary_Eval_Task::eval(const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const 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 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 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) : ""; 200std::string Ternary_Eval_Task::eval(uint pos, const Element_With_Context< Way_Skeleton >& data, const std::string* key) const 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) : ""; 210std::string Ternary_Eval_Task::eval(uint pos, const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const 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) : ""; 220std::string Ternary_Eval_Task::eval(uint pos, const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const 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) : ""; 230std::string Ternary_Eval_Task::eval(uint pos, const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const 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) : ""; 240Eval_Geometry_Task* Ternary_Evaluator::get_geometry_task(Prepare_Task_Context& context) 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); 249Opaque_Geometry* Ternary_Eval_Geometry_Task::eval() const 2530 if (string_represents_boolean_true(condition->eval(0))) 2540 return lhs ? lhs->eval() : 0; 2550 return rhs ? rhs->eval() : 0; 259Opaque_Geometry* Ternary_Eval_Geometry_Task::eval(const Element_With_Context< Node_Skeleton >& data) const 2630 if (string_represents_boolean_true(condition->eval(data, 0))) 2640 return lhs ? lhs->eval(data) : 0; 2650 return rhs ? rhs->eval(data) : 0; 269Opaque_Geometry* Ternary_Eval_Geometry_Task::eval(const Element_With_Context< Attic< Node_Skeleton > >& data) const 2730 if (string_represents_boolean_true(condition->eval(data, 0))) 2740 return lhs ? lhs->eval(data) : 0; 2750 return rhs ? rhs->eval(data) : 0; 279Opaque_Geometry* Ternary_Eval_Geometry_Task::eval(const Element_With_Context< Way_Skeleton >& data) const 2830 if (string_represents_boolean_true(condition->eval(data, 0))) 2840 return lhs ? lhs->eval(data) : 0; 2850 return rhs ? rhs->eval(data) : 0; 289Opaque_Geometry* Ternary_Eval_Geometry_Task::eval(const Element_With_Context< Attic< Way_Skeleton > >& data) const 2930 if (string_represents_boolean_true(condition->eval(data, 0))) 2940 return lhs ? lhs->eval(data) : 0; 2950 return rhs ? rhs->eval(data) : 0; 299Opaque_Geometry* Ternary_Eval_Geometry_Task::eval(const Element_With_Context< Relation_Skeleton >& data) const 3030 if (string_represents_boolean_true(condition->eval(data, 0))) 3040 return lhs ? lhs->eval(data) : 0; 3050 return rhs ? rhs->eval(data) : 0; 309Opaque_Geometry* Ternary_Eval_Geometry_Task::eval(const Element_With_Context< Attic< Relation_Skeleton > >& data) const 3130 if (string_represents_boolean_true(condition->eval(data, 0))) 3140 return lhs ? lhs->eval(data) : 0; 3150 return rhs ? rhs->eval(data) : 0; 319Opaque_Geometry* Ternary_Eval_Geometry_Task::eval(const Element_With_Context< Area_Skeleton >& data) const 3230 if (string_represents_boolean_true(condition->eval(data, 0))) 3240 return lhs ? lhs->eval(data) : 0; 3250 return rhs ? rhs->eval(data) : 0; 329Opaque_Geometry* Ternary_Eval_Geometry_Task::eval(const Element_With_Context< Derived_Skeleton >& data) const 3330 if (string_represents_boolean_true(condition->eval(data, 0))) 3340 return lhs ? lhs->eval(data) : 0; 3350 return rhs ? rhs->eval(data) : 0; 339Requested_Context Ternary_Evaluator::request_context() const 34311869 q Requested_Context result = condition->request_context(); 34511869 q result.add(lhs->request_context()); 34711869 q result.add(rhs->request_context()); 3510 return Requested_Context(); 355Operator_Stmt_Maker< Ternary_Evaluator > Ternary_Evaluator::statement_maker; 356Operator_Eval_Maker< Ternary_Evaluator > Ternary_Evaluator::evaluator_maker;