1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author: Viskov Sergey |
4
|
|
|
* @date: 3/10/16 |
5
|
|
|
* @time: 6:27 PM |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace LTDBeget\sphinx; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use Exception; |
12
|
|
|
use LTDBeget\stringstream\StringStream; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class SyntaxErrorException |
16
|
|
|
* @package LTDBeget\sphinx\configurator\exceptions |
17
|
|
|
*/ |
18
|
|
|
class SyntaxErrorException extends \Exception |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* SyntaxErrorException constructor. |
22
|
|
|
* @param StringStream $stream |
23
|
|
|
* @param int $code |
24
|
|
|
* @param Exception|null $previous |
25
|
|
|
*/ |
26
|
7 |
|
public function __construct(StringStream $stream, int $code = 0, Exception $previous = null) |
27
|
|
|
{ |
28
|
7 |
|
if($stream->isEnd()) { |
29
|
1 |
|
$stream->end(); |
30
|
|
|
} |
31
|
|
|
|
32
|
7 |
|
$this->unexpected_char = $stream->current(); |
33
|
7 |
|
$this->error_line = $this->getParseErrorLineNumber($stream); |
34
|
7 |
|
$message = sprintf($this->messageTemplate, $this->unexpected_char, $this->error_line); |
35
|
7 |
|
parent::__construct($message, $code, $previous); |
36
|
7 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return int |
40
|
|
|
*/ |
41
|
|
|
public function getErrorLine() |
42
|
|
|
{ |
43
|
|
|
return $this->error_line; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getUnexpectedChar() |
50
|
|
|
{ |
51
|
|
|
return $this->unexpected_char; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @internal |
56
|
|
|
* @param StringStream $stream |
57
|
|
|
* @return int |
58
|
|
|
*/ |
59
|
7 |
|
private function getParseErrorLineNumber(StringStream $stream) : int |
60
|
|
|
{ |
61
|
7 |
|
$parse_error_char_position = $stream->position(); |
62
|
7 |
|
$plain_data = $this->getFullString($stream); |
63
|
7 |
|
$exploded_by_lines = explode("\n", $plain_data); |
64
|
7 |
|
foreach ($exploded_by_lines as $key => $line) { |
65
|
7 |
|
$line_length = strlen($line) + 1; |
66
|
7 |
|
$parse_error_char_position -= $line_length; |
67
|
7 |
|
if ($parse_error_char_position < 0) { |
68
|
7 |
|
return $key + 1; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return 1; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param StringStream $stream |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
private function getFullString(StringStream $stream) : string |
80
|
|
|
{ |
81
|
|
|
$stream->start(); |
82
|
|
|
$string = ''; |
83
|
|
|
do { |
84
|
|
|
$string .= $stream->current(); |
85
|
|
|
$stream->next(); |
86
|
|
|
} while (! $stream->isEnd()); |
87
|
|
|
|
88
|
|
|
return $string; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var int |
93
|
|
|
*/ |
94
|
|
|
private $error_line; |
95
|
|
|
/** |
96
|
|
|
* @var string |
97
|
|
|
*/ |
98
|
|
|
private $unexpected_char; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @var string |
102
|
|
|
*/ |
103
|
|
|
private $messageTemplate = "Parse error: syntax error, unexpected '%s' on line %d."; |
104
|
|
|
} |