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

Documentation generated on Sun, 27 Feb 2011 16:31:47 -0800 by phpDocumentor 1.4.3