Overpass API regression suite coverage (Nova Scotia) / overpass_api/statements/binary_operators.h
overpass_api/statements/binary_operators.h
64 of 68 instrumented lines run by the suite (94.1%) 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 modify6 * it under the terms of the GNU Affero General Public License as7 * published by the Free Software Foundation, either version 3 of the8 * 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 of12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13 * GNU General Public License for more details.14 *15 * You should have received a copy of the GNU Affero General Public License16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.17 */19#ifndef DE__OSM3S___OVERPASS_API__STATEMENTS__BINARY_OPERATORS_H20#define DE__OSM3S___OVERPASS_API__STATEMENTS__BINARY_OPERATORS_H23#include "../data/tag_store.h"24#include "../data/utils.h"25#include "evaluator.h"26#include "statement.h"28#include <map>29#include <string>30#include <vector>33/* === Binary Operators ===35Binary operators need for execution two operands.36They are always written in infix notation.37The operators can be grouped with parentheses:38The two variants40 <Evaulator><Operator><Evaluator>42and44 (<Evaulator><Operator><Evaluator>)46have as standalone expressions precisely the same semantics.47The parenthesis variant exists to override operator precedence:49 2 + 3 * 451is evaluated to <em>2 + 12</em>, then finally <em>14</em>.53 (2 + 3) * 455is evaluated to <em>5 * 4</em>, then finally <em>20</em>.57The order of precedence is as follows, ordered weak to strong binding:58* the ternary operator59* logical disjunction60* logical conjunction61* equality, inequality62* less, less-equal, greater, greater-equal63* plus, binary minus64* times, divided65* logical negation66* unary minus68In the following, the operators are ordered by precedence, stronger binding last.69*/71class Evaluator_Pair_Operator : public Evaluator72{73public:74 Evaluator_Pair_Operator(int line_number_);77 virtual void add_statement(Statement* statement, std::string text);81 virtual Requested_Context request_context() const;84 virtual Eval_Task* get_string_task(Prepare_Task_Context& context, const std::string* key);86 virtual std::string process(const std::string& lhs_result, const std::string& rhs_result) const = 0;881168121 q static bool applicable_by_subtree_structure(const Token_Node_Ptr& tree_it) { return tree_it->lhs && tree_it->rhs; }89 static void add_substatements(Statement* result, const std::string& operator_name, const Token_Node_Ptr& tree_it,90 Statement::QL_Context tree_context, Statement::Factory& stmt_factory, Error_Output* error_output);92protected:93 Evaluator* lhs;94 Evaluator* rhs;95};98struct Binary_Eval_Task : public Eval_Task99{100 Binary_Eval_Task(Eval_Task* lhs_, Eval_Task* rhs_, Evaluator_Pair_Operator* evaluator_)102 ~Binary_Eval_Task()108 virtual std::string eval(const std::string* key) const;110 virtual std::string eval(const Element_With_Context< Node_Skeleton >& data, const std::string* key) const;111 virtual std::string eval(const Element_With_Context< Attic< Node_Skeleton > >& data, const std::string* key) const;112 virtual std::string eval(const Element_With_Context< Way_Skeleton >& data, const std::string* key) const;113 virtual std::string eval(const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const;114 virtual std::string eval(const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const;115 virtual std::string eval(const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const;116 virtual std::string eval(const Element_With_Context< Area_Skeleton >& data, const std::string* key) const;117 virtual std::string eval(const Element_With_Context< Derived_Skeleton >& data, const std::string* key) const;119 virtual std::string eval(uint pos, const Element_With_Context< Way_Skeleton >& data, const std::string* key) const;120 virtual std::string eval(uint pos, const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const;121 virtual std::string eval(uint pos, const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const;122 virtual std::string eval(uint pos, const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const;124private:125 Eval_Task* lhs;126 Eval_Task* rhs;127 Evaluator_Pair_Operator* evaluator;128};131template< typename Evaluator_ >132struct Evaluator_Pair_Operator_Syntax : public Evaluator_Pair_Operator133{134 Evaluator_Pair_Operator_Syntax(int line_number_, const std::map< std::string, std::string >& input_attributes)141 virtual std::string dump_xml(const std::string& indent) const149 virtual std::string dump_compact_ql(const std::string&) const152538117 q (lhs->get_operator_priority() < get_operator_priority() ? std::string("(") + lhs->dump_compact_ql("") + ")"156538117 q (rhs->get_operator_priority() < get_operator_priority() ? std::string("(") + rhs->dump_compact_ql("") + ")"1611300117 q virtual int get_operator_priority() const { return operator_priority(Evaluator_::stmt_operator(), false); }162};165/* ==== Boolean Disjunction ====167The boolean disjunction evaluates to "1" if one or both of its arguments evaluate to a representation of boolean true.168Otherwise it evaluates to "0".169Representations of boolean false are the empty string and every string that is a numerical representation of zero.170Every other string represents boolean true.171Currently, both arguments are always evaluated.172This may change in future versions.174Its syntax is176 <Evaluator> || <Evaluator>178The whitespace is optional.179*/181struct Evaluator_Or : public Evaluator_Pair_Operator_Syntax< Evaluator_Or >182{183 static Operator_Stmt_Maker< Evaluator_Or > statement_maker;184 static Operator_Eval_Maker< Evaluator_Or > evaluator_maker;188 Evaluator_Or(int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)191 virtual std::string process(const std::string& lhs_result, const std::string& rhs_result) const;192};195/* ==== Boolean Conjunction ====197The boolean conjunction evaluates to "1" if both of its arguments evaluate to a representation of boolean true.198Otherwise it evaluates to "0".199Representations of boolean false are the empty string and every string that is a numerical representation of zero.200Every other string represents boolean true.201Currently, both arguments are always evaluated.202This may change in future versions.204Its syntax is206 <Evaluator> && <Evaluator>208The whitespace is optional.209*/211struct Evaluator_And : public Evaluator_Pair_Operator_Syntax< Evaluator_And >212{213 static Operator_Stmt_Maker< Evaluator_And > statement_maker;214 static Operator_Eval_Maker< Evaluator_And > evaluator_maker;218 Evaluator_And(int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)221 virtual std::string process(const std::string& lhs_result, const std::string& rhs_result) const;222};225/* ==== Equality and Inequality ====227The equaliy operator evaluates to "1" if both of its arguments are equal.228Otherwise it evaluates to "0".229The inequality operator evaluates to "0" if both of its arguments are equal.230Otherwise it evaluates to "1".231If both arguments can be interpreted as integers then the represented values are compared.232Otherwise, if both arguments can be interpreted as floating point numbers then the represented values are compared.233In all other cases the arguments are treated as strings.235Its syntax is for equality237 <Evaluator> == <Evaluator>239and for inequality241 <Evaluator> != <Evaluator>243The whitespace is optional.244*/246struct Evaluator_Equal : public Evaluator_Pair_Operator_Syntax< Evaluator_Equal >247{248 static Operator_Stmt_Maker< Evaluator_Equal > statement_maker;249 static Operator_Eval_Maker< Evaluator_Equal > evaluator_maker;253 Evaluator_Equal(int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)256 virtual std::string process(const std::string& lhs_result, const std::string& rhs_result) const;257};260struct Evaluator_Not_Equal : public Evaluator_Pair_Operator_Syntax< Evaluator_Not_Equal >261{262 static Operator_Stmt_Maker< Evaluator_Not_Equal > statement_maker;263 static Operator_Eval_Maker< Evaluator_Not_Equal > evaluator_maker;267 Evaluator_Not_Equal(int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)2684010 q : Evaluator_Pair_Operator_Syntax< Evaluator_Not_Equal >(line_number_, input_attributes) {}270 virtual std::string process(const std::string& lhs_result, const std::string& rhs_result) const;271};274/* ==== Less, Less-Equal, Greater, and Greater-Equal ====276These operators evaluate to "1" if their arguments compare respectively.277Otherwise they evaluate to "0".278If both arguments can be interpreted as integers then the represented values are compared.279Otherwise, if both arguments can be interpreted as floating point numbers then the represented values are compared.280In all other cases the arguments are treated as strings.282The syntaxes for less, less-equal, greater, and greater-equal in this order are284 <Evaluator> < <Evaluator>286 <Evaluator> <= <Evaluator>288 <Evaluator> > <Evaluator>290 <Evaluator> >= <Evaluator>292The whitespace is optional.293*/295struct Evaluator_Less : public Evaluator_Pair_Operator_Syntax< Evaluator_Less >296{297 static Operator_Stmt_Maker< Evaluator_Less > statement_maker;298 static Operator_Eval_Maker< Evaluator_Less > evaluator_maker;302 Evaluator_Less(int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)305 virtual std::string process(const std::string& lhs_result, const std::string& rhs_result) const;306};309struct Evaluator_Less_Equal : public Evaluator_Pair_Operator_Syntax< Evaluator_Less_Equal >310{311 static Operator_Stmt_Maker< Evaluator_Less_Equal > statement_maker;312 static Operator_Eval_Maker< Evaluator_Less_Equal > evaluator_maker;316 Evaluator_Less_Equal(int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)317328 q : Evaluator_Pair_Operator_Syntax< Evaluator_Less_Equal >(line_number_, input_attributes) {}319 virtual std::string process(const std::string& lhs_result, const std::string& rhs_result) const;320};323struct Evaluator_Greater : public Evaluator_Pair_Operator_Syntax< Evaluator_Greater >324{325 static Operator_Stmt_Maker< Evaluator_Greater > statement_maker;326 static Operator_Eval_Maker< Evaluator_Greater > evaluator_maker;330 Evaluator_Greater(int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)333 virtual std::string process(const std::string& lhs_result, const std::string& rhs_result) const;334};337struct Evaluator_Greater_Equal : public Evaluator_Pair_Operator_Syntax< Evaluator_Greater_Equal >338{339 static Operator_Stmt_Maker< Evaluator_Greater_Equal > statement_maker;340 static Operator_Eval_Maker< Evaluator_Greater_Equal > evaluator_maker;344 Evaluator_Greater_Equal(int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)3454811 q : Evaluator_Pair_Operator_Syntax< Evaluator_Greater_Equal >(line_number_, input_attributes) {}347 virtual std::string process(const std::string& lhs_result, const std::string& rhs_result) const;348};351/* ==== Plus and Minus ====353While both operators have the same priority, they accept different kinds of arguments.354If the arguments are both integers then they are added resp. subtracted as integers.355If the arguments are both floating point numbers then they are added resp. subtracted as floating point numbers.356Otherwise the plus operator concatenates the arguments as strings.357Opposed to this, the minus operator returns "NaN".359The unary minus is an operator distinct from the binary minus operator defined here.360It has a higher priority.362The syntaxes for plus and minus in this order are364 <Evaluator> + <Evaluator>366 <Evaluator> - <Evaluator>368The whitespace is optional.369*/371struct Evaluator_Plus : public Evaluator_Pair_Operator_Syntax< Evaluator_Plus >372{373 static Operator_Stmt_Maker< Evaluator_Plus > statement_maker;374 static Operator_Eval_Maker< Evaluator_Plus > evaluator_maker;378 Evaluator_Plus(int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)381 virtual std::string process(const std::string& lhs_result, const std::string& rhs_result) const;382};385struct Evaluator_Minus : public Evaluator_Pair_Operator_Syntax< Evaluator_Minus >386{387 static Operator_Stmt_Maker< Evaluator_Minus > statement_maker;388 static Operator_Eval_Maker< Evaluator_Minus > evaluator_maker;392 Evaluator_Minus(int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)395 virtual std::string process(const std::string& lhs_result, const std::string& rhs_result) const;396};399/* ==== Times and Divided ====401The times operator and the divided operator perform the respective arithmetic operations.402If the arguments are both integers then they are multiplied as integers.403Otherwise, if the arguments are both floating point numbers then they are multiplied resp. divided as floating point numbers.404Division does treat integers like floating point numbers.405If one or both arguments are no numbers then both the operators return "NaN".407The syntaxes for plus and minus in this order are409 <Evaluator> * <Evaluator>411 <Evaluator> / <Evaluator>413The whitespace is optional.414*/416struct Evaluator_Times : public Evaluator_Pair_Operator_Syntax< Evaluator_Times >417{418 static Operator_Stmt_Maker< Evaluator_Times > statement_maker;419 static Operator_Eval_Maker< Evaluator_Times > evaluator_maker;423 Evaluator_Times(int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)426 virtual std::string process(const std::string& lhs_result, const std::string& rhs_result) const;427};430struct Evaluator_Divided : public Evaluator_Pair_Operator_Syntax< Evaluator_Divided >431{432 static Operator_Stmt_Maker< Evaluator_Divided > statement_maker;433 static Operator_Eval_Maker< Evaluator_Divided > evaluator_maker;437 Evaluator_Divided(int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)440 virtual std::string process(const std::string& lhs_result, const std::string& rhs_result) const;441};444#endif