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

Source for file Parser.php

Documentation is available at Parser.php

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

Documentation generated on Thu, 12 Jun 2008 14:10:23 -0500 by phpDocumentor 1.4.1