/var/www/hkosl.com/imusiccircle/webadmin/libraies/phpoffice/phpexcel/Classes/PHPExcel/Calculation/FormulaParser.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
<?php
/**
 * PHPExcel
 *
 * Copyright (c) 2006 - 2014 PHPExcel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category   PHPExcel
 * @package    PHPExcel_Calculation
 * @copyright  Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
 * @version    ##VERSION##, ##DATE##
 */


/*
PARTLY BASED ON:
    Copyright (c) 2007 E. W. Bachtal, Inc.

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software
    and associated documentation files (the "Software"), to deal in the Software without restriction,
    including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
    subject to the following conditions:

      The above copyright notice and this permission notice shall be included in all copies or substantial
      portions of the Software.

    The software is provided "as is", without warranty of any kind, express or implied, including but not
    limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In
    no event shall the authors or copyright holders be liable for any claim, damages or other liability,
    whether in an action of contract, tort or otherwise, arising from, out of or in connection with the
    software or the use or other dealings in the software.

    http://ewbi.blogs.com/develops/2007/03/excel_formula_p.html
    http://ewbi.blogs.com/develops/2004/12/excel_formula_p.html
*/

/**
 * PHPExcel_Calculation_FormulaParser
 *
 * @category   PHPExcel
 * @package    PHPExcel_Calculation
 * @copyright  Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
 */
class PHPExcel_Calculation_FormulaParser {
    
/* Character constants */
    
const QUOTE_DOUBLE  '"';
    const 
QUOTE_SINGLE  '\'';
    const 
BRACKET_CLOSE ']';
    const 
BRACKET_OPEN  '[';
    const 
BRACE_OPEN    '{';
    const 
BRACE_CLOSE   '}';
    const 
PAREN_OPEN    '(';
    const 
PAREN_CLOSE   ')';
    const 
SEMICOLON     ';';
    const 
WHITESPACE    ' ';
    const 
COMMA         ',';
    const 
ERROR_START   '#';

    const 
OPERATORS_SN             "+-";
    const 
OPERATORS_INFIX         "+-*/^&=><";
    const 
OPERATORS_POSTFIX     "%";

    
/**
     * Formula
     *
     * @var string
     */
    
private $_formula;

    
/**
     * Tokens
     *
     * @var PHPExcel_Calculation_FormulaToken[]
     */
    
private $_tokens = array();

    
/**
     * Create a new PHPExcel_Calculation_FormulaParser
     *
     * @param     string        $pFormula    Formula to parse
     * @throws     PHPExcel_Calculation_Exception
     */
    
public function __construct($pFormula '')
    {
        
// Check parameters
        
if (is_null($pFormula)) {
            throw new 
PHPExcel_Calculation_Exception("Invalid parameter passed: formula");
        }

        
// Initialise values
        
$this->_formula trim($pFormula);
        
// Parse!
        
$this->_parseToTokens();
    }

    
/**
     * Get Formula
     *
     * @return string
     */
    
public function getFormula() {
        return 
$this->_formula;
    }

    
/**
     * Get Token
     *
     * @param     int        $pId    Token id
     * @return    string
     * @throws  PHPExcel_Calculation_Exception
     */
    
public function getToken($pId 0) {
        if (isset(
$this->_tokens[$pId])) {
            return 
$this->_tokens[$pId];
        } else {
            throw new 
PHPExcel_Calculation_Exception("Token with id $pId does not exist.");
        }
    }

    
/**
     * Get Token count
     *
     * @return string
     */
    
public function getTokenCount() {
        return 
count($this->_tokens);
    }

    
/**
     * Get Tokens
     *
     * @return PHPExcel_Calculation_FormulaToken[]
     */
    
public function getTokens() {
        return 
$this->_tokens;
    }

    
/**
     * Parse to tokens
     */
    
private function _parseToTokens() {
        
// No attempt is made to verify formulas; assumes formulas are derived from Excel, where
        // they can only exist if valid; stack overflows/underflows sunk as nulls without exceptions.

        // Check if the formula has a valid starting =
        
$formulaLength strlen($this->_formula);
        if (
$formulaLength || $this->_formula{0} != '=') return;

        
// Helper variables
        
$tokens1    $tokens2     $stack = array();
        
$inString    $inPath     $inRange     $inError false;
        
$token        $previousToken    $nextToken    null;

        
$index    1;
        
$value    '';

        
$ERRORS             = array("#NULL!""#DIV/0!""#VALUE!""#REF!""#NAME?""#NUM!""#N/A");
        
$COMPARATORS_MULTI     = array(">=""<=""<>");

        while (
$index $formulaLength) {
            
// state-dependent character evaluation (order is important)

            // double-quoted strings
            // embeds are doubled
            // end marks token
            
if ($inString) {
                if (
$this->_formula{$index} == PHPExcel_Calculation_FormulaParser::QUOTE_DOUBLE) {
                    if (((
$index 2) <= $formulaLength) && ($this->_formula{$index 1} == PHPExcel_Calculation_FormulaParser::QUOTE_DOUBLE)) {
                        
$value .= PHPExcel_Calculation_FormulaParser::QUOTE_DOUBLE;
                        ++
$index;
                    } else {
                        
$inString false;
                        
$tokens1[] = new PHPExcel_Calculation_FormulaToken($valuePHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERANDPHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_TEXT);
                        
$value "";
                    }
                } else {
                    
$value .= $this->_formula{$index};
                }
                ++
$index;
                continue;
            }

            
// single-quoted strings (links)
            // embeds are double
            // end does not mark a token
            
if ($inPath) {
                if (
$this->_formula{$index} == PHPExcel_Calculation_FormulaParser::QUOTE_SINGLE) {
                    if (((
$index 2) <= $formulaLength) && ($this->_formula{$index 1} == PHPExcel_Calculation_FormulaParser::QUOTE_SINGLE)) {
                        
$value .= PHPExcel_Calculation_FormulaParser::QUOTE_SINGLE;
                        ++
$index;
                    } else {
                        
$inPath false;
                    }
                } else {
                    
$value .= $this->_formula{$index};
                }
                ++
$index;
                continue;
            }

            
// bracked strings (R1C1 range index or linked workbook name)
            // no embeds (changed to "()" by Excel)
            // end does not mark a token
            
if ($inRange) {
                if (
$this->_formula{$index} == PHPExcel_Calculation_FormulaParser::BRACKET_CLOSE) {
                    
$inRange false;
                }
                
$value .= $this->_formula{$index};
                ++
$index;
                continue;
            }

            
// error values
            // end marks a token, determined from absolute list of values
            
if ($inError) {
                
$value .= $this->_formula{$index};
                ++
$index;
                if (
in_array($value$ERRORS)) {
                    
$inError false;
                    
$tokens1[] = new PHPExcel_Calculation_FormulaToken($valuePHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERANDPHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_ERROR);
                    
$value "";
                }
                continue;
            }

            
// scientific notation check
            
if (strpos(PHPExcel_Calculation_FormulaParser::OPERATORS_SN$this->_formula{$index}) !== false) {
                if (
strlen($value) > 1) {
                    if (
preg_match("/^[1-9]{1}(\.[0-9]+)?E{1}$/"$this->_formula{$index}) != 0) {
                        
$value .= $this->_formula{$index};
                        ++
$index;
                        continue;
                    }
                }
            }

            
// independent character evaluation (order not important)

            // establish state-dependent character evaluations
            
if ($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::QUOTE_DOUBLE) {
                if (
strlen($value 0)) {  // unexpected
                    
$tokens1[] = new PHPExcel_Calculation_FormulaToken($valuePHPExcel_Calculation_FormulaToken::TOKEN_TYPE_UNKNOWN);
                    
$value "";
                }
                
$inString true;
                ++
$index;
                continue;
             }

            if (
$this->_formula{$index} == PHPExcel_Calculation_FormulaParser::QUOTE_SINGLE) {
                if (
strlen($value) > 0) { // unexpected
                    
$tokens1[] = new PHPExcel_Calculation_FormulaToken($valuePHPExcel_Calculation_FormulaToken::TOKEN_TYPE_UNKNOWN);
                    
$value "";
                }
                
$inPath true;
                ++
$index;
                continue;
            }

            if (
$this->_formula{$index} == PHPExcel_Calculation_FormulaParser::BRACKET_OPEN) {
                
$inRange true;
                
$value .= PHPExcel_Calculation_FormulaParser::BRACKET_OPEN;
                ++
$index;
                continue;
            }

            if (
$this->_formula{$index} == PHPExcel_Calculation_FormulaParser::ERROR_START) {
                if (
strlen($value) > 0) { // unexpected
                    
$tokens1[] = new PHPExcel_Calculation_FormulaToken($valuePHPExcel_Calculation_FormulaToken::TOKEN_TYPE_UNKNOWN);
                    
$value "";
                }
                
$inError true;
                
$value .= PHPExcel_Calculation_FormulaParser::ERROR_START;
                ++
$index;
                continue;
            }

            
// mark start and end of arrays and array rows
            
if ($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::BRACE_OPEN) {
                if (
strlen($value) > 0) { // unexpected
                    
$tokens1[] = new PHPExcel_Calculation_FormulaToken($valuePHPExcel_Calculation_FormulaToken::TOKEN_TYPE_UNKNOWN);
                    
$value "";
                }

                
$tmp = new PHPExcel_Calculation_FormulaToken("ARRAY"PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_FUNCTIONPHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_START);
                
$tokens1[] = $tmp;
                
$stack[] = clone $tmp;

                
$tmp = new PHPExcel_Calculation_FormulaToken("ARRAYROW"PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_FUNCTIONPHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_START);
                
$tokens1[] = $tmp;
                
$stack[] = clone $tmp;

                ++
$index;
                continue;
            }

            if (
$this->_formula{$index} == PHPExcel_Calculation_FormulaParser::SEMICOLON) {
                if (
strlen($value) > 0) {
                    
$tokens1[] = new PHPExcel_Calculation_FormulaToken($valuePHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND);
                    
$value "";
                }

                
$tmp array_pop($stack);
                
$tmp->setValue("");
                
$tmp->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP);
                
$tokens1[] = $tmp;

                
$tmp = new PHPExcel_Calculation_FormulaToken(","PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_ARGUMENT);
                
$tokens1[] = $tmp;

                
$tmp = new PHPExcel_Calculation_FormulaToken("ARRAYROW"PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_FUNCTIONPHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_START);
                
$tokens1[] = $tmp;
                
$stack[] = clone $tmp;

                ++
$index;
                continue;
            }

            if (
$this->_formula{$index} == PHPExcel_Calculation_FormulaParser::BRACE_CLOSE) {
                if (
strlen($value) > 0) {
                    
$tokens1[] = new PHPExcel_Calculation_FormulaToken($valuePHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND);
                    
$value "";
                }

                
$tmp array_pop($stack);
                
$tmp->setValue("");
                
$tmp->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP);
                
$tokens1[] = $tmp;

                
$tmp array_pop($stack);
                
$tmp->setValue("");
                
$tmp->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP);
                
$tokens1[] = $tmp;

                ++
$index;
                continue;
            }

            
// trim white-space
            
if ($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::WHITESPACE) {
                if (
strlen($value) > 0) {
                    
$tokens1[] = new PHPExcel_Calculation_FormulaToken($valuePHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND);
                    
$value "";
                }
                
$tokens1[] = new PHPExcel_Calculation_FormulaToken(""PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_WHITESPACE);
                ++
$index;
                while ((
$this->_formula{$index} == PHPExcel_Calculation_FormulaParser::WHITESPACE) && ($index $formulaLength)) {
                    ++
$index;
                }
                continue;
            }

            
// multi-character comparators
            
if (($index 2) <= $formulaLength) {
                if (
in_array(substr($this->_formula$index2), $COMPARATORS_MULTI)) {
                    if (
strlen($value) > 0) {
                        
$tokens1[] = new PHPExcel_Calculation_FormulaToken($valuePHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND);
                        
$value "";
                    }
                    
$tokens1[] = new PHPExcel_Calculation_FormulaToken(substr($this->_formula$index2), PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORINFIXPHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_LOGICAL);
                    
$index += 2;
                    continue;
                }
            }

            
// standard infix operators
            
if (strpos(PHPExcel_Calculation_FormulaParser::OPERATORS_INFIX$this->_formula{$index}) !== false) {
                if (
strlen($value) > 0) {
                    
$tokens1[] =new PHPExcel_Calculation_FormulaToken($valuePHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND);
                    
$value "";
                }
                
$tokens1[] = new PHPExcel_Calculation_FormulaToken($this->_formula{$index}, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORINFIX);
                ++
$index;
                continue;
            }

            
// standard postfix operators (only one)
            
if (strpos(PHPExcel_Calculation_FormulaParser::OPERATORS_POSTFIX$this->_formula{$index}) !== false) {
                if (
strlen($value) > 0) {
                    
$tokens1[] = new PHPExcel_Calculation_FormulaToken($valuePHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND);
                    
$value "";
                }
                
$tokens1[] = new PHPExcel_Calculation_FormulaToken($this->_formula{$index}, PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORPOSTFIX);
                ++
$index;
                continue;
            }

            
// start subexpression or function
            
if ($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::PAREN_OPEN) {
                if (
strlen($value) > 0) {
                    
$tmp = new PHPExcel_Calculation_FormulaToken($valuePHPExcel_Calculation_FormulaToken::TOKEN_TYPE_FUNCTIONPHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_START);
                    
$tokens1[] = $tmp;
                    
$stack[] = clone $tmp;
                    
$value "";
                } else {
                    
$tmp = new PHPExcel_Calculation_FormulaToken(""PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_SUBEXPRESSIONPHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_START);
                    
$tokens1[] = $tmp;
                    
$stack[] = clone $tmp;
                }
                ++
$index;
                continue;
            }

            
// function, subexpression, or array parameters, or operand unions
            
if ($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::COMMA) {
                if (
strlen($value) > 0) {
                    
$tokens1[] = new PHPExcel_Calculation_FormulaToken($valuePHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND);
                    
$value "";
                }

                
$tmp array_pop($stack);
                
$tmp->setValue("");
                
$tmp->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP);
                
$stack[] = $tmp;

                if (
$tmp->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_FUNCTION) {
                    
$tokens1[] = new PHPExcel_Calculation_FormulaToken(","PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORINFIXPHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_UNION);
                } else {
                    
$tokens1[] = new PHPExcel_Calculation_FormulaToken(","PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_ARGUMENT);
                }
                ++
$index;
                continue;
            }

            
// stop subexpression
            
if ($this->_formula{$index} == PHPExcel_Calculation_FormulaParser::PAREN_CLOSE) {
                if (
strlen($value) > 0) {
                    
$tokens1[] = new PHPExcel_Calculation_FormulaToken($valuePHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND);
                    
$value "";
                }

                
$tmp array_pop($stack);
                
$tmp->setValue("");
                
$tmp->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP);
                
$tokens1[] = $tmp;

                ++
$index;
                continue;
            }

            
// token accumulation
            
$value .= $this->_formula{$index};
            ++
$index;
        }

        
// dump remaining accumulation
        
if (strlen($value) > 0) {
            
$tokens1[] = new PHPExcel_Calculation_FormulaToken($valuePHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND);
        }

        
// move tokenList to new set, excluding unnecessary white-space tokens and converting necessary ones to intersections
        
$tokenCount count($tokens1);
        for (
$i 0$i $tokenCount; ++$i) {
            
$token $tokens1[$i];
            if (isset(
$tokens1[$i 1])) {
                
$previousToken $tokens1[$i 1];
            } else {
                
$previousToken null;
            }
            if (isset(
$tokens1[$i 1])) {
                
$nextToken $tokens1[$i 1];
            } else {
                
$nextToken null;
            }

            if (
is_null($token)) {
                continue;
            }

            if (
$token->getTokenType() != PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_WHITESPACE) {
                
$tokens2[] = $token;
                continue;
            }

            if (
is_null($previousToken)) {
                continue;
            }

            if (! (
                    ((
$previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_FUNCTION) && ($previousToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP)) ||
                    ((
$previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_SUBEXPRESSION) && ($previousToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP)) ||
                    (
$previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND)
                  ) ) {
                continue;
            }

            if (
is_null($nextToken)) {
                continue;
            }

            if (! (
                    ((
$nextToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_FUNCTION) && ($nextToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_START)) ||
                    ((
$nextToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_SUBEXPRESSION) && ($nextToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_START)) ||
                    (
$nextToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND)
                  ) ) {
                continue;
            }

            
$tokens2[] = new PHPExcel_Calculation_FormulaToken($valuePHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORINFIXPHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_INTERSECTION);
        }

        
// move tokens to final list, switching infix "-" operators to prefix when appropriate, switching infix "+" operators
        // to noop when appropriate, identifying operand and infix-operator subtypes, and pulling "@" from function names
        
$this->_tokens = array();

        
$tokenCount count($tokens2);
        for (
$i 0$i $tokenCount; ++$i) {
            
$token $tokens2[$i];
            if (isset(
$tokens2[$i 1])) {
                
$previousToken $tokens2[$i 1];
            } else {
                
$previousToken null;
            }
            if (isset(
$tokens2[$i 1])) {
                
$nextToken $tokens2[$i 1];
            } else {
                
$nextToken null;
            }

            if (
is_null($token)) {
                continue;
            }

            if (
$token->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORINFIX && $token->getValue() == "-") {
                if (
$i == 0) {
                    
$token->setTokenType(PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORPREFIX);
                } else if (
                            ((
$previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_FUNCTION) && ($previousToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP)) ||
                            ((
$previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_SUBEXPRESSION) && ($previousToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP)) ||
                            (
$previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORPOSTFIX) ||
                            (
$previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND)
                        ) {
                    
$token->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_MATH);
                } else {
                    
$token->setTokenType(PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORPREFIX);
                }

                
$this->_tokens[] = $token;
                continue;
            }

            if (
$token->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORINFIX && $token->getValue() == "+") {
                if (
$i == 0) {
                    continue;
                } else if (
                            ((
$previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_FUNCTION) && ($previousToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP)) ||
                            ((
$previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_SUBEXPRESSION) && ($previousToken->getTokenSubType() == PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_STOP)) ||
                            (
$previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORPOSTFIX) ||
                            (
$previousToken->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND)
                        ) {
                    
$token->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_MATH);
                } else {
                    continue;
                }

                
$this->_tokens[] = $token;
                continue;
            }

            if (
$token->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERATORINFIX && $token->getTokenSubType() == PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_NOTHING) {
                if (
strpos("<>="substr($token->getValue(), 01)) !== false) {
                    
$token->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_LOGICAL);
                } else if (
$token->getValue() == "&") {
                    
$token->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_CONCATENATION);
                } else {
                    
$token->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_MATH);
                }

                
$this->_tokens[] = $token;
                continue;
            }

            if (
$token->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_OPERAND && $token->getTokenSubType() == PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_NOTHING) {
                if (!
is_numeric($token->getValue())) {
                    if (
strtoupper($token->getValue()) == "TRUE" || strtoupper($token->getValue() == "FALSE")) {
                        
$token->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_LOGICAL);
                    } else {
                        
$token->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_RANGE);
                    }
                } else {
                    
$token->setTokenSubType(PHPExcel_Calculation_FormulaToken::TOKEN_SUBTYPE_NUMBER);
                }

                
$this->_tokens[] = $token;
                continue;
            }

            if (
$token->getTokenType() == PHPExcel_Calculation_FormulaToken::TOKEN_TYPE_FUNCTION) {
                if (
strlen($token->getValue() > 0)) {
                    if (
substr($token->getValue(), 01) == "@") {
                        
$token->setValue(substr($token->getValue(), 1));
                    }
                }
            }

            
$this->_tokens[] = $token;
        }
    }
}