Passed
Push — master ( b504ba...c2a964 )
by
unknown
21:09 queued 09:01
created

Parser::term()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

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