5 * Copyright (c) 2006 - 2014 PHPExcel
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.
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.
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
22 * @package PHPExcel_Calculation
23 * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
24 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25 * @version ##VERSION##, ##DATE##
31 Copyright (c) 2007 E. W. Bachtal, Inc.
33 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
34 and associated documentation files (the "Software"), to deal in the Software without restriction,
35 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
36 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
37 subject to the following conditions:
39 The above copyright notice and this permission notice shall be included in all copies or substantial
40 portions of the Software.
42 The software is provided "as is", without warranty of any kind, express or implied, including but not
43 limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In
44 no event shall the authors or copyright holders be liable for any claim, damages or other liability,
45 whether in an action of contract, tort or otherwise, arising from, out of or in connection with the
46 software or the use or other dealings in the software.
48 http://ewbi.blogs.com/develops/2007/03/excel_formula_p.html
49 http://ewbi.blogs.com/develops/2004/12/excel_formula_p.html
53 * PHPExcel_Calculation_FormulaParser
56 * @package PHPExcel_Calculation
57 * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
59 class PHPExcel_Calculation_FormulaParser
{
60 /* Character constants */
61 const QUOTE_DOUBLE
= '"';
62 const QUOTE_SINGLE
= '\'';
63 const BRACKET_CLOSE
= ']';
64 const BRACKET_OPEN
= '[';
65 const BRACE_OPEN
= '{';
66 const BRACE_CLOSE
= '}';
67 const PAREN_OPEN
= '(';
68 const PAREN_CLOSE
= ')';
69 const SEMICOLON
= ';';
70 const WHITESPACE
= ' ';
72 const ERROR_START
= '#';
74 const OPERATORS_SN
= "+-";
75 const OPERATORS_INFIX
= "+-*/^&=><";
76 const OPERATORS_POSTFIX
= "%";
88 * @var PHPExcel_Calculation_FormulaToken[]
90 private $_tokens = array();
93 * Create a new PHPExcel_Calculation_FormulaParser
95 * @param string $pFormula Formula to parse
96 * @throws PHPExcel_Calculation_Exception
98 public function __construct($pFormula = '')
101 if (is_null($pFormula)) {
102 throw new PHPExcel_Calculation_Exception("Invalid parameter passed: formula");
106 $this->_formula
= trim($pFormula);
108 $this->_parseToTokens();
116 public function getFormula() {
117 return $this->_formula
;
123 * @param int $pId Token id
125 * @throws PHPExcel_Calculation_Exception
127 public function getToken($pId = 0) {
128 if (isset($this->_tokens
[$pId])) {
129 return $this->_tokens
[$pId];
131 throw new PHPExcel_Calculation_Exception("Token with id $pId does not exist.");
140 public function getTokenCount() {
141 return count($this->_tokens
);
147 * @return PHPExcel_Calculation_FormulaToken[]
149 public function getTokens() {
150 return $this->_tokens
;
156 private function _parseToTokens() {
157 // No attempt is made to verify formulas; assumes formulas are derived from Excel, where
158 // they can only exist if valid; stack overflows/underflows sunk as nulls without exceptions.
160 // Check if the formula has a valid starting =
161 $formulaLength = strlen($this->_formula
);
162 if ($formulaLength < 2 ||
$this->_formula
{0} != '=') return;
165 $tokens1 = $tokens2 = $stack = array();
166 $inString = $inPath = $inRange = $inError = false;
167 $token = $previousToken = $nextToken = null;
172 $ERRORS = array("#NULL!", "#DIV/0!", "#VALUE!", "#REF!", "#NAME?", "#NUM!", "#N/A");
173 $COMPARATORS_MULTI = array(">=", "<=", "<>");
175 while ($index < $formulaLength) {
176 // state-dependent character evaluation (order is important)
178 // double-quoted strings
179 // embeds are doubled
182 if ($this->_formula
{$index} == PHPExcel_Calculation_FormulaParser
::QUOTE_DOUBLE
) {
183 if ((($index +
2) <= $formulaLength) && ($this->_formula
{$index +
1} == PHPExcel_Calculation_FormulaParser
::QUOTE_DOUBLE
)) {
184 $value .= PHPExcel_Calculation_FormulaParser
::QUOTE_DOUBLE
;
188 $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERAND
, PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_TEXT
);
192 $value .= $this->_formula
{$index};
198 // single-quoted strings (links)
200 // end does not mark a token
202 if ($this->_formula
{$index} == PHPExcel_Calculation_FormulaParser
::QUOTE_SINGLE
) {
203 if ((($index +
2) <= $formulaLength) && ($this->_formula
{$index +
1} == PHPExcel_Calculation_FormulaParser
::QUOTE_SINGLE
)) {
204 $value .= PHPExcel_Calculation_FormulaParser
::QUOTE_SINGLE
;
210 $value .= $this->_formula
{$index};
216 // bracked strings (R1C1 range index or linked workbook name)
217 // no embeds (changed to "()" by Excel)
218 // end does not mark a token
220 if ($this->_formula
{$index} == PHPExcel_Calculation_FormulaParser
::BRACKET_CLOSE
) {
223 $value .= $this->_formula
{$index};
229 // end marks a token, determined from absolute list of values
231 $value .= $this->_formula
{$index};
233 if (in_array($value, $ERRORS)) {
235 $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERAND
, PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_ERROR
);
241 // scientific notation check
242 if (strpos(PHPExcel_Calculation_FormulaParser
::OPERATORS_SN
, $this->_formula
{$index}) !== false) {
243 if (strlen($value) > 1) {
244 if (preg_match("/^[1-9]{1}(\.[0-9]+)?E{1}$/", $this->_formula
{$index}) != 0) {
245 $value .= $this->_formula
{$index};
252 // independent character evaluation (order not important)
254 // establish state-dependent character evaluations
255 if ($this->_formula
{$index} == PHPExcel_Calculation_FormulaParser
::QUOTE_DOUBLE
) {
256 if (strlen($value > 0)) { // unexpected
257 $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_UNKNOWN
);
265 if ($this->_formula
{$index} == PHPExcel_Calculation_FormulaParser
::QUOTE_SINGLE
) {
266 if (strlen($value) > 0) { // unexpected
267 $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_UNKNOWN
);
275 if ($this->_formula
{$index} == PHPExcel_Calculation_FormulaParser
::BRACKET_OPEN
) {
277 $value .= PHPExcel_Calculation_FormulaParser
::BRACKET_OPEN
;
282 if ($this->_formula
{$index} == PHPExcel_Calculation_FormulaParser
::ERROR_START
) {
283 if (strlen($value) > 0) { // unexpected
284 $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_UNKNOWN
);
288 $value .= PHPExcel_Calculation_FormulaParser
::ERROR_START
;
293 // mark start and end of arrays and array rows
294 if ($this->_formula
{$index} == PHPExcel_Calculation_FormulaParser
::BRACE_OPEN
) {
295 if (strlen($value) > 0) { // unexpected
296 $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_UNKNOWN
);
300 $tmp = new PHPExcel_Calculation_FormulaToken("ARRAY", PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_FUNCTION
, PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_START
);
302 $stack[] = clone $tmp;
304 $tmp = new PHPExcel_Calculation_FormulaToken("ARRAYROW", PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_FUNCTION
, PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_START
);
306 $stack[] = clone $tmp;
312 if ($this->_formula
{$index} == PHPExcel_Calculation_FormulaParser
::SEMICOLON
) {
313 if (strlen($value) > 0) {
314 $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERAND
);
318 $tmp = array_pop($stack);
320 $tmp->setTokenSubType(PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_STOP
);
323 $tmp = new PHPExcel_Calculation_FormulaToken(",", PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_ARGUMENT
);
326 $tmp = new PHPExcel_Calculation_FormulaToken("ARRAYROW", PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_FUNCTION
, PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_START
);
328 $stack[] = clone $tmp;
334 if ($this->_formula
{$index} == PHPExcel_Calculation_FormulaParser
::BRACE_CLOSE
) {
335 if (strlen($value) > 0) {
336 $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERAND
);
340 $tmp = array_pop($stack);
342 $tmp->setTokenSubType(PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_STOP
);
345 $tmp = array_pop($stack);
347 $tmp->setTokenSubType(PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_STOP
);
355 if ($this->_formula
{$index} == PHPExcel_Calculation_FormulaParser
::WHITESPACE
) {
356 if (strlen($value) > 0) {
357 $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERAND
);
360 $tokens1[] = new PHPExcel_Calculation_FormulaToken("", PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_WHITESPACE
);
362 while (($this->_formula
{$index} == PHPExcel_Calculation_FormulaParser
::WHITESPACE
) && ($index < $formulaLength)) {
368 // multi-character comparators
369 if (($index +
2) <= $formulaLength) {
370 if (in_array(substr($this->_formula
, $index, 2), $COMPARATORS_MULTI)) {
371 if (strlen($value) > 0) {
372 $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERAND
);
375 $tokens1[] = new PHPExcel_Calculation_FormulaToken(substr($this->_formula
, $index, 2), PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERATORINFIX
, PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_LOGICAL
);
381 // standard infix operators
382 if (strpos(PHPExcel_Calculation_FormulaParser
::OPERATORS_INFIX
, $this->_formula
{$index}) !== false) {
383 if (strlen($value) > 0) {
384 $tokens1[] =new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERAND
);
387 $tokens1[] = new PHPExcel_Calculation_FormulaToken($this->_formula
{$index}, PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERATORINFIX
);
392 // standard postfix operators (only one)
393 if (strpos(PHPExcel_Calculation_FormulaParser
::OPERATORS_POSTFIX
, $this->_formula
{$index}) !== false) {
394 if (strlen($value) > 0) {
395 $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERAND
);
398 $tokens1[] = new PHPExcel_Calculation_FormulaToken($this->_formula
{$index}, PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERATORPOSTFIX
);
403 // start subexpression or function
404 if ($this->_formula
{$index} == PHPExcel_Calculation_FormulaParser
::PAREN_OPEN
) {
405 if (strlen($value) > 0) {
406 $tmp = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_FUNCTION
, PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_START
);
408 $stack[] = clone $tmp;
411 $tmp = new PHPExcel_Calculation_FormulaToken("", PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_SUBEXPRESSION
, PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_START
);
413 $stack[] = clone $tmp;
419 // function, subexpression, or array parameters, or operand unions
420 if ($this->_formula
{$index} == PHPExcel_Calculation_FormulaParser
::COMMA
) {
421 if (strlen($value) > 0) {
422 $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERAND
);
426 $tmp = array_pop($stack);
428 $tmp->setTokenSubType(PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_STOP
);
431 if ($tmp->getTokenType() == PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_FUNCTION
) {
432 $tokens1[] = new PHPExcel_Calculation_FormulaToken(",", PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERATORINFIX
, PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_UNION
);
434 $tokens1[] = new PHPExcel_Calculation_FormulaToken(",", PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_ARGUMENT
);
440 // stop subexpression
441 if ($this->_formula
{$index} == PHPExcel_Calculation_FormulaParser
::PAREN_CLOSE
) {
442 if (strlen($value) > 0) {
443 $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERAND
);
447 $tmp = array_pop($stack);
449 $tmp->setTokenSubType(PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_STOP
);
456 // token accumulation
457 $value .= $this->_formula
{$index};
461 // dump remaining accumulation
462 if (strlen($value) > 0) {
463 $tokens1[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERAND
);
466 // move tokenList to new set, excluding unnecessary white-space tokens and converting necessary ones to intersections
467 $tokenCount = count($tokens1);
468 for ($i = 0; $i < $tokenCount; ++
$i) {
469 $token = $tokens1[$i];
470 if (isset($tokens1[$i - 1])) {
471 $previousToken = $tokens1[$i - 1];
473 $previousToken = null;
475 if (isset($tokens1[$i +
1])) {
476 $nextToken = $tokens1[$i +
1];
481 if (is_null($token)) {
485 if ($token->getTokenType() != PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_WHITESPACE
) {
490 if (is_null($previousToken)) {
495 (($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_FUNCTION
) && ($previousToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_STOP
)) ||
496 (($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_SUBEXPRESSION
) && ($previousToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_STOP
)) ||
497 ($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERAND
)
502 if (is_null($nextToken)) {
507 (($nextToken->getTokenType() == PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_FUNCTION
) && ($nextToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_START
)) ||
508 (($nextToken->getTokenType() == PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_SUBEXPRESSION
) && ($nextToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_START
)) ||
509 ($nextToken->getTokenType() == PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERAND
)
514 $tokens2[] = new PHPExcel_Calculation_FormulaToken($value, PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERATORINFIX
, PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_INTERSECTION
);
517 // move tokens to final list, switching infix "-" operators to prefix when appropriate, switching infix "+" operators
518 // to noop when appropriate, identifying operand and infix-operator subtypes, and pulling "@" from function names
519 $this->_tokens
= array();
521 $tokenCount = count($tokens2);
522 for ($i = 0; $i < $tokenCount; ++
$i) {
523 $token = $tokens2[$i];
524 if (isset($tokens2[$i - 1])) {
525 $previousToken = $tokens2[$i - 1];
527 $previousToken = null;
529 if (isset($tokens2[$i +
1])) {
530 $nextToken = $tokens2[$i +
1];
535 if (is_null($token)) {
539 if ($token->getTokenType() == PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERATORINFIX
&& $token->getValue() == "-") {
541 $token->setTokenType(PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERATORPREFIX
);
543 (($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_FUNCTION
) && ($previousToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_STOP
)) ||
544 (($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_SUBEXPRESSION
) && ($previousToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_STOP
)) ||
545 ($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERATORPOSTFIX
) ||
546 ($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERAND
)
548 $token->setTokenSubType(PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_MATH
);
550 $token->setTokenType(PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERATORPREFIX
);
553 $this->_tokens
[] = $token;
557 if ($token->getTokenType() == PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERATORINFIX
&& $token->getValue() == "+") {
561 (($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_FUNCTION
) && ($previousToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_STOP
)) ||
562 (($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_SUBEXPRESSION
) && ($previousToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_STOP
)) ||
563 ($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERATORPOSTFIX
) ||
564 ($previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERAND
)
566 $token->setTokenSubType(PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_MATH
);
571 $this->_tokens
[] = $token;
575 if ($token->getTokenType() == PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERATORINFIX
&& $token->getTokenSubType() == PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_NOTHING
) {
576 if (strpos("<>=", substr($token->getValue(), 0, 1)) !== false) {
577 $token->setTokenSubType(PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_LOGICAL
);
578 } else if ($token->getValue() == "&") {
579 $token->setTokenSubType(PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_CONCATENATION
);
581 $token->setTokenSubType(PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_MATH
);
584 $this->_tokens
[] = $token;
588 if ($token->getTokenType() == PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_OPERAND
&& $token->getTokenSubType() == PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_NOTHING
) {
589 if (!is_numeric($token->getValue())) {
590 if (strtoupper($token->getValue()) == "TRUE" ||
strtoupper($token->getValue() == "FALSE")) {
591 $token->setTokenSubType(PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_LOGICAL
);
593 $token->setTokenSubType(PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_RANGE
);
596 $token->setTokenSubType(PHPExcel_Calculation_FormulaToken
::TOKEN_SUBTYPE_NUMBER
);
599 $this->_tokens
[] = $token;
603 if ($token->getTokenType() == PHPExcel_Calculation_FormulaToken
::TOKEN_TYPE_FUNCTION
) {
604 if (strlen($token->getValue() > 0)) {
605 if (substr($token->getValue(), 0, 1) == "@") {
606 $token->setValue(substr($token->getValue(), 1));
611 $this->_tokens
[] = $token;