KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

351 lines
8.7KB

  1. /* This file is part of the KDE project
  2. Copyright (C) 2003,2004 Ariya Hidayat <ariya@kde.org>
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public License
  12. along with this library; see the file COPYING.LIB. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  14. * Boston, MA 02110-1301, USA.
  15. */
  16. #ifndef KSPREAD_FORMULA
  17. #define KSPREAD_FORMULA
  18. #include <tqstring.h>
  19. #include <tqvaluevector.h>
  20. class TDELocale;
  21. namespace KSpread
  22. {
  23. class Cell;
  24. class Sheet;
  25. class Value;
  26. /**
  27. * Token
  28. */
  29. class Token
  30. {
  31. public:
  32. /**
  33. * token types
  34. */
  35. enum Type
  36. {
  37. Unknown = 0, ///< unknown type
  38. Boolean, ///< True, False (also i18n-ized)
  39. Integer, ///< 14, 3, 1977
  40. Float, ///< 3.141592, 1e10, 5.9e-7
  41. String, ///< "KOffice", "The quick brown fox..."
  42. Operator, ///< +, *, /, -
  43. Cell, ///< $A$1, F4, Sheet2!B5, 'Sales Forecast'!Sum
  44. Range, ///< C1:C100
  45. Identifier ///< function name or named area
  46. };
  47. /**
  48. * operator types
  49. */
  50. enum Op
  51. {
  52. InvalidOp = 0, ///< invalid operator
  53. Plus, ///< + (addition)
  54. Minus, ///< - (substraction, negation)
  55. Asterisk, ///< * (multiplication)
  56. Slash, ///< / (division)
  57. Caret, ///< ^ (power)
  58. LeftPar, ///< (
  59. RightPar, ///< )
  60. Comma, ///< ,
  61. Semicolon, ///< ; (argument separator)
  62. Ampersand, ///< & (string concat)
  63. Equal, ///< =
  64. NotEqual, ///< <>
  65. Less, ///< <
  66. Greater, ///< >
  67. LessEqual, ///< <=
  68. GreaterEqual, ///< >=
  69. Percent ///< %
  70. };
  71. /**
  72. * Creates a token.
  73. */
  74. Token( Type type = Unknown, const TQString& text = TQString(), int pos = -1 );
  75. static const Token null;
  76. Token( const Token& );
  77. Token& operator=( const Token& );
  78. /**
  79. * Returns type of the token.
  80. */
  81. Type type() const { return m_type; }
  82. /**
  83. * Returns text associated with the token.
  84. *
  85. * If you want to obtain meaningful value of this token, instead of
  86. * text(), you might use asInteger(), asFloat(), asString(), sheetName(),
  87. * etc.
  88. */
  89. TQString text() const { return m_text; }
  90. int pos() const { return m_pos; };
  91. /**
  92. * Returns true if token is a boolean token.
  93. */
  94. bool isBoolean() const { return m_type == Boolean; }
  95. /**
  96. * Returns true if token is a integer token.
  97. */
  98. bool isInteger() const { return m_type == Integer; }
  99. /**
  100. * Returns true if token is a floating-point token.
  101. */
  102. bool isFloat() const { return m_type == Float; }
  103. /**
  104. * Returns true if token is either integer or floating-point token.
  105. */
  106. bool isNumber() const { return (m_type == Integer) || (m_type == Float); }
  107. /**
  108. * Returns true if token is a string token.
  109. */
  110. bool isString() const { return m_type == String; }
  111. /**
  112. * Returns true if token is an operator token.
  113. */
  114. bool isOperator() const { return m_type == Operator; }
  115. /**
  116. * Returns true if token is a cell reference token.
  117. */
  118. bool isCell() const { return m_type == Cell; }
  119. /**
  120. * Returns true if token is a range reference token.
  121. */
  122. bool isRange() const { return m_type == Range; }
  123. /**
  124. * Returns true if token is an identifier.
  125. */
  126. bool isIdentifier() const { return m_type == Identifier; }
  127. /**
  128. * Returns boolean value for an boolean token.
  129. * For any other type of token, return value is undefined.
  130. */
  131. bool asBoolean() const;
  132. /**
  133. * Returns integer value for an integer token.
  134. * For any other type of token, returns 0.
  135. */
  136. long asInteger() const;
  137. /**
  138. * Returns floating-point value for a floating-point token.
  139. * For any other type of token, returns 0.0.
  140. */
  141. double asFloat() const;
  142. /**
  143. * Returns string value for a string token.
  144. * For any other type of token, it returns TQString().
  145. *
  146. * Note that token text for a string token still has leading and trailing
  147. * double-quotes, i.e for "KOffice", text() return "KOffice"
  148. * (with the quotes, 9 characters) while asString() only return KOffice
  149. * (without quotes, 7 characters).
  150. */
  151. TQString asString() const;
  152. /**
  153. * Returns operator value for an operator token.
  154. * For any other type of token, returns Token::InvalidOp.
  155. */
  156. Op asOperator() const;
  157. /**
  158. * Returns sheet name in a cell reference token.
  159. * For any other type of token, it returns TQString().
  160. *
  161. * If the cell reference doesn't specify sheet name, an empty string
  162. * is returned. As example, for "Sheet1!B3" , sheetName() returns
  163. * "Sheet1" while for "A2" sheetName() returns "".
  164. *
  165. * When sheet name contains quotes (as if the name has spaces) like
  166. * in "'Sales Forecast'!F4", sheetName() returns the name
  167. * without the quotes, i.e "Sales Forecast" in this case.
  168. */
  169. TQString sheetName() const;
  170. /**
  171. * Returns a short description of the token.
  172. * Should be used only to assist debugging.
  173. */
  174. TQString description() const;
  175. protected:
  176. Type m_type;
  177. TQString m_text;
  178. int m_pos;
  179. };
  180. /**
  181. * Class Tokens represents array of tokens.
  182. *
  183. */
  184. class Tokens: public TQValueVector<Token>
  185. {
  186. public:
  187. Tokens(): TQValueVector<Token>(), m_valid(true) {};
  188. bool valid() const { return m_valid; }
  189. void setValid( bool v ){ m_valid = v; }
  190. protected:
  191. bool m_valid;
  192. };
  193. /**
  194. * Class Formula encapsulates a formula for a cell.
  195. *
  196. * A Formula is a equations which perform calculations on values in the cells
  197. * and sheets. Every formula must start with an equal sign (=).
  198. *
  199. *
  200. */
  201. class Formula
  202. {
  203. public:
  204. /**
  205. * Creates a formula. It must be owned by a sheet, and optionally sheet.
  206. */
  207. Formula (Sheet *sheet, Cell *cell = 0);
  208. /**
  209. * Creates a formula that is not owned by any sheet.
  210. * This might be useful in some cases.
  211. */
  212. Formula();
  213. /**
  214. * Destroys the formula.
  215. */
  216. ~Formula();
  217. /**
  218. * Returns the cell which owns this formula.
  219. */
  220. Sheet* sheet() const;
  221. /**
  222. * Returns the cell which owns this formula.
  223. */
  224. Cell* cell() const;
  225. /**
  226. * Sets the expression for this formula.
  227. */
  228. void setExpression( const TQString& expr );
  229. /**
  230. * Gets the expression of this formula.
  231. */
  232. TQString expression() const;
  233. /**
  234. * Clears everything, makes as like a newly constructed formula.
  235. */
  236. void clear();
  237. /**
  238. * Returns true if the specified expression is valid, i.e. it contains
  239. * no parsing error.
  240. * Empty formula (i.e. without expression) is always invalid.
  241. */
  242. bool isValid() const;
  243. /**
  244. * Returns list of tokens associated with this formula. This has nothing to
  245. * with the formula evaluation but might be useful, e.g. for syntax
  246. * highlight or similar features.
  247. * If the formula contains error, the returned tokens is invalid.
  248. */
  249. Tokens tokens() const;
  250. /**
  251. * Evaluates the formula and returns the result.
  252. */
  253. Value eval() const;
  254. /**
  255. * Given an expression, this function separates it into tokens.
  256. * If the expression contains error (e.g. unknown operator, string no terminated)
  257. * this function returns tokens which is not valid.
  258. */
  259. Tokens scan( const TQString& expr, TDELocale* locale = 0 ) const;
  260. TQString dump() const;
  261. protected:
  262. void compile( const Tokens& tokens ) const;
  263. /**
  264. * helper function: return true for valid named area
  265. */
  266. bool isNamedArea( const TQString& expr ) const;
  267. private:
  268. class Private;
  269. Private *d;
  270. // no copy or assign
  271. Formula( const Formula& );
  272. Formula& operator=( const Formula& );
  273. };
  274. /**
  275. * Dumps the formula, should be used only to assist debugging.
  276. */
  277. TQTextStream& operator<<( TQTextStream& ts, Formula formula );
  278. /**
  279. * helper function: return operator of given token text
  280. * e.g. "*" yields Operator::Asterisk, and so on
  281. */
  282. Token::Op matchOperator( const TQString& text );
  283. /**
  284. * helper function: return true for valid identifier character
  285. */
  286. bool isIdentifier( TQChar ch );
  287. } // namespace KSpread
  288. #endif // KSPREAD_FORMULA