PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/PHPExcel/Calculation/FormulaParser.php

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