1 | <?php |
||
26 | abstract class BaseNode |
||
27 | { |
||
28 | /** |
||
29 | * @var AST |
||
30 | */ |
||
31 | protected $ast; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $methodName = ''; |
||
37 | |||
38 | /** |
||
39 | * @var int |
||
40 | */ |
||
41 | protected $methodOffset = 0; |
||
42 | |||
43 | /** |
||
44 | * @param AST $ast |
||
45 | */ |
||
46 | 206 | public function __construct(AST $ast) |
|
47 | { |
||
48 | 206 | $this->ast = $ast; |
|
49 | 206 | } |
|
50 | |||
51 | abstract public function getNode() : BaseToken; |
||
52 | |||
53 | /** |
||
54 | * Looks ahead, but does not move the pointer. |
||
55 | */ |
||
56 | 194 | protected function hasMethodCall() : bool |
|
79 | |||
80 | /** |
||
81 | * @throws ParserException |
||
82 | */ |
||
83 | 108 | public function getMethod(BaseToken $token) : CallableFunction |
|
109 | |||
110 | 108 | private function getMethodName() : string |
|
111 | { |
||
112 | do { |
||
113 | 108 | $this->ast->next(); |
|
114 | 108 | } while ($this->ast->getStack()->current()->getOffset() < $this->methodOffset); |
|
115 | |||
116 | 108 | return trim(ltrim(rtrim($this->methodName, "\r\n("), '.')); |
|
117 | } |
||
118 | |||
119 | 52 | public function getArrayItems() : TokenCollection |
|
123 | |||
124 | 126 | public function getArguments() : TokenCollection |
|
125 | { |
||
128 | |||
129 | /** |
||
130 | * @throws ParserException |
||
131 | */ |
||
132 | 148 | private function getCommaSeparatedValues(string $stopAt) : TokenCollection |
|
133 | { |
||
134 | 148 | $commaExpected = false; |
|
135 | 148 | $items = new TokenCollection(); |
|
136 | |||
137 | do { |
||
138 | 148 | $this->ast->next(); |
|
139 | |||
140 | 148 | if (!$current = $this->ast->current()) { |
|
141 | 4 | throw new ParserException(sprintf( |
|
142 | 4 | 'Unexpected end of string. Expected "%s"', |
|
143 | 4 | $stopAt |
|
144 | )); |
||
145 | } |
||
146 | |||
147 | 148 | if ($current->getGroup() === Constants::GROUP_VALUE) { |
|
148 | 124 | if ($commaExpected) { |
|
149 | 4 | throw new ParserException(sprintf( |
|
150 | 4 | 'Unexpected value at position %d on line %d', |
|
151 | 4 | $current->getPosition(), |
|
152 | 4 | $current->getLine() |
|
153 | )); |
||
154 | } |
||
155 | |||
156 | 124 | $commaExpected = true; |
|
157 | 124 | $items->attach($current); |
|
158 | 148 | } elseif ($current instanceof Tokens\TokenComma) { |
|
159 | 60 | if (!$commaExpected) { |
|
160 | 4 | throw new ParserException(sprintf( |
|
161 | 4 | 'Unexpected token "," at position %d on line %d', |
|
162 | 4 | $current->getPosition(), |
|
163 | 4 | $current->getLine() |
|
164 | )); |
||
165 | } |
||
166 | |||
167 | 60 | $commaExpected = false; |
|
168 | 148 | } elseif ($current->getValue() === $stopAt) { |
|
169 | 132 | break; |
|
170 | 64 | } elseif (!$this->isIgnoredToken($current)) { |
|
171 | 4 | throw new ParserException(sprintf( |
|
172 | 4 | 'Unexpected token "%s" at position %d on line %d', |
|
173 | 4 | $current->getOriginalValue(), |
|
174 | 4 | $current->getPosition(), |
|
175 | 4 | $current->getLine() |
|
176 | )); |
||
177 | } |
||
178 | 124 | } while ($this->ast->valid()); |
|
179 | |||
180 | 132 | if (!$commaExpected && $items->count() > 0) { |
|
181 | 6 | throw new ParserException(sprintf( |
|
182 | 6 | 'Unexpected token "," at position %d on line %d', |
|
183 | 6 | $current->getPosition(), |
|
184 | 6 | $current->getLine() |
|
185 | )); |
||
186 | } |
||
187 | |||
188 | 126 | $items->rewind(); |
|
189 | |||
190 | 126 | return $items; |
|
191 | } |
||
192 | |||
193 | 192 | private function isIgnoredToken(BaseToken $token) : bool |
|
201 | } |
||
202 |