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