kspread
formula.h00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef KSPREAD_FORMULA
00021 #define KSPREAD_FORMULA
00022
00023 #include <qstring.h>
00024 #include <qvaluevector.h>
00025
00026 class KLocale;
00027
00028 namespace KSpread
00029 {
00030 class Cell;
00031 class Sheet;
00032 class Value;
00033
00037 class Token
00038 {
00039 public:
00043 enum Type
00044 {
00045 Unknown = 0,
00046 Boolean,
00047 Integer,
00048 Float,
00049 String,
00050 Operator,
00051 Cell,
00052 Range,
00053 Identifier
00054 };
00055
00059 enum Op
00060 {
00061 InvalidOp = 0,
00062 Plus,
00063 Minus,
00064 Asterisk,
00065 Slash,
00066 Caret,
00067 LeftPar,
00068 RightPar,
00069 Comma,
00070 Semicolon,
00071 Ampersand,
00072 Equal,
00073 NotEqual,
00074 Less,
00075 Greater,
00076 LessEqual,
00077 GreaterEqual,
00078 Percent
00079 };
00080
00084 Token( Type type = Unknown, const QString& text = QString::null, int pos = -1 );
00085
00086 static const Token null;
00087
00088 Token( const Token& );
00089 Token& operator=( const Token& );
00090
00094 Type type() const { return m_type; }
00095
00103 QString text() const { return m_text; }
00104
00105 int pos() const { return m_pos; };
00106
00110 bool isBoolean() const { return m_type == Boolean; }
00111
00115 bool isInteger() const { return m_type == Integer; }
00116
00120 bool isFloat() const { return m_type == Float; }
00121
00125 bool isNumber() const { return (m_type == Integer) || (m_type == Float); }
00126
00130 bool isString() const { return m_type == String; }
00131
00135 bool isOperator() const { return m_type == Operator; }
00136
00140 bool isCell() const { return m_type == Cell; }
00141
00145 bool isRange() const { return m_type == Range; }
00146
00150 bool isIdentifier() const { return m_type == Identifier; }
00151
00156 bool asBoolean() const;
00157
00162 long asInteger() const;
00163
00168 double asFloat() const;
00169
00179 QString asString() const;
00180
00185 Op asOperator() const;
00186
00199 QString sheetName() const;
00200
00205 QString description() const;
00206
00207 protected:
00208
00209 Type m_type;
00210 QString m_text;
00211 int m_pos;
00212
00213 };
00214
00219 class Tokens: public QValueVector<Token>
00220 {
00221 public:
00222 Tokens(): QValueVector<Token>(), m_valid(true) {};
00223 bool valid() const { return m_valid; }
00224 void setValid( bool v ){ m_valid = v; }
00225 protected:
00226 bool m_valid;
00227 };
00228
00229
00238 class Formula
00239 {
00240 public:
00241
00245 Formula (Sheet *sheet, Cell *cell = 0);
00246
00251 Formula();
00252
00256 ~Formula();
00257
00261 Sheet* sheet() const;
00265 Cell* cell() const;
00266
00270 void setExpression( const QString& expr );
00271
00275 QString expression() const;
00276
00280 void clear();
00281
00287 bool isValid() const;
00288
00295 Tokens tokens() const;
00296
00300 Value eval() const;
00301
00307 Tokens scan( const QString& expr, KLocale* locale = 0 ) const;
00308
00309 QString dump() const;
00310
00311 protected:
00312
00313 void compile( const Tokens& tokens ) const;
00314
00318 bool isNamedArea( const QString& expr ) const;
00319
00320 private:
00321 class Private;
00322 Private *d;
00323
00324 Formula( const Formula& );
00325 Formula& operator=( const Formula& );
00326 };
00327
00331 QTextStream& operator<<( QTextStream& ts, Formula formula );
00332
00333
00338 Token::Op matchOperator( const QString& text );
00339
00343 bool isIdentifier( QChar ch );
00344
00345 }
00346
00347
00348
00349 #endif // KSPREAD_FORMULA
00350
|