| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file is part of the Peast package |
| 4 |
* |
| 5 |
* (c) Marco Marchiò <[email protected]> |
| 6 |
* |
| 7 |
* For the full copyright and license information refer to the LICENSE file |
| 8 |
* distributed with this source code |
| 9 |
*/ |
| 10 |
namespace Peast\Syntax; |
| 11 |
|
| 12 |
/** |
| 13 |
* Base class for parsers. |
| 14 |
* |
| 15 |
* @author Marco Marchiò <[email protected]> |
| 16 |
* |
| 17 |
* @abstract |
| 18 |
*/ |
| 19 |
abstract class ParserAbstract |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Associated scanner |
| 23 |
* |
| 24 |
* @var Scanner |
| 25 |
*/ |
| 26 |
protected $scanner; |
| 27 |
|
| 28 |
/** |
| 29 |
* Parser features |
| 30 |
* |
| 31 |
* @var Features |
| 32 |
*/ |
| 33 |
protected $features; |
| 34 |
|
| 35 |
/** |
| 36 |
* Parser context |
| 37 |
* |
| 38 |
* @var \stdClass |
| 39 |
*/ |
| 40 |
protected $context; |
| 41 |
|
| 42 |
/** |
| 43 |
* Source type |
| 44 |
* |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
protected $sourceType; |
| 48 |
|
| 49 |
/** |
| 50 |
* Comments handling |
| 51 |
* |
| 52 |
* @var bool |
| 53 |
*/ |
| 54 |
protected $comments; |
| 55 |
|
| 56 |
/** |
| 57 |
* JSX syntax handling |
| 58 |
* |
| 59 |
* @var bool |
| 60 |
*/ |
| 61 |
protected $jsx; |
| 62 |
|
| 63 |
/** |
| 64 |
* Events emitter |
| 65 |
* |
| 66 |
* @var EventsEmitter |
| 67 |
*/ |
| 68 |
protected $eventsEmitter; |
| 69 |
|
| 70 |
/** |
| 71 |
* Class constructor |
| 72 |
* |
| 73 |
* @param string $source Source code |
| 74 |
* @param Features $features Parser features |
| 75 |
* @param array $options Parsing options |
| 76 |
*/ |
| 77 |
public function __construct( |
| 78 |
$source, Features $features, $options = array() |
| 79 |
) { |
| 80 |
$this->features = $features; |
| 81 |
|
| 82 |
$this->sourceType = isset($options["sourceType"]) ? |
| 83 |
$options["sourceType"] : |
| 84 |
\Peast\Peast::SOURCE_TYPE_SCRIPT; |
| 85 |
|
| 86 |
//Create the scanner |
| 87 |
$this->scanner = new Scanner($source, $features, $options); |
| 88 |
|
| 89 |
//Enable module scanning if required |
| 90 |
if ($this->sourceType === \Peast\Peast::SOURCE_TYPE_MODULE) { |
| 91 |
$this->scanner->enableModuleMode(); |
| 92 |
} |
| 93 |
|
| 94 |
//Enable comments scanning |
| 95 |
$this->comments = isset($options["comments"]) && $options["comments"]; |
| 96 |
if ($this->comments) { |
| 97 |
$this->scanner->enableComments(); |
| 98 |
//Create the comments registry |
| 99 |
new CommentsRegistry($this); |
| 100 |
} |
| 101 |
|
| 102 |
// Enable jsx syntax if required |
| 103 |
$this->jsx = isset($options["jsx"]) && $options["jsx"]; |
| 104 |
|
| 105 |
$this->initContext(); |
| 106 |
$this->postInit(); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Initializes parser context |
| 111 |
* |
| 112 |
* @return void |
| 113 |
*/ |
| 114 |
abstract protected function initContext(); |
| 115 |
|
| 116 |
/** |
| 117 |
* Post initialize operations |
| 118 |
* |
| 119 |
* @return void |
| 120 |
*/ |
| 121 |
abstract protected function postInit(); |
| 122 |
|
| 123 |
/** |
| 124 |
* Parses the source |
| 125 |
* |
| 126 |
* @return Node\Node |
| 127 |
* |
| 128 |
* @abstract |
| 129 |
*/ |
| 130 |
abstract public function parse(); |
| 131 |
|
| 132 |
/** |
| 133 |
* Returns parsed tokens from the source code |
| 134 |
* |
| 135 |
* @return Token[] |
| 136 |
*/ |
| 137 |
public function tokenize() |
| 138 |
{ |
| 139 |
$this->scanner->enableTokenRegistration(); |
| 140 |
$this->parse(); |
| 141 |
return $this->scanner->getTokens(); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Returns the scanner associated with the parser |
| 146 |
* |
| 147 |
* @return Scanner |
| 148 |
*/ |
| 149 |
public function getScanner() |
| 150 |
{ |
| 151 |
return $this->scanner; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Returns the parser features class |
| 156 |
* |
| 157 |
* @return Features |
| 158 |
*/ |
| 159 |
public function getFeatures() |
| 160 |
{ |
| 161 |
return $this->features; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Returns the parser's events emitter |
| 166 |
* |
| 167 |
* @return EventsEmitter |
| 168 |
*/ |
| 169 |
public function getEventsEmitter() |
| 170 |
{ |
| 171 |
if (!$this->eventsEmitter) { |
| 172 |
//The event emitter is created here so that it won't exist if not |
| 173 |
//necessary |
| 174 |
$this->eventsEmitter = new EventsEmitter; |
| 175 |
} |
| 176 |
return $this->eventsEmitter; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Calls a method with an isolated parser context, applying the given flags, |
| 181 |
* but restoring their values after the execution. |
| 182 |
* |
| 183 |
* @param array|null $flags Key/value array of changes to apply to the |
| 184 |
* context flags. If it's null or the first |
| 185 |
* element of the array is null the context will |
| 186 |
* be reset before applying new values. |
| 187 |
* @param string $fn Method to call |
| 188 |
* @param array|null $args Method arguments |
| 189 |
* |
| 190 |
* @return mixed |
| 191 |
*/ |
| 192 |
protected function isolateContext($flags, $fn, $args = null) |
| 193 |
{ |
| 194 |
//Store the current context |
| 195 |
$oldContext = clone $this->context; |
| 196 |
|
| 197 |
//If flag argument is null reset the context |
| 198 |
if ($flags === null) { |
| 199 |
$this->initContext(); |
| 200 |
} else { |
| 201 |
//Apply new values to the flags |
| 202 |
foreach ($flags as $k => $v) { |
| 203 |
// If null reset the context |
| 204 |
if ($v === null) { |
| 205 |
$this->initContext(); |
| 206 |
} else { |
| 207 |
$this->context->$k = $v; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
//Call the method with the given arguments |
| 213 |
$ret = $args ? call_user_func_array(array($this, $fn), $args) : $this->$fn(); |
| 214 |
|
| 215 |
//Restore previous context |
| 216 |
$this->context = $oldContext; |
| 217 |
|
| 218 |
return $ret; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Creates a node |
| 223 |
* |
| 224 |
* @param string $nodeType Node's type |
| 225 |
* @param mixed $position Node's start position |
| 226 |
* |
| 227 |
* @return Node\Node |
| 228 |
* |
| 229 |
* @codeCoverageIgnore |
| 230 |
*/ |
| 231 |
protected function createNode($nodeType, $position) |
| 232 |
{ |
| 233 |
//Use the right class to get an instance of the node |
| 234 |
$nodeClass = "\\Peast\\Syntax\\Node\\" . $nodeType; |
| 235 |
$node = new $nodeClass; |
| 236 |
|
| 237 |
//Add the node start position |
| 238 |
if ($position instanceof Node\Node || $position instanceof Token) { |
| 239 |
$position = $position->location->start; |
| 240 |
} elseif (is_array($position)) { |
| 241 |
if (count($position)) { |
| 242 |
$position = $position[0]->location->start; |
| 243 |
} else { |
| 244 |
$position = $this->scanner->getPosition(); |
| 245 |
} |
| 246 |
} |
| 247 |
$node->location->start = $position; |
| 248 |
|
| 249 |
//Emit the NodeCreated event for the node |
| 250 |
$this->eventsEmitter && $this->eventsEmitter->fire( |
| 251 |
"NodeCreated", array($node) |
| 252 |
); |
| 253 |
|
| 254 |
return $node; |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Completes a node by adding the end position |
| 259 |
* |
| 260 |
* @param Node\Node $node Node to complete |
| 261 |
* @param Position $position Node's end position |
| 262 |
* |
| 263 |
* @return mixed It actually returns a Node but mixed solves |
| 264 |
* a lot of PHPDoc problems |
| 265 |
* |
| 266 |
* @codeCoverageIgnore |
| 267 |
*/ |
| 268 |
protected function completeNode(Node\Node $node, $position = null) |
| 269 |
{ |
| 270 |
//Add the node end position |
| 271 |
$node->location->end = $position ?: $this->scanner->getPosition(); |
| 272 |
|
| 273 |
//Emit the NodeCompleted event for the node |
| 274 |
$this->eventsEmitter && $this->eventsEmitter->fire( |
| 275 |
"NodeCompleted", array($node) |
| 276 |
); |
| 277 |
|
| 278 |
return $node; |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Throws a syntax error |
| 283 |
* |
| 284 |
* @param string $message Error message |
| 285 |
* @param Position $position Error position |
| 286 |
* |
| 287 |
* @return void |
| 288 |
* |
| 289 |
* @throws Exception |
| 290 |
*/ |
| 291 |
protected function error($message = "", $position = null) |
| 292 |
{ |
| 293 |
if (!$message) { |
| 294 |
$token = $this->scanner->getToken(); |
| 295 |
if ($token === null) { |
| 296 |
$message = "Unexpected end of input"; |
| 297 |
} else { |
| 298 |
$position = $token->location->start; |
| 299 |
$message = "Unexpected: " . $token->value; |
| 300 |
} |
| 301 |
} |
| 302 |
if (!$position) { |
| 303 |
$position = $this->scanner->getPosition(); |
| 304 |
} |
| 305 |
throw new Exception($message, $position); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Asserts that a valid end of statement follows the current position |
| 310 |
* |
| 311 |
* @return boolean |
| 312 |
* |
| 313 |
* @throws Exception |
| 314 |
*/ |
| 315 |
protected function assertEndOfStatement() |
| 316 |
{ |
| 317 |
//The end of statement is reached when it is followed by line |
| 318 |
//terminators, end of source, "}" or ";". In the last case the token |
| 319 |
//must be consumed |
| 320 |
if (!$this->scanner->noLineTerminators()) { |
| 321 |
return true; |
| 322 |
} else { |
| 323 |
if ($this->scanner->consume(";")) { |
| 324 |
return true; |
| 325 |
} |
| 326 |
$token = $this->scanner->getToken(); |
| 327 |
if (!$token || $token->value === "}") { |
| 328 |
return true; |
| 329 |
} |
| 330 |
} |
| 331 |
$this->error(); |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Parses a character separated list of instructions or null if the |
| 336 |
* sequence is not valid |
| 337 |
* |
| 338 |
* @param callable $fn Parsing instruction function |
| 339 |
* @param string $char Separator |
| 340 |
* |
| 341 |
* @return array |
| 342 |
* |
| 343 |
* @throws Exception |
| 344 |
*/ |
| 345 |
protected function charSeparatedListOf($fn, $char = ",") |
| 346 |
{ |
| 347 |
$list = array(); |
| 348 |
$valid = true; |
| 349 |
while ($param = $this->$fn()) { |
| 350 |
$list[] = $param; |
| 351 |
$valid = true; |
| 352 |
if (!$this->scanner->consume($char)) { |
| 353 |
break; |
| 354 |
} else { |
| 355 |
$valid = false; |
| 356 |
} |
| 357 |
} |
| 358 |
if (!$valid) { |
| 359 |
$this->error(); |
| 360 |
} |
| 361 |
return $list; |
| 362 |
} |
| 363 |
} |