FormulaParser.php 27 KB

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