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

Source for file Parser.php

Documentation is available at Parser.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_Writer_Excel5
  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.3c, 2010-06-01
  26.  */
  27.  
  28. // Original file header of PEAR::Spreadsheet_Excel_Writer_Parser (used as the base for this class):
  29. // -----------------------------------------------------------------------------------------
  30. // *  Class for parsing Excel formulas
  31. // *
  32. // *  License Information:
  33. // *
  34. // *    Spreadsheet_Excel_Writer:  A library for generating Excel Spreadsheets
  35. // *    Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
  36. // *
  37. // *    This library is free software; you can redistribute it and/or
  38. // *    modify it under the terms of the GNU Lesser General Public
  39. // *    License as published by the Free Software Foundation; either
  40. // *    version 2.1 of the License, or (at your option) any later version.
  41. // *
  42. // *    This library is distributed in the hope that it will be useful,
  43. // *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  44. // *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  45. // *    Lesser General Public License for more details.
  46. // *
  47. // *    You should have received a copy of the GNU Lesser General Public
  48. // *    License along with this library; if not, write to the Free Software
  49. // *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  50. // */
  51.  
  52.  
  53. /**
  54.  * PHPExcel_Writer_Excel5_Parser
  55.  *
  56.  * @category   PHPExcel
  57.  * @package    PHPExcel_Writer_Excel5
  58.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  59.  */
  60. {
  61.             const REGEX_SHEET_TITLE_UNQUOTED '[^\*\:\/\\\\\?\[\]\+\-\% \\\'\^\&\<\>\=\,\;\#\(\)\"\{\}]+';
  62.  
  63.     // Sheet title in quoted form (without surrounding quotes)
  64.     // Invalid sheet title characters cannot occur in the sheet title:
  65.     // *:/\?[]                    (usual invalid sheet title characters)
  66.     // Single quote is represented as a pair ''
  67.     const REGEX_SHEET_TITLE_QUOTED '(([^\*\:\/\\\\\?\[\]\\\'])+|(\\\'\\\')+)+';
  68.  
  69.     /**
  70.      * The index of the character we are currently looking at
  71.      * @var integer 
  72.      */
  73.     public $_current_char;
  74.  
  75.     /**
  76.      * The token we are working on.
  77.      * @var string 
  78.      */
  79.     public $_current_token;
  80.  
  81.     /**
  82.      * The formula to parse
  83.      * @var string 
  84.      */
  85.     public $_formula;
  86.  
  87.     /**
  88.      * The character ahead of the current char
  89.      * @var string 
  90.      */
  91.     public $_lookahead;
  92.  
  93.     /**
  94.      * The parse tree to be generated
  95.      * @var string 
  96.      */
  97.     public $_parse_tree;
  98.  
  99.     /**
  100.      * Array of external sheets
  101.      * @var array 
  102.      */
  103.     public $_ext_sheets;
  104.  
  105.     /**
  106.      * Array of sheet references in the form of REF structures
  107.      * @var array 
  108.      */
  109.     public $_references;
  110.  
  111.     /**
  112.      * The BIFF version for the workbook
  113.      * @var integer 
  114.      */
  115.     public $_BIFF_version;
  116.  
  117.     /**
  118.      * The class constructor
  119.      *
  120.      * @param integer $byte_order The byte order (Little endian or Big endian) of the architecture
  121.      *                            (optional). 1 => big endian, 0 (default) little endian.
  122.      */
  123.     public function __construct($biff_version)
  124.     {
  125.         $this->_current_char  = 0;
  126.         $this->_BIFF_version  = $biff_version;
  127.         $this->_current_token = '';       // The token we are working on.
  128.         $this->_formula       = '';       // The formula to parse.
  129.         $this->_lookahead     = '';       // The character ahead of the current char.
  130.         $this->_parse_tree    = '';       // The parse tree to be generated.
  131.         $this->_initializeHashes();      // Initialize the hashes: ptg's and function's ptg's
  132.         $this->_ext_sheets = array();
  133.         $this->_references = array();
  134.     }
  135.  
  136.     /**
  137.      * Initialize the ptg and function hashes.
  138.      *
  139.      * @access private
  140.      */
  141.     function _initializeHashes()
  142.     {
  143.         // The Excel ptg indices
  144.         $this->ptg array(
  145.             'ptgExp'       => 0x01,
  146.             'ptgTbl'       => 0x02,
  147.             'ptgAdd'       => 0x03,
  148.             'ptgSub'       => 0x04,
  149.             'ptgMul'       => 0x05,
  150.             'ptgDiv'       => 0x06,
  151.             'ptgPower'     => 0x07,
  152.             'ptgConcat'    => 0x08,
  153.             'ptgLT'        => 0x09,
  154.             'ptgLE'        => 0x0A,
  155.             'ptgEQ'        => 0x0B,
  156.             'ptgGE'        => 0x0C,
  157.             'ptgGT'        => 0x0D,
  158.             'ptgNE'        => 0x0E,
  159.             'ptgIsect'     => 0x0F,
  160.             'ptgUnion'     => 0x10,
  161.             'ptgRange'     => 0x11,
  162.             'ptgUplus'     => 0x12,
  163.             'ptgUminus'    => 0x13,
  164.             'ptgPercent'   => 0x14,
  165.             'ptgParen'     => 0x15,
  166.             'ptgMissArg'   => 0x16,
  167.             'ptgStr'       => 0x17,
  168.             'ptgAttr'      => 0x19,
  169.             'ptgSheet'     => 0x1A,
  170.             'ptgEndSheet'  => 0x1B,
  171.             'ptgErr'       => 0x1C,
  172.             'ptgBool'      => 0x1D,
  173.             'ptgInt'       => 0x1E,
  174.             'ptgNum'       => 0x1F,
  175.             'ptgArray'     => 0x20,
  176.             'ptgFunc'      => 0x21,
  177.             'ptgFuncVar'   => 0x22,
  178.             'ptgName'      => 0x23,
  179.             'ptgRef'       => 0x24,
  180.             'ptgArea'      => 0x25,
  181.             'ptgMemArea'   => 0x26,
  182.             'ptgMemErr'    => 0x27,
  183.             'ptgMemNoMem'  => 0x28,
  184.             'ptgMemFunc'   => 0x29,
  185.             'ptgRefErr'    => 0x2A,
  186.             'ptgAreaErr'   => 0x2B,
  187.             'ptgRefN'      => 0x2C,
  188.             'ptgAreaN'     => 0x2D,
  189.             'ptgMemAreaN'  => 0x2E,
  190.             'ptgMemNoMemN' => 0x2F,
  191.             'ptgNameX'     => 0x39,
  192.             'ptgRef3d'     => 0x3A,
  193.             'ptgArea3d'    => 0x3B,
  194.             'ptgRefErr3d'  => 0x3C,
  195.             'ptgAreaErr3d' => 0x3D,
  196.             'ptgArrayV'    => 0x40,
  197.             'ptgFuncV'     => 0x41,
  198.             'ptgFuncVarV'  => 0x42,
  199.             'ptgNameV'     => 0x43,
  200.             'ptgRefV'      => 0x44,
  201.             'ptgAreaV'     => 0x45,
  202.             'ptgMemAreaV'  => 0x46,
  203.             'ptgMemErrV'   => 0x47,
  204.             'ptgMemNoMemV' => 0x48,
  205.             'ptgMemFuncV'  => 0x49,
  206.             'ptgRefErrV'   => 0x4A,
  207.             'ptgAreaErrV'  => 0x4B,
  208.             'ptgRefNV'     => 0x4C,
  209.             'ptgAreaNV'    => 0x4D,
  210.             'ptgMemAreaNV' => 0x4E,
  211.             'ptgMemNoMemN' => 0x4F,
  212.             'ptgFuncCEV'   => 0x58,
  213.             'ptgNameXV'    => 0x59,
  214.             'ptgRef3dV'    => 0x5A,
  215.             'ptgArea3dV'   => 0x5B,
  216.             'ptgRefErr3dV' => 0x5C,
  217.             'ptgAreaErr3d' => 0x5D,
  218.             'ptgArrayA'    => 0x60,
  219.             'ptgFuncA'     => 0x61,
  220.             'ptgFuncVarA'  => 0x62,
  221.             'ptgNameA'     => 0x63,
  222.             'ptgRefA'      => 0x64,
  223.             'ptgAreaA'     => 0x65,
  224.             'ptgMemAreaA'  => 0x66,
  225.             'ptgMemErrA'   => 0x67,
  226.             'ptgMemNoMemA' => 0x68,
  227.             'ptgMemFuncA'  => 0x69,
  228.             'ptgRefErrA'   => 0x6A,
  229.             'ptgAreaErrA'  => 0x6B,
  230.             'ptgRefNA'     => 0x6C,
  231.             'ptgAreaNA'    => 0x6D,
  232.             'ptgMemAreaNA' => 0x6E,
  233.             'ptgMemNoMemN' => 0x6F,
  234.             'ptgFuncCEA'   => 0x78,
  235.             'ptgNameXA'    => 0x79,
  236.             'ptgRef3dA'    => 0x7A,
  237.             'ptgArea3dA'   => 0x7B,
  238.             'ptgRefErr3dA' => 0x7C,
  239.             'ptgAreaErr3d' => 0x7D
  240.             );
  241.  
  242.         // Thanks to Michael Meeks and Gnumeric for the initial arg values.
  243.         //
  244.         // The following hash was generated by "function_locale.pl" in the distro.
  245.         // Refer to function_locale.pl for non-English function names.
  246.         //
  247.         // The array elements are as follow:
  248.         // ptg:   The Excel function ptg code.
  249.         // args:  The number of arguments that the function takes:
  250.         //           >=0 is a fixed number of arguments.
  251.         //           -1  is a variable  number of arguments.
  252.         // class: The reference, value or array class of the function args.
  253.         // vol:   The function is volatile.
  254.         //
  255.         $this->_functions array(
  256.               // function                  ptg  args  class  vol
  257.               'COUNT'           => array(   0,   -1,    0,    ),
  258.               'IF'              => array(   1,   -1,    1,    ),
  259.               'ISNA'            => array(   2,    1,    1,    ),
  260.               'ISERROR'         => array(   3,    1,    1,    ),
  261.               'SUM'             => array(   4,   -1,    0,    ),
  262.               'AVERAGE'         => array(   5,   -1,    0,    ),
  263.               'MIN'             => array(   6,   -1,    0,    ),
  264.               'MAX'             => array(   7,   -1,    0,    ),
  265.               'ROW'             => array(   8,   -1,    0,    ),
  266.               'COLUMN'          => array(   9,   -1,    0,    ),
  267.               'NA'              => array(  10,    0,    0,    ),
  268.               'NPV'             => array(  11,   -1,    1,    ),
  269.               'STDEV'           => array(  12,   -1,    0,    ),
  270.               'DOLLAR'          => array(  13,   -1,    1,    ),
  271.               'FIXED'           => array(  14,   -1,    1,    ),
  272.               'SIN'             => array(  15,    1,    1,    ),
  273.               'COS'             => array(  16,    1,    1,    ),
  274.               'TAN'             => array(  17,    1,    1,    ),
  275.               'ATAN'            => array(  18,    1,    1,    ),
  276.               'PI'              => array(  19,    0,    1,    ),
  277.               'SQRT'            => array(  20,    1,    1,    ),
  278.               'EXP'             => array(  21,    1,    1,    ),
  279.               'LN'              => array(  22,    1,    1,    ),
  280.               'LOG10'           => array(  23,    1,    1,    ),
  281.               'ABS'             => array(  24,    1,    1,    ),
  282.               'INT'             => array(  25,    1,    1,    ),
  283.               'SIGN'            => array(  26,    1,    1,    ),
  284.               'ROUND'           => array(  27,    2,    1,    ),
  285.               'LOOKUP'          => array(  28,   -1,    0,    ),
  286.               'INDEX'           => array(  29,   -1,    0,    ),
  287.               'REPT'            => array(  30,    2,    1,    ),
  288.               'MID'             => array(  31,    3,    1,    ),
  289.               'LEN'             => array(  32,    1,    1,    ),
  290.               'VALUE'           => array(  33,    1,    1,    ),
  291.               'TRUE'            => array(  34,    0,    1,    ),
  292.               'FALSE'           => array(  35,    0,    1,    ),
  293.               'AND'             => array(  36,   -1,    0,    ),
  294.               'OR'              => array(  37,   -1,    0,    ),
  295.               'NOT'             => array(  38,    1,    1,    ),
  296.               'MOD'             => array(  39,    2,    1,    ),
  297.               'DCOUNT'          => array(  40,    3,    0,    ),
  298.               'DSUM'            => array(  41,    3,    0,    ),
  299.               'DAVERAGE'        => array(  42,    3,    0,    ),
  300.               'DMIN'            => array(  43,    3,    0,    ),
  301.               'DMAX'            => array(  44,    3,    0,    ),
  302.               'DSTDEV'          => array(  45,    3,    0,    ),
  303.               'VAR'             => array(  46,   -1,    0,    ),
  304.               'DVAR'            => array(  47,    3,    0,    ),
  305.               'TEXT'            => array(  48,    2,    1,    ),
  306.               'LINEST'          => array(  49,   -1,    0,    ),
  307.               'TREND'           => array(  50,   -1,    0,    ),
  308.               'LOGEST'          => array(  51,   -1,    0,    ),
  309.               'GROWTH'          => array(  52,   -1,    0,    ),
  310.               'PV'              => array(  56,   -1,    1,    ),
  311.               'FV'              => array(  57,   -1,    1,    ),
  312.               'NPER'            => array(  58,   -1,    1,    ),
  313.               'PMT'             => array(  59,   -1,    1,    ),
  314.               'RATE'            => array(  60,   -1,    1,    ),
  315.               'MIRR'            => array(  61,    3,    0,    ),
  316.               'IRR'             => array(  62,   -1,    0,    ),
  317.               'RAND'            => array(  63,    0,    1,    ),
  318.               'MATCH'           => array(  64,   -1,    0,    ),
  319.               'DATE'            => array(  65,    3,    1,    ),
  320.               'TIME'            => array(  66,    3,    1,    ),
  321.               'DAY'             => array(  67,    1,    1,    ),
  322.               'MONTH'           => array(  68,    1,    1,    ),
  323.               'YEAR'            => array(  69,    1,    1,    ),
  324.               'WEEKDAY'         => array(  70,   -1,    1,    ),
  325.               'HOUR'            => array(  71,    1,    1,    ),
  326.               'MINUTE'          => array(  72,    1,    1,    ),
  327.               'SECOND'          => array(  73,    1,    1,    ),
  328.               'NOW'             => array(  74,    0,    1,    ),
  329.               'AREAS'           => array(  75,    1,    0,    ),
  330.               'ROWS'            => array(  76,    1,    0,    ),
  331.               'COLUMNS'         => array(  77,    1,    0,    ),
  332.               'OFFSET'          => array(  78,   -1,    0,    ),
  333.               'SEARCH'          => array(  82,   -1,    1,    ),
  334.               'TRANSPOSE'       => array(  83,    1,    1,    ),
  335.               'TYPE'            => array(  86,    1,    1,    ),
  336.               'ATAN2'           => array(  97,    2,    1,    ),
  337.               'ASIN'            => array(  98,    1,    1,    ),
  338.               'ACOS'            => array(  99,    1,    1,    ),
  339.               'CHOOSE'          => array100,   -1,    1,    ),
  340.               'HLOOKUP'         => array101,   -1,    0,    ),
  341.               'VLOOKUP'         => array102,   -1,    0,    ),
  342.               'ISREF'           => array105,    1,    0,    ),
  343.               'LOG'             => array109,   -1,    1,    ),
  344.               'CHAR'            => array111,    1,    1,    ),
  345.               'LOWER'           => array112,    1,    1,    ),
  346.               'UPPER'           => array113,    1,    1,    ),
  347.               'PROPER'          => array114,    1,    1,    ),
  348.               'LEFT'            => array115,   -1,    1,    ),
  349.               'RIGHT'           => array116,   -1,    1,    ),
  350.               'EXACT'           => array117,    2,    1,    ),
  351.               'TRIM'            => array118,    1,    1,    ),
  352.               'REPLACE'         => array119,    4,    1,    ),
  353.               'SUBSTITUTE'      => array120,   -1,    1,    ),
  354.               'CODE'            => array121,    1,    1,    ),
  355.               'FIND'            => array124,   -1,    1,    ),
  356.               'CELL'            => array125,   -1,    0,    ),
  357.               'ISERR'           => array126,    1,    1,    ),
  358.               'ISTEXT'          => array127,    1,    1,    ),
  359.               'ISNUMBER'        => array128,    1,    1,    ),
  360.               'ISBLANK'         => array129,    1,    1,    ),
  361.               'T'               => array130,    1,    0,    ),
  362.               'N'               => array131,    1,    0,    ),
  363.               'DATEVALUE'       => array140,    1,    1,    ),
  364.               'TIMEVALUE'       => array141,    1,    1,    ),
  365.               'SLN'             => array142,    3,    1,    ),
  366.               'SYD'             => array143,    4,    1,    ),
  367.               'DDB'             => array144,   -1,    1,    ),
  368.               'INDIRECT'        => array148,   -1,    1,    ),
  369.               'CALL'            => array150,   -1,    1,    ),
  370.               'CLEAN'           => array162,    1,    1,    ),
  371.               'MDETERM'         => array163,    1,    2,    ),
  372.               'MINVERSE'        => array164,    1,    2,    ),
  373.               'MMULT'           => array165,    2,    2,    ),
  374.               'IPMT'            => array167,   -1,    1,    ),
  375.               'PPMT'            => array168,   -1,    1,    ),
  376.               'COUNTA'          => array169,   -1,    0,    ),
  377.               'PRODUCT'         => array183,   -1,    0,    ),
  378.               'FACT'            => array184,    1,    1,    ),
  379.               'DPRODUCT'        => array189,    3,    0,    ),
  380.               'ISNONTEXT'       => array190,    1,    1,    ),
  381.               'STDEVP'          => array193,   -1,    0,    ),
  382.               'VARP'            => array194,   -1,    0,    ),
  383.               'DSTDEVP'         => array195,    3,    0,    ),
  384.               'DVARP'           => array196,    3,    0,    ),
  385.               'TRUNC'           => array197,   -1,    1,    ),
  386.               'ISLOGICAL'       => array198,    1,    1,    ),
  387.               'DCOUNTA'         => array199,    3,    0,    ),
  388.               'USDOLLAR'        => array204,   -1,    1,    ),
  389.               'FINDB'           => array205,   -1,    1,    ),
  390.               'SEARCHB'         => array206,   -1,    1,    ),
  391.               'REPLACEB'        => array207,    4,    1,    ),
  392.               'LEFTB'           => array208,   -1,    1,    ),
  393.               'RIGHTB'          => array209,   -1,    1,    ),
  394.               'MIDB'            => array210,    3,    1,    ),
  395.               'LENB'            => array211,    1,    1,    ),
  396.               'ROUNDUP'         => array212,    2,    1,    ),
  397.               'ROUNDDOWN'       => array213,    2,    1,    ),
  398.               'ASC'             => array214,    1,    1,    ),
  399.               'DBCS'            => array215,    1,    1,    ),
  400.               'RANK'            => array216,   -1,    0,    ),
  401.               'ADDRESS'         => array219,   -1,    1,    ),
  402.               'DAYS360'         => array220,   -1,    1,    ),
  403.               'TODAY'           => array221,    0,    1,    ),
  404.               'VDB'             => array222,   -1,    1,    ),
  405.               'MEDIAN'          => array227,   -1,    0,    ),
  406.               'SUMPRODUCT'      => array228,   -1,    2,    ),
  407.               'SINH'            => array229,    1,    1,    ),
  408.               'COSH'            => array230,    1,    1,    ),
  409.               'TANH'            => array231,    1,    1,    ),
  410.               'ASINH'           => array232,    1,    1,    ),
  411.               'ACOSH'           => array233,    1,    1,    ),
  412.               'ATANH'           => array234,    1,    1,    ),
  413.               'DGET'            => array235,    3,    0,    ),
  414.               'INFO'            => array244,    1,    1,    ),
  415.               'DB'              => array247,   -1,    1,    ),
  416.               'FREQUENCY'       => array252,    2,    0,    ),
  417.               'ERROR.TYPE'      => array261,    1,    1,    ),
  418.               'REGISTER.ID'     => array267,   -1,    1,    ),
  419.               'AVEDEV'          => array269,   -1,    0,    ),
  420.               'BETADIST'        => array270,   -1,    1,    ),
  421.               'GAMMALN'         => array271,    1,    1,    ),
  422.               'BETAINV'         => array272,   -1,    1,    ),
  423.               'BINOMDIST'       => array273,    4,    1,    ),
  424.               'CHIDIST'         => array274,    2,    1,    ),
  425.               'CHIINV'          => array275,    2,    1,    ),
  426.               'COMBIN'          => array276,    2,    1,    ),
  427.               'CONFIDENCE'      => array277,    3,    1,    ),
  428.               'CRITBINOM'       => array278,    3,    1,    ),
  429.               'EVEN'            => array279,    1,    1,    ),
  430.               'EXPONDIST'       => array280,    3,    1,    ),
  431.               'FDIST'           => array281,    3,    1,    ),
  432.               'FINV'            => array282,    3,    1,    ),
  433.               'FISHER'          => array283,    1,    1,    ),
  434.               'FISHERINV'       => array284,    1,    1,    ),
  435.               'FLOOR'           => array285,    2,    1,    ),
  436.               'GAMMADIST'       => array286,    4,    1,    ),
  437.               'GAMMAINV'        => array287,    3,    1,    ),
  438.               'CEILING'         => array288,    2,    1,    ),
  439.               'HYPGEOMDIST'     => array289,    4,    1,    ),
  440.               'LOGNORMDIST'     => array290,    3,    1,    ),
  441.               'LOGINV'          => array291,    3,    1,    ),
  442.               'NEGBINOMDIST'    => array292,    3,    1,    ),
  443.               'NORMDIST'        => array293,    4,    1,    ),
  444.               'NORMSDIST'       => array294,    1,    1,    ),
  445.               'NORMINV'         => array295,    3,    1,    ),
  446.               'NORMSINV'        => array296,    1,    1,    ),
  447.               'STANDARDIZE'     => array297,    3,    1,    ),
  448.               'ODD'             => array298,    1,    1,    ),
  449.               'PERMUT'          => array299,    2,    1,    ),
  450.               'POISSON'         => array300,    3,    1,    ),
  451.               'TDIST'           => array301,    3,    1,    ),
  452.               'WEIBULL'         => array302,    4,    1,    ),
  453.               'SUMXMY2'         => array303,    2,    2,    ),
  454.               'SUMX2MY2'        => array304,    2,    2,    ),
  455.               'SUMX2PY2'        => array305,    2,    2,    ),
  456.               'CHITEST'         => array306,    2,    2,    ),
  457.               'CORREL'          => array307,    2,    2,    ),
  458.               'COVAR'           => array308,    2,    2,    ),
  459.               'FORECAST'        => array309,    3,    2,    ),
  460.               'FTEST'           => array310,    2,    2,    ),
  461.               'INTERCEPT'       => array311,    2,    2,    ),
  462.               'PEARSON'         => array312,    2,    2,    ),
  463.               'RSQ'             => array313,    2,    2,    ),
  464.               'STEYX'           => array314,    2,    2,    ),
  465.               'SLOPE'           => array315,    2,    2,    ),
  466.               'TTEST'           => array316,    4,    2,    ),
  467.               'PROB'            => array317,   -1,    2,    ),
  468.               'DEVSQ'           => array318,   -1,    0,    ),
  469.               'GEOMEAN'         => array319,   -1,    0,    ),
  470.               'HARMEAN'         => array320,   -1,    0,    ),
  471.               'SUMSQ'           => array321,   -1,    0,    ),
  472.               'KURT'            => array322,   -1,    0,    ),
  473.               'SKEW'            => array323,   -1,    0,    ),
  474.               'ZTEST'           => array324,   -1,    0,    ),
  475.               'LARGE'           => array325,    2,    0,    ),
  476.               'SMALL'           => array326,    2,    0,    ),
  477.               'QUARTILE'        => array327,    2,    0,    ),
  478.               'PERCENTILE'      => array328,    2,    0,    ),
  479.               'PERCENTRANK'     => array329,   -1,    0,    ),
  480.               'MODE'            => array330,   -1,    2,    ),
  481.               'TRIMMEAN'        => array331,    2,    0,    ),
  482.               'TINV'            => array332,    2,    1,    ),
  483.               'CONCATENATE'     => array336,   -1,    1,    ),
  484.               'POWER'           => array337,    2,    1,    ),
  485.               'RADIANS'         => array342,    1,    1,    ),
  486.               'DEGREES'         => array343,    1,    1,    ),
  487.               'SUBTOTAL'        => array344,   -1,    0,    ),
  488.               'SUMIF'           => array345,   -1,    0,    ),
  489.               'COUNTIF'         => array346,    2,    0,    ),
  490.               'COUNTBLANK'      => array347,    1,    0,    ),
  491.               'ISPMT'           => array350,    4,    1,    ),
  492.               'DATEDIF'         => array351,    3,    1,    ),
  493.               'DATESTRING'      => array352,    1,    1,    ),
  494.               'NUMBERSTRING'    => array353,    2,    1,    ),
  495.               'ROMAN'           => array354,   -1,    1,    ),
  496.               'GETPIVOTDATA'    => array358,   -1,    0,    ),
  497.               'HYPERLINK'       => array359,   -1,    1,    ),
  498.               'PHONETIC'        => array360,    1,    0,    ),
  499.               'AVERAGEA'        => array361,   -1,    0,    ),
  500.               'MAXA'            => array362,   -1,    0,    ),
  501.               'MINA'            => array363,   -1,    0,    ),
  502.               'STDEVPA'         => array364,   -1,    0,    ),
  503.               'VARPA'           => array365,   -1,    0,    ),
  504.               'STDEVA'          => array366,   -1,    0,    ),
  505.               'VARA'            => array367,   -1,    0,    ),
  506.               );
  507.     }
  508.  
  509.     /**
  510.      * Convert a token to the proper ptg value.
  511.      *
  512.      * @access private
  513.      * @param mixed $token The token to convert.
  514.      * @return mixed the converted token on success
  515.      */
  516.     function _convert($token)
  517.     {
  518.         if (preg_match("/^\"[^\"]{0,255}\"$/"$token)) {
  519.             return $this->_convertString($token);
  520.  
  521.         elseif (is_numeric($token)) {
  522.             return $this->_convertNumber($token);
  523.  
  524.         // match references like A1 or $A$1
  525.         elseif (preg_match('/^\$?([A-Ia-i]?[A-Za-z])\$?(\d+)$/',$token)) {
  526.             return $this->_convertRef2d($token);
  527.  
  528.         // match external references like Sheet1!A1 or Sheet1:Sheet2!A1 or Sheet1!$A$1 or Sheet1:Sheet2!$A$1
  529.         elseif (preg_match("/^" self::REGEX_SHEET_TITLE_UNQUOTED "(\:" self::REGEX_SHEET_TITLE_UNQUOTED ")?\!\\$?[A-Ia-i]?[A-Za-z]\\$?(\d+)$/u",$token)) {
  530.             return $this->_convertRef3d($token);
  531.  
  532.         // match external references like 'Sheet1'!A1 or 'Sheet1:Sheet2'!A1 or 'Sheet1'!$A$1 or 'Sheet1:Sheet2'!$A$1
  533.         elseif (preg_match("/^'" self::REGEX_SHEET_TITLE_QUOTED "(\:" self::REGEX_SHEET_TITLE_QUOTED ")?'\!\\$?[A-Ia-i]?[A-Za-z]\\$?(\d+)$/u",$token)) {
  534.             return $this->_convertRef3d($token);
  535.  
  536.         // match ranges like A1:B2 or $A$1:$B$2
  537.         elseif (preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?(\d+)\:(\$)?[A-Ia-i]?[A-Za-z](\$)?(\d+)$/'$token)) {
  538.             return $this->_convertRange2d($token);
  539.  
  540.         // match external ranges like Sheet1!A1:B2 or Sheet1:Sheet2!A1:B2 or Sheet1!$A$1:$B$2 or Sheet1:Sheet2!$A$1:$B$2
  541.         elseif (preg_match("/^" self::REGEX_SHEET_TITLE_UNQUOTED "(\:" self::REGEX_SHEET_TITLE_UNQUOTED ")?\!\\$?([A-Ia-i]?[A-Za-z])?\\$?(\d+)\:\\$?([A-Ia-i]?[A-Za-z])?\\$?(\d+)$/u",$token)) {
  542.             return $this->_convertRange3d($token);
  543.  
  544.         // match external ranges like 'Sheet1'!A1:B2 or 'Sheet1:Sheet2'!A1:B2 or 'Sheet1'!$A$1:$B$2 or 'Sheet1:Sheet2'!$A$1:$B$2
  545.         elseif (preg_match("/^'" self::REGEX_SHEET_TITLE_QUOTED "(\:" self::REGEX_SHEET_TITLE_QUOTED ")?'\!\\$?([A-Ia-i]?[A-Za-z])?\\$?(\d+)\:\\$?([A-Ia-i]?[A-Za-z])?\\$?(\d+)$/u",$token)) {
  546.             return $this->_convertRange3d($token);
  547.  
  548.         // operators (including parentheses)
  549.         elseif (isset($this->ptg[$token])) {
  550.             return pack("C"$this->ptg[$token]);
  551.  
  552.         // commented so argument number can be processed correctly. See toReversePolish().
  553.         /*elseif (preg_match("/[A-Z0-9\xc0-\xdc\.]+/",$token))
  554.         {
  555.             return($this->_convertFunction($token,$this->_func_args));
  556.         }*/
  557.  
  558.         // if it's an argument, ignore the token (the argument remains)
  559.         elseif ($token == 'arg'{
  560.             return '';
  561.         }
  562.         // TODO: use real error codes
  563.         throw new Exception("Unknown token $token");
  564.     }
  565.  
  566.     /**
  567.      * Convert a number token to ptgInt or ptgNum
  568.      *
  569.      * @access private
  570.      * @param mixed $num an integer or double for conversion to its ptg value
  571.      */
  572.     function _convertNumber($num)
  573.     {
  574.         // Integer in the range 0..2**16-1
  575.         if ((preg_match("/^\d+$/"$num)) and ($num <= 65535)) {
  576.             return pack("Cv"$this->ptg['ptgInt']$num);
  577.         else // A float
  578.             if (PHPExcel_Writer_Excel5_BIFFwriter::getByteOrder()) // if it's Big Endian
  579.                 $num strrev($num);
  580.             }
  581.             return pack("Cd"$this->ptg['ptgNum']$num);
  582.         }
  583.     }
  584.  
  585.     /**
  586.      * Convert a string token to ptgStr
  587.      *
  588.      * @access private
  589.      * @param string $string A string for conversion to its ptg value.
  590.      * @return mixed the converted token on success
  591.      */
  592.     function _convertString($string)
  593.     {
  594.         // chop away beggining and ending quotes
  595.         $string substr($string1strlen($string2);
  596.         if (strlen($string255{
  597.             throw new Exception("String is too long");
  598.         }
  599.  
  600.         if ($this->_BIFF_version == 0x0500{
  601.             return pack("CC"$this->ptg['ptgStr']strlen($string)).$string;
  602.         elseif ($this->_BIFF_version == 0x0600{
  603.             return pack('C'$this->ptg['ptgStr']PHPExcel_Shared_String::UTF8toBIFF8UnicodeShort($string);
  604.         }
  605.     }
  606.  
  607.     /**
  608.      * Convert a function to a ptgFunc or ptgFuncVarV depending on the number of
  609.      * args that it takes.
  610.      *
  611.      * @access private
  612.      * @param string  $token    The name of the function for convertion to ptg value.
  613.      * @param integer $num_args The number of arguments the function receives.
  614.      * @return string The packed ptg for the function
  615.      */
  616.     function _convertFunction($token$num_args)
  617.     {
  618.         $args     $this->_functions[$token][1];
  619.         $volatile $this->_functions[$token][3];
  620.  
  621.         // Fixed number of args eg. TIME($i,$j,$k).
  622.         if ($args >= 0{
  623.             return pack("Cv"$this->ptg['ptgFuncV']$this->_functions[$token][0]);
  624.         }
  625.         // Variable number of args eg. SUM($i,$j,$k, ..).
  626.         if ($args == -1{
  627.             return pack("CCv"$this->ptg['ptgFuncVarV']$num_args$this->_functions[$token][0]);
  628.         }
  629.     }
  630.  
  631.     /**
  632.      * Convert an Excel range such as A1:D4 to a ptgRefV.
  633.      *
  634.      * @access private
  635.      * @param string $range An Excel range in the A1:A2
  636.      */
  637.     function _convertRange2d($range$class=0)
  638.     {
  639.  
  640.         // TODO: possible class value 0,1,2 check Formula.pm
  641.         // Split the range into 2 cell refs
  642.         if (preg_match('/^(\$)?([A-Ia-i]?[A-Za-z])(\$)?(\d+)\:(\$)?([A-Ia-i]?[A-Za-z])(\$)?(\d+)$/'$range)) {
  643.             list($cell1$cell2explode(':'$range);
  644.         else {
  645.             // TODO: use real error codes
  646.             throw new Exception("Unknown range separator");
  647.         }
  648.  
  649.         // Convert the cell references
  650.         $cell_array1 $this->_cellToPackedRowcol($cell1);
  651.         list($row1$col1$cell_array1;
  652.         $cell_array2 $this->_cellToPackedRowcol($cell2);
  653.         list($row2$col2$cell_array2;
  654.  
  655.         // The ptg value depends on the class of the ptg.
  656.         if ($class == 0{
  657.             $ptgArea pack("C"$this->ptg['ptgArea']);
  658.         elseif ($class == 1{
  659.             $ptgArea pack("C"$this->ptg['ptgAreaV']);
  660.         elseif ($class == 2{
  661.             $ptgArea pack("C"$this->ptg['ptgAreaA']);
  662.         else {
  663.             // TODO: use real error codes
  664.             throw new Exception("Unknown class $class");
  665.         }
  666.         return $ptgArea $row1 $row2 $col1$col2;
  667.     }
  668.  
  669.     /**
  670.      * Convert an Excel 3d range such as "Sheet1!A1:D4" or "Sheet1:Sheet2!A1:D4" to
  671.      * a ptgArea3d.
  672.      *
  673.      * @access private
  674.      * @param string $token An Excel range in the Sheet1!A1:A2 format.
  675.      * @return mixed The packed ptgArea3d token on success.
  676.      */
  677.     function _convertRange3d($token)
  678.     {
  679.         $class 0// formulas like Sheet1!$A$1:$A$2 in list type data validation need this class (0x3B)
  680.  
  681.         // Split the ref at the ! symbol
  682.         list($ext_ref$rangeexplode('!'$token);
  683.  
  684.         // Convert the external reference part (different for BIFF8)
  685.         if ($this->_BIFF_version == 0x0500{
  686.             $ext_ref $this->_packExtRef($ext_ref);
  687.         elseif ($this->_BIFF_version == 0x0600{
  688.              $ext_ref $this->_getRefIndex($ext_ref);
  689.         }
  690.  
  691.         // Split the range into 2 cell refs
  692.         list($cell1$cell2explode(':'$range);
  693.  
  694.         // Convert the cell references
  695.         if (preg_match("/^(\\$)?[A-Ia-i]?[A-Za-z](\\$)?(\d+)$/"$cell1)) {
  696.             $cell_array1 $this->_cellToPackedRowcol($cell1);
  697.             list($row1$col1$cell_array1;
  698.             $cell_array2 $this->_cellToPackedRowcol($cell2);
  699.             list($row2$col2$cell_array2;
  700.         else // It's a rows range (like 26:27)
  701.              $cells_array $this->_rangeToPackedRange($cell1.':'.$cell2);
  702.              list($row1$col1$row2$col2$cells_array;
  703.         }
  704.  
  705.         // The ptg value depends on the class of the ptg.
  706.         if ($class == 0{
  707.             $ptgArea pack("C"$this->ptg['ptgArea3d']);
  708.         elseif ($class == 1{
  709.             $ptgArea pack("C"$this->ptg['ptgArea3dV']);
  710.         elseif ($class == 2{
  711.             $ptgArea pack("C"$this->ptg['ptgArea3dA']);
  712.         else {
  713.             throw new Exception("Unknown class $class");
  714.         }
  715.  
  716.         return $ptgArea $ext_ref $row1 $row2 $col1$col2;
  717.     }
  718.  
  719.     /**
  720.      * Convert an Excel reference such as A1, $B2, C$3 or $D$4 to a ptgRefV.
  721.      *
  722.      * @access private
  723.      * @param string $cell An Excel cell reference
  724.      * @return string The cell in packed() format with the corresponding ptg
  725.      */
  726.     function _convertRef2d($cell)
  727.     {
  728.         $class 2// as far as I know, this is magick.
  729.  
  730.         // Convert the cell reference
  731.         $cell_array $this->_cellToPackedRowcol($cell);
  732.         list($row$col$cell_array;
  733.  
  734.         // The ptg value depends on the class of the ptg.
  735.         if ($class == 0{
  736.             $ptgRef pack("C"$this->ptg['ptgRef']);
  737.         elseif ($class == 1{
  738.             $ptgRef pack("C"$this->ptg['ptgRefV']);
  739.         elseif ($class == 2{
  740.             $ptgRef pack("C"$this->ptg['ptgRefA']);
  741.         else {
  742.             // TODO: use real error codes
  743.             throw new Exception("Unknown class $class");
  744.         }
  745.         return $ptgRef.$row.$col;
  746.     }
  747.  
  748.     /**
  749.      * Convert an Excel 3d reference such as "Sheet1!A1" or "Sheet1:Sheet2!A1" to a
  750.      * ptgRef3d.
  751.      *
  752.      * @access private
  753.      * @param string $cell An Excel cell reference
  754.      * @return mixed The packed ptgRef3d token on success.
  755.      */
  756.     function _convertRef3d($cell)
  757.     {
  758.         $class 2// as far as I know, this is magick.
  759.  
  760.         // Split the ref at the ! symbol
  761.         list($ext_ref$cellexplode('!'$cell);
  762.  
  763.         // Convert the external reference part (different for BIFF8)
  764.         if ($this->_BIFF_version == 0x0500{
  765.             $ext_ref $this->_packExtRef($ext_ref);
  766.         elseif ($this->_BIFF_version == 0x0600{
  767.             $ext_ref $this->_getRefIndex($ext_ref);
  768.         }
  769.  
  770.         // Convert the cell reference part
  771.         list($row$col$this->_cellToPackedRowcol($cell);
  772.  
  773.         // The ptg value depends on the class of the ptg.
  774.         if ($class == 0{
  775.             $ptgRef pack("C"$this->ptg['ptgRef3d']);
  776.         elseif ($class == 1{
  777.             $ptgRef pack("C"$this->ptg['ptgRef3dV']);
  778.         elseif ($class == 2{
  779.             $ptgRef pack("C"$this->ptg['ptgRef3dA']);
  780.         else {
  781.             throw new Exception("Unknown class $class");
  782.         }
  783.  
  784.         return $ptgRef $ext_ref$row $col;
  785.     }
  786.  
  787.     /**
  788.      * Convert the sheet name part of an external reference, for example "Sheet1" or
  789.      * "Sheet1:Sheet2", to a packed structure.
  790.      *
  791.      * @access private
  792.      * @param string $ext_ref The name of the external reference
  793.      * @return string The reference index in packed() format
  794.      */
  795.     function _packExtRef($ext_ref)
  796.     {
  797.         $ext_ref preg_replace("/^'/"''$ext_ref)// Remove leading  ' if any.
  798.         $ext_ref preg_replace("/'$/"''$ext_ref)// Remove trailing ' if any.
  799.  
  800.         // Check if there is a sheet range eg., Sheet1:Sheet2.
  801.         if (preg_match("/:/"$ext_ref)) {
  802.             list($sheet_name1$sheet_name2explode(':'$ext_ref);
  803.  
  804.             $sheet1 $this->_getSheetIndex($sheet_name1);
  805.             if ($sheet1 == -1{
  806.                 throw new Exception("Unknown sheet name $sheet_name1 in formula");
  807.             }
  808.             $sheet2 $this->_getSheetIndex($sheet_name2);
  809.             if ($sheet2 == -1{
  810.                 throw new Exception("Unknown sheet name $sheet_name2 in formula");
  811.             }
  812.  
  813.             // Reverse max and min sheet numbers if necessary
  814.             if ($sheet1 $sheet2{
  815.                 list($sheet1$sheet2array($sheet2$sheet1);
  816.             }
  817.         else // Single sheet name only.
  818.             $sheet1 $this->_getSheetIndex($ext_ref);
  819.             if ($sheet1 == -1{
  820.                 throw new Exception("Unknown sheet name $ext_ref in formula");
  821.             }
  822.             $sheet2 $sheet1;
  823.         }
  824.  
  825.         // References are stored relative to 0xFFFF.
  826.         $offset = -$sheet1;
  827.  
  828.         return pack('vdvv'$offset0x00$sheet1$sheet2);
  829.     }
  830.  
  831.     /**
  832.      * Look up the REF index that corresponds to an external sheet name
  833.      * (or range). If it doesn't exist yet add it to the workbook's references
  834.      * array. It assumes all sheet names given must exist.
  835.      *
  836.      * @access private
  837.      * @param string $ext_ref The name of the external reference
  838.      * @return mixed The reference index in packed() format on success
  839.      */
  840.     function _getRefIndex($ext_ref)
  841.     {
  842.         $ext_ref preg_replace("/^'/"''$ext_ref)// Remove leading  ' if any.
  843.         $ext_ref preg_replace("/'$/"''$ext_ref)// Remove trailing ' if any.
  844.         $ext_ref str_replace('\'\'''\''$ext_ref)// Replace escaped '' with '
  845.  
  846.         // Check if there is a sheet range eg., Sheet1:Sheet2.
  847.         if (preg_match("/:/"$ext_ref)) {
  848.             list($sheet_name1$sheet_name2explode(':'$ext_ref);
  849.  
  850.             $sheet1 $this->_getSheetIndex($sheet_name1);
  851.             if ($sheet1 == -1{
  852.                 throw new Exception("Unknown sheet name $sheet_name1 in formula");
  853.             }
  854.             $sheet2 $this->_getSheetIndex($sheet_name2);
  855.             if ($sheet2 == -1{
  856.                 throw new Exception("Unknown sheet name $sheet_name2 in formula");
  857.             }
  858.  
  859.             // Reverse max and min sheet numbers if necessary
  860.             if ($sheet1 $sheet2{
  861.                 list($sheet1$sheet2array($sheet2$sheet1);
  862.             }
  863.         else // Single sheet name only.
  864.             $sheet1 $this->_getSheetIndex($ext_ref);
  865.             if ($sheet1 == -1{
  866.                 throw new Exception("Unknown sheet name $ext_ref in formula");
  867.             }
  868.             $sheet2 $sheet1;
  869.         }
  870.  
  871.         // assume all references belong to this document
  872.         $supbook_index 0x00;
  873.         $ref pack('vvv'$supbook_index$sheet1$sheet2);
  874.         $total_references count($this->_references);
  875.         $index = -1;
  876.         for ($i 0$i $total_references++$i{
  877.             if ($ref == $this->_references[$i]{
  878.                 $index $i;
  879.                 break;
  880.             }
  881.         }
  882.         // if REF was not found add it to references array
  883.         if ($index == -1{
  884.             $this->_references[$total_references$ref;
  885.             $index $total_references;
  886.         }
  887.  
  888.         return pack('v'$index);
  889.     }
  890.  
  891.     /**
  892.      * Look up the index that corresponds to an external sheet name. The hash of
  893.      * sheet names is updated by the addworksheet() method of the
  894.      * PHPExcel_Writer_Excel5_Workbook class.
  895.      *
  896.      * @access private
  897.      * @return integer The sheet index, -1 if the sheet was not found
  898.      */
  899.     function _getSheetIndex($sheet_name)
  900.     {
  901.         if (!isset($this->_ext_sheets[$sheet_name])) {
  902.             return -1;
  903.         else {
  904.             return $this->_ext_sheets[$sheet_name];
  905.         }
  906.     }
  907.  
  908.     /**
  909.      * This method is used to update the array of sheet names. It is
  910.      * called by the addWorksheet() method of the
  911.      * PHPExcel_Writer_Excel5_Workbook class.
  912.      *
  913.      * @access public
  914.      * @see PHPExcel_Writer_Excel5_Workbook::addWorksheet()
  915.      * @param string  $name  The name of the worksheet being added
  916.      * @param integer $index The index of the worksheet being added
  917.      */
  918.     function setExtSheet($name$index)
  919.     {
  920.         $this->_ext_sheets[$name$index;
  921.     }
  922.  
  923.     /**
  924.      * pack() row and column into the required 3 or 4 byte format.
  925.      *
  926.      * @access private
  927.      * @param string $cell The Excel cell reference to be packed
  928.      * @return array Array containing the row and column in packed() format
  929.      */
  930.     function _cellToPackedRowcol($cell)
  931.     {
  932.         $cell strtoupper($cell);
  933.         list($row$col$row_rel$col_rel$this->_cellToRowcol($cell);
  934.         if ($col >= 256{
  935.             throw new Exception("Column in: $cell greater than 255");
  936.         }
  937.         // FIXME: change for BIFF8
  938.         if ($row >= 16384{
  939.             throw new Exception("Row in: $cell greater than 16384 ");
  940.         }
  941.  
  942.         // Set the high bits to indicate if row or col are relative.
  943.         if ($this->_BIFF_version == 0x0500{
  944.             $row    |= $col_rel << 14;
  945.             $row    |= $row_rel << 15;
  946.             $col     pack('C'$col);
  947.         elseif ($this->_BIFF_version == 0x0600{
  948.             $col    |= $col_rel << 14;
  949.             $col    |= $row_rel << 15;
  950.             $col     pack('v'$col);
  951.         }
  952.         $row     pack('v'$row);
  953.  
  954.         return array($row$col);
  955.     }
  956.  
  957.     /**
  958.      * pack() row range into the required 3 or 4 byte format.
  959.      * Just using maximum col/rows, which is probably not the correct solution
  960.      *
  961.      * @access private
  962.      * @param string $range The Excel range to be packed
  963.      * @return array Array containing (row1,col1,row2,col2) in packed() format
  964.      */
  965.     function _rangeToPackedRange($range)
  966.     {
  967.         preg_match('/(\$)?(\d+)\:(\$)?(\d+)/'$range$match);
  968.         // return absolute rows if there is a $ in the ref
  969.         $row1_rel empty($match[1]0;
  970.         $row1     $match[2];
  971.         $row2_rel empty($match[3]0;
  972.         $row2     $match[4];
  973.         // Convert 1-index to zero-index
  974.         --$row1;
  975.         --$row2;
  976.         // Trick poor inocent Excel
  977.         $col1 0;
  978.         $col2 16383// FIXME: maximum possible value for Excel 5 (change this!!!)
  979.  
  980.         // FIXME: this changes for BIFF8
  981.         if (($row1 >= 16384or ($row2 >= 16384)) {
  982.             throw new Exception("Row in: $range greater than 16384 ");
  983.         }
  984.  
  985.         // Set the high bits to indicate if rows are relative.
  986.         if ($this->_BIFF_version == 0x0500{
  987.             $row1    |= $row1_rel << 14// FIXME: probably a bug
  988.             $row2    |= $row2_rel << 15;
  989.             $col1     pack('C'$col1);
  990.             $col2     pack('C'$col2);
  991.         elseif ($this->_BIFF_version == 0x0600{
  992.             $col1    |= $row1_rel << 15;
  993.             $col2    |= $row2_rel << 15;
  994.             $col1     pack('v'$col1);
  995.             $col2     pack('v'$col2);
  996.         }
  997.         $row1     pack('v'$row1);
  998.         $row2     pack('v'$row2);
  999.  
  1000.         return array($row1$col1$row2$col2);
  1001.     }
  1002.  
  1003.     /**
  1004.      * Convert an Excel cell reference such as A1 or $B2 or C$3 or $D$4 to a zero
  1005.      * indexed row and column number. Also returns two (0,1) values to indicate
  1006.      * whether the row or column are relative references.
  1007.      *
  1008.      * @access private
  1009.      * @param string $cell The Excel cell reference in A1 format.
  1010.      * @return array 
  1011.      */
  1012.     function _cellToRowcol($cell)
  1013.     {
  1014.         preg_match('/(\$)?([A-I]?[A-Z])(\$)?(\d+)/',$cell,$match);
  1015.         // return absolute column if there is a $ in the ref
  1016.         $col_rel empty($match[1]0;
  1017.         $col_ref $match[2];
  1018.         $row_rel empty($match[3]0;
  1019.         $row     $match[4];
  1020.  
  1021.         // Convert base26 column string to a number.
  1022.         $expn   strlen($col_ref1;
  1023.         $col    0;
  1024.         $col_ref_length strlen($col_ref);
  1025.         for ($i 0$i $col_ref_length++$i{
  1026.             $col += (ord($col_ref{$i}ord('A'1pow(26$expn);
  1027.             --$expn;
  1028.         }
  1029.  
  1030.         // Convert 1-index to zero-index
  1031.         --$row;
  1032.         --$col;
  1033.  
  1034.         return array($row$col$row_rel$col_rel);
  1035.     }
  1036.  
  1037.     /**
  1038.      * Advance to the next valid token.
  1039.      *
  1040.      * @access private
  1041.      */
  1042.     function _advance()
  1043.     {
  1044.         $i $this->_current_char;
  1045.         $formula_length strlen($this->_formula);
  1046.         // eat up white spaces
  1047.         if ($i $formula_length{
  1048.             while ($this->_formula{$i== " "{
  1049.                 ++$i;
  1050.             }
  1051.  
  1052.             if ($i ($formula_length 1)) {
  1053.                 $this->_lookahead = $this->_formula{$i+1};
  1054.             }
  1055.             $token '';
  1056.         }
  1057.  
  1058.         while ($i $formula_length{
  1059.             $token .= $this->_formula{$i};
  1060.             if ($i ($formula_length 1)) {
  1061.                 $this->_lookahead $this->_formula{$i+1};
  1062.             else {
  1063.                 $this->_lookahead '';
  1064.             }
  1065.  
  1066.             if ($this->_match($token!= ''{
  1067.                 //if ($i < strlen($this->_formula) - 1) {
  1068.                 //    $this->_lookahead = $this->_formula{$i+1};
  1069.                 //}
  1070.                 $this->_current_char $i 1;
  1071.                 $this->_current_token $token;
  1072.                 return 1;
  1073.             }
  1074.  
  1075.             if ($i ($formula_length 2)) {
  1076.                 $this->_lookahead $this->_formula{$i+2};
  1077.             else // if we run out of characters _lookahead becomes empty
  1078.                 $this->_lookahead '';
  1079.             }
  1080.             ++$i;
  1081.         }
  1082.         //die("Lexical error ".$this->_current_char);
  1083.     }
  1084.  
  1085.     /**
  1086.      * Checks if it's a valid token.
  1087.      *
  1088.      * @access private
  1089.      * @param mixed $token The token to check.
  1090.      * @return mixed       The checked token or false on failure
  1091.      */
  1092.     function _match($token)
  1093.     {
  1094.         switch($token{
  1095.             case "+":
  1096.                 return $token;
  1097.                 break;
  1098.             case "-":
  1099.                 return $token;
  1100.                 break;
  1101.             case "*":
  1102.                 return $token;
  1103.                 break;
  1104.             case "/":
  1105.                 return $token;
  1106.                 break;
  1107.             case "(":
  1108.                 return $token;
  1109.                 break;
  1110.             case ")":
  1111.                 return $token;
  1112.                 break;
  1113.             case ",":
  1114.                 return $token;
  1115.                 break;
  1116.             case ";":
  1117.                 return $token;
  1118.                 break;
  1119.             case ">":
  1120.                 if ($this->_lookahead == '='// it's a GE token
  1121.                     break;
  1122.                 }
  1123.                 return $token;
  1124.                 break;
  1125.             case "<":
  1126.                 // it's a LE or a NE token
  1127.                 if (($this->_lookahead == '='or ($this->_lookahead == '>')) {
  1128.                     break;
  1129.                 }
  1130.                 return $token;
  1131.                 break;
  1132.             case ">=":
  1133.                 return $token;
  1134.                 break;
  1135.             case "<=":
  1136.                 return $token;
  1137.                 break;
  1138.             case "=":
  1139.                 return $token;
  1140.                 break;
  1141.             case "<>":
  1142.                 return $token;
  1143.                 break;
  1144.             default:
  1145.                 // if it's a reference A1 or $A$1 or $A1 or A$1
  1146.                 if (preg_match('/^\$?[A-Ia-i]?[A-Za-z]\$?[0-9]+$/',$tokenand
  1147.                    !preg_match("/[0-9]/",$this->_lookaheadand
  1148.                    ($this->_lookahead != ':'and ($this->_lookahead != '.'and
  1149.                    ($this->_lookahead != '!'))
  1150.                 {
  1151.                     return $token;
  1152.                 }
  1153.                 // If it's an external reference (Sheet1!A1 or Sheet1:Sheet2!A1 or Sheet1!$A$1 or Sheet1:Sheet2!$A$1)
  1154.                 elseif (preg_match("/^" self::REGEX_SHEET_TITLE_UNQUOTED "(\:" self::REGEX_SHEET_TITLE_UNQUOTED ")?\!\\$?[A-Ia-i]?[A-Za-z]\\$?[0-9]+$/u",$tokenand
  1155.                        !preg_match("/[0-9]/",$this->_lookaheadand
  1156.                        ($this->_lookahead != ':'and ($this->_lookahead != '.'))
  1157.                 {
  1158.                     return $token;
  1159.                 }
  1160.                 // If it's an external reference ('Sheet1'!A1 or 'Sheet1:Sheet2'!A1 or 'Sheet1'!$A$1 or 'Sheet1:Sheet2'!$A$1)
  1161.                 elseif (preg_match("/^'" self::REGEX_SHEET_TITLE_QUOTED "(\:" self::REGEX_SHEET_TITLE_QUOTED ")?'\!\\$?[A-Ia-i]?[A-Za-z]\\$?[0-9]+$/u",$tokenand
  1162.                        !preg_match("/[0-9]/",$this->_lookaheadand
  1163.                        ($this->_lookahead != ':'and ($this->_lookahead != '.'))
  1164.                 {
  1165.                     return $token;
  1166.                 }
  1167.                 // if it's a range A1:A2 or $A$1:$A$2
  1168.                 elseif (preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+:(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/'$tokenand
  1169.                        !preg_match("/[0-9]/",$this->_lookahead))
  1170.                 {
  1171.                     return $token;
  1172.                 }
  1173.                 // If it's an external range like Sheet1!A1:B2 or Sheet1:Sheet2!A1:B2 or Sheet1!$A$1:$B$2 or Sheet1:Sheet2!$A$1:$B$2
  1174.                 elseif (preg_match("/^" self::REGEX_SHEET_TITLE_UNQUOTED "(\:" self::REGEX_SHEET_TITLE_UNQUOTED ")?\!\\$?([A-Ia-i]?[A-Za-z])?\\$?[0-9]+:\\$?([A-Ia-i]?[A-Za-z])?\\$?[0-9]+$/u",$tokenand
  1175.                        !preg_match("/[0-9]/",$this->_lookahead))
  1176.                 {
  1177.                     return $token;
  1178.                 }
  1179.                 // If it's an external range like 'Sheet1'!A1:B2 or 'Sheet1:Sheet2'!A1:B2 or 'Sheet1'!$A$1:$B$2 or 'Sheet1:Sheet2'!$A$1:$B$2
  1180.                 elseif (preg_match("/^'" self::REGEX_SHEET_TITLE_QUOTED "(\:" self::REGEX_SHEET_TITLE_QUOTED ")?'\!\\$?([A-Ia-i]?[A-Za-z])?\\$?[0-9]+:\\$?([A-Ia-i]?[A-Za-z])?\\$?[0-9]+$/u",$tokenand
  1181.                        !preg_match("/[0-9]/",$this->_lookahead))
  1182.                 {
  1183.                     return $token;
  1184.                 }
  1185.                 // If it's a number (check that it's not a sheet name or range)
  1186.                 elseif (is_numeric($tokenand
  1187.                         (!is_numeric($token.$this->_lookaheador ($this->_lookahead == '')) and
  1188.                         ($this->_lookahead != '!'and ($this->_lookahead != ':'))
  1189.                 {
  1190.                     return $token;
  1191.                 }
  1192.                 // If it's a string (of maximum 255 characters)
  1193.                 elseif (preg_match("/^\"[^\"]{0,255}\"$/",$token))
  1194.                 {
  1195.                     return $token;
  1196.                 }
  1197.                 // if it's a function call
  1198.                 elseif (preg_match("/^[A-Z0-9\xc0-\xdc\.]+$/i",$tokenand ($this->_lookahead == "("))
  1199.                 {
  1200.                     return $token;
  1201.                 }
  1202.                 return '';
  1203.         }
  1204.     }
  1205.  
  1206.     /**
  1207.      * The parsing method. It parses a formula.
  1208.      *
  1209.      * @access public
  1210.      * @param string $formula The formula to parse, without the initial equal
  1211.      *                         sign (=).
  1212.      * @return mixed true on success
  1213.      */
  1214.     function parse($formula)
  1215.     {
  1216.         $this->_current_char 0;
  1217.         $this->_formula      $formula;
  1218.         $this->_lookahead    = isset($formula{1}$formula{1'';
  1219.         $this->_advance();
  1220.         $this->_parse_tree   $this->_condition();
  1221.         return true;
  1222.     }
  1223.  
  1224.     /**
  1225.      * It parses a condition. It assumes the following rule:
  1226.      * Cond -> Expr [(">" | "<") Expr]
  1227.      *
  1228.      * @access private
  1229.      * @return mixed The parsed ptg'd tree on success
  1230.      */
  1231.     function _condition()
  1232.     {
  1233.         $result $this->_expression();
  1234.         if ($this->_current_token == "<"{
  1235.             $this->_advance();
  1236.             $result2 $this->_expression();
  1237.             $result $this->_createTree('ptgLT'$result$result2);
  1238.         elseif ($this->_current_token == ">"{
  1239.             $this->_advance();
  1240.             $result2 $this->_expression();
  1241.             $result $this->_createTree('ptgGT'$result$result2);
  1242.         elseif ($this->_current_token == "<="{
  1243.             $this->_advance();
  1244.             $result2 $this->_expression();
  1245.             $result $this->_createTree('ptgLE'$result$result2);
  1246.         elseif ($this->_current_token == ">="{
  1247.             $this->_advance();
  1248.             $result2 $this->_expression();
  1249.             $result $this->_createTree('ptgGE'$result$result2);
  1250.         elseif ($this->_current_token == "="{
  1251.             $this->_advance();
  1252.             $result2 $this->_expression();
  1253.             $result $this->_createTree('ptgEQ'$result$result2);
  1254.         elseif ($this->_current_token == "<>"{
  1255.             $this->_advance();
  1256.             $result2 $this->_expression();
  1257.             $result $this->_createTree('ptgNE'$result$result2);
  1258.         }
  1259.         return $result;
  1260.     }
  1261.  
  1262.     /**
  1263.      * It parses a expression. It assumes the following rule:
  1264.      * Expr -> Term [("+" | "-") Term]
  1265.      *      -> "string"
  1266.      *      -> "-" Term
  1267.      *
  1268.      * @access private
  1269.      * @return mixed The parsed ptg'd tree on success
  1270.      */
  1271.     function _expression()
  1272.     {
  1273.         // If it's a string return a string node
  1274.         if (preg_match("/^\"[^\"]{0,255}\"$/"$this->_current_token)) {
  1275.             $result $this->_createTree($this->_current_token'''');
  1276.             $this->_advance();
  1277.             return $result;
  1278.         elseif ($this->_current_token == "-"{
  1279.             // catch "-" Term
  1280.             $this->_advance();
  1281.             $result2 $this->_expression();
  1282.             $result $this->_createTree('ptgUminus'$result2'');
  1283.             return $result;
  1284.         }
  1285.         $result $this->_term();
  1286.         while (($this->_current_token == "+"or
  1287.                ($this->_current_token == "-")) {
  1288.         /**/
  1289.             if ($this->_current_token == "+"{
  1290.                 $this->_advance();
  1291.                 $result2 $this->_term();
  1292.                 $result $this->_createTree('ptgAdd'$result$result2);
  1293.             else {
  1294.                 $this->_advance();
  1295.                 $result2 $this->_term();
  1296.                 $result $this->_createTree('ptgSub'$result$result2);
  1297.             }
  1298.         }
  1299.         return $result;
  1300.     }
  1301.  
  1302.     /**
  1303.      * This function just introduces a ptgParen element in the tree, so that Excel
  1304.      * doesn't get confused when working with a parenthesized formula afterwards.
  1305.      *
  1306.      * @access private
  1307.      * @see _fact()
  1308.      * @return array The parsed ptg'd tree
  1309.      */
  1310.     function _parenthesizedExpression()
  1311.     {
  1312.         $result $this->_createTree('ptgParen'$this->_expression()'');
  1313.         return $result;
  1314.     }
  1315.  
  1316.     /**
  1317.      * It parses a term. It assumes the following rule:
  1318.      * Term -> Fact [("*" | "/") Fact]
  1319.      *
  1320.      * @access private
  1321.      * @return mixed The parsed ptg'd tree on success
  1322.      */
  1323.     function _term()
  1324.     {
  1325.         $result $this->_fact();
  1326.         while (($this->_current_token == "*"or
  1327.                ($this->_current_token == "/")) {
  1328.         /**/
  1329.             if ($this->_current_token == "*"{
  1330.                 $this->_advance();
  1331.                 $result2 $this->_fact();
  1332.                 $result $this->_createTree('ptgMul'$result$result2);
  1333.             else {
  1334.                 $this->_advance();
  1335.                 $result2 $this->_fact();
  1336.                 $result $this->_createTree('ptgDiv'$result$result2);
  1337.             }
  1338.         }
  1339.         return $result;
  1340.     }
  1341.  
  1342.     /**
  1343.      * It parses a factor. It assumes the following rule:
  1344.      * Fact -> ( Expr )
  1345.      *       | CellRef
  1346.      *       | CellRange
  1347.      *       | Number
  1348.      *       | Function
  1349.      *
  1350.      * @access private
  1351.      * @return mixed The parsed ptg'd tree on success
  1352.      */
  1353.     function _fact()
  1354.     {
  1355.         if ($this->_current_token == "("{
  1356.             $this->_advance();         // eat the "("
  1357.             $result $this->_parenthesizedExpression();
  1358.             if ($this->_current_token != ")"{
  1359.                 throw new Exception("')' token expected.");
  1360.             }
  1361.             $this->_advance();         // eat the ")"
  1362.             return $result;
  1363.         }
  1364.         // if it's a reference
  1365.         if (preg_match('/^\$?[A-Ia-i]?[A-Za-z]\$?[0-9]+$/',$this->_current_token))
  1366.         {
  1367.             $result $this->_createTree($this->_current_token'''');
  1368.             $this->_advance();
  1369.             return $result;
  1370.         }
  1371.         // If it's an external reference (Sheet1!A1 or Sheet1:Sheet2!A1 or Sheet1!$A$1 or Sheet1:Sheet2!$A$1)
  1372.         elseif (preg_match("/^" self::REGEX_SHEET_TITLE_UNQUOTED "(\:" self::REGEX_SHEET_TITLE_UNQUOTED ")?\!\\$?[A-Ia-i]?[A-Za-z]\\$?[0-9]+$/u",$this->_current_token))
  1373.         {
  1374.             $result $this->_createTree($this->_current_token'''');
  1375.             $this->_advance();
  1376.             return $result;
  1377.         }
  1378.         // If it's an external reference ('Sheet1'!A1 or 'Sheet1:Sheet2'!A1 or 'Sheet1'!$A$1 or 'Sheet1:Sheet2'!$A$1)
  1379.         elseif (preg_match("/^'" self::REGEX_SHEET_TITLE_QUOTED "(\:" self::REGEX_SHEET_TITLE_QUOTED ")?'\!\\$?[A-Ia-i]?[A-Za-z]\\$?[0-9]+$/u",$this->_current_token))
  1380.         {
  1381.             $result $this->_createTree($this->_current_token'''');
  1382.             $this->_advance();
  1383.             return $result;
  1384.         }
  1385.         // if it's a range A1:B2 or $A$1:$B$2
  1386.         elseif (preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+:(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/',$this->_current_tokenor
  1387.                 preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+\.\.(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/',$this->_current_token))
  1388.         {
  1389.             // must be an error?
  1390.             $result $this->_createTree($this->_current_token'''');
  1391.             $this->_advance();
  1392.             return $result;
  1393.         }
  1394.         // If it's an external range (Sheet1!A1:B2 or Sheet1:Sheet2!A1:B2 or Sheet1!$A$1:$B$2 or Sheet1:Sheet2!$A$1:$B$2)
  1395.         elseif (preg_match("/^" self::REGEX_SHEET_TITLE_UNQUOTED "(\:" self::REGEX_SHEET_TITLE_UNQUOTED ")?\!\\$?([A-Ia-i]?[A-Za-z])?\\$?[0-9]+:\\$?([A-Ia-i]?[A-Za-z])?\\$?[0-9]+$/u",$this->_current_token))
  1396.         {
  1397.             // must be an error?
  1398.             //$result = $this->_current_token;
  1399.             $result $this->_createTree($this->_current_token'''');
  1400.             $this->_advance();
  1401.             return $result;
  1402.         }
  1403.         // If it's an external range ('Sheet1'!A1:B2 or 'Sheet1'!A1:B2 or 'Sheet1'!$A$1:$B$2 or 'Sheet1'!$A$1:$B$2)
  1404.         elseif (preg_match("/^'" self::REGEX_SHEET_TITLE_QUOTED "(\:" self::REGEX_SHEET_TITLE_QUOTED ")?'\!\\$?([A-Ia-i]?[A-Za-z])?\\$?[0-9]+:\\$?([A-Ia-i]?[A-Za-z])?\\$?[0-9]+$/u",$this->_current_token))
  1405.         {
  1406.             // must be an error?
  1407.             //$result = $this->_current_token;
  1408.             $result $this->_createTree($this->_current_token'''');
  1409.             $this->_advance();
  1410.             return $result;
  1411.         }
  1412.         elseif (is_numeric($this->_current_token))
  1413.         {
  1414.             $result $this->_createTree($this->_current_token'''');
  1415.             $this->_advance();
  1416.             return $result;
  1417.         }
  1418.         // if it's a function call
  1419.         elseif (preg_match("/^[A-Z0-9\xc0-\xdc\.]+$/i",$this->_current_token))
  1420.         {
  1421.             $result $this->_func();
  1422.             return $result;
  1423.         }
  1424.         throw new Exception("Syntax error: ".$this->_current_token.
  1425.                                  ", lookahead: ".$this->_lookahead.
  1426.                                  ", current char: ".$this->_current_char);
  1427.     }
  1428.  
  1429.     /**
  1430.      * It parses a function call. It assumes the following rule:
  1431.      * Func -> ( Expr [,Expr]* )
  1432.      *
  1433.      * @access private
  1434.      * @return mixed The parsed ptg'd tree on success
  1435.      */
  1436.     function _func()
  1437.     {
  1438.         $num_args 0// number of arguments received
  1439.         $function strtoupper($this->_current_token);
  1440.         $result   ''// initialize result
  1441.         $this->_advance();
  1442.         $this->_advance();         // eat the "("
  1443.         while ($this->_current_token != ')'{
  1444.         /**/
  1445.             if ($num_args 0{
  1446.                 if ($this->_current_token == "," or
  1447.                     $this->_current_token == ";")
  1448.                 {
  1449.                     $this->_advance();  // eat the "," or ";"
  1450.                 else {
  1451.                     throw new Exception("Syntax error: comma expected in ".
  1452.                                       "function $function, arg #{$num_args}");
  1453.                 }
  1454.                 $result2 $this->_condition();
  1455.                 $result $this->_createTree('arg'$result$result2);
  1456.             else // first argument
  1457.                 $result2 $this->_condition();
  1458.                 $result $this->_createTree('arg'''$result2);
  1459.             }
  1460.             ++$num_args;
  1461.         }
  1462.         if (!isset($this->_functions[$function])) {
  1463.             throw new Exception("Function $function() doesn't exist");
  1464.         }
  1465.         $args $this->_functions[$function][1];
  1466.         // If fixed number of args eg. TIME($i,$j,$k). Check that the number of args is valid.
  1467.         if (($args >= 0and ($args != $num_args)) {
  1468.             throw new Exception("Incorrect number of arguments in function $function() ");
  1469.         }
  1470.  
  1471.         $result $this->_createTree($function$result$num_args);
  1472.         $this->_advance();         // eat the ")"
  1473.         return $result;
  1474.     }
  1475.  
  1476.     /**
  1477.      * Creates a tree. In fact an array which may have one or two arrays (sub-trees)
  1478.      * as elements.
  1479.      *
  1480.      * @access private
  1481.      * @param mixed $value The value of this node.
  1482.      * @param mixed $left  The left array (sub-tree) or a final node.
  1483.      * @param mixed $right The right array (sub-tree) or a final node.
  1484.      * @return array A tree
  1485.      */
  1486.     function _createTree($value$left$right)
  1487.     {
  1488.         return array('value' => $value'left' => $left'right' => $right);
  1489.     }
  1490.  
  1491.     /**
  1492.      * Builds a string containing the tree in reverse polish notation (What you
  1493.      * would use in a HP calculator stack).
  1494.      * The following tree:
  1495.      *
  1496.      *    +
  1497.      *   / \
  1498.      *  2   3
  1499.      *
  1500.      * produces: "23+"
  1501.      *
  1502.      * The following tree:
  1503.      *
  1504.      *    +
  1505.      *   / \
  1506.      *  3   *
  1507.      *     / \
  1508.      *    6   A1
  1509.      *
  1510.      * produces: "36A1*+"
  1511.      *
  1512.      * In fact all operands, functions, references, etc... are written as ptg's
  1513.      *
  1514.      * @access public
  1515.      * @param array $tree The optional tree to convert.
  1516.      * @return string The tree in reverse polish notation
  1517.      */
  1518.     function toReversePolish($tree array())
  1519.     {
  1520.         $polish ""// the string we are going to return
  1521.         if (empty($tree)) // If it's the first call use _parse_tree
  1522.             $tree $this->_parse_tree;
  1523.         }
  1524.         if (is_array($tree['left'])) {
  1525.             $converted_tree $this->toReversePolish($tree['left']);
  1526.             $polish .= $converted_tree;
  1527.         elseif ($tree['left'!= ''// It's a final node
  1528.             $converted_tree $this->_convert($tree['left']);
  1529.             $polish .= $converted_tree;
  1530.         }
  1531.         if (is_array($tree['right'])) {
  1532.             $converted_tree $this->toReversePolish($tree['right']);
  1533.             $polish .= $converted_tree;
  1534.         elseif ($tree['right'!= ''// It's a final node
  1535.             $converted_tree $this->_convert($tree['right']);
  1536.             $polish .= $converted_tree;
  1537.         }
  1538.         // if it's a function convert it here (so we can set it's arguments)
  1539.         if (preg_match("/^[A-Z0-9\xc0-\xdc\.]+$/",$tree['value']and
  1540.             !preg_match('/^([A-Ia-i]?[A-Za-z])(\d+)$/',$tree['value']and
  1541.             !preg_match("/^[A-Ia-i]?[A-Za-z](\d+)\.\.[A-Ia-i]?[A-Za-z](\d+)$/",$tree['value']and
  1542.             !is_numeric($tree['value']and
  1543.             !isset($this->ptg[$tree['value']]))
  1544.         {
  1545.             // left subtree for a function is always an array.
  1546.             if ($tree['left'!= ''{
  1547.                 $left_tree $this->toReversePolish($tree['left']);
  1548.             else {
  1549.                 $left_tree '';
  1550.             }
  1551.             // add it's left subtree and return.
  1552.             return $left_tree.$this->_convertFunction($tree['value']$tree['right']);
  1553.         else {
  1554.             $converted_tree $this->_convert($tree['value']);
  1555.         }
  1556.         $polish .= $converted_tree;
  1557.         return $polish;
  1558.     }
  1559.  
  1560. }

Documentation generated on Tue, 01 Jun 2010 17:05:36 +0200 by phpDocumentor 1.4.3