1<?php 2/** 3* Class for parsing Excel formulas 4* 5* License Information: 6* 7* Spreadsheet_Excel_Writer: A library for generating Excel Spreadsheets 8* Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com 9* 10* This library is free software; you can redistribute it and/or 11* modify it under the terms of the GNU Lesser General Public 12* License as published by the Free Software Foundation; either 13* version 2.1 of the License, or (at your option) any later version. 14* 15* This library is distributed in the hope that it will be useful, 16* but WITHOUT ANY WARRANTY; without even the implied warranty of 17* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18* Lesser General Public License for more details. 19* 20* You should have received a copy of the GNU Lesser General Public 21* License along with this library; if not, write to the Free Software 22* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23*/ 24 25/** 26* @const SPREADSHEET_EXCEL_WRITER_ADD token identifier for character "+" 27*/ 28define('SPREADSHEET_EXCEL_WRITER_ADD',"+"); 29 30/** 31* @const SPREADSHEET_EXCEL_WRITER_SUB token identifier for character "-" 32*/ 33define('SPREADSHEET_EXCEL_WRITER_SUB',"-"); 34 35/** 36* @const SPREADSHEET_EXCEL_WRITER_MUL token identifier for character "*" 37*/ 38define('SPREADSHEET_EXCEL_WRITER_MUL',"*"); 39 40/** 41* @const SPREADSHEET_EXCEL_WRITER_DIV token identifier for character "/" 42*/ 43define('SPREADSHEET_EXCEL_WRITER_DIV',"/"); 44 45/** 46* @const SPREADSHEET_EXCEL_WRITER_OPEN token identifier for character "(" 47*/ 48define('SPREADSHEET_EXCEL_WRITER_OPEN',"("); 49 50/** 51* @const SPREADSHEET_EXCEL_WRITER_CLOSE token identifier for character ")" 52*/ 53define('SPREADSHEET_EXCEL_WRITER_CLOSE',")"); 54 55/** 56* @const SPREADSHEET_EXCEL_WRITER_COMA token identifier for character "," 57*/ 58define('SPREADSHEET_EXCEL_WRITER_COMA',","); 59 60/** 61* @const SPREADSHEET_EXCEL_WRITER_SEMICOLON token identifier for character ";" 62*/ 63define('SPREADSHEET_EXCEL_WRITER_SEMICOLON',";"); 64 65/** 66* @const SPREADSHEET_EXCEL_WRITER_GT token identifier for character ">" 67*/ 68define('SPREADSHEET_EXCEL_WRITER_GT',">"); 69 70/** 71* @const SPREADSHEET_EXCEL_WRITER_LT token identifier for character "<" 72*/ 73define('SPREADSHEET_EXCEL_WRITER_LT',"<"); 74 75/** 76* @const SPREADSHEET_EXCEL_WRITER_LE token identifier for character "<=" 77*/ 78define('SPREADSHEET_EXCEL_WRITER_LE',"<="); 79 80/** 81* @const SPREADSHEET_EXCEL_WRITER_GE token identifier for character ">=" 82*/ 83define('SPREADSHEET_EXCEL_WRITER_GE',">="); 84 85/** 86* @const SPREADSHEET_EXCEL_WRITER_EQ token identifier for character "=" 87*/ 88define('SPREADSHEET_EXCEL_WRITER_EQ',"="); 89 90/** 91* @const SPREADSHEET_EXCEL_WRITER_NE token identifier for character "<>" 92*/ 93define('SPREADSHEET_EXCEL_WRITER_NE',"<>"); 94 95 96/** 97* Class for parsing Excel formulas 98* 99* @author Xavier Noguer <xnoguer@rezebra.com> 100* @category FileFormats 101* @package Spreadsheet_Excel_Writer 102*/ 103 104class Spreadsheet_Excel_Writer_Parser extends PEAR 105{ 106 /** 107 * The index of the character we are currently looking at 108 * @var integer 109 */ 110 var $_current_char; 111 112 /** 113 * The token we are working on. 114 * @var string 115 */ 116 var $_current_token; 117 118 /** 119 * The formula to parse 120 * @var string 121 */ 122 var $_formula; 123 124 /** 125 * The character ahead of the current char 126 * @var string 127 */ 128 var $_lookahead; 129 130 /** 131 * The parse tree to be generated 132 * @var string 133 */ 134 var $_parse_tree; 135 136 /** 137 * The byte order. 1 => big endian, 0 => little endian. 138 * @var integer 139 */ 140 var $_byte_order; 141 142 /** 143 * Array of external sheets 144 * @var array 145 */ 146 var $_ext_sheets; 147 148 /** 149 * Array of sheet references in the form of REF structures 150 * @var array 151 */ 152 var $_references; 153 154 /** 155 * The BIFF version for the workbook 156 * @var integer 157 */ 158 var $_BIFF_version; 159 160 /** 161 * The class constructor 162 * 163 * @param integer $byte_order The byte order (Little endian or Big endian) of the architecture 164 (optional). 1 => big endian, 0 (default) little endian. 165 */ 166 function __construct($byte_order, $biff_version) 167 { 168 $this->_current_char = 0; 169 $this->_BIFF_version = $biff_version; 170 $this->_current_token = ''; // The token we are working on. 171 $this->_formula = ""; // The formula to parse. 172 $this->_lookahead = ''; // The character ahead of the current char. 173 $this->_parse_tree = ''; // The parse tree to be generated. 174 $this->_initializeHashes(); // Initialize the hashes: ptg's and function's ptg's 175 $this->_byte_order = $byte_order; // Little Endian or Big Endian 176 $this->_ext_sheets = array(); 177 $this->_references = array(); 178 } 179 180 /** 181 * Initialize the ptg and function hashes. 182 * 183 * @access private 184 */ 185 function _initializeHashes() 186 { 187 // The Excel ptg indices 188 $this->ptg = array( 189 'ptgExp' => 0x01, 190 'ptgTbl' => 0x02, 191 'ptgAdd' => 0x03, 192 'ptgSub' => 0x04, 193 'ptgMul' => 0x05, 194 'ptgDiv' => 0x06, 195 'ptgPower' => 0x07, 196 'ptgConcat' => 0x08, 197 'ptgLT' => 0x09, 198 'ptgLE' => 0x0A, 199 'ptgEQ' => 0x0B, 200 'ptgGE' => 0x0C, 201 'ptgGT' => 0x0D, 202 'ptgNE' => 0x0E, 203 'ptgIsect' => 0x0F, 204 'ptgUnion' => 0x10, 205 'ptgRange' => 0x11, 206 'ptgUplus' => 0x12, 207 'ptgUminus' => 0x13, 208 'ptgPercent' => 0x14, 209 'ptgParen' => 0x15, 210 'ptgMissArg' => 0x16, 211 'ptgStr' => 0x17, 212 'ptgAttr' => 0x19, 213 'ptgSheet' => 0x1A, 214 'ptgEndSheet' => 0x1B, 215 'ptgErr' => 0x1C, 216 'ptgBool' => 0x1D, 217 'ptgInt' => 0x1E, 218 'ptgNum' => 0x1F, 219 'ptgArray' => 0x20, 220 'ptgFunc' => 0x21, 221 'ptgFuncVar' => 0x22, 222 'ptgName' => 0x23, 223 'ptgRef' => 0x24, 224 'ptgArea' => 0x25, 225 'ptgMemArea' => 0x26, 226 'ptgMemErr' => 0x27, 227 'ptgMemNoMem' => 0x28, 228 'ptgMemFunc' => 0x29, 229 'ptgRefErr' => 0x2A, 230 'ptgAreaErr' => 0x2B, 231 'ptgRefN' => 0x2C, 232 'ptgAreaN' => 0x2D, 233 'ptgMemAreaN' => 0x2E, 234 'ptgMemNoMemN' => 0x2F, 235 'ptgNameX' => 0x39, 236 'ptgRef3d' => 0x3A, 237 'ptgArea3d' => 0x3B, 238 'ptgRefErr3d' => 0x3C, 239 'ptgAreaErr3d' => 0x3D, 240 'ptgArrayV' => 0x40, 241 'ptgFuncV' => 0x41, 242 'ptgFuncVarV' => 0x42, 243 'ptgNameV' => 0x43, 244 'ptgRefV' => 0x44, 245 'ptgAreaV' => 0x45, 246 'ptgMemAreaV' => 0x46, 247 'ptgMemErrV' => 0x47, 248 'ptgMemNoMemV' => 0x48, 249 'ptgMemFuncV' => 0x49, 250 'ptgRefErrV' => 0x4A, 251 'ptgAreaErrV' => 0x4B, 252 'ptgRefNV' => 0x4C, 253 'ptgAreaNV' => 0x4D, 254 'ptgMemAreaNV' => 0x4E, 255 'ptgMemNoMemN' => 0x4F, 256 'ptgFuncCEV' => 0x58, 257 'ptgNameXV' => 0x59, 258 'ptgRef3dV' => 0x5A, 259 'ptgArea3dV' => 0x5B, 260 'ptgRefErr3dV' => 0x5C, 261 'ptgAreaErr3d' => 0x5D, 262 'ptgArrayA' => 0x60, 263 'ptgFuncA' => 0x61, 264 'ptgFuncVarA' => 0x62, 265 'ptgNameA' => 0x63, 266 'ptgRefA' => 0x64, 267 'ptgAreaA' => 0x65, 268 'ptgMemAreaA' => 0x66, 269 'ptgMemErrA' => 0x67, 270 'ptgMemNoMemA' => 0x68, 271 'ptgMemFuncA' => 0x69, 272 'ptgRefErrA' => 0x6A, 273 'ptgAreaErrA' => 0x6B, 274 'ptgRefNA' => 0x6C, 275 'ptgAreaNA' => 0x6D, 276 'ptgMemAreaNA' => 0x6E, 277 'ptgMemNoMemN' => 0x6F, 278 'ptgFuncCEA' => 0x78, 279 'ptgNameXA' => 0x79, 280 'ptgRef3dA' => 0x7A, 281 'ptgArea3dA' => 0x7B, 282 'ptgRefErr3dA' => 0x7C, 283 'ptgAreaErr3d' => 0x7D 284 ); 285 286 // Thanks to Michael Meeks and Gnumeric for the initial arg values. 287 // 288 // The following hash was generated by "function_locale.pl" in the distro. 289 // Refer to function_locale.pl for non-English function names. 290 // 291 // The array elements are as follow: 292 // ptg: The Excel function ptg code. 293 // args: The number of arguments that the function takes: 294 // >=0 is a fixed number of arguments. 295 // -1 is a variable number of arguments. 296 // class: The reference, value or array class of the function args. 297 // vol: The function is volatile. 298 // 299 $this->_functions = array( 300 // function ptg args class vol 301 'COUNT' => array( 0, -1, 0, 0 ), 302 'IF' => array( 1, -1, 1, 0 ), 303 'ISNA' => array( 2, 1, 1, 0 ), 304 'ISERROR' => array( 3, 1, 1, 0 ), 305 'SUM' => array( 4, -1, 0, 0 ), 306 'AVERAGE' => array( 5, -1, 0, 0 ), 307 'MIN' => array( 6, -1, 0, 0 ), 308 'MAX' => array( 7, -1, 0, 0 ), 309 'ROW' => array( 8, -1, 0, 0 ), 310 'COLUMN' => array( 9, -1, 0, 0 ), 311 'NA' => array( 10, 0, 0, 0 ), 312 'NPV' => array( 11, -1, 1, 0 ), 313 'STDEV' => array( 12, -1, 0, 0 ), 314 'DOLLAR' => array( 13, -1, 1, 0 ), 315 'FIXED' => array( 14, -1, 1, 0 ), 316 'SIN' => array( 15, 1, 1, 0 ), 317 'COS' => array( 16, 1, 1, 0 ), 318 'TAN' => array( 17, 1, 1, 0 ), 319 'ATAN' => array( 18, 1, 1, 0 ), 320 'PI' => array( 19, 0, 1, 0 ), 321 'SQRT' => array( 20, 1, 1, 0 ), 322 'EXP' => array( 21, 1, 1, 0 ), 323 'LN' => array( 22, 1, 1, 0 ), 324 'LOG10' => array( 23, 1, 1, 0 ), 325 'ABS' => array( 24, 1, 1, 0 ), 326 'INT' => array( 25, 1, 1, 0 ), 327 'SIGN' => array( 26, 1, 1, 0 ), 328 'ROUND' => array( 27, 2, 1, 0 ), 329 'LOOKUP' => array( 28, -1, 0, 0 ), 330 'INDEX' => array( 29, -1, 0, 1 ), 331 'REPT' => array( 30, 2, 1, 0 ), 332 'MID' => array( 31, 3, 1, 0 ), 333 'LEN' => array( 32, 1, 1, 0 ), 334 'VALUE' => array( 33, 1, 1, 0 ), 335 'TRUE' => array( 34, 0, 1, 0 ), 336 'FALSE' => array( 35, 0, 1, 0 ), 337 'AND' => array( 36, -1, 0, 0 ), 338 'OR' => array( 37, -1, 0, 0 ), 339 'NOT' => array( 38, 1, 1, 0 ), 340 'MOD' => array( 39, 2, 1, 0 ), 341 'DCOUNT' => array( 40, 3, 0, 0 ), 342 'DSUM' => array( 41, 3, 0, 0 ), 343 'DAVERAGE' => array( 42, 3, 0, 0 ), 344 'DMIN' => array( 43, 3, 0, 0 ), 345 'DMAX' => array( 44, 3, 0, 0 ), 346 'DSTDEV' => array( 45, 3, 0, 0 ), 347 'VAR' => array( 46, -1, 0, 0 ), 348 'DVAR' => array( 47, 3, 0, 0 ), 349 'TEXT' => array( 48, 2, 1, 0 ), 350 'LINEST' => array( 49, -1, 0, 0 ), 351 'TREND' => array( 50, -1, 0, 0 ), 352 'LOGEST' => array( 51, -1, 0, 0 ), 353 'GROWTH' => array( 52, -1, 0, 0 ), 354 'PV' => array( 56, -1, 1, 0 ), 355 'FV' => array( 57, -1, 1, 0 ), 356 'NPER' => array( 58, -1, 1, 0 ), 357 'PMT' => array( 59, -1, 1, 0 ), 358 'RATE' => array( 60, -1, 1, 0 ), 359 'MIRR' => array( 61, 3, 0, 0 ), 360 'IRR' => array( 62, -1, 0, 0 ), 361 'RAND' => array( 63, 0, 1, 1 ), 362 'MATCH' => array( 64, -1, 0, 0 ), 363 'DATE' => array( 65, 3, 1, 0 ), 364 'TIME' => array( 66, 3, 1, 0 ), 365 'DAY' => array( 67, 1, 1, 0 ), 366 'MONTH' => array( 68, 1, 1, 0 ), 367 'YEAR' => array( 69, 1, 1, 0 ), 368 'WEEKDAY' => array( 70, -1, 1, 0 ), 369 'HOUR' => array( 71, 1, 1, 0 ), 370 'MINUTE' => array( 72, 1, 1, 0 ), 371 'SECOND' => array( 73, 1, 1, 0 ), 372 'NOW' => array( 74, 0, 1, 1 ), 373 'AREAS' => array( 75, 1, 0, 1 ), 374 'ROWS' => array( 76, 1, 0, 1 ), 375 'COLUMNS' => array( 77, 1, 0, 1 ), 376 'OFFSET' => array( 78, -1, 0, 1 ), 377 'SEARCH' => array( 82, -1, 1, 0 ), 378 'TRANSPOSE' => array( 83, 1, 1, 0 ), 379 'TYPE' => array( 86, 1, 1, 0 ), 380 'ATAN2' => array( 97, 2, 1, 0 ), 381 'ASIN' => array( 98, 1, 1, 0 ), 382 'ACOS' => array( 99, 1, 1, 0 ), 383 'CHOOSE' => array( 100, -1, 1, 0 ), 384 'HLOOKUP' => array( 101, -1, 0, 0 ), 385 'VLOOKUP' => array( 102, -1, 0, 0 ), 386 'ISREF' => array( 105, 1, 0, 0 ), 387 'LOG' => array( 109, -1, 1, 0 ), 388 'CHAR' => array( 111, 1, 1, 0 ), 389 'LOWER' => array( 112, 1, 1, 0 ), 390 'UPPER' => array( 113, 1, 1, 0 ), 391 'PROPER' => array( 114, 1, 1, 0 ), 392 'LEFT' => array( 115, -1, 1, 0 ), 393 'RIGHT' => array( 116, -1, 1, 0 ), 394 'EXACT' => array( 117, 2, 1, 0 ), 395 'TRIM' => array( 118, 1, 1, 0 ), 396 'REPLACE' => array( 119, 4, 1, 0 ), 397 'SUBSTITUTE' => array( 120, -1, 1, 0 ), 398 'CODE' => array( 121, 1, 1, 0 ), 399 'FIND' => array( 124, -1, 1, 0 ), 400 'CELL' => array( 125, -1, 0, 1 ), 401 'ISERR' => array( 126, 1, 1, 0 ), 402 'ISTEXT' => array( 127, 1, 1, 0 ), 403 'ISNUMBER' => array( 128, 1, 1, 0 ), 404 'ISBLANK' => array( 129, 1, 1, 0 ), 405 'T' => array( 130, 1, 0, 0 ), 406 'N' => array( 131, 1, 0, 0 ), 407 'DATEVALUE' => array( 140, 1, 1, 0 ), 408 'TIMEVALUE' => array( 141, 1, 1, 0 ), 409 'SLN' => array( 142, 3, 1, 0 ), 410 'SYD' => array( 143, 4, 1, 0 ), 411 'DDB' => array( 144, -1, 1, 0 ), 412 'INDIRECT' => array( 148, -1, 1, 1 ), 413 'CALL' => array( 150, -1, 1, 0 ), 414 'CLEAN' => array( 162, 1, 1, 0 ), 415 'MDETERM' => array( 163, 1, 2, 0 ), 416 'MINVERSE' => array( 164, 1, 2, 0 ), 417 'MMULT' => array( 165, 2, 2, 0 ), 418 'IPMT' => array( 167, -1, 1, 0 ), 419 'PPMT' => array( 168, -1, 1, 0 ), 420 'COUNTA' => array( 169, -1, 0, 0 ), 421 'PRODUCT' => array( 183, -1, 0, 0 ), 422 'FACT' => array( 184, 1, 1, 0 ), 423 'DPRODUCT' => array( 189, 3, 0, 0 ), 424 'ISNONTEXT' => array( 190, 1, 1, 0 ), 425 'STDEVP' => array( 193, -1, 0, 0 ), 426 'VARP' => array( 194, -1, 0, 0 ), 427 'DSTDEVP' => array( 195, 3, 0, 0 ), 428 'DVARP' => array( 196, 3, 0, 0 ), 429 'TRUNC' => array( 197, -1, 1, 0 ), 430 'ISLOGICAL' => array( 198, 1, 1, 0 ), 431 'DCOUNTA' => array( 199, 3, 0, 0 ), 432 'ROUNDUP' => array( 212, 2, 1, 0 ), 433 'ROUNDDOWN' => array( 213, 2, 1, 0 ), 434 'RANK' => array( 216, -1, 0, 0 ), 435 'ADDRESS' => array( 219, -1, 1, 0 ), 436 'DAYS360' => array( 220, -1, 1, 0 ), 437 'TODAY' => array( 221, 0, 1, 1 ), 438 'VDB' => array( 222, -1, 1, 0 ), 439 'MEDIAN' => array( 227, -1, 0, 0 ), 440 'SUMPRODUCT' => array( 228, -1, 2, 0 ), 441 'SINH' => array( 229, 1, 1, 0 ), 442 'COSH' => array( 230, 1, 1, 0 ), 443 'TANH' => array( 231, 1, 1, 0 ), 444 'ASINH' => array( 232, 1, 1, 0 ), 445 'ACOSH' => array( 233, 1, 1, 0 ), 446 'ATANH' => array( 234, 1, 1, 0 ), 447 'DGET' => array( 235, 3, 0, 0 ), 448 'INFO' => array( 244, 1, 1, 1 ), 449 'DB' => array( 247, -1, 1, 0 ), 450 'FREQUENCY' => array( 252, 2, 0, 0 ), 451 'ERROR.TYPE' => array( 261, 1, 1, 0 ), 452 'REGISTER.ID' => array( 267, -1, 1, 0 ), 453 'AVEDEV' => array( 269, -1, 0, 0 ), 454 'BETADIST' => array( 270, -1, 1, 0 ), 455 'GAMMALN' => array( 271, 1, 1, 0 ), 456 'BETAINV' => array( 272, -1, 1, 0 ), 457 'BINOMDIST' => array( 273, 4, 1, 0 ), 458 'CHIDIST' => array( 274, 2, 1, 0 ), 459 'CHIINV' => array( 275, 2, 1, 0 ), 460 'COMBIN' => array( 276, 2, 1, 0 ), 461 'CONFIDENCE' => array( 277, 3, 1, 0 ), 462 'CRITBINOM' => array( 278, 3, 1, 0 ), 463 'EVEN' => array( 279, 1, 1, 0 ), 464 'EXPONDIST' => array( 280, 3, 1, 0 ), 465 'FDIST' => array( 281, 3, 1, 0 ), 466 'FINV' => array( 282, 3, 1, 0 ), 467 'FISHER' => array( 283, 1, 1, 0 ), 468 'FISHERINV' => array( 284, 1, 1, 0 ), 469 'FLOOR' => array( 285, 2, 1, 0 ), 470 'GAMMADIST' => array( 286, 4, 1, 0 ), 471 'GAMMAINV' => array( 287, 3, 1, 0 ), 472 'CEILING' => array( 288, 2, 1, 0 ), 473 'HYPGEOMDIST' => array( 289, 4, 1, 0 ), 474 'LOGNORMDIST' => array( 290, 3, 1, 0 ), 475 'LOGINV' => array( 291, 3, 1, 0 ), 476 'NEGBINOMDIST' => array( 292, 3, 1, 0 ), 477 'NORMDIST' => array( 293, 4, 1, 0 ), 478 'NORMSDIST' => array( 294, 1, 1, 0 ), 479 'NORMINV' => array( 295, 3, 1, 0 ), 480 'NORMSINV' => array( 296, 1, 1, 0 ), 481 'STANDARDIZE' => array( 297, 3, 1, 0 ), 482 'ODD' => array( 298, 1, 1, 0 ), 483 'PERMUT' => array( 299, 2, 1, 0 ), 484 'POISSON' => array( 300, 3, 1, 0 ), 485 'TDIST' => array( 301, 3, 1, 0 ), 486 'WEIBULL' => array( 302, 4, 1, 0 ), 487 'SUMXMY2' => array( 303, 2, 2, 0 ), 488 'SUMX2MY2' => array( 304, 2, 2, 0 ), 489 'SUMX2PY2' => array( 305, 2, 2, 0 ), 490 'CHITEST' => array( 306, 2, 2, 0 ), 491 'CORREL' => array( 307, 2, 2, 0 ), 492 'COVAR' => array( 308, 2, 2, 0 ), 493 'FORECAST' => array( 309, 3, 2, 0 ), 494 'FTEST' => array( 310, 2, 2, 0 ), 495 'INTERCEPT' => array( 311, 2, 2, 0 ), 496 'PEARSON' => array( 312, 2, 2, 0 ), 497 'RSQ' => array( 313, 2, 2, 0 ), 498 'STEYX' => array( 314, 2, 2, 0 ), 499 'SLOPE' => array( 315, 2, 2, 0 ), 500 'TTEST' => array( 316, 4, 2, 0 ), 501 'PROB' => array( 317, -1, 2, 0 ), 502 'DEVSQ' => array( 318, -1, 0, 0 ), 503 'GEOMEAN' => array( 319, -1, 0, 0 ), 504 'HARMEAN' => array( 320, -1, 0, 0 ), 505 'SUMSQ' => array( 321, -1, 0, 0 ), 506 'KURT' => array( 322, -1, 0, 0 ), 507 'SKEW' => array( 323, -1, 0, 0 ), 508 'ZTEST' => array( 324, -1, 0, 0 ), 509 'LARGE' => array( 325, 2, 0, 0 ), 510 'SMALL' => array( 326, 2, 0, 0 ), 511 'QUARTILE' => array( 327, 2, 0, 0 ), 512 'PERCENTILE' => array( 328, 2, 0, 0 ), 513 'PERCENTRANK' => array( 329, -1, 0, 0 ), 514 'MODE' => array( 330, -1, 2, 0 ), 515 'TRIMMEAN' => array( 331, 2, 0, 0 ), 516 'TINV' => array( 332, 2, 1, 0 ), 517 'CONCATENATE' => array( 336, -1, 1, 0 ), 518 'POWER' => array( 337, 2, 1, 0 ), 519 'RADIANS' => array( 342, 1, 1, 0 ), 520 'DEGREES' => array( 343, 1, 1, 0 ), 521 'SUBTOTAL' => array( 344, -1, 0, 0 ), 522 'SUMIF' => array( 345, -1, 0, 0 ), 523 'COUNTIF' => array( 346, 2, 0, 0 ), 524 'COUNTBLANK' => array( 347, 1, 0, 0 ), 525 'ROMAN' => array( 354, -1, 1, 0 ) 526 ); 527 } 528 529 /** 530 * Convert a token to the proper ptg value. 531 * 532 * @access private 533 * @param mixed $token The token to convert. 534 * @return mixed the converted token on success. PEAR_Error if the token 535 * is not recognized 536 */ 537 function _convert($token) 538 { 539 if (preg_match('/^"[^"]{0,255}"$/', $token)) 540 { 541 return $this->_convertString($token); 542 } 543 elseif (is_numeric($token)) 544 { 545 return $this->_convertNumber($token); 546 } 547 // match references like A1 or $A$1 548 elseif (preg_match('/^\$?([A-Ia-i]?[A-Za-z])\$?(\d+)$/',$token)) 549 { 550 return $this->_convertRef2d($token); 551 } 552 // match external references like Sheet1!A1 or Sheet1:Sheet2!A1 553 elseif (preg_match('/^\w+(\:\w+)?\![A-Ia-i]?[A-Za-z](\d+)$/',$token)) 554 { 555 return $this->_convertRef3d($token); 556 } 557 // match external references like Sheet1!A1 or Sheet1:Sheet2!A1 558 elseif (preg_match("/^'\w+(\:\w+)?'\![A-Ia-i]?[A-Za-z](\d+)$/",$token)) 559 { 560 return $this->_convertRef3d($token); 561 } 562 // match ranges like A1:B2 563 elseif (preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?(\d+)\:(\$)?[A-Ia-i]?[A-Za-z](\$)?(\d+)$/',$token)) 564 { 565 return $this->_convertRange2d($token); 566 } 567 // match ranges like A1..B2 568 elseif (preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?(\d+)\.\.(\$)?[A-Ia-i]?[A-Za-z](\$)?(\d+)$/',$token)) 569 { 570 return $this->_convertRange2d($token); 571 } 572 // match external ranges like Sheet1!A1 or Sheet1:Sheet2!A1:B2 573 elseif (preg_match('/^\w+(\:\w+)?\!([A-Ia-i]?[A-Za-z])?(\d+)\:([A-Ia-i]?[A-Za-z])?(\d+)$/',$token)) 574 { 575 return $this->_convertRange3d($token); 576 } 577 // match external ranges like 'Sheet1'!A1 or 'Sheet1:Sheet2'!A1:B2 578 elseif (preg_match("/^'\w+(\:\w+)?'\!([A-Ia-i]?[A-Za-z])?(\d+)\:([A-Ia-i]?[A-Za-z])?(\d+)$/",$token)) 579 { 580 return $this->_convertRange3d($token); 581 } 582 elseif (isset($this->ptg[$token])) // operators (including parentheses) 583 { 584 return pack("C", $this->ptg[$token]); 585 } 586 // commented so argument number can be processed correctly. See toReversePolish(). 587 /*elseif (preg_match('/[A-Z0-9\xc0-\xdc\.]+/',$token)) 588 { 589 return($this->_convertFunction($token,$this->_func_args)); 590 }*/ 591 // if it's an argument, ignore the token (the argument remains) 592 elseif ($token == 'arg') 593 { 594 return ''; 595 } 596 // TODO: use real error codes 597 return $this->raiseError("Unknown token $token"); 598 } 599 600 /** 601 * Convert a number token to ptgInt or ptgNum 602 * 603 * @access private 604 * @param mixed $num an integer or double for conversion to its ptg value 605 */ 606 function _convertNumber($num) 607 { 608 // Integer in the range 0..2**16-1 609 if ((preg_match('/^\d+$/',$num)) and ($num <= 65535)) { 610 return pack("Cv", $this->ptg['ptgInt'], $num); 611 } 612 else // A float 613 { 614 if ($this->_byte_order) { // if it's Big Endian 615 $num = strrev($num); 616 } 617 return pack("Cd", $this->ptg['ptgNum'], $num); 618 } 619 } 620 621 /** 622 * Convert a string token to ptgStr 623 * 624 * @access private 625 * @param string $string A string for conversion to its ptg value. 626 * @return mixed the converted token on success. PEAR_Error if the string 627 * is longer than 255 characters. 628 */ 629 function _convertString($string) 630 { 631 // chop away beggining and ending quotes 632 $string = substr($string, 1, strlen($string) - 2); 633 if (strlen($string) > 255) { 634 return $this->raiseError("String is too long"); 635 } 636 if ($this->_BIFF_version == 0x0500) { 637 return pack("CC", $this->ptg['ptgStr'], strlen($string)).$string; 638 } 639 elseif ($this->_BIFF_version == 0x0600) { 640 $encoding = 0; // TODO: Unicode support 641 return pack("CCC", $this->ptg['ptgStr'], strlen($string), $encoding).$string; 642 } 643 } 644 645 /** 646 * Convert a function to a ptgFunc or ptgFuncVarV depending on the number of 647 * args that it takes. 648 * 649 * @access private 650 * @param string $token The name of the function for convertion to ptg value. 651 * @param integer $num_args The number of arguments the function receives. 652 * @return string The packed ptg for the function 653 */ 654 function _convertFunction($token, $num_args) 655 { 656 $args = $this->_functions[$token][1]; 657 $volatile = $this->_functions[$token][3]; 658 659 // Fixed number of args eg. TIME($i,$j,$k). 660 if ($args >= 0) { 661 return pack("Cv", $this->ptg['ptgFuncV'], $this->_functions[$token][0]); 662 } 663 // Variable number of args eg. SUM($i,$j,$k, ..). 664 if ($args == -1) { 665 return pack("CCv", $this->ptg['ptgFuncVarV'], $num_args, $this->_functions[$token][0]); 666 } 667 } 668 669 /** 670 * Convert an Excel range such as A1:D4 to a ptgRefV. 671 * 672 * @access private 673 * @param string $range An Excel range in the A1:A2 or A1..A2 format. 674 */ 675 function _convertRange2d($range) 676 { 677 $class = 2; // as far as I know, this is magick. 678 679 // Split the range into 2 cell refs 680 if (preg_match('/^([A-Ia-i]?[A-Za-z])(\d+)\:([A-Ia-i]?[A-Za-z])(\d+)$/',$range)) { 681 list($cell1, $cell2) = explode(':', $range); 682 } 683 elseif (preg_match('/^([A-Ia-i]?[A-Za-z])(\d+)\.\.([A-Ia-i]?[A-Za-z])(\d+)$/',$range)) { 684 list($cell1, $cell2) = explode('..', $range); 685 686 } 687 else { 688 // TODO: use real error codes 689 return $this->raiseError("Unknown range separator", 0, PEAR_ERROR_DIE); 690 } 691 692 // Convert the cell references 693 $cell_array1 = $this->_cellToPackedRowcol($cell1); 694 if (PEAR::isError($cell_array1)) { 695 return $cell_array1; 696 } 697 list($row1, $col1) = $cell_array1; 698 $cell_array2 = $this->_cellToPackedRowcol($cell2); 699 if (PEAR::isError($cell_array2)) { 700 return $cell_array2; 701 } 702 list($row2, $col2) = $cell_array2; 703 704 // The ptg value depends on the class of the ptg. 705 if ($class == 0) { 706 $ptgArea = pack("C", $this->ptg['ptgArea']); 707 } 708 elseif ($class == 1) { 709 $ptgArea = pack("C", $this->ptg['ptgAreaV']); 710 } 711 elseif ($class == 2) { 712 $ptgArea = pack("C", $this->ptg['ptgAreaA']); 713 } 714 else { 715 // TODO: use real error codes 716 return $this->raiseError("Unknown class $class", 0, PEAR_ERROR_DIE); 717 } 718 return $ptgArea . $row1 . $row2 . $col1. $col2; 719 } 720 721 /** 722 * Convert an Excel 3d range such as "Sheet1!A1:D4" or "Sheet1:Sheet2!A1:D4" to 723 * a ptgArea3d. 724 * 725 * @access private 726 * @param string $token An Excel range in the Sheet1!A1:A2 format. 727 * @return mixed The packed ptgArea3d token on success, PEAR_Error on failure. 728 */ 729 function _convertRange3d($token) 730 { 731 $class = 2; // as far as I know, this is magick. 732 733 // Split the ref at the ! symbol 734 list($ext_ref, $range) = explode('!', $token); 735 736 // Convert the external reference part (different for BIFF8) 737 if ($this->_BIFF_version == 0x0500) { 738 $ext_ref = $this->_packExtRef($ext_ref); 739 if (PEAR::isError($ext_ref)) { 740 return $ext_ref; 741 } 742 } 743 elseif ($this->_BIFF_version == 0x0600) { 744 $ext_ref = $this->_getRefIndex($ext_ref); 745 if (PEAR::isError($ext_ref)) { 746 return $ext_ref; 747 } 748 } 749 750 // Split the range into 2 cell refs 751 list($cell1, $cell2) = explode(':', $range); 752 753 // Convert the cell references 754 if (preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?(\d+)$/', $cell1)) 755 { 756 $cell_array1 = $this->_cellToPackedRowcol($cell1); 757 if (PEAR::isError($cell_array1)) { 758 return $cell_array1; 759 } 760 list($row1, $col1) = $cell_array1; 761 $cell_array2 = $this->_cellToPackedRowcol($cell2); 762 if (PEAR::isError($cell_array2)) { 763 return $cell_array2; 764 } 765 list($row2, $col2) = $cell_array2; 766 } 767 else { // It's a rows range (like 26:27) 768 $cells_array = $this->_rangeToPackedRange($cell1.':'.$cell2); 769 if (PEAR::isError($cells_array)) { 770 return $cells_array; 771 } 772 list($row1, $col1, $row2, $col2) = $cells_array; 773 } 774 775 // The ptg value depends on the class of the ptg. 776 if ($class == 0) { 777 $ptgArea = pack("C", $this->ptg['ptgArea3d']); 778 } 779 elseif ($class == 1) { 780 $ptgArea = pack("C", $this->ptg['ptgArea3dV']); 781 } 782 elseif ($class == 2) { 783 $ptgArea = pack("C", $this->ptg['ptgArea3dA']); 784 } 785 else { 786 return $this->raiseError("Unknown class $class", 0, PEAR_ERROR_DIE); 787 } 788 789 return $ptgArea . $ext_ref . $row1 . $row2 . $col1. $col2; 790 } 791 792 /** 793 * Convert an Excel reference such as A1, $B2, C$3 or $D$4 to a ptgRefV. 794 * 795 * @access private 796 * @param string $cell An Excel cell reference 797 * @return string The cell in packed() format with the corresponding ptg 798 */ 799 function _convertRef2d($cell) 800 { 801 $class = 2; // as far as I know, this is magick. 802 803 // Convert the cell reference 804 $cell_array = $this->_cellToPackedRowcol($cell); 805 if (PEAR::isError($cell_array)) { 806 return $cell_array; 807 } 808 list($row, $col) = $cell_array; 809 810 // The ptg value depends on the class of the ptg. 811 if ($class == 0) { 812 $ptgRef = pack("C", $this->ptg['ptgRef']); 813 } 814 elseif ($class == 1) { 815 $ptgRef = pack("C", $this->ptg['ptgRefV']); 816 } 817 elseif ($class == 2) { 818 $ptgRef = pack("C", $this->ptg['ptgRefA']); 819 } 820 else { 821 // TODO: use real error codes 822 return $this->raiseError("Unknown class $class"); 823 } 824 return $ptgRef.$row.$col; 825 } 826 827 /** 828 * Convert an Excel 3d reference such as "Sheet1!A1" or "Sheet1:Sheet2!A1" to a 829 * ptgRef3d. 830 * 831 * @access private 832 * @param string $cell An Excel cell reference 833 * @return mixed The packed ptgRef3d token on success, PEAR_Error on failure. 834 */ 835 function _convertRef3d($cell) 836 { 837 $class = 2; // as far as I know, this is magick. 838 839 // Split the ref at the ! symbol 840 list($ext_ref, $cell) = explode('!', $cell); 841 842 // Convert the external reference part (different for BIFF8) 843 if ($this->_BIFF_version == 0x0500) { 844 $ext_ref = $this->_packExtRef($ext_ref); 845 if (PEAR::isError($ext_ref)) { 846 return $ext_ref; 847 } 848 } 849 elseif ($this->_BIFF_version == 0x0600) { 850 $ext_ref = $this->_getRefIndex($ext_ref); 851 if (PEAR::isError($ext_ref)) { 852 return $ext_ref; 853 } 854 } 855 856 // Convert the cell reference part 857 list($row, $col) = $this->_cellToPackedRowcol($cell); 858 859 // The ptg value depends on the class of the ptg. 860 if ($class == 0) { 861 $ptgRef = pack("C", $this->ptg['ptgRef3d']); 862 } 863 elseif ($class == 1) { 864 $ptgRef = pack("C", $this->ptg['ptgRef3dV']); 865 } 866 elseif ($class == 2) { 867 $ptgRef = pack("C", $this->ptg['ptgRef3dA']); 868 } 869 else { 870 return $this->raiseError("Unknown class $class", 0, PEAR_ERROR_DIE); 871 } 872 873 return $ptgRef . $ext_ref. $row . $col; 874 } 875 876 /** 877 * Convert the sheet name part of an external reference, for example "Sheet1" or 878 * "Sheet1:Sheet2", to a packed structure. 879 * 880 * @access private 881 * @param string $ext_ref The name of the external reference 882 * @return string The reference index in packed() format 883 */ 884 function _packExtRef($ext_ref) 885 { 886 $ext_ref = preg_replace("/^'/", '', $ext_ref); // Remove leading ' if any. 887 $ext_ref = preg_replace("/'$/", '', $ext_ref); // Remove trailing ' if any. 888 889 // Check if there is a sheet range eg., Sheet1:Sheet2. 890 if (preg_match('/:/', $ext_ref)) 891 { 892 list($sheet_name1, $sheet_name2) = explode(':', $ext_ref); 893 894 $sheet1 = $this->_getSheetIndex($sheet_name1); 895 if ($sheet1 == -1) { 896 return $this->raiseError("Unknown sheet name $sheet_name1 in formula"); 897 } 898 $sheet2 = $this->_getSheetIndex($sheet_name2); 899 if ($sheet2 == -1) { 900 return $this->raiseError("Unknown sheet name $sheet_name2 in formula"); 901 } 902 903 // Reverse max and min sheet numbers if necessary 904 if ($sheet1 > $sheet2) { 905 list($sheet1, $sheet2) = array($sheet2, $sheet1); 906 } 907 } 908 else // Single sheet name only. 909 { 910 $sheet1 = $this->_getSheetIndex($ext_ref); 911 if ($sheet1 == -1) { 912 return $this->raiseError("Unknown sheet name $ext_ref in formula"); 913 } 914 $sheet2 = $sheet1; 915 } 916 917 // References are stored relative to 0xFFFF. 918 $offset = -1 - $sheet1; 919 920 return pack('vdvv', $offset, 0x00, $sheet1, $sheet2); 921 } 922 923 /** 924 * Look up the REF index that corresponds to an external sheet name 925 * (or range). If it doesn't exist yet add it to the workbook's references 926 * array. It assumes all sheet names given must exist. 927 * 928 * @access private 929 * @param string $ext_ref The name of the external reference 930 * @return mixed The reference index in packed() format on success, 931 * PEAR_Error on failure 932 */ 933 function _getRefIndex($ext_ref) 934 { 935 $ext_ref = preg_replace("/^'/", '', $ext_ref); // Remove leading ' if any. 936 $ext_ref = preg_replace("/'$/", '', $ext_ref); // Remove trailing ' if any. 937 938 // Check if there is a sheet range eg., Sheet1:Sheet2. 939 if (preg_match('/:/', $ext_ref)) 940 { 941 list($sheet_name1, $sheet_name2) = explode(':', $ext_ref); 942 943 $sheet1 = $this->_getSheetIndex($sheet_name1); 944 if ($sheet1 == -1) { 945 return $this->raiseError("Unknown sheet name $sheet_name1 in formula"); 946 } 947 $sheet2 = $this->_getSheetIndex($sheet_name2); 948 if ($sheet2 == -1) { 949 return $this->raiseError("Unknown sheet name $sheet_name2 in formula"); 950 } 951 952 // Reverse max and min sheet numbers if necessary 953 if ($sheet1 > $sheet2) { 954 list($sheet1, $sheet2) = array($sheet2, $sheet1); 955 } 956 } 957 else // Single sheet name only. 958 { 959 $sheet1 = $this->_getSheetIndex($ext_ref); 960 if ($sheet1 == -1) { 961 return $this->raiseError("Unknown sheet name $ext_ref in formula"); 962 } 963 $sheet2 = $sheet1; 964 } 965 966 // assume all references belong to this document 967 $supbook_index = 0x00; 968 $ref = pack('vvv', $supbook_index, $sheet1, $sheet2); 969 $total_references = count($this->_references); 970 $index = -1; 971 for ($i = 0; $i < $total_references; $i++) 972 { 973 if ($ref == $this->_references[$i]) { 974 $index = $i; 975 break; 976 } 977 } 978 // if REF was not found add it to references array 979 if ($index == -1) 980 { 981 $this->_references[$total_references] = $ref; 982 $index = $total_references; 983 } 984 985 return pack('v', $index); 986 } 987 988 /** 989 * Look up the index that corresponds to an external sheet name. The hash of 990 * sheet names is updated by the addworksheet() method of the 991 * Spreadsheet_Excel_Writer_Workbook class. 992 * 993 * @access private 994 * @return integer The sheet index, -1 if the sheet was not found 995 */ 996 function _getSheetIndex($sheet_name) 997 { 998 if (!isset($this->_ext_sheets[$sheet_name])) { 999 return -1; 1000 } 1001 else { 1002 return $this->_ext_sheets[$sheet_name]; 1003 } 1004 } 1005 1006 /** 1007 * This method is used to update the array of sheet names. It is 1008 * called by the addWorksheet() method of the 1009 * Spreadsheet_Excel_Writer_Workbook class. 1010 * 1011 * @access public 1012 * @see Spreadsheet_Excel_Writer_Workbook::addWorksheet() 1013 * @param string $name The name of the worksheet being added 1014 * @param integer $index The index of the worksheet being added 1015 */ 1016 function setExtSheet($name, $index) 1017 { 1018 $this->_ext_sheets[$name] = $index; 1019 } 1020 1021 /** 1022 * pack() row and column into the required 3 or 4 byte format. 1023 * 1024 * @access private 1025 * @param string $cell The Excel cell reference to be packed 1026 * @return array Array containing the row and column in packed() format 1027 */ 1028 function _cellToPackedRowcol($cell) 1029 { 1030 $cell = strtoupper($cell); 1031 list($row, $col, $row_rel, $col_rel) = $this->_cellToRowcol($cell); 1032 if ($col >= 256) { 1033 return $this->raiseError("Column in: $cell greater than 255"); 1034 } 1035 // FIXME: change for BIFF8 1036 if ($row >= 16384) { 1037 return $this->raiseError("Row in: $cell greater than 16384 "); 1038 } 1039 1040 // Set the high bits to indicate if row or col are relative. 1041 if ($this->_BIFF_version == 0x0500) { 1042 $row |= $col_rel << 14; 1043 $row |= $row_rel << 15; 1044 $col = pack('C', $col); 1045 } 1046 elseif ($this->_BIFF_version == 0x0600) { 1047 $col |= $col_rel << 14; 1048 $col |= $row_rel << 15; 1049 $col = pack('v', $col); 1050 } 1051 $row = pack('v', $row); 1052 1053 return array($row, $col); 1054 } 1055 1056 /** 1057 * pack() row range into the required 3 or 4 byte format. 1058 * Just using maximum col/rows, which is probably not the correct solution 1059 * 1060 * @access private 1061 * @param string $range The Excel range to be packed 1062 * @return array Array containing (row1,col1,row2,col2) in packed() format 1063 */ 1064 function _rangeToPackedRange($range) 1065 { 1066 preg_match('/(\$)?(\d+)\:(\$)?(\d+)/', $range, $match); 1067 // return absolute rows if there is a $ in the ref 1068 $row1_rel = empty($match[1]) ? 1 : 0; 1069 $row1 = $match[2]; 1070 $row2_rel = empty($match[3]) ? 1 : 0; 1071 $row2 = $match[4]; 1072 // Convert 1-index to zero-index 1073 $row1--; 1074 $row2--; 1075 // Trick poor inocent Excel 1076 $col1 = 0; 1077 $col2 = 16383; // FIXME: maximum possible value for Excel 5 (change this!!!) 1078 1079 // FIXME: this changes for BIFF8 1080 if (($row1 >= 16384) or ($row2 >= 16384)) { 1081 return $this->raiseError("Row in: $range greater than 16384 "); 1082 } 1083 1084 // Set the high bits to indicate if rows are relative. 1085 if ($this->_BIFF_version == 0x0500) { 1086 $row1 |= $row1_rel << 14; // FIXME: probably a bug 1087 $row2 |= $row2_rel << 15; 1088 $col1 = pack('C', $col1); 1089 $col2 = pack('C', $col2); 1090 } 1091 elseif ($this->_BIFF_version == 0x0600) { 1092 $col1 |= $row1_rel << 15; 1093 $col2 |= $row2_rel << 15; 1094 $col1 = pack('v', $col1); 1095 $col2 = pack('v', $col2); 1096 } 1097 $row1 = pack('v', $row1); 1098 $row2 = pack('v', $row2); 1099 1100 return array($row1, $col1, $row2, $col2); 1101 } 1102 1103 /** 1104 * Convert an Excel cell reference such as A1 or $B2 or C$3 or $D$4 to a zero 1105 * indexed row and column number. Also returns two (0,1) values to indicate 1106 * whether the row or column are relative references. 1107 * 1108 * @access private 1109 * @param string $cell The Excel cell reference in A1 format. 1110 * @return array 1111 */ 1112 function _cellToRowcol($cell) 1113 { 1114 preg_match('/(\$)?([A-I]?[A-Z])(\$)?(\d+)/',$cell,$match); 1115 // return absolute column if there is a $ in the ref 1116 $col_rel = empty($match[1]) ? 1 : 0; 1117 $col_ref = $match[2]; 1118 $row_rel = empty($match[3]) ? 1 : 0; 1119 $row = $match[4]; 1120 1121 // Convert base26 column string to a number. 1122 $expn = strlen($col_ref) - 1; 1123 $col = 0; 1124 for ($i=0, $tmp_len = strlen($col_ref); $i < $tmp_len; $i++) 1125 { 1126 $col += (ord($col_ref{$i}) - ord('A') + 1) * pow(26, $expn); 1127 $expn--; 1128 } 1129 1130 // Convert 1-index to zero-index 1131 $row--; 1132 $col--; 1133 1134 return array($row, $col, $row_rel, $col_rel); 1135 } 1136 1137 /** 1138 * Advance to the next valid token. 1139 * 1140 * @access private 1141 */ 1142 function _advance() 1143 { 1144 $i = $this->_current_char; 1145 // eat up white spaces 1146 if ($i < strlen($this->_formula)) 1147 { 1148 while ($this->_formula{$i} == " ") { 1149 $i++; 1150 } 1151 if ($i < strlen($this->_formula) - 1) { 1152 $this->_lookahead = $this->_formula{$i+1}; 1153 } 1154 $token = ""; 1155 } 1156 while ($i < strlen($this->_formula)) 1157 { 1158 $token .= $this->_formula{$i}; 1159 if ($i < strlen($this->_formula) - 1) { 1160 $this->_lookahead = $this->_formula{$i+1}; 1161 } 1162 else { 1163 $this->_lookahead = ''; 1164 } 1165 if ($this->_match($token) != '') 1166 { 1167 //if ($i < strlen($this->_formula) - 1) { 1168 // $this->_lookahead = $this->_formula{$i+1}; 1169 //} 1170 $this->_current_char = $i + 1; 1171 $this->_current_token = $token; 1172 return 1; 1173 } 1174 if ($i < strlen($this->_formula) - 2) { 1175 $this->_lookahead = $this->_formula{$i+2}; 1176 } 1177 // if we run out of characters _lookahead becomes empty 1178 else { 1179 $this->_lookahead = ''; 1180 } 1181 $i++; 1182 } 1183 //die("Lexical error ".$this->_current_char); 1184 } 1185 1186 /** 1187 * Checks if it's a valid token. 1188 * 1189 * @access private 1190 * @param mixed $token The token to check. 1191 * @return mixed The checked token or false on failure 1192 */ 1193 function _match($token) 1194 { 1195 switch($token) 1196 { 1197 case SPREADSHEET_EXCEL_WRITER_ADD: 1198 return $token; 1199 break; 1200 case SPREADSHEET_EXCEL_WRITER_SUB: 1201 return $token; 1202 break; 1203 case SPREADSHEET_EXCEL_WRITER_MUL: 1204 return $token; 1205 break; 1206 case SPREADSHEET_EXCEL_WRITER_DIV: 1207 return $token; 1208 break; 1209 case SPREADSHEET_EXCEL_WRITER_OPEN: 1210 return $token; 1211 break; 1212 case SPREADSHEET_EXCEL_WRITER_CLOSE: 1213 return $token; 1214 break; 1215 case SPREADSHEET_EXCEL_WRITER_COMA: 1216 return $token; 1217 break; 1218 case SPREADSHEET_EXCEL_WRITER_SEMICOLON: 1219 return $token; 1220 break; 1221 case SPREADSHEET_EXCEL_WRITER_GT: 1222 if ($this->_lookahead == '=') { // it's a GE token 1223 break; 1224 } 1225 return $token; 1226 break; 1227 case SPREADSHEET_EXCEL_WRITER_LT: 1228 // it's a LE or a NE token 1229 if (($this->_lookahead == '=') or ($this->_lookahead == '>')) { 1230 break; 1231 } 1232 return $token; 1233 break; 1234 case SPREADSHEET_EXCEL_WRITER_GE: 1235 return $token; 1236 break; 1237 case SPREADSHEET_EXCEL_WRITER_LE: 1238 return $token; 1239 break; 1240 case SPREADSHEET_EXCEL_WRITER_EQ: 1241 return $token; 1242 break; 1243 case SPREADSHEET_EXCEL_WRITER_NE: 1244 return $token; 1245 break; 1246 default: 1247 // if it's a reference 1248 if (preg_match('/^\$?[A-Ia-i]?[A-Za-z]\$?[0-9]+$/',$token) and 1249 !preg_match('/[0-9]/',$this->_lookahead) and 1250 ($this->_lookahead != ':') and ($this->_lookahead != '.') and 1251 ($this->_lookahead != '!')) 1252 { 1253 return $token; 1254 } 1255 // If it's an external reference (Sheet1!A1 or Sheet1:Sheet2!A1) 1256 elseif (preg_match('/^\w+(\:\w+)?\![A-Ia-i]?[A-Za-z][0-9]+$/',$token) and 1257 !preg_match('/[0-9]/',$this->_lookahead) and 1258 ($this->_lookahead != ':') and ($this->_lookahead != '.')) 1259 { 1260 return $token; 1261 } 1262 // If it's an external reference (Sheet1!A1 or Sheet1:Sheet2!A1) 1263 elseif (preg_match("/^'\w+(\:\w+)?'\![A-Ia-i]?[A-Za-z][0-9]+$/",$token) and 1264 !preg_match('/[0-9]/',$this->_lookahead) and 1265 ($this->_lookahead != ':') and ($this->_lookahead != '.')) 1266 { 1267 return $token; 1268 } 1269 // if it's a range (A1:A2) 1270 elseif (preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+:(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/',$token) and 1271 !preg_match('/[0-9]/',$this->_lookahead)) 1272 { 1273 return $token; 1274 } 1275 // if it's a range (A1..A2) 1276 elseif (preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+\.\.(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/',$token) and 1277 !preg_match('/[0-9]/',$this->_lookahead)) 1278 { 1279 return $token; 1280 } 1281 // If it's an external range like Sheet1!A1 or Sheet1:Sheet2!A1:B2 1282 elseif (preg_match('/^\w+(\:\w+)?\!([A-Ia-i]?[A-Za-z])?[0-9]+:([A-Ia-i]?[A-Za-z])?[0-9]+$/',$token) and 1283 !preg_match('/[0-9]/',$this->_lookahead)) 1284 { 1285 return $token; 1286 } 1287 // If it's an external range like 'Sheet1'!A1 or 'Sheet1:Sheet2'!A1:B2 1288 elseif (preg_match("/^'\w+(\:\w+)?'\!([A-Ia-i]?[A-Za-z])?[0-9]+:([A-Ia-i]?[A-Za-z])?[0-9]+$/",$token) and 1289 !preg_match('/[0-9]/',$this->_lookahead)) 1290 { 1291 return $token; 1292 } 1293 // If it's a number (check that it's not a sheet name or range) 1294 elseif (is_numeric($token) and 1295 (!is_numeric($token.$this->_lookahead) or ($this->_lookahead == '')) and 1296 ($this->_lookahead != '!') and ($this->_lookahead != ':')) 1297 { 1298 return $token; 1299 } 1300 // If it's a string (of maximum 255 characters) 1301 elseif (preg_match('/^"[^"]{0,255}"$/',$token)) 1302 { 1303 return $token; 1304 } 1305 // if it's a function call 1306 elseif (preg_match('/^[A-Z0-9\xc0-\xdc\.]+$/i',$token) and ($this->_lookahead == '(')) 1307 { 1308 return $token; 1309 } 1310 return ''; 1311 } 1312 } 1313 1314 /** 1315 * The parsing method. It parses a formula. 1316 * 1317 * @access public 1318 * @param string $formula The formula to parse, without the initial equal 1319 * sign (=). 1320 * @return mixed true on success, PEAR_Error on failure 1321 */ 1322 function parse($formula) 1323 { 1324 $this->_current_char = 0; 1325 $this->_formula = $formula; 1326 $this->_lookahead = $formula{1}; 1327 $this->_advance(); 1328 $this->_parse_tree = $this->_condition(); 1329 if (PEAR::isError($this->_parse_tree)) { 1330 return $this->_parse_tree; 1331 } 1332 return true; 1333 } 1334 1335 /** 1336 * It parses a condition. It assumes the following rule: 1337 * Cond -> Expr [(">" | "<") Expr] 1338 * 1339 * @access private 1340 * @return mixed The parsed ptg'd tree on success, PEAR_Error on failure 1341 */ 1342 function _condition() 1343 { 1344 $result = $this->_expression(); 1345 if (PEAR::isError($result)) { 1346 return $result; 1347 } 1348 if ($this->_current_token == SPREADSHEET_EXCEL_WRITER_LT) 1349 { 1350 $this->_advance(); 1351 $result2 = $this->_expression(); 1352 if (PEAR::isError($result2)) { 1353 return $result2; 1354 } 1355 $result = $this->_createTree('ptgLT', $result, $result2); 1356 } 1357 elseif ($this->_current_token == SPREADSHEET_EXCEL_WRITER_GT) 1358 { 1359 $this->_advance(); 1360 $result2 = $this->_expression(); 1361 if (PEAR::isError($result2)) { 1362 return $result2; 1363 } 1364 $result = $this->_createTree('ptgGT', $result, $result2); 1365 } 1366 elseif ($this->_current_token == SPREADSHEET_EXCEL_WRITER_LE) 1367 { 1368 $this->_advance(); 1369 $result2 = $this->_expression(); 1370 if (PEAR::isError($result2)) { 1371 return $result2; 1372 } 1373 $result = $this->_createTree('ptgLE', $result, $result2); 1374 } 1375 elseif ($this->_current_token == SPREADSHEET_EXCEL_WRITER_GE) 1376 { 1377 $this->_advance(); 1378 $result2 = $this->_expression(); 1379 if (PEAR::isError($result2)) { 1380 return $result2; 1381 } 1382 $result = $this->_createTree('ptgGE', $result, $result2); 1383 } 1384 elseif ($this->_current_token == SPREADSHEET_EXCEL_WRITER_EQ) 1385 { 1386 $this->_advance(); 1387 $result2 = $this->_expression(); 1388 if (PEAR::isError($result2)) { 1389 return $result2; 1390 } 1391 $result = $this->_createTree('ptgEQ', $result, $result2); 1392 } 1393 elseif ($this->_current_token == SPREADSHEET_EXCEL_WRITER_NE) 1394 { 1395 $this->_advance(); 1396 $result2 = $this->_expression(); 1397 if (PEAR::isError($result2)) { 1398 return $result2; 1399 } 1400 $result = $this->_createTree('ptgNE', $result, $result2); 1401 } 1402 return $result; 1403 } 1404 1405 /** 1406 * It parses a expression. It assumes the following rule: 1407 * Expr -> Term [("+" | "-") Term] 1408 * -> "string" 1409 * -> "-" Term 1410 * 1411 * @access private 1412 * @return mixed The parsed ptg'd tree on success, PEAR_Error on failure 1413 */ 1414 function _expression() 1415 { 1416 // If it's a string return a string node 1417 if (preg_match('/^"[^"]{0,255}"$/', $this->_current_token)) { 1418 $result = $this->_createTree($this->_current_token, '', ''); 1419 $this->_advance(); 1420 return $result; 1421 } 1422 // catch "-" Term 1423 elseif ($this->_current_token == SPREADSHEET_EXCEL_WRITER_SUB) { 1424 $this->_advance(); 1425 $result2 = $this->_expression(); 1426 $result = $this->_createTree('ptgUminus', $result2, ''); 1427 return $result; 1428 } 1429 $result = $this->_term(); 1430 if (PEAR::isError($result)) { 1431 return $result; 1432 } 1433 while (($this->_current_token == SPREADSHEET_EXCEL_WRITER_ADD) or 1434 ($this->_current_token == SPREADSHEET_EXCEL_WRITER_SUB)) 1435 { 1436 if ($this->_current_token == SPREADSHEET_EXCEL_WRITER_ADD) 1437 { 1438 $this->_advance(); 1439 $result2 = $this->_term(); 1440 if (PEAR::isError($result2)) { 1441 return $result2; 1442 } 1443 $result = $this->_createTree('ptgAdd', $result, $result2); 1444 } 1445 else 1446 { 1447 $this->_advance(); 1448 $result2 = $this->_term(); 1449 if (PEAR::isError($result2)) { 1450 return $result2; 1451 } 1452 $result = $this->_createTree('ptgSub', $result, $result2); 1453 } 1454 } 1455 return $result; 1456 } 1457 1458 /** 1459 * This function just introduces a ptgParen element in the tree, so that Excel 1460 * doesn't get confused when working with a parenthesized formula afterwards. 1461 * 1462 * @access private 1463 * @see _fact() 1464 * @return array The parsed ptg'd tree 1465 */ 1466 function _parenthesizedExpression() 1467 { 1468 $result = $this->_createTree('ptgParen', $this->_expression(), ''); 1469 return $result; 1470 } 1471 1472 /** 1473 * It parses a term. It assumes the following rule: 1474 * Term -> Fact [("*" | "/") Fact] 1475 * 1476 * @access private 1477 * @return mixed The parsed ptg'd tree on success, PEAR_Error on failure 1478 */ 1479 function _term() 1480 { 1481 $result = $this->_fact(); 1482 if (PEAR::isError($result)) { 1483 return $result; 1484 } 1485 while (($this->_current_token == SPREADSHEET_EXCEL_WRITER_MUL) or 1486 ($this->_current_token == SPREADSHEET_EXCEL_WRITER_DIV)) 1487 { 1488 if ($this->_current_token == SPREADSHEET_EXCEL_WRITER_MUL) 1489 { 1490 $this->_advance(); 1491 $result2 = $this->_fact(); 1492 if (PEAR::isError($result2)) { 1493 return $result2; 1494 } 1495 $result = $this->_createTree('ptgMul', $result, $result2); 1496 } 1497 else 1498 { 1499 $this->_advance(); 1500 $result2 = $this->_fact(); 1501 if (PEAR::isError($result2)) { 1502 return $result2; 1503 } 1504 $result = $this->_createTree('ptgDiv', $result, $result2); 1505 } 1506 } 1507 return $result; 1508 } 1509 1510 /** 1511 * It parses a factor. It assumes the following rule: 1512 * Fact -> ( Expr ) 1513 * | CellRef 1514 * | CellRange 1515 * | Number 1516 * | Function 1517 * 1518 * @access private 1519 * @return mixed The parsed ptg'd tree on success, PEAR_Error on failure 1520 */ 1521 function _fact() 1522 { 1523 if ($this->_current_token == SPREADSHEET_EXCEL_WRITER_OPEN) 1524 { 1525 $this->_advance(); // eat the "(" 1526 $result = $this->_parenthesizedExpression(); 1527 if ($this->_current_token != SPREADSHEET_EXCEL_WRITER_CLOSE) { 1528 return $this->raiseError("')' token expected."); 1529 } 1530 $this->_advance(); // eat the ")" 1531 return $result; 1532 } 1533 // if it's a reference 1534 if (preg_match('/^\$?[A-Ia-i]?[A-Za-z]\$?[0-9]+$/',$this->_current_token)) 1535 { 1536 $result = $this->_createTree($this->_current_token, '', ''); 1537 $this->_advance(); 1538 return $result; 1539 } 1540 // If it's an external reference (Sheet1!A1 or Sheet1:Sheet2!A1) 1541 elseif (preg_match('/^\w+(\:\w+)?\![A-Ia-i]?[A-Za-z][0-9]+$/',$this->_current_token)) 1542 { 1543 $result = $this->_createTree($this->_current_token, '', ''); 1544 $this->_advance(); 1545 return $result; 1546 } 1547 // If it's an external reference (Sheet1!A1 or Sheet1:Sheet2!A1) 1548 elseif (preg_match("/^'\w+(\:\w+)?'\![A-Ia-i]?[A-Za-z][0-9]+$/",$this->_current_token)) 1549 { 1550 $result = $this->_createTree($this->_current_token, '', ''); 1551 $this->_advance(); 1552 return $result; 1553 } 1554 // if it's a range 1555 elseif (preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+:(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/',$this->_current_token) or 1556 preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+\.\.(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/',$this->_current_token)) 1557 { 1558 $result = $this->_current_token; 1559 $this->_advance(); 1560 return $result; 1561 } 1562 // If it's an external range (Sheet1!A1 or Sheet1!A1:B2) 1563 elseif (preg_match('/^\w+(\:\w+)?\!([A-Ia-i]?[A-Za-z])?[0-9]+:([A-Ia-i]?[A-Za-z])?[0-9]+$/',$this->_current_token)) 1564 { 1565 $result = $this->_current_token; 1566 $this->_advance(); 1567 return $result; 1568 } 1569 // If it's an external range ('Sheet1'!A1 or 'Sheet1'!A1:B2) 1570 elseif (preg_match("/^'\w+(\:\w+)?'\!([A-Ia-i]?[A-Za-z])?[0-9]+:([A-Ia-i]?[A-Za-z])?[0-9]+$/",$this->_current_token)) 1571 { 1572 $result = $this->_current_token; 1573 $this->_advance(); 1574 return $result; 1575 } 1576 elseif (is_numeric($this->_current_token)) 1577 { 1578 $result = $this->_createTree($this->_current_token, '', ''); 1579 $this->_advance(); 1580 return $result; 1581 } 1582 // if it's a function call 1583 elseif (preg_match('/^[A-Z0-9\xc0-\xdc\.]+$/i',$this->_current_token)) 1584 { 1585 $result = $this->_func(); 1586 return $result; 1587 } 1588 return $this->raiseError("Syntax error: ".$this->_current_token. 1589 ", lookahead: ".$this->_lookahead. 1590 ", current char: ".$this->_current_char); 1591 } 1592 1593 /** 1594 * It parses a function call. It assumes the following rule: 1595 * Func -> ( Expr [,Expr]* ) 1596 * 1597 * @access private 1598 * @return mixed The parsed ptg'd tree on success, PEAR_Error on failure 1599 */ 1600 function _func() 1601 { 1602 $num_args = 0; // number of arguments received 1603 $function = $this->_current_token; 1604 $this->_advance(); 1605 $this->_advance(); // eat the "(" 1606 while ($this->_current_token != ')') 1607 { 1608 if ($num_args > 0) 1609 { 1610 if ($this->_current_token == SPREADSHEET_EXCEL_WRITER_COMA || 1611 $this->_current_token == SPREADSHEET_EXCEL_WRITER_SEMICOLON) 1612 { 1613 $this->_advance(); // eat the "," or ";" 1614 } 1615 else { 1616 return $this->raiseError("Syntax error: comma expected in ". 1617 "function $function, arg #{$num_args}"); 1618 } 1619 $result2 = $this->_condition(); 1620 if (PEAR::isError($result2)) { 1621 return $result2; 1622 } 1623 $result = $this->_createTree('arg', $result, $result2); 1624 } 1625 else // first argument 1626 { 1627 $result2 = $this->_condition(); 1628 if (PEAR::isError($result2)) { 1629 return $result2; 1630 } 1631 $result = $this->_createTree('arg', '', $result2); 1632 } 1633 $num_args++; 1634 } 1635 $args = $this->_functions[$function][1]; 1636 // If fixed number of args eg. TIME($i,$j,$k). Check that the number of args is valid. 1637 if (($args >= 0) and ($args != $num_args)) { 1638 return $this->raiseError("Incorrect number of arguments in function $function() "); 1639 } 1640 1641 $result = $this->_createTree($function, $result, $num_args); 1642 $this->_advance(); // eat the ")" 1643 return $result; 1644 } 1645 1646 /** 1647 * Creates a tree. In fact an array which may have one or two arrays (sub-trees) 1648 * as elements. 1649 * 1650 * @access private 1651 * @param mixed $value The value of this node. 1652 * @param mixed $left The left array (sub-tree) or a final node. 1653 * @param mixed $right The right array (sub-tree) or a final node. 1654 * @return array A tree 1655 */ 1656 function _createTree($value, $left, $right) 1657 { 1658 return array('value' => $value, 'left' => $left, 'right' => $right); 1659 } 1660 1661 /** 1662 * Builds a string containing the tree in reverse polish notation (What you 1663 * would use in a HP calculator stack). 1664 * The following tree: 1665 * 1666 * + 1667 * / \ 1668 * 2 3 1669 * 1670 * produces: "23+" 1671 * 1672 * The following tree: 1673 * 1674 * + 1675 * / \ 1676 * 3 * 1677 * / \ 1678 * 6 A1 1679 * 1680 * produces: "36A1*+" 1681 * 1682 * In fact all operands, functions, references, etc... are written as ptg's 1683 * 1684 * @access public 1685 * @param array $tree The optional tree to convert. 1686 * @return string The tree in reverse polish notation 1687 */ 1688 function toReversePolish($tree = array()) 1689 { 1690 $polish = ""; // the string we are going to return 1691 if (empty($tree)) // If it's the first call use _parse_tree 1692 { 1693 $tree = $this->_parse_tree; 1694 } 1695 if (is_array($tree['left'])) 1696 { 1697 $converted_tree = $this->toReversePolish($tree['left']); 1698 if (PEAR::isError($converted_tree)) { 1699 return $converted_tree; 1700 } 1701 $polish .= $converted_tree; 1702 } 1703 elseif ($tree['left'] != '') // It's a final node 1704 { 1705 $converted_tree = $this->_convert($tree['left']); 1706 if (PEAR::isError($converted_tree)) { 1707 return $converted_tree; 1708 } 1709 $polish .= $converted_tree; 1710 } 1711 if (is_array($tree['right'])) 1712 { 1713 $converted_tree = $this->toReversePolish($tree['right']); 1714 if (PEAR::isError($converted_tree)) { 1715 return $converted_tree; 1716 } 1717 $polish .= $converted_tree; 1718 } 1719 elseif ($tree['right'] != '') // It's a final node 1720 { 1721 $converted_tree = $this->_convert($tree['right']); 1722 if (PEAR::isError($converted_tree)) { 1723 return $converted_tree; 1724 } 1725 $polish .= $converted_tree; 1726 } 1727 // if it's a function convert it here (so we can set it's arguments) 1728 if (preg_match('/^[A-Z0-9\xc0-\xdc\.]+$/',$tree['value']) and 1729 !preg_match('/^([A-Ia-i]?[A-Za-z])(\d+)$/',$tree['value']) and 1730 !preg_match('/^[A-Ia-i]?[A-Za-z](\d+)\.\.[A-Ia-i]?[A-Za-z](\d+)$/',$tree['value']) and 1731 !is_numeric($tree['value']) and 1732 !isset($this->ptg[$tree['value']])) 1733 { 1734 // left subtree for a function is always an array. 1735 if ($tree['left'] != '') { 1736 $left_tree = $this->toReversePolish($tree['left']); 1737 } 1738 else { 1739 $left_tree = ''; 1740 } 1741 if (PEAR::isError($left_tree)) { 1742 return $left_tree; 1743 } 1744 // add it's left subtree and return. 1745 return $left_tree.$this->_convertFunction($tree['value'], $tree['right']); 1746 } 1747 else 1748 { 1749 $converted_tree = $this->_convert($tree['value']); 1750 if (PEAR::isError($converted_tree)) { 1751 return $converted_tree; 1752 } 1753 } 1754 $polish .= $converted_tree; 1755 return $polish; 1756 } 1757} 1758