| 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 scanner trait |
| 16 |
* |
| 17 |
* @author Marco Marchiò <[email protected]> |
| 18 |
*/ |
| 19 |
trait Scanner |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Tries to reconsume the current token as a jsx text if possible |
| 23 |
* |
| 24 |
* @return Token|null |
| 25 |
*/ |
| 26 |
public function reconsumeCurrentTokenAsJSXText() |
| 27 |
{ |
| 28 |
//Current and next tokens must be reset and the open brackets count must be correct |
| 29 |
//like they were never scanned |
| 30 |
foreach (array($this->currentToken, $this->nextToken) as $token) { |
| 31 |
if ($token && isset($this->brackets[$token->value])) { |
| 32 |
if ($refBracket = $this->brackets[$token->value]) { |
| 33 |
$this->openBrackets[$refBracket]++; |
| 34 |
} else { |
| 35 |
$this->openBrackets[$token->value]--; |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
$this->nextToken = null; |
| 40 |
$this->currentToken = null; |
| 41 |
|
| 42 |
$startPosition = $this->getPosition(); |
| 43 |
$this->setScanPosition($startPosition); |
| 44 |
$result = $this->consumeUntil(array("{", "<"), false, false); |
| 45 |
if ($result) { |
| 46 |
$this->currentToken = new Token(Token::TYPE_JSX_TEXT, $result[0]); |
| 47 |
$this->currentToken->location->start = $startPosition; |
| 48 |
$this->currentToken->location->end = $this->getPosition(true); |
| 49 |
} |
| 50 |
return $this->currentToken; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* Reconsumes the current token in jsx mode |
| 56 |
* |
| 57 |
* @return Token|null |
| 58 |
*/ |
| 59 |
public function reconsumeCurrentTokenInJSXMode() |
| 60 |
{ |
| 61 |
$this->jsx = true; |
| 62 |
$this->nextToken = null; |
| 63 |
$this->currentToken = null; |
| 64 |
$startPosition = $this->getPosition(); |
| 65 |
$this->setScanPosition($startPosition); |
| 66 |
$token = $this->getToken(); |
| 67 |
$this->jsx = false; |
| 68 |
return $token; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* String scanning method in jsx mode |
| 73 |
* |
| 74 |
* @return Token|null |
| 75 |
*/ |
| 76 |
public function scanJSXString() |
| 77 |
{ |
| 78 |
return $this->scanString(false); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* String punctuator method in jsx mode |
| 83 |
* |
| 84 |
* @return Token|null |
| 85 |
*/ |
| 86 |
public function scanJSXPunctuator() |
| 87 |
{ |
| 88 |
//The ">" character in jsx mode must be emitted in its own token |
| 89 |
//without matching longer sequences like ">>" |
| 90 |
$char = $this->charAt(); |
| 91 |
if ($char === ">") { |
| 92 |
$this->index++; |
| 93 |
$this->column++; |
| 94 |
return new Token(Token::TYPE_PUNCTUATOR, $char); |
| 95 |
} |
| 96 |
return $this->scanPunctuator(); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Identifier scanning method in jsx mode |
| 101 |
* |
| 102 |
* @return Token|null |
| 103 |
*/ |
| 104 |
public function scanJSXIdentifier() |
| 105 |
{ |
| 106 |
$buffer = ""; |
| 107 |
$char = $this->charAt(); |
| 108 |
if ($char !== null && $this->isIdentifierChar($char)) { |
| 109 |
|
| 110 |
do { |
| 111 |
$buffer .= $char; |
| 112 |
$this->index++; |
| 113 |
$this->column++; |
| 114 |
$char = $this->charAt(); |
| 115 |
} while ( |
| 116 |
$char !== null && |
| 117 |
($this->isIdentifierChar($char, false) || $char === "-") |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
return $buffer === "" ? null : new Token(Token::TYPE_JSX_IDENTIFIER, $buffer); |
| 122 |
} |
| 123 |
} |