PageRenderTime 53ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_chronocontact/excelwriter/Writer/Parser.php

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