PHPExcel_Calculation
[ class tree: PHPExcel_Calculation ] [ index: PHPExcel_Calculation ] [ all elements ]

Source for file FormulaParser.php

Documentation is available at FormulaParser.php

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

Documentation generated on Mon, 11 Jan 2010 08:09:43 +0100 by phpDocumentor 1.4.1