FormulaParser.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 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_Calculation
  23. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. /*
  28. PARTLY BASED ON:
  29. Copyright (c) 2007 E. W. Bachtal, Inc.
  30. Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  31. and associated documentation files (the "Software"), to deal in the Software without restriction,
  32. including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  33. and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
  34. subject to the following conditions:
  35. The above copyright notice and this permission notice shall be included in all copies or substantial
  36. portions of the Software.
  37. The software is provided "as is", without warranty of any kind, express or implied, including but not
  38. limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In
  39. no event shall the authors or copyright holders be liable for any claim, damages or other liability,
  40. whether in an action of contract, tort or otherwise, arising from, out of or in connection with the
  41. software or the use or other dealings in the software.
  42. http://ewbi.blogs.com/develops/2007/03/excel_formula_p.html
  43. http://ewbi.blogs.com/develops/2004/12/excel_formula_p.html
  44. */
  45. /**
  46. * PHPExcel_Calculation_FormulaParser
  47. *
  48. * @category PHPExcel
  49. * @package PHPExcel_Calculation
  50. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  51. */
  52. class PHPExcel_Calculation_FormulaParser {
  53. /* Character constants */
  54. const QUOTE_DOUBLE = '"';
  55. const QUOTE_SINGLE = '\'';
  56. const BRACKET_CLOSE = ']';
  57. const BRACKET_OPEN = '[';
  58. const BRACE_OPEN = '{';
  59. const BRACE_CLOSE = '}';
  60. const PAREN_OPEN = '(';
  61. const PAREN_CLOSE = ')';
  62. const SEMICOLON = ';';
  63. const WHITESPACE = ' ';
  64. const COMMA = ',';
  65. const ERROR_START = '#';
  66. const OPERATORS_SN = "+-";
  67. const OPERATORS_INFIX = "+-*/^&=><";
  68. const OPERATORS_POSTFIX = "%";
  69. /**
  70. * Formula
  71. *
  72. * @var string
  73. */
  74. private $_formula;
  75. /**
  76. * Tokens
  77. *
  78. * @var PHPExcel_Calculation_FormulaToken[]
  79. */
  80. private $_tokens = array();
  81. /**
  82. * Create a new PHPExcel_Calculation_FormulaParser
  83. *
  84. * @param string $pFormula Formula to parse
  85. * @throws PHPExcel_Calculation_Exception
  86. */
  87. public function __construct($pFormula = '')
  88. {
  89. // Check parameters
  90. if (is_null($pFormula)) {
  91. throw new PHPExcel_Calculation_Exception("Invalid parameter passed: formula");
  92. }
  93. // Initialise values
  94. $this->_formula = trim($pFormula);
  95. // Parse!
  96. $this->_parseToTokens();
  97. }
  98. /**
  99. * Get Formula
  100. *
  101. * @return string
  102. */
  103. public function getFormula() {
  104. return $this->_formula;
  105. }
  106. /**
  107. * Get Token
  108. *
  109. * @param int $pId Token id
  110. * @return string
  111. * @throws PHPExcel_Calculation_Exception
  112. */
  113. public function getToken($pId = 0) {
  114. if (isset($this->_tokens[$pId])) {
  115. return $this->_tokens[$pId];
  116. } else {
  117. throw new PHPExcel_Calculation_Exception("Token with id $pId does not exist.");
  118. }
  119. }
  120. /**
  121. * Get Token count
  122. *
  123. * @return string
  124. */
  125. public function getTokenCount() {
  126. return count($this->_tokens);
  127. }
  128. /**
  129. * Get Tokens
  130. *
  131. * @return PHPExcel_Calculation_FormulaToken[]
  132. */
  133. public function getTokens() {
  134. return $this->_tokens;
  135. }
  136. /**
  137. * Parse to tokens
  138. */
  139. private function _parseToTokens() {
  140. // No attempt is made to verify formulas; assumes formulas are derived from Excel, where
  141. // they can only exist if valid; stack overflows/underflows sunk as nulls without exceptions.
  142. // Check if the formula has a valid starting =
  143. $formulaLength = strlen($this->_formula);
  144. if ($formulaLength < 2 || $this->_formula{0} != '=') return;
  145. // Helper variables
  146. $tokens1 = $tokens2 = $stack = array();
  147. $inString = $inPath = $inRange = $inError = false;
  148. $token = $previousToken = $nextToken = null;
  149. $index = 1;
  150. $value = '';
  151. $ERRORS = array("#NULL!", "#DIV/0!", "#VALUE!", "#REF!", "#NAME?", "#NUM!", "#N/A");
  152. $COMPARATORS_MULTI = array(">=", "<=", "<>");
  153. while ($index < $formulaLength) {
  154. // state-dependent character evaluation (order is important)
  155. // double-quoted strings
  156. // embeds are doubled
  157. // end marks token
  158. if ($inString) {
  159. if ($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::QUOTE_DOUBLE) {
  160. if ((($index + 2) <= $formulaLength) && ($this->_formula{$index + 1} == PHPExcel_Calculation_FormulaParser::QUOTE_DOUBLE)) {
  161. $value .= PHPExcel_Calculation_FormulaParser::QUOTE_DOUBLE;
  162. ++$index;
  163. } else {
  164. $inString = false;
  165. $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND, PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_TEXT);
  166. $value = "";
  167. }
  168. } else {
  169. $value .= $this->_formula{$index};
  170. }
  171. ++$index;
  172. continue;
  173. }
  174. // single-quoted strings (links)
  175. // embeds are double
  176. // end does not mark a token
  177. if ($inPath) {
  178. if ($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::QUOTE_SINGLE) {
  179. if ((($index + 2) <= $formulaLength) && ($this->_formula{$index + 1} == PHPExcel_Calculation_FormulaParser::QUOTE_SINGLE)) {
  180. $value .= PHPExcel_Calculation_FormulaParser::QUOTE_SINGLE;
  181. ++$index;
  182. } else {
  183. $inPath = false;
  184. }
  185. } else {
  186. $value .= $this->_formula{$index};
  187. }
  188. ++$index;
  189. continue;
  190. }
  191. // bracked strings (R1C1 range index or linked workbook name)
  192. // no embeds (changed to "()" by Excel)
  193. // end does not mark a token
  194. if ($inRange) {
  195. if ($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::BRACKET_CLOSE) {
  196. $inRange = false;
  197. }
  198. $value .= $this->_formula{$index};
  199. ++$index;
  200. continue;
  201. }
  202. // error values
  203. // end marks a token, determined from absolute list of values
  204. if ($inError) {
  205. $value .= $this->_formula{$index};
  206. ++$index;
  207. if (in_array($value, $ERRORS)) {
  208. $inError = false;
  209. $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND, PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_ERROR);
  210. $value = "";
  211. }
  212. continue;
  213. }
  214. // scientific notation check
  215. if (strpos(PHPExcel_Calculation_FormulaParser::OPERATORS_SN, $this->_formula{$index}) !== false) {
  216. if (strlen($value) > 1) {
  217. if (preg_match("/^[1-9]{1}(\.[0-9]+)?E{1}$/", $this->_formula{$index}) != 0) {
  218. $value .= $this->_formula{$index};
  219. ++$index;
  220. continue;
  221. }
  222. }
  223. }
  224. // independent character evaluation (order not important)
  225. // establish state-dependent character evaluations
  226. if ($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::QUOTE_DOUBLE) {
  227. if (strlen($value > 0)) { // unexpected
  228. $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_UNKNOWN);
  229. $value = "";
  230. }
  231. $inString = true;
  232. ++$index;
  233. continue;
  234. }
  235. if ($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::QUOTE_SINGLE) {
  236. if (strlen($value) > 0) { // unexpected
  237. $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_UNKNOWN);
  238. $value = "";
  239. }
  240. $inPath = true;
  241. ++$index;
  242. continue;
  243. }
  244. if ($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::BRACKET_OPEN) {
  245. $inRange = true;
  246. $value .= PHPExcel_Calculation_FormulaParser::BRACKET_OPEN;
  247. ++$index;
  248. continue;
  249. }
  250. if ($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::ERROR_START) {
  251. if (strlen($value) > 0) { // unexpected
  252. $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_UNKNOWN);
  253. $value = "";
  254. }
  255. $inError = true;
  256. $value .= PHPExcel_Calculation_FormulaParser::ERROR_START;
  257. ++$index;
  258. continue;
  259. }
  260. // mark start and end of arrays and array rows
  261. if ($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::BRACE_OPEN) {
  262. if (strlen($value) > 0) { // unexpected
  263. $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_UNKNOWN);
  264. $value = "";
  265. }
  266. $tmp = new PHPExcel_Calculation_FormulaToken("ARRAY", PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_FUNCTION, PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_START);
  267. $tokens1[] = $tmp;
  268. $stack[] = clone $tmp;
  269. $tmp = new PHPExcel_Calculation_FormulaToken("ARRAYROW", PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_FUNCTION, PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_START);
  270. $tokens1[] = $tmp;
  271. $stack[] = clone $tmp;
  272. ++$index;
  273. continue;
  274. }
  275. if ($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::SEMICOLON) {
  276. if (strlen($value) > 0) {
  277. $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND);
  278. $value = "";
  279. }
  280. $tmp = array_pop($stack);
  281. $tmp->setValue("");
  282. $tmp->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP);
  283. $tokens1[] = $tmp;
  284. $tmp = new PHPExcel_Calculation_FormulaToken(",", PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_ARGUMENT);
  285. $tokens1[] = $tmp;
  286. $tmp = new PHPExcel_Calculation_FormulaToken("ARRAYROW", PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_FUNCTION, PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_START);
  287. $tokens1[] = $tmp;
  288. $stack[] = clone $tmp;
  289. ++$index;
  290. continue;
  291. }
  292. if ($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::BRACE_CLOSE) {
  293. if (strlen($value) > 0) {
  294. $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND);
  295. $value = "";
  296. }
  297. $tmp = array_pop($stack);
  298. $tmp->setValue("");
  299. $tmp->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP);
  300. $tokens1[] = $tmp;
  301. $tmp = array_pop($stack);
  302. $tmp->setValue("");
  303. $tmp->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP);
  304. $tokens1[] = $tmp;
  305. ++$index;
  306. continue;
  307. }
  308. // trim white-space
  309. if ($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::WHITESPACE) {
  310. if (strlen($value) > 0) {
  311. $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND);
  312. $value = "";
  313. }
  314. $tokens1[] = new PHPExcel_Calculation_FormulaToken("", PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_WHITESPACE);
  315. ++$index;
  316. while (($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::WHITESPACE) && ($index < $formulaLength)) {
  317. ++$index;
  318. }
  319. continue;
  320. }
  321. // multi-character comparators
  322. if (($index + 2) <= $formulaLength) {
  323. if (in_array(substr($this->_formula, $index, 2), $COMPARATORS_MULTI)) {
  324. if (strlen($value) > 0) {
  325. $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND);
  326. $value = "";
  327. }
  328. $tokens1[] = new PHPExcel_Calculation_FormulaToken(substr($this->_formula, $index, 2), PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORINFIX, PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_LOGICAL);
  329. $index += 2;
  330. continue;
  331. }
  332. }
  333. // standard infix operators
  334. if (strpos(PHPExcel_Calculation_FormulaParser::OPERATORS_INFIX, $this->_formula{$index}) !== false) {
  335. if (strlen($value) > 0) {
  336. $tokens1[] =new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND);
  337. $value = "";
  338. }
  339. $tokens1[] = new PHPExcel_Calculation_FormulaToken($this->_formula{$index}, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORINFIX);
  340. ++$index;
  341. continue;
  342. }
  343. // standard postfix operators (only one)
  344. if (strpos(PHPExcel_Calculation_FormulaParser::OPERATORS_POSTFIX, $this->_formula{$index}) !== false) {
  345. if (strlen($value) > 0) {
  346. $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND);
  347. $value = "";
  348. }
  349. $tokens1[] = new PHPExcel_Calculation_FormulaToken($this->_formula{$index}, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORPOSTFIX);
  350. ++$index;
  351. continue;
  352. }
  353. // start subexpression or function
  354. if ($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::PAREN_OPEN) {
  355. if (strlen($value) > 0) {
  356. $tmp = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_FUNCTION, PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_START);
  357. $tokens1[] = $tmp;
  358. $stack[] = clone $tmp;
  359. $value = "";
  360. } else {
  361. $tmp = new PHPExcel_Calculation_FormulaToken("", PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_SUBEXPRESSION, PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_START);
  362. $tokens1[] = $tmp;
  363. $stack[] = clone $tmp;
  364. }
  365. ++$index;
  366. continue;
  367. }
  368. // function, subexpression, or array parameters, or operand unions
  369. if ($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::COMMA) {
  370. if (strlen($value) > 0) {
  371. $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND);
  372. $value = "";
  373. }
  374. $tmp = array_pop($stack);
  375. $tmp->setValue("");
  376. $tmp->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP);
  377. $stack[] = $tmp;
  378. if ($tmp->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_FUNCTION) {
  379. $tokens1[] = new PHPExcel_Calculation_FormulaToken(",", PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORINFIX, PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_UNION);
  380. } else {
  381. $tokens1[] = new PHPExcel_Calculation_FormulaToken(",", PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_ARGUMENT);
  382. }
  383. ++$index;
  384. continue;
  385. }
  386. // stop subexpression
  387. if ($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::PAREN_CLOSE) {
  388. if (strlen($value) > 0) {
  389. $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND);
  390. $value = "";
  391. }
  392. $tmp = array_pop($stack);
  393. $tmp->setValue("");
  394. $tmp->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP);
  395. $tokens1[] = $tmp;
  396. ++$index;
  397. continue;
  398. }
  399. // token accumulation
  400. $value .= $this->_formula{$index};
  401. ++$index;
  402. }
  403. // dump remaining accumulation
  404. if (strlen($value) > 0) {
  405. $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND);
  406. }
  407. // move tokenList to new set, excluding unnecessary white-space tokens and converting necessary ones to intersections
  408. $tokenCount = count($tokens1);
  409. for ($i = 0; $i < $tokenCount; ++$i) {
  410. $token = $tokens1[$i];
  411. if (isset($tokens1[$i - 1])) {
  412. $previousToken = $tokens1[$i - 1];
  413. } else {
  414. $previousToken = null;
  415. }
  416. if (isset($tokens1[$i + 1])) {
  417. $nextToken = $tokens1[$i + 1];
  418. } else {
  419. $nextToken = null;
  420. }
  421. if (is_null($token)) {
  422. continue;
  423. }
  424. if ($token->getTokenType() != PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_WHITESPACE) {
  425. $tokens2[] = $token;
  426. continue;
  427. }
  428. if (is_null($previousToken)) {
  429. continue;
  430. }
  431. if (! (
  432. (($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_FUNCTION) && ($previousToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP)) ||
  433. (($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_SUBEXPRESSION) && ($previousToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP)) ||
  434. ($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND)
  435. ) ) {
  436. continue;
  437. }
  438. if (is_null($nextToken)) {
  439. continue;
  440. }
  441. if (! (
  442. (($nextToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_FUNCTION) && ($nextToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_START)) ||
  443. (($nextToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_SUBEXPRESSION) && ($nextToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_START)) ||
  444. ($nextToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND)
  445. ) ) {
  446. continue;
  447. }
  448. $tokens2[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORINFIX, PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_INTERSECTION);
  449. }
  450. // move tokens to final list, switching infix "-" operators to prefix when appropriate, switching infix "+" operators
  451. // to noop when appropriate, identifying operand and infix-operator subtypes, and pulling "@" from function names
  452. $this->_tokens = array();
  453. $tokenCount = count($tokens2);
  454. for ($i = 0; $i < $tokenCount; ++$i) {
  455. $token = $tokens2[$i];
  456. if (isset($tokens2[$i - 1])) {
  457. $previousToken = $tokens2[$i - 1];
  458. } else {
  459. $previousToken = null;
  460. }
  461. if (isset($tokens2[$i + 1])) {
  462. $nextToken = $tokens2[$i + 1];
  463. } else {
  464. $nextToken = null;
  465. }
  466. if (is_null($token)) {
  467. continue;
  468. }
  469. if ($token->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORINFIX && $token->getValue() == "-") {
  470. if ($i == 0) {
  471. $token->setTokenType(PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORPREFIX);
  472. } else if (
  473. (($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_FUNCTION) && ($previousToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP)) ||
  474. (($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_SUBEXPRESSION) && ($previousToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP)) ||
  475. ($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORPOSTFIX) ||
  476. ($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND)
  477. ) {
  478. $token->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_MATH);
  479. } else {
  480. $token->setTokenType(PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORPREFIX);
  481. }
  482. $this->_tokens[] = $token;
  483. continue;
  484. }
  485. if ($token->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORINFIX && $token->getValue() == "+") {
  486. if ($i == 0) {
  487. continue;
  488. } else if (
  489. (($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_FUNCTION) && ($previousToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP)) ||
  490. (($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_SUBEXPRESSION) && ($previousToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP)) ||
  491. ($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORPOSTFIX) ||
  492. ($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND)
  493. ) {
  494. $token->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_MATH);
  495. } else {
  496. continue;
  497. }
  498. $this->_tokens[] = $token;
  499. continue;
  500. }
  501. if ($token->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORINFIX && $token->getTokenSubType() == PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_NOTHING) {
  502. if (strpos("<>=", substr($token->getValue(), 0, 1)) !== false) {
  503. $token->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_LOGICAL);
  504. } else if ($token->getValue() == "&") {
  505. $token->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_CONCATENATION);
  506. } else {
  507. $token->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_MATH);
  508. }
  509. $this->_tokens[] = $token;
  510. continue;
  511. }
  512. if ($token->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND && $token->getTokenSubType() == PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_NOTHING) {
  513. if (!is_numeric($token->getValue())) {
  514. if (strtoupper($token->getValue()) == "TRUE" || strtoupper($token->getValue() == "FALSE")) {
  515. $token->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_LOGICAL);
  516. } else {
  517. $token->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_RANGE);
  518. }
  519. } else {
  520. $token->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_NUMBER);
  521. }
  522. $this->_tokens[] = $token;
  523. continue;
  524. }
  525. if ($token->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_FUNCTION) {
  526. if (strlen($token->getValue() > 0)) {
  527. if (substr($token->getValue(), 0, 1) == "@") {
  528. $token->setValue(substr($token->getValue(), 1));
  529. }
  530. }
  531. }
  532. $this->_tokens[] = $token;
  533. }
  534. }
  535. }