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

Documentation generated on Mon, 05 Jan 2009 20:37:18 +0100 by phpDocumentor 1.4.1