File indexing completed on 2024-04-28 15:24:57
0001 /* 0002 * predicate.h - Copyright 2005 Frerich Raabe <raabe@kde.org> 0003 * 0004 * Redistribution and use in source and binary forms, with or without 0005 * modification, are permitted provided that the following conditions 0006 * are met: 0007 * 0008 * 1. Redistributions of source code must retain the above copyright 0009 * notice, this list of conditions and the following disclaimer. 0010 * 2. Redistributions in binary form must reproduce the above copyright 0011 * notice, this list of conditions and the following disclaimer in the 0012 * documentation and/or other materials provided with the distribution. 0013 * 0014 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 0015 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 0016 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 0017 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 0018 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 0019 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0020 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0021 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0022 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 0023 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0024 */ 0025 #ifndef PREDICATE_H 0026 #define PREDICATE_H 0027 0028 #include "expression.h" 0029 0030 namespace khtml 0031 { 0032 namespace XPath 0033 { 0034 0035 class Number : public Expression 0036 { 0037 public: 0038 Number(double value); 0039 0040 bool isConstant() const override; 0041 QString dump() const override; 0042 0043 private: 0044 Value doEvaluate() const override; 0045 0046 double m_value; 0047 }; 0048 0049 class String : public Expression 0050 { 0051 public: 0052 String(const DOM::DOMString &value); 0053 0054 bool isConstant() const override; 0055 QString dump() const override; 0056 0057 private: 0058 Value doEvaluate() const override; 0059 0060 DOM::DOMString m_value; 0061 }; 0062 0063 class Negative : public Expression 0064 { 0065 public: 0066 QString dump() const override; 0067 0068 private: 0069 Value doEvaluate() const override; 0070 }; 0071 0072 class BinaryExprBase : public Expression 0073 { 0074 public: 0075 QString dump() const override; 0076 0077 protected: 0078 virtual QString opName() const = 0; 0079 }; 0080 0081 class NumericOp : public BinaryExprBase 0082 { 0083 public: 0084 enum { 0085 OP_Add = 1, 0086 OP_Sub, 0087 OP_Mul, 0088 OP_Div, 0089 OP_Mod 0090 }; 0091 0092 NumericOp(int opCode, Expression *lhs, Expression *rhs); 0093 0094 private: 0095 QString opName() const override; 0096 Value doEvaluate() const override; 0097 int opCode; 0098 }; 0099 0100 class RelationOp : public BinaryExprBase 0101 { 0102 public: 0103 enum { 0104 OP_GT = 1, 0105 OP_LT, 0106 OP_GE, 0107 OP_LE, 0108 OP_EQ, 0109 OP_NE 0110 }; 0111 0112 RelationOp(int opCode, Expression *lhs, Expression *rhs); 0113 0114 private: 0115 QString opName() const override; 0116 Value doEvaluate() const override; 0117 int opCode; 0118 0119 // compares strings based on the op-code 0120 bool compareStrings(const DOM::DOMString &l, const DOM::DOMString &r) const; 0121 bool compareNumbers(double l, double r) const; 0122 }; 0123 0124 class LogicalOp : public BinaryExprBase 0125 { 0126 public: 0127 enum { 0128 OP_And = 1, 0129 OP_Or 0130 }; 0131 0132 LogicalOp(int opCode, Expression *lhs, Expression *rhs); 0133 0134 bool isConstant() const override; 0135 0136 private: 0137 bool shortCircuitOn() const; 0138 QString opName() const override; 0139 Value doEvaluate() const override; 0140 int opCode; 0141 }; 0142 0143 class Union : public BinaryExprBase 0144 { 0145 private: 0146 QString opName() const override; 0147 Value doEvaluate() const override; 0148 }; 0149 0150 class Predicate 0151 { 0152 public: 0153 Predicate(Expression *expr); 0154 ~Predicate(); 0155 0156 bool evaluate() const; 0157 0158 void optimize(); 0159 QString dump() const; 0160 0161 private: 0162 Predicate(const Predicate &rhs); 0163 Predicate &operator=(const Predicate &rhs); 0164 0165 Expression *m_expr; 0166 }; 0167 0168 } // namespace XPath 0169 0170 } // namespace khtml 0171 0172 #endif // PREDICATE_H 0173