PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/lib/classes/PHPExcel/Writer/Excel5/Parser.php

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