|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cerbero\JsonParser\Tokens; |
|
4
|
|
|
|
|
5
|
|
|
use Cerbero\JsonParser\Exceptions\SyntaxException; |
|
6
|
|
|
use Cerbero\JsonParser\Sources\Source; |
|
7
|
|
|
use Cerbero\JsonParser\Tokens\Token; |
|
8
|
|
|
use Cerbero\JsonParser\Tokens\Tokenizer; |
|
9
|
|
|
use Cerbero\JsonParser\Tokens\Tokens; |
|
10
|
|
|
use Cerbero\JsonParser\ValueObjects\Progress; |
|
11
|
|
|
use IteratorAggregate; |
|
12
|
|
|
use Traversable; |
|
13
|
|
|
|
|
14
|
|
|
use function strlen; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The JSON lexer. |
|
18
|
|
|
* |
|
19
|
|
|
* @implements IteratorAggregate<int, Token> |
|
20
|
|
|
*/ |
|
21
|
|
|
final class Lexer implements IteratorAggregate |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* The parsing progress. |
|
25
|
|
|
* |
|
26
|
|
|
* @var Progress |
|
27
|
|
|
*/ |
|
28
|
|
|
private Progress $progress; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The current position. |
|
32
|
|
|
* |
|
33
|
|
|
* @var int |
|
34
|
|
|
*/ |
|
35
|
|
|
private int $position = 0; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Instantiate the class. |
|
39
|
|
|
* |
|
40
|
|
|
* @param Source $source |
|
41
|
|
|
*/ |
|
42
|
345 |
|
public function __construct(private Source $source) |
|
43
|
|
|
{ |
|
44
|
345 |
|
$this->progress = new Progress(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Retrieve the JSON fragments |
|
49
|
|
|
* |
|
50
|
|
|
* @return \Generator<int, Token> |
|
51
|
|
|
*/ |
|
52
|
333 |
|
public function getIterator(): Traversable |
|
53
|
|
|
{ |
|
54
|
333 |
|
$buffer = ''; |
|
55
|
333 |
|
$inString = $isEscaping = false; |
|
56
|
333 |
|
$tokenizer = Tokenizer::instance(); |
|
57
|
|
|
|
|
58
|
333 |
|
foreach ($this->source as $chunk) { |
|
59
|
330 |
|
for ($i = 0, $size = strlen($chunk); $i < $size; $i++, $this->position++) { |
|
60
|
330 |
|
$character = $chunk[$i]; |
|
61
|
330 |
|
$inString = ($character == '"') != $inString || $isEscaping; |
|
62
|
330 |
|
$isEscaping = $character == '\\' && !$isEscaping; |
|
63
|
|
|
|
|
64
|
330 |
|
if ($inString || !isset(Tokens::BOUNDARIES[$character])) { |
|
65
|
280 |
|
$buffer == '' && !isset(Tokens::TYPES[$character]) && throw new SyntaxException($character); |
|
66
|
277 |
|
$buffer .= $character; |
|
67
|
277 |
|
continue; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
329 |
|
if ($buffer != '') { |
|
71
|
277 |
|
yield $tokenizer->toToken($buffer); |
|
72
|
273 |
|
$buffer = ''; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
329 |
|
if (isset(Tokens::DELIMITERS[$character])) { |
|
76
|
329 |
|
yield $tokenizer->toToken($character); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Retrieve the current position |
|
84
|
|
|
* |
|
85
|
|
|
* @return int |
|
86
|
|
|
*/ |
|
87
|
11 |
|
public function position(): int |
|
88
|
|
|
{ |
|
89
|
11 |
|
return $this->position; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Retrieve the parsing progress |
|
94
|
|
|
* |
|
95
|
|
|
* @return Progress |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public function progress(): Progress |
|
98
|
|
|
{ |
|
99
|
1 |
|
return $this->progress->setCurrent($this->position)->setTotal($this->source->size()); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|