| 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\JSX; |
| 11 |
|
| 12 |
use Peast\Syntax\Token; |
| 13 |
|
| 14 |
/** |
| 15 |
* JSX parser trait |
| 16 |
* |
| 17 |
* @author Marco Marchiò <[email protected]> |
| 18 |
*/ |
| 19 |
trait Parser |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Creates a JSX node |
| 23 |
* |
| 24 |
* @param string $nodeType Node's type |
| 25 |
* @param mixed $position Node's start position |
| 26 |
* |
| 27 |
* @return \Peast\Syntax\Node\Node |
| 28 |
*/ |
| 29 |
protected function createJSXNode($nodeType, $position) |
| 30 |
{ |
| 31 |
return $this->createNode("JSX\\$nodeType", $position); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Parses a jsx fragment |
| 36 |
* |
| 37 |
* @return \Peast\Syntax\Node\JSX\JSXFragment|null |
| 38 |
*/ |
| 39 |
protected function parseJSXFragment() |
| 40 |
{ |
| 41 |
$startOpeningToken = $this->scanner->getToken(); |
| 42 |
if (!$startOpeningToken || $startOpeningToken->value !== "<") { |
| 43 |
return null; |
| 44 |
} |
| 45 |
|
| 46 |
$endOpeningToken = $this->scanner->getNextToken(); |
| 47 |
if (!$endOpeningToken || $endOpeningToken->value !== ">") { |
| 48 |
return null; |
| 49 |
} |
| 50 |
|
| 51 |
$this->scanner->consumeToken(); |
| 52 |
$this->scanner->consumeToken(); |
| 53 |
|
| 54 |
$children = $this->parseJSXChildren(); |
| 55 |
|
| 56 |
if (!($startClosingToken = $this->scanner->consume("<")) || |
| 57 |
!$this->scanner->consume("/") || |
| 58 |
!$this->scanner->reconsumeCurrentTokenInJSXMode() || |
| 59 |
$endOpeningToken->value !== ">") { |
| 60 |
$this->error(); |
| 61 |
} |
| 62 |
$this->scanner->consumeToken(); |
| 63 |
|
| 64 |
//Opening tag |
| 65 |
$openingNode = $this->createJSXNode( |
| 66 |
"JSXOpeningFragment", |
| 67 |
$startOpeningToken |
| 68 |
); |
| 69 |
$this->completeNode( |
| 70 |
$openingNode, |
| 71 |
$endOpeningToken->location->end |
| 72 |
); |
| 73 |
|
| 74 |
//Closing tag |
| 75 |
$closingNode = $this->createJSXNode( |
| 76 |
"JSXClosingFragment", |
| 77 |
$startClosingToken |
| 78 |
); |
| 79 |
$this->completeNode($closingNode); |
| 80 |
|
| 81 |
//Fragment |
| 82 |
$node = $this->createJSXNode("JSXFragment", $startOpeningToken); |
| 83 |
$node->setOpeningFragment($openingNode); |
| 84 |
$node->setClosingFragment($closingNode); |
| 85 |
if ($children) { |
| 86 |
$node->setChildren($children); |
| 87 |
} |
| 88 |
return $this->completeNode($node); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Parses a group of jsx children |
| 93 |
* |
| 94 |
* @return \Peast\Syntax\Node\Node[]|null |
| 95 |
*/ |
| 96 |
protected function parseJSXChildren() |
| 97 |
{ |
| 98 |
$children = array(); |
| 99 |
while ($child = $this->parseJSXChild()) { |
| 100 |
$children[] = $child; |
| 101 |
} |
| 102 |
return count($children) ? $children : null; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Parses a jsx child |
| 107 |
* |
| 108 |
* @return \Peast\Syntax\Node\Node|null |
| 109 |
*/ |
| 110 |
protected function parseJSXChild() |
| 111 |
{ |
| 112 |
if ($node = $this->parseJSXText()) { |
| 113 |
return $node; |
| 114 |
} elseif ($node = $this->parseJSXFragment()) { |
| 115 |
return $node; |
| 116 |
} elseif($node = $this->parseJSXElement()) { |
| 117 |
return $node; |
| 118 |
} elseif ($startToken = $this->scanner->consume("{")) { |
| 119 |
$spread = $this->scanner->consume("..."); |
| 120 |
$exp = $this->parseAssignmentExpression(); |
| 121 |
$midPos = $this->scanner->getPosition(); |
| 122 |
if (($spread && !$exp) || !$this->scanner->consume("}")) { |
| 123 |
$this->error(); |
| 124 |
} |
| 125 |
$node = $this->createJSXNode( |
| 126 |
$spread ? "JSXSpreadChild" : "JSXExpressionContainer", |
| 127 |
$startToken |
| 128 |
); |
| 129 |
if (!$exp) { |
| 130 |
$exp = $this->createJSXNode("JSXEmptyExpression", $midPos); |
| 131 |
$this->completeNode($exp, $midPos); |
| 132 |
} |
| 133 |
$node->setExpression($exp); |
| 134 |
return $this->completeNode($node); |
| 135 |
} |
| 136 |
return null; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Parses a jsx text |
| 141 |
* |
| 142 |
* @return \Peast\Syntax\Node\JSX\JSXText|null |
| 143 |
*/ |
| 144 |
protected function parseJSXText() |
| 145 |
{ |
| 146 |
if (!($token = $this->scanner->reconsumeCurrentTokenAsJSXText())) { |
| 147 |
return null; |
| 148 |
} |
| 149 |
$this->scanner->consumeToken(); |
| 150 |
$node = $this->createJSXNode("JSXText", $token); |
| 151 |
$node->setRaw($token->value); |
| 152 |
return $this->completeNode($node, $token->location->end); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Parses a jsx element |
| 157 |
* |
| 158 |
* @return \Peast\Syntax\Node\JSX\JSXElement|null |
| 159 |
*/ |
| 160 |
protected function parseJSXElement() |
| 161 |
{ |
| 162 |
$startOpeningToken = $this->scanner->getToken(); |
| 163 |
if (!$startOpeningToken || $startOpeningToken->value !== "<") { |
| 164 |
return null; |
| 165 |
} |
| 166 |
|
| 167 |
$nextToken = $this->scanner->getNextToken(); |
| 168 |
if ($nextToken && $nextToken->value === "/") { |
| 169 |
return null; |
| 170 |
} |
| 171 |
|
| 172 |
$this->scanner->consumeToken(); |
| 173 |
|
| 174 |
if (!($name = $this->parseJSXIdentifierOrMemberExpression())) { |
| 175 |
$this->error(); |
| 176 |
} |
| 177 |
|
| 178 |
$attributes = $this->parseJSXAttributes(); |
| 179 |
|
| 180 |
$selfClosing = $this->scanner->consume("/"); |
| 181 |
|
| 182 |
$endOpeningToken = $this->scanner->reconsumeCurrentTokenInJSXMode(); |
| 183 |
if (!$endOpeningToken || $endOpeningToken->value !== ">") { |
| 184 |
$this->error(); |
| 185 |
} |
| 186 |
$this->scanner->consumeToken(); |
| 187 |
|
| 188 |
if (!$selfClosing) { |
| 189 |
|
| 190 |
$children = $this->parseJSXChildren(); |
| 191 |
|
| 192 |
if ( |
| 193 |
($startClosingToken = $this->scanner->consume("<")) && |
| 194 |
$this->scanner->consume("/") && |
| 195 |
($closingName = $this->parseJSXIdentifierOrMemberExpression()) && |
| 196 |
($endClosingToken = $this->scanner->reconsumeCurrentTokenInJSXMode()) && |
| 197 |
($endClosingToken->value === ">") |
| 198 |
) { |
| 199 |
$this->scanner->consumeToken(); |
| 200 |
if (!$this->isSameJSXElementName($name, $closingName)) { |
| 201 |
$this->error("Closing tag does not match opening tag"); |
| 202 |
} |
| 203 |
} else { |
| 204 |
$this->error(); |
| 205 |
} |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
//Opening tag |
| 210 |
$openingNode = $this->createJSXNode( |
| 211 |
"JSXOpeningElement", |
| 212 |
$startOpeningToken |
| 213 |
); |
| 214 |
$openingNode->setName($name); |
| 215 |
$openingNode->setSelfClosing($selfClosing); |
| 216 |
if ($attributes) { |
| 217 |
$openingNode->setAttributes($attributes); |
| 218 |
} |
| 219 |
$this->completeNode( |
| 220 |
$openingNode, |
| 221 |
$endOpeningToken->location->end |
| 222 |
); |
| 223 |
|
| 224 |
//Closing tag |
| 225 |
$closingNode = null; |
| 226 |
if (!$selfClosing) { |
| 227 |
$closingNode = $this->createJSXNode( |
| 228 |
"JSXClosingElement", |
| 229 |
$startClosingToken |
| 230 |
); |
| 231 |
$closingNode->setName($closingName); |
| 232 |
$this->completeNode($closingNode); |
| 233 |
} |
| 234 |
|
| 235 |
//Element |
| 236 |
$node = $this->createJSXNode("JSXElement", $startOpeningToken); |
| 237 |
$node->setOpeningElement($openingNode); |
| 238 |
if ($closingNode) { |
| 239 |
$node->setClosingElement($closingNode); |
| 240 |
if ($children) { |
| 241 |
$node->setChildren($children); |
| 242 |
} |
| 243 |
} |
| 244 |
return $this->completeNode($node); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Parses a jsx identifier, namespaced identifier or member expression |
| 249 |
* |
| 250 |
* @param bool $allowMember True to allow member expressions |
| 251 |
* |
| 252 |
* @return \Peast\Syntax\Node\Node|null |
| 253 |
*/ |
| 254 |
protected function parseJSXIdentifierOrMemberExpression($allowMember = true) |
| 255 |
{ |
| 256 |
$idToken = $this->scanner->reconsumeCurrentTokenInJSXMode(); |
| 257 |
if (!$idToken || $idToken->type !== Token::TYPE_JSX_IDENTIFIER) { |
| 258 |
return null; |
| 259 |
} |
| 260 |
$this->scanner->consumeToken(); |
| 261 |
|
| 262 |
$idNode = $this->createJSXNode("JSXIdentifier", $idToken); |
| 263 |
$idNode->setName($idToken->value); |
| 264 |
$idNode = $this->completeNode($idNode); |
| 265 |
|
| 266 |
//Namespaced identifier |
| 267 |
if ($this->scanner->consume(":")) { |
| 268 |
|
| 269 |
$idToken2 = $this->scanner->reconsumeCurrentTokenInJSXMode(); |
| 270 |
if (!$idToken2 || $idToken2->type !== Token::TYPE_JSX_IDENTIFIER) { |
| 271 |
$this->error(); |
| 272 |
} |
| 273 |
$this->scanner->consumeToken(); |
| 274 |
|
| 275 |
$idNode2 = $this->createJSXNode("JSXIdentifier", $idToken2); |
| 276 |
$idNode2->setName($idToken2->value); |
| 277 |
$idNode2 = $this->completeNode($idNode2); |
| 278 |
|
| 279 |
$node = $this->createJSXNode("JSXNamespacedName", $idToken); |
| 280 |
$node->setNamespace($idNode); |
| 281 |
$node->setName($idNode2); |
| 282 |
return $this->completeNode($node); |
| 283 |
|
| 284 |
} |
| 285 |
|
| 286 |
//Get following identifiers |
| 287 |
$nextIds = array(); |
| 288 |
if ($allowMember) { |
| 289 |
while ($this->scanner->consume(".")) { |
| 290 |
$nextId = $this->scanner->reconsumeCurrentTokenInJSXMode(); |
| 291 |
if (!$nextId || $nextId->type !== Token::TYPE_JSX_IDENTIFIER) { |
| 292 |
$this->error(); |
| 293 |
} |
| 294 |
$this->scanner->consumeToken(); |
| 295 |
$nextIds[] = $nextId; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
//Create the member expression if required |
| 300 |
$objectNode = $idNode; |
| 301 |
foreach ($nextIds as $nid) { |
| 302 |
$propEnd = $nid->location->end; |
| 303 |
$propNode = $this->createJSXNode("JSXIdentifier", $nid); |
| 304 |
$propNode->setName($nid->value); |
| 305 |
$propNode = $this->completeNode($propNode, $propEnd); |
| 306 |
|
| 307 |
$node = $this->createJSXNode("JSXMemberExpression", $objectNode); |
| 308 |
$node->setObject($objectNode); |
| 309 |
$node->setProperty($propNode); |
| 310 |
$objectNode = $this->completeNode($node, $propEnd); |
| 311 |
} |
| 312 |
|
| 313 |
return $objectNode; |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Parses a jsx attributes list |
| 318 |
* |
| 319 |
* @return \Peast\Syntax\Node\Node[]|null |
| 320 |
*/ |
| 321 |
protected function parseJSXAttributes() |
| 322 |
{ |
| 323 |
$attributes = array(); |
| 324 |
while ( |
| 325 |
($attr = $this->parseJSXSpreadAttribute()) || |
| 326 |
($attr = $this->parseJSXAttribute()) |
| 327 |
) { |
| 328 |
$attributes[] = $attr; |
| 329 |
} |
| 330 |
return count($attributes) ? $attributes : null; |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Parses a jsx spread attribute |
| 335 |
* |
| 336 |
* @return \Peast\Syntax\Node\JSX\JSXSpreadAttribute|null |
| 337 |
*/ |
| 338 |
protected function parseJSXSpreadAttribute() |
| 339 |
{ |
| 340 |
if (!($openToken = $this->scanner->consume("{"))) { |
| 341 |
return null; |
| 342 |
} |
| 343 |
|
| 344 |
if ( |
| 345 |
$this->scanner->consume("...") && |
| 346 |
($exp = $this->parseAssignmentExpression()) && |
| 347 |
$this->scanner->consume("}") |
| 348 |
) { |
| 349 |
$node = $this->createJSXNode("JSXSpreadAttribute", $openToken); |
| 350 |
$node->setArgument($exp); |
| 351 |
return $this->completeNode($node); |
| 352 |
} |
| 353 |
|
| 354 |
$this->error(); |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* Parses a jsx spread attribute |
| 359 |
* |
| 360 |
* @return \Peast\Syntax\Node\JSX\JSXSpreadAttribute|null |
| 361 |
*/ |
| 362 |
protected function parseJSXAttribute() |
| 363 |
{ |
| 364 |
if (!($name = $this->parseJSXIdentifierOrMemberExpression(false))) { |
| 365 |
return null; |
| 366 |
} |
| 367 |
|
| 368 |
$value = null; |
| 369 |
if ($this->scanner->consume("=")) { |
| 370 |
$strToken = $this->scanner->reconsumeCurrentTokenInJSXMode(); |
| 371 |
if ($strToken && $strToken->type === Token::TYPE_STRING_LITERAL) { |
| 372 |
$this->scanner->consumeToken(); |
| 373 |
$value = $this->createNode("StringLiteral", $strToken); |
| 374 |
$value->setRaw($strToken->value); |
| 375 |
$value = $this->completeNode($value); |
| 376 |
} elseif ($startExp = $this->scanner->consume("{")) { |
| 377 |
|
| 378 |
if ( |
| 379 |
($exp = $this->parseAssignmentExpression()) && |
| 380 |
$this->scanner->consume("}") |
| 381 |
) { |
| 382 |
|
| 383 |
$value = $this->createJSXNode( |
| 384 |
"JSXExpressionContainer", |
| 385 |
$startExp |
| 386 |
); |
| 387 |
$value->setExpression($exp); |
| 388 |
$value = $this->completeNode($value); |
| 389 |
|
| 390 |
} else { |
| 391 |
$this->error(); |
| 392 |
} |
| 393 |
|
| 394 |
} elseif ( |
| 395 |
!($value = $this->parseJSXFragment()) && |
| 396 |
!($value = $this->parseJSXElement()) |
| 397 |
) { |
| 398 |
$this->error(); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
$node = $this->createJSXNode("JSXAttribute", $name); |
| 403 |
$node->setName($name); |
| 404 |
if ($value) { |
| 405 |
$node->setValue($value); |
| 406 |
} |
| 407 |
return $this->completeNode($node); |
| 408 |
} |
| 409 |
|
| 410 |
/** |
| 411 |
* Checks that 2 tag names are equal |
| 412 |
* |
| 413 |
* @param \Peast\Syntax\Node\Node $n1 First name |
| 414 |
* @param \Peast\Syntax\Node\Node $n2 Second name |
| 415 |
* |
| 416 |
* @return bool |
| 417 |
*/ |
| 418 |
protected function isSameJSXElementName($n1, $n2) |
| 419 |
{ |
| 420 |
$type = $n1->getType(); |
| 421 |
if ($type !== $n2->getType()) { |
| 422 |
return false; |
| 423 |
} elseif ($type === "JSXNamespacedName") { |
| 424 |
return $this->isSameJSXElementName( |
| 425 |
$n1->getNamespace(), $n2->getNamespace() |
| 426 |
) && $this->isSameJSXElementName( |
| 427 |
$n1->getName(), $n2->getName() |
| 428 |
); |
| 429 |
} elseif ($type === "JSXMemberExpression") { |
| 430 |
return $this->isSameJSXElementName( |
| 431 |
$n1->getObject(), $n2->getObject() |
| 432 |
) && $this->isSameJSXElementName( |
| 433 |
$n1->getProperty(), $n2->getProperty() |
| 434 |
); |
| 435 |
} |
| 436 |
return $type === "JSXIdentifier" && $n1->getName() === $n2->getName(); |
| 437 |
} |
| 438 |
} |