1: | <?php declare(strict_types = 1); |
2: | |
3: | namespace PHPStan\PhpDocParser\Parser; |
4: | |
5: | use Exception; |
6: | use PHPStan\PhpDocParser\Lexer\Lexer; |
7: | use function assert; |
8: | use function json_encode; |
9: | use function sprintf; |
10: | use const JSON_INVALID_UTF8_SUBSTITUTE; |
11: | use const JSON_UNESCAPED_SLASHES; |
12: | use const JSON_UNESCAPED_UNICODE; |
13: | |
14: | class ParserException extends Exception |
15: | { |
16: | |
17: | |
18: | private $currentTokenValue; |
19: | |
20: | |
21: | private $currentTokenType; |
22: | |
23: | |
24: | private $currentOffset; |
25: | |
26: | |
27: | private $expectedTokenType; |
28: | |
29: | |
30: | private $expectedTokenValue; |
31: | |
32: | public function __construct( |
33: | string $currentTokenValue, |
34: | int $currentTokenType, |
35: | int $currentOffset, |
36: | int $expectedTokenType, |
37: | ?string $expectedTokenValue = null |
38: | ) |
39: | { |
40: | $this->currentTokenValue = $currentTokenValue; |
41: | $this->currentTokenType = $currentTokenType; |
42: | $this->currentOffset = $currentOffset; |
43: | $this->expectedTokenType = $expectedTokenType; |
44: | $this->expectedTokenValue = $expectedTokenValue; |
45: | |
46: | parent::__construct(sprintf( |
47: | 'Unexpected token %s, expected %s%s at offset %d', |
48: | $this->formatValue($currentTokenValue), |
49: | Lexer::TOKEN_LABELS[$expectedTokenType], |
50: | $expectedTokenValue !== null ? sprintf(' (%s)', $this->formatValue($expectedTokenValue)) : '', |
51: | $currentOffset |
52: | )); |
53: | } |
54: | |
55: | |
56: | public function getCurrentTokenValue(): string |
57: | { |
58: | return $this->currentTokenValue; |
59: | } |
60: | |
61: | |
62: | public function getCurrentTokenType(): int |
63: | { |
64: | return $this->currentTokenType; |
65: | } |
66: | |
67: | |
68: | public function getCurrentOffset(): int |
69: | { |
70: | return $this->currentOffset; |
71: | } |
72: | |
73: | |
74: | public function getExpectedTokenType(): int |
75: | { |
76: | return $this->expectedTokenType; |
77: | } |
78: | |
79: | |
80: | public function getExpectedTokenValue(): ?string |
81: | { |
82: | return $this->expectedTokenValue; |
83: | } |
84: | |
85: | |
86: | private function formatValue(string $value): string |
87: | { |
88: | $json = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE); |
89: | assert($json !== false); |
90: | |
91: | return $json; |
92: | } |
93: | |
94: | } |
95: | |