-
Notifications
You must be signed in to change notification settings - Fork 5
/
Lexer.php
773 lines (687 loc) · 27.7 KB
/
Lexer.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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
<?php
/**
* Query builder for Postgres backed by SQL parser
*
* LICENSE
*
* This source file is subject to BSD 2-Clause License that is bundled
* with this package in the file LICENSE and available at the URL
* https://raw.githubusercontent.com/sad-spirit/pg-builder/master/LICENSE
*
* @package sad_spirit\pg_builder
* @copyright 2014-2022 Alexey Borzov
* @author Alexey Borzov <avb@php.net>
* @license https://opensource.org/licenses/BSD-2-Clause BSD 2-Clause license
* @link https://github.com/sad-spirit/pg-builder
*/
declare(strict_types=1);
namespace sad_spirit\pg_builder;
/**
* Scans an SQL string for tokens
*/
class Lexer
{
/**
* Characters that may appear in operators
*/
public const CHARS_OPERATOR = '~!@#^&|`?+-*/%<>=';
/**
* Characters that should be returned as Token::TYPE_SPECIAL_CHAR
*/
private const CHARS_SPECIAL = ',()[].;:+-*/%^<>=';
/**
* Replacements for simple backslash escapes in e'...' strings
* @var array<string, string>
* @link https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS-ESCAPE
*/
private static $replacements = [
'b' => "\10", // backspace, no equivalent in PHP
'f' => "\f",
'n' => "\n",
'r' => "\r",
't' => "\t"
];
/** @var string */
private $source;
/** @var int */
private $position;
/** @var int */
private $length;
/** @var Token[] */
private $tokens;
/** @var array<string,mixed> */
private $options;
/**
* Set to true if Unicode escapes were seen during main lexer run, triggers processing these
* @var bool
*/
private $unescape;
/**
* First codepoint of Unicode surrogate pair
* @var int|null
*/
private $pairFirst;
/**
* Position of last Unicode escape in string
* @var int
*/
private $lastUnicodeEscape;
/** @var array<string, int> */
private $operatorCharHash;
/** @var array<string, int> */
private $specialCharHash;
/** @var array<string, int> */
private $nonStandardCharHash;
/** @var string */
private $baseRegexp;
/**
* Constructor, sets options for lexer
*
* Possible options are
* 'standard_conforming_strings' - the same meaning as the postgresql.conf parameter. When true (default),
* then backslashes in '...' strings are treated literally, when false they are treated as escape
* characters.
*
* @param array<string, mixed> $options
*/
public function __construct(array $options = [])
{
$this->options = array_merge(
['standard_conforming_strings' => true],
$options
);
$this->operatorCharHash = array_flip(str_split(self::CHARS_OPERATOR));
$this->specialCharHash = array_flip(str_split(self::CHARS_SPECIAL));
$this->nonStandardCharHash = array_flip(str_split('~!@#^&|`?%'));
$uniqueSpecialChars = array_unique(str_split(self::CHARS_SPECIAL . self::CHARS_OPERATOR));
$quotedSpecialChars = preg_quote(implode('', $uniqueSpecialChars));
$this->baseRegexp = <<<REGEXP
{
-- | # start of single-line comment
/\* | # start of multi-line comment
' | # string literal
" | # double-quoted identifier
([bBeEnNxX])' | # string literal with prefix, group 1
# positional parameter or dollar-quoted string, groups 2, 3 (junk test), 4
\\$(?: ( \d+ ) ( [A-Za-z\\x80-\\xFF_] )? | ( (?: [A-Za-z\\x80-\\xFF_][A-Za-z\\x80-\\xFF_0-9]*)? )\\$ ) |
# typecast, named function argument or named parameter, group 5
: (?: = | : | ( [A-Za-z\\x80-\\xFF_][A-Za-z\\x80-\\xFF_0-9]* ) ) |
# numeric constant, groups 6, 7 (junk test)
( (?: \d+ (?: \.\d+|\.(?!\.))? | \.\d+) (?: [eE][+-]\d+ )? ( [A-Za-z\\x80-\\xFF_] )? ) |
[uU]&["'] | # string/identifier with unicode escapes
\.\. | # double dot (error outside of PL/PgSQL)
([{$quotedSpecialChars}]) | # everything that looks, well, special, group 8
( [A-Za-z\\x80-\\xff_][A-Za-z\\x80-\\xff_0-9$]* ) # identifier, obviously, group 9
}Ax
REGEXP;
}
/**
* Tokenizes the input string
*
* @param string $sql
* @return TokenStream
* @throws exceptions\NotImplementedException
* @throws exceptions\SyntaxException
*
* @psalm-suppress RedundantFunctionCall
*/
public function tokenize(string $sql): TokenStream
{
if (extension_loaded('mbstring') && (2 & (int)ini_get('mbstring.func_overload'))) {
throw new exceptions\RuntimeException(
'Multibyte function overloading must be disabled for correct parser operation'
);
}
$this->source = $sql;
$this->position = strspn($this->source, " \r\n\t\f", 0);
$this->length = strlen($sql);
$this->tokens = [];
$this->unescape = false;
$this->doTokenize();
$this->tokens[] = new Token(Token::TYPE_EOF, '', $this->position);
if (!$this->unescape) {
return new TokenStream($this->tokens, $this->source);
} else {
$this->unescapeUnicodeTokens();
return new TokenStream(array_values($this->tokens), $this->source);
}
}
/**
* Does the actual tokenizing work
*
* @throws exceptions\SyntaxException
* @throws exceptions\NotImplementedException
*/
private function doTokenize(): void
{
while ($this->position < $this->length) {
if (!preg_match($this->baseRegexp, $this->source, $m, 0, $this->position)) {
throw exceptions\SyntaxException::atPosition(
"Unexpected '{$this->source[$this->position]}'",
$this->source,
$this->position
);
}
// check for found subpatterns first
if (isset($m[9])) {
// ASCII-only downcasing
$lowCase = strtr($m[9], 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
if (isset(Keywords::LIST[$lowCase])) {
$this->tokens[] = new Token(Keywords::LIST[$lowCase][0], $lowCase, $this->position);
} else {
$this->tokens[] = new Token(Token::TYPE_IDENTIFIER, $lowCase, $this->position);
}
$this->position += strlen($m[9]);
} elseif (isset($m[8])) {
if (isset($this->operatorCharHash[$m[8]])) {
$this->lexOperator();
} else {
$this->tokens[] = new Token(Token::TYPE_SPECIAL_CHAR, $m[8], $this->position++);
}
} elseif (isset($m[6])) {
if (isset($m[7])) {
throw exceptions\SyntaxException::atPosition(
"Trailing junk after numeric literal: '$m[0]'",
$this->source,
$this->position
);
}
$this->tokens[] = new Token(
ctype_digit($m[6]) ? Token::TYPE_INTEGER : Token::TYPE_FLOAT,
$m[6],
$this->position
);
$this->position += strlen($m[6]);
} elseif (isset($m[5])) {
$this->tokens[] = new Token(Token::TYPE_NAMED_PARAM, $m[5], $this->position);
$this->position += strlen($m[0]);
} elseif (isset($m[4])) {
$this->lexDollarQuoted($m[0]);
} elseif (isset($m[2])) {
if (isset($m[3])) {
throw exceptions\SyntaxException::atPosition(
"Trailing junk after positional parameter: '$m[0]'",
$this->source,
$this->position
);
}
$this->tokens[] = new Token(Token::TYPE_POSITIONAL_PARAM, $m[2], $this->position);
$this->position += strlen($m[0]);
} elseif (isset($m[1])) {
$this->lexString(strtolower($m[1]));
} else {
// now check the complete match
switch ($m[0]) {
case '--':
// single line comment, skip until newline
$this->position += strcspn($this->source, "\r\n", $this->position);
break;
case '/*':
// multiline comment, skip
if (
!preg_match(
'!/\* ( (?>[^/*]+ | /[^*] | \*[^/] ) | (?R) )* \*/!Ax',
$this->source,
$m,
0,
$this->position
)
) {
throw exceptions\SyntaxException::atPosition(
'Unterminated /* comment',
$this->source,
$this->position
);
}
$this->position += strlen($m[0]);
break;
case "'":
$this->lexString();
break;
case '"':
$this->lexDoubleQuoted();
break;
case '::':
$this->tokens[] = new Token(Token::TYPE_TYPECAST, '::', $this->position);
$this->position += 2;
break;
case ':=':
$this->tokens[] = new Token(Token::TYPE_COLON_EQUALS, ':=', $this->position);
$this->position += 2;
break;
case '..':
throw exceptions\SyntaxException::atPosition(
"Unexpected '..'",
$this->source,
$this->position
);
case 'u&"':
case 'U&"':
$this->unescape = true;
$this->lexDoubleQuoted(true);
break;
case "u&'":
case "U&'":
if (!$this->options['standard_conforming_strings']) {
throw exceptions\SyntaxException::atPosition(
"String constants with Unicode escapes cannot be used "
. "when standard_conforming_strings is off.",
$this->source,
$this->position
);
}
$this->unescape = true;
$this->lexString('u&');
break;
default:
// should not reach this
throw exceptions\SyntaxException::atPosition(
"Unexpected '{$m[0]}'",
$this->source,
$this->position
);
}
}
// skip whitespace
$this->position += strspn($this->source, " \r\n\t\f", $this->position);
}
}
/**
* Processes a double-quoted identifier
*
* @param bool $unicode Whether this is a u&"..." identifier
* @throws exceptions\SyntaxException
*/
private function lexDoubleQuoted(bool $unicode = false): void
{
$skip = $unicode ? 2 : 0;
if (!preg_match('/" ( (?>[^"]+ | "")* ) "/Ax', $this->source, $m, 0, $this->position + $skip)) {
throw exceptions\SyntaxException::atPosition(
'Unterminated quoted identifier',
$this->source,
$this->position
);
} elseif ('' === $m[1]) {
throw exceptions\SyntaxException::atPosition(
'Zero-length quoted identifier',
$this->source,
$this->position
);
}
$this->tokens[] = new Token(
$unicode ? Token::TYPE_UNICODE_IDENTIFIER : Token::TYPE_IDENTIFIER,
strtr($m[1], ['""' => '"']),
$this->position
);
$this->position += $skip + strlen($m[0]);
}
/**
* Processes a dollar-quoted string
*
* @param string $delimiter
* @throws exceptions\SyntaxException
*/
private function lexDollarQuoted(string $delimiter): void
{
$delimiterLength = strlen($delimiter);
if (false === ($pos = strpos($this->source, $delimiter, $this->position + $delimiterLength))) {
throw exceptions\SyntaxException::atPosition(
'Unterminated dollar-quoted string',
$this->source,
$this->position
);
}
$this->tokens[] = new Token(
Token::TYPE_STRING,
substr($this->source, $this->position + $delimiterLength, $pos - $this->position - $delimiterLength),
$this->position
);
$this->position = $pos + $delimiterLength;
}
/**
* Processes a single-quoted literal
*
* @param string $prefix Characters before quote that define string type, one of 'b', 'e', 'x', 'n', 'u&'
* @throws exceptions\SyntaxException
*/
private function lexString(string $prefix = ''): void
{
$realPosition = $this->position;
$type = Token::TYPE_STRING;
$regexNoQuotes = "'[^']*'";
$regexSlashes = "' ( (?>[^'\\\\]+ | '' | \\\\.)* ) '";
$regexNoSlashes = "' ( (?>[^']+ | '')* ) '";
switch ($prefix) {
case 'b':
$type = Token::TYPE_BINARY_STRING;
$regex = $regexNoQuotes;
break;
case 'e':
$regex = $regexSlashes;
break;
case 'x':
$regex = $regexNoQuotes;
$type = Token::TYPE_HEX_STRING;
break;
case 'u&':
$regex = $regexNoSlashes;
$type = Token::TYPE_UNICODE_STRING;
break;
/** @noinspection PhpMissingBreakStatementInspection */
case 'n':
$type = Token::TYPE_NCHAR_STRING;
// fall-through is intentional here
default:
$regex = $this->options['standard_conforming_strings'] ? $regexNoSlashes : $regexSlashes;
}
if (!preg_match("/{$regex}/Ax", $this->source, $m, 0, $this->position + strlen($prefix))) {
throw exceptions\SyntaxException::atPosition('Unterminated string literal', $this->source, $this->position);
}
$value = '';
$concat = '[ \t\f]* (?: --[^\n\r]* )? [\n\r] (?> [ \t\n\r\f]+ | --[^\n\r]*[\n\r] )*' . $regex;
do {
if ($regex === $regexNoQuotes) {
$value .= $m[1];
} elseif ($regex === $regexNoSlashes) {
$value .= strtr($m[1], ["''" => "'"]);
} else {
$value .= $this->unescapeCStyle($m[1], $this->position + strlen($prefix));
}
$this->position += strlen($prefix) + strlen($m[0]);
$prefix = '';
} while (preg_match("/{$concat}/Ax", $this->source, $m, 0, $this->position));
$this->tokens[] = new Token($type, $value, $realPosition);
}
/**
* Replaces escape sequences in string constants with C-style escapes
*
* @param string $escaped String constant without delimiters
* @param int $position Position of string constant in source SQL (for exceptions)
* @return string String with escape sequences replaced
*/
private function unescapeCStyle(string $escaped, int $position): string
{
$this->pairFirst = null;
$this->lastUnicodeEscape = 0;
$unescaped = '';
foreach (
preg_split(
"!(\\\\(?:x[0-9a-fA-F]{1,2}|[0-7]{1,3}|u[0-9a-fA-F]{0,4}|U[0-9a-fA-F]{0,8}|[^0-7]))!",
$escaped,
-1,
PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE
) ?: [] as [$part, $partPosition]
) {
if ('\\' !== $part[0]) {
if (null !== $this->pairFirst) {
break;
}
$unescaped .= strtr($part, ["''" => "'"]);
} elseif ('u' !== $part[1] && 'U' !== $part[1]) {
if (null !== $this->pairFirst) {
break;
}
if (isset(self::$replacements[$part[1]])) {
$unescaped .= self::$replacements[$part[1]];
} elseif ('x' === $part[1]) {
$unescaped .= chr((int)hexdec(substr($part, 2)));
} elseif (strspn($part, '01234567', 1)) {
$unescaped .= chr((int)octdec(substr($part, 1)));
} else {
// Just strip the escape and return the following char, as Postgres does
$unescaped .= $part[1];
}
} else {
$expected = 'u' === $part[1] ? 6 : 10;
if ($expected > strlen($part)) {
throw exceptions\SyntaxException::atPosition(
'Invalid Unicode escape value',
$this->source,
$position + $partPosition
);
}
$unescaped .= $this->handlePossibleSurrogatePairs(
(int)hexdec(substr($part, 2)),
$partPosition,
$position
);
}
}
if (null !== $this->pairFirst) {
throw exceptions\SyntaxException::atPosition(
'Unfinished Unicode surrogate pair',
$this->source,
$position + $this->lastUnicodeEscape
);
}
return $unescaped;
}
/**
* Converts a Unicode code point to its UTF-8 encoded representation.
*
* Borrowed from nikic/php-parser
*
* @param int $codepoint Code point
* @return string UTF-8 representation of code point
*/
private static function codePointToUtf8(int $codepoint): string
{
if ($codepoint <= 0x7F) {
return chr($codepoint);
}
if ($codepoint <= 0x7FF) {
return chr(($codepoint >> 6) + 0xC0) . chr(($codepoint & 0x3F) + 0x80);
}
if ($codepoint <= 0xFFFF) {
return chr(($codepoint >> 12) + 0xE0)
. chr((($codepoint >> 6) & 0x3F) + 0x80)
. chr(($codepoint & 0x3F) + 0x80);
}
if ($codepoint <= 0x1FFFFF) {
return chr(($codepoint >> 18) + 0xF0)
. chr((($codepoint >> 12) & 0x3F) + 0x80)
. chr((($codepoint >> 6) & 0x3F) + 0x80)
. chr(($codepoint & 0x3F) + 0x80);
}
throw new exceptions\InvalidArgumentException('Invalid Unicode codepoint');
}
/**
* Processes an operator
*/
private function lexOperator(): void
{
$length = strspn($this->source, self::CHARS_OPERATOR, $this->position);
$operator = substr($this->source, $this->position, $length);
if ($commentSingle = strpos($operator, '--')) {
$length = min($length, $commentSingle);
}
if ($commentMulti = strpos($operator, '/*')) {
$length = min($length, $commentMulti);
}
if (
$length > 1
&& ('+' === $operator[$length - 1] || '-' === $operator[$length - 1])
) {
for ($i = $length - 2; $i >= 0; $i--) {
if (isset($this->nonStandardCharHash[$operator[$i]])) {
break;
}
}
if ($i < 0) {
do {
$length--;
} while (
$length > 1
&& ('+' === $operator[$length - 1] || '-' === $operator[$length - 1])
);
}
}
$operator = substr($operator, 0, $length);
if (1 === $length && isset($this->specialCharHash[$operator])) {
$this->tokens[] = new Token(Token::TYPE_SPECIAL_CHAR, $operator, $this->position++);
return;
}
if (2 === $length) {
if ('=' === $operator[0] && '>' === $operator[1]) {
$this->tokens[] = new Token(Token::TYPE_EQUALS_GREATER, '=>', $this->position);
$this->position += 2;
return;
}
if (
'=' === $operator[1] && ('<' === $operator[0] || '>' === $operator[0] || '!' === $operator[0])
|| '>' === $operator[1] && '<' === $operator[0]
) {
$this->tokens[] = new Token(Token::TYPE_INEQUALITY, $operator, $this->position);
$this->position += 2;
return;
}
}
$this->tokens[] = new Token(Token::TYPE_OPERATOR, $operator, $this->position);
$this->position += $length;
}
/**
* Processes Unicode escapes in u&'...' string literals and u&"..." identifiers
*
* Handles possible trailing UESCAPE clauses, replaces TYPE_UNICODE_* tokens with simple
* TYPE_STRING and TYPE_IDENTIFIER ones having Unicode escapes in their values replaced by UTF-8 representations.
* Similar to what base_yylex() function in PostgreSQL's /src/backend/parser/parser.c does.
*/
private function unescapeUnicodeTokens(): void
{
for ($i = 0, $tokenCount = count($this->tokens); $i < $tokenCount; $i++) {
$tokenType = $this->tokens[$i]->getType();
if (Token::TYPE_UNICODE_STRING !== $tokenType && Token::TYPE_UNICODE_IDENTIFIER !== $tokenType) {
continue;
}
if (!($hasUescape = $this->tokens[$i + 1]->matches(Token::TYPE_KEYWORD, 'uescape'))) {
$newValue = $this->unescapeUnicode($this->tokens[$i]);
} else {
if (Token::TYPE_STRING !== $this->tokens[$i + 2]->getType()) {
throw exceptions\SyntaxException::atPosition(
'UESCAPE must be followed by a simple string literal',
$this->source,
$this->tokens[$i + 2]->getPosition()
);
}
$escape = $this->tokens[$i + 2]->getValue();
if (
1 !== strlen($escape)
|| ctype_xdigit($escape)
|| ctype_space($escape)
|| in_array($escape, ['"', "'", '+'], true)
) {
throw exceptions\SyntaxException::atPosition(
'Invalid Unicode escape character',
$this->source,
$this->tokens[$i + 2]->getPosition()
);
}
$newValue = $this->unescapeUnicode($this->tokens[$i], $escape);
}
$this->tokens[$i] = new Token(
Token::TYPE_UNICODE_STRING === $tokenType ? Token::TYPE_STRING : Token::TYPE_IDENTIFIER,
$newValue,
$this->tokens[$i]->getPosition()
);
// Skip and remove uescape tokens, they aren't needed in TokenStream
if ($hasUescape) {
unset($this->tokens[$i + 1], $this->tokens[$i + 2]);
$i += 2;
}
}
}
/**
* Replaces Unicode escapes in values of string or identifier Tokens
*
* This is a bit more complex than replaceEscapeSequences() as the escapes may specify UTF-16 surrogate pairs
* that should be combined into single codepoint.
*
* @param Token $token The whole Token is needed to be able to use its position in exception messages
* @param string $escapeChar Value of escape character provided by UESCAPE 'X' clause, if present
* @return string value of Token with Unicode escapes replaced by their UTF-8 equivalents
*/
private function unescapeUnicode(Token $token, string $escapeChar = '\\'): string
{
$this->pairFirst = null;
$this->lastUnicodeEscape = 0;
$unescaped = '';
$quoted = preg_quote($escapeChar);
foreach (
preg_split(
"/({$quoted}(?:{$quoted}|[0-9a-fA-F]{4}|\\+[0-9a-fA-F]{6}))/",
$token->getValue(),
-1,
PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE | PREG_SPLIT_DELIM_CAPTURE
) ?: [] as [$part, $position]
) {
if ($escapeChar === $part[0] && $escapeChar !== $part[1]) {
$unescaped .= $this->handlePossibleSurrogatePairs(
(int)hexdec(ltrim($part, $escapeChar . '+')),
$position,
$token->getPosition() + 3
);
} elseif (null !== $this->pairFirst) {
break;
} elseif ($escapeChar === $part[0]) {
$unescaped .= $escapeChar;
} elseif (false !== ($escapePos = strpos($part, $escapeChar))) {
throw exceptions\SyntaxException::atPosition(
"Invalid Unicode escape",
$this->source,
$token->getPosition() + 3 + $position + $escapePos
);
} else {
$unescaped .= $part;
}
}
if (null !== $this->pairFirst) {
throw exceptions\SyntaxException::atPosition(
'Unfinished Unicode surrogate pair',
$this->source,
$token->getPosition() + 3 + $this->lastUnicodeEscape
);
}
return $unescaped;
}
/**
* Converts a Unicode code point that can be a part of surrogate pair to its UTF-8 encoded representation
*
* @param int $codepoint
* @param int $position
* @param int $basePosition
* @return string
* @throws exceptions\SyntaxException
*/
private function handlePossibleSurrogatePairs(int $codepoint, int $position, int $basePosition): string
{
$utf8 = '';
$isSurrogateFirst = 0xD800 <= $codepoint && 0xDBFF >= $codepoint;
$isSurrogateSecond = 0xDC00 <= $codepoint && 0xDFFF >= $codepoint;
try {
if ((null !== $this->pairFirst) !== $isSurrogateSecond) {
throw exceptions\SyntaxException::atPosition(
"Invalid Unicode surrogate pair",
$this->source,
$basePosition + (null !== $this->pairFirst ? $this->lastUnicodeEscape : $position)
);
} elseif (null !== $this->pairFirst) {
$utf8 = self::codePointToUtf8(
(($this->pairFirst & 0x3FF) << 10) + 0x10000 + ($codepoint & 0x3FF)
);
$this->pairFirst = null;
} elseif ($isSurrogateFirst) {
$this->pairFirst = $codepoint;
} else {
$utf8 = self::codePointToUtf8($codepoint);
}
} catch (exceptions\InvalidArgumentException $e) {
throw exceptions\SyntaxException::atPosition(
$e->getMessage(),
$this->source,
$basePosition + $position
);
}
$this->lastUnicodeEscape = $position;
return $utf8;
}
}