@@ -23,35 +23,35 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class SelectorNode extends AbstractNode |
| 25 | 25 | { |
| 26 | - private $tree; |
|
| 27 | - private $pseudoElement; |
|
| 28 | - |
|
| 29 | - public function __construct(NodeInterface $tree, string $pseudoElement = null) |
|
| 30 | - { |
|
| 31 | - $this->tree = $tree; |
|
| 32 | - $this->pseudoElement = $pseudoElement ? strtolower($pseudoElement) : null; |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - public function getTree(): NodeInterface |
|
| 36 | - { |
|
| 37 | - return $this->tree; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - public function getPseudoElement(): ?string |
|
| 41 | - { |
|
| 42 | - return $this->pseudoElement; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * {@inheritdoc} |
|
| 47 | - */ |
|
| 48 | - public function getSpecificity(): Specificity |
|
| 49 | - { |
|
| 50 | - return $this->tree->getSpecificity()->plus(new Specificity(0, 0, $this->pseudoElement ? 1 : 0)); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - public function __toString(): string |
|
| 54 | - { |
|
| 55 | - return sprintf('%s[%s%s]', $this->getNodeName(), $this->tree, $this->pseudoElement ? '::'.$this->pseudoElement : ''); |
|
| 56 | - } |
|
| 26 | + private $tree; |
|
| 27 | + private $pseudoElement; |
|
| 28 | + |
|
| 29 | + public function __construct(NodeInterface $tree, string $pseudoElement = null) |
|
| 30 | + { |
|
| 31 | + $this->tree = $tree; |
|
| 32 | + $this->pseudoElement = $pseudoElement ? strtolower($pseudoElement) : null; |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + public function getTree(): NodeInterface |
|
| 36 | + { |
|
| 37 | + return $this->tree; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + public function getPseudoElement(): ?string |
|
| 41 | + { |
|
| 42 | + return $this->pseudoElement; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * {@inheritdoc} |
|
| 47 | + */ |
|
| 48 | + public function getSpecificity(): Specificity |
|
| 49 | + { |
|
| 50 | + return $this->tree->getSpecificity()->plus(new Specificity(0, 0, $this->pseudoElement ? 1 : 0)); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + public function __toString(): string |
|
| 54 | + { |
|
| 55 | + return sprintf('%s[%s%s]', $this->getNodeName(), $this->tree, $this->pseudoElement ? '::'.$this->pseudoElement : ''); |
|
| 56 | + } |
|
| 57 | 57 | } |
@@ -23,35 +23,35 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class HashNode extends AbstractNode |
| 25 | 25 | { |
| 26 | - private $selector; |
|
| 27 | - private $id; |
|
| 28 | - |
|
| 29 | - public function __construct(NodeInterface $selector, string $id) |
|
| 30 | - { |
|
| 31 | - $this->selector = $selector; |
|
| 32 | - $this->id = $id; |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - public function getSelector(): NodeInterface |
|
| 36 | - { |
|
| 37 | - return $this->selector; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - public function getId(): string |
|
| 41 | - { |
|
| 42 | - return $this->id; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * {@inheritdoc} |
|
| 47 | - */ |
|
| 48 | - public function getSpecificity(): Specificity |
|
| 49 | - { |
|
| 50 | - return $this->selector->getSpecificity()->plus(new Specificity(1, 0, 0)); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - public function __toString(): string |
|
| 54 | - { |
|
| 55 | - return sprintf('%s[%s#%s]', $this->getNodeName(), $this->selector, $this->id); |
|
| 56 | - } |
|
| 26 | + private $selector; |
|
| 27 | + private $id; |
|
| 28 | + |
|
| 29 | + public function __construct(NodeInterface $selector, string $id) |
|
| 30 | + { |
|
| 31 | + $this->selector = $selector; |
|
| 32 | + $this->id = $id; |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + public function getSelector(): NodeInterface |
|
| 36 | + { |
|
| 37 | + return $this->selector; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + public function getId(): string |
|
| 41 | + { |
|
| 42 | + return $this->id; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * {@inheritdoc} |
|
| 47 | + */ |
|
| 48 | + public function getSpecificity(): Specificity |
|
| 49 | + { |
|
| 50 | + return $this->selector->getSpecificity()->plus(new Specificity(1, 0, 0)); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + public function __toString(): string |
|
| 54 | + { |
|
| 55 | + return sprintf('%s[%s#%s]', $this->getNodeName(), $this->selector, $this->id); |
|
| 56 | + } |
|
| 57 | 57 | } |
@@ -23,43 +23,43 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class SyntaxErrorException extends ParseException |
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * @return self |
|
| 28 | - */ |
|
| 29 | - public static function unexpectedToken(string $expectedValue, Token $foundToken) |
|
| 30 | - { |
|
| 31 | - return new self(sprintf('Expected %s, but %s found.', $expectedValue, $foundToken)); |
|
| 32 | - } |
|
| 26 | + /** |
|
| 27 | + * @return self |
|
| 28 | + */ |
|
| 29 | + public static function unexpectedToken(string $expectedValue, Token $foundToken) |
|
| 30 | + { |
|
| 31 | + return new self(sprintf('Expected %s, but %s found.', $expectedValue, $foundToken)); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @return self |
|
| 36 | - */ |
|
| 37 | - public static function pseudoElementFound(string $pseudoElement, string $unexpectedLocation) |
|
| 38 | - { |
|
| 39 | - return new self(sprintf('Unexpected pseudo-element "::%s" found %s.', $pseudoElement, $unexpectedLocation)); |
|
| 40 | - } |
|
| 34 | + /** |
|
| 35 | + * @return self |
|
| 36 | + */ |
|
| 37 | + public static function pseudoElementFound(string $pseudoElement, string $unexpectedLocation) |
|
| 38 | + { |
|
| 39 | + return new self(sprintf('Unexpected pseudo-element "::%s" found %s.', $pseudoElement, $unexpectedLocation)); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * @return self |
|
| 44 | - */ |
|
| 45 | - public static function unclosedString(int $position) |
|
| 46 | - { |
|
| 47 | - return new self(sprintf('Unclosed/invalid string at %s.', $position)); |
|
| 48 | - } |
|
| 42 | + /** |
|
| 43 | + * @return self |
|
| 44 | + */ |
|
| 45 | + public static function unclosedString(int $position) |
|
| 46 | + { |
|
| 47 | + return new self(sprintf('Unclosed/invalid string at %s.', $position)); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @return self |
|
| 52 | - */ |
|
| 53 | - public static function nestedNot() |
|
| 54 | - { |
|
| 55 | - return new self('Got nested ::not().'); |
|
| 56 | - } |
|
| 50 | + /** |
|
| 51 | + * @return self |
|
| 52 | + */ |
|
| 53 | + public static function nestedNot() |
|
| 54 | + { |
|
| 55 | + return new self('Got nested ::not().'); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * @return self |
|
| 60 | - */ |
|
| 61 | - public static function stringAsFunctionArgument() |
|
| 62 | - { |
|
| 63 | - return new self('String not allowed as function argument.'); |
|
| 64 | - } |
|
| 58 | + /** |
|
| 59 | + * @return self |
|
| 60 | + */ |
|
| 61 | + public static function stringAsFunctionArgument() |
|
| 62 | + { |
|
| 63 | + return new self('String not allowed as function argument.'); |
|
| 64 | + } |
|
| 65 | 65 | } |
@@ -23,64 +23,64 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class Reader |
| 25 | 25 | { |
| 26 | - private $source; |
|
| 27 | - private $length; |
|
| 28 | - private $position = 0; |
|
| 26 | + private $source; |
|
| 27 | + private $length; |
|
| 28 | + private $position = 0; |
|
| 29 | 29 | |
| 30 | - public function __construct(string $source) |
|
| 31 | - { |
|
| 32 | - $this->source = $source; |
|
| 33 | - $this->length = \strlen($source); |
|
| 34 | - } |
|
| 30 | + public function __construct(string $source) |
|
| 31 | + { |
|
| 32 | + $this->source = $source; |
|
| 33 | + $this->length = \strlen($source); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - public function isEOF(): bool |
|
| 37 | - { |
|
| 38 | - return $this->position >= $this->length; |
|
| 39 | - } |
|
| 36 | + public function isEOF(): bool |
|
| 37 | + { |
|
| 38 | + return $this->position >= $this->length; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - public function getPosition(): int |
|
| 42 | - { |
|
| 43 | - return $this->position; |
|
| 44 | - } |
|
| 41 | + public function getPosition(): int |
|
| 42 | + { |
|
| 43 | + return $this->position; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - public function getRemainingLength(): int |
|
| 47 | - { |
|
| 48 | - return $this->length - $this->position; |
|
| 49 | - } |
|
| 46 | + public function getRemainingLength(): int |
|
| 47 | + { |
|
| 48 | + return $this->length - $this->position; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - public function getSubstring(int $length, int $offset = 0): string |
|
| 52 | - { |
|
| 53 | - return substr($this->source, $this->position + $offset, $length); |
|
| 54 | - } |
|
| 51 | + public function getSubstring(int $length, int $offset = 0): string |
|
| 52 | + { |
|
| 53 | + return substr($this->source, $this->position + $offset, $length); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - public function getOffset(string $string) |
|
| 57 | - { |
|
| 58 | - $position = strpos($this->source, $string, $this->position); |
|
| 56 | + public function getOffset(string $string) |
|
| 57 | + { |
|
| 58 | + $position = strpos($this->source, $string, $this->position); |
|
| 59 | 59 | |
| 60 | - return false === $position ? false : $position - $this->position; |
|
| 61 | - } |
|
| 60 | + return false === $position ? false : $position - $this->position; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * @return array|false |
|
| 65 | - */ |
|
| 66 | - public function findPattern(string $pattern) |
|
| 67 | - { |
|
| 68 | - $source = substr($this->source, $this->position); |
|
| 63 | + /** |
|
| 64 | + * @return array|false |
|
| 65 | + */ |
|
| 66 | + public function findPattern(string $pattern) |
|
| 67 | + { |
|
| 68 | + $source = substr($this->source, $this->position); |
|
| 69 | 69 | |
| 70 | - if (preg_match($pattern, $source, $matches)) { |
|
| 71 | - return $matches; |
|
| 72 | - } |
|
| 70 | + if (preg_match($pattern, $source, $matches)) { |
|
| 71 | + return $matches; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - return false; |
|
| 75 | - } |
|
| 74 | + return false; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - public function moveForward(int $length) |
|
| 78 | - { |
|
| 79 | - $this->position += $length; |
|
| 80 | - } |
|
| 77 | + public function moveForward(int $length) |
|
| 78 | + { |
|
| 79 | + $this->position += $length; |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - public function moveToEnd() |
|
| 83 | - { |
|
| 84 | - $this->position = $this->length; |
|
| 85 | - } |
|
| 82 | + public function moveToEnd() |
|
| 83 | + { |
|
| 84 | + $this->position = $this->length; |
|
| 85 | + } |
|
| 86 | 86 | } |
@@ -28,27 +28,27 @@ |
||
| 28 | 28 | */ |
| 29 | 29 | class NumberHandler implements HandlerInterface |
| 30 | 30 | { |
| 31 | - private $patterns; |
|
| 31 | + private $patterns; |
|
| 32 | 32 | |
| 33 | - public function __construct(TokenizerPatterns $patterns) |
|
| 34 | - { |
|
| 35 | - $this->patterns = $patterns; |
|
| 36 | - } |
|
| 33 | + public function __construct(TokenizerPatterns $patterns) |
|
| 34 | + { |
|
| 35 | + $this->patterns = $patterns; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * {@inheritdoc} |
|
| 40 | - */ |
|
| 41 | - public function handle(Reader $reader, TokenStream $stream): bool |
|
| 42 | - { |
|
| 43 | - $match = $reader->findPattern($this->patterns->getNumberPattern()); |
|
| 38 | + /** |
|
| 39 | + * {@inheritdoc} |
|
| 40 | + */ |
|
| 41 | + public function handle(Reader $reader, TokenStream $stream): bool |
|
| 42 | + { |
|
| 43 | + $match = $reader->findPattern($this->patterns->getNumberPattern()); |
|
| 44 | 44 | |
| 45 | - if (!$match) { |
|
| 46 | - return false; |
|
| 47 | - } |
|
| 45 | + if (!$match) { |
|
| 46 | + return false; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - $stream->push(new Token(Token::TYPE_NUMBER, $match[0], $reader->getPosition())); |
|
| 50 | - $reader->moveForward(\strlen($match[0])); |
|
| 49 | + $stream->push(new Token(Token::TYPE_NUMBER, $match[0], $reader->getPosition())); |
|
| 50 | + $reader->moveForward(\strlen($match[0])); |
|
| 51 | 51 | |
| 52 | - return true; |
|
| 53 | - } |
|
| 52 | + return true; |
|
| 53 | + } |
|
| 54 | 54 | } |
@@ -29,30 +29,30 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | class HashHandler implements HandlerInterface |
| 31 | 31 | { |
| 32 | - private $patterns; |
|
| 33 | - private $escaping; |
|
| 34 | - |
|
| 35 | - public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping) |
|
| 36 | - { |
|
| 37 | - $this->patterns = $patterns; |
|
| 38 | - $this->escaping = $escaping; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * {@inheritdoc} |
|
| 43 | - */ |
|
| 44 | - public function handle(Reader $reader, TokenStream $stream): bool |
|
| 45 | - { |
|
| 46 | - $match = $reader->findPattern($this->patterns->getHashPattern()); |
|
| 47 | - |
|
| 48 | - if (!$match) { |
|
| 49 | - return false; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - $value = $this->escaping->escapeUnicode($match[1]); |
|
| 53 | - $stream->push(new Token(Token::TYPE_HASH, $value, $reader->getPosition())); |
|
| 54 | - $reader->moveForward(\strlen($match[0])); |
|
| 55 | - |
|
| 56 | - return true; |
|
| 57 | - } |
|
| 32 | + private $patterns; |
|
| 33 | + private $escaping; |
|
| 34 | + |
|
| 35 | + public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping) |
|
| 36 | + { |
|
| 37 | + $this->patterns = $patterns; |
|
| 38 | + $this->escaping = $escaping; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * {@inheritdoc} |
|
| 43 | + */ |
|
| 44 | + public function handle(Reader $reader, TokenStream $stream): bool |
|
| 45 | + { |
|
| 46 | + $match = $reader->findPattern($this->patterns->getHashPattern()); |
|
| 47 | + |
|
| 48 | + if (!$match) { |
|
| 49 | + return false; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + $value = $this->escaping->escapeUnicode($match[1]); |
|
| 53 | + $stream->push(new Token(Token::TYPE_HASH, $value, $reader->getPosition())); |
|
| 54 | + $reader->moveForward(\strlen($match[0])); |
|
| 55 | + |
|
| 56 | + return true; |
|
| 57 | + } |
|
| 58 | 58 | } |
@@ -31,47 +31,47 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | class StringHandler implements HandlerInterface |
| 33 | 33 | { |
| 34 | - private $patterns; |
|
| 35 | - private $escaping; |
|
| 34 | + private $patterns; |
|
| 35 | + private $escaping; |
|
| 36 | 36 | |
| 37 | - public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping) |
|
| 38 | - { |
|
| 39 | - $this->patterns = $patterns; |
|
| 40 | - $this->escaping = $escaping; |
|
| 41 | - } |
|
| 37 | + public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping) |
|
| 38 | + { |
|
| 39 | + $this->patterns = $patterns; |
|
| 40 | + $this->escaping = $escaping; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * {@inheritdoc} |
|
| 45 | - */ |
|
| 46 | - public function handle(Reader $reader, TokenStream $stream): bool |
|
| 47 | - { |
|
| 48 | - $quote = $reader->getSubstring(1); |
|
| 43 | + /** |
|
| 44 | + * {@inheritdoc} |
|
| 45 | + */ |
|
| 46 | + public function handle(Reader $reader, TokenStream $stream): bool |
|
| 47 | + { |
|
| 48 | + $quote = $reader->getSubstring(1); |
|
| 49 | 49 | |
| 50 | - if (!\in_array($quote, ["'", '"'])) { |
|
| 51 | - return false; |
|
| 52 | - } |
|
| 50 | + if (!\in_array($quote, ["'", '"'])) { |
|
| 51 | + return false; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - $reader->moveForward(1); |
|
| 55 | - $match = $reader->findPattern($this->patterns->getQuotedStringPattern($quote)); |
|
| 54 | + $reader->moveForward(1); |
|
| 55 | + $match = $reader->findPattern($this->patterns->getQuotedStringPattern($quote)); |
|
| 56 | 56 | |
| 57 | - if (!$match) { |
|
| 58 | - throw new InternalErrorException(sprintf('Should have found at least an empty match at %d.', $reader->getPosition())); |
|
| 59 | - } |
|
| 57 | + if (!$match) { |
|
| 58 | + throw new InternalErrorException(sprintf('Should have found at least an empty match at %d.', $reader->getPosition())); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - // check unclosed strings |
|
| 62 | - if (\strlen($match[0]) === $reader->getRemainingLength()) { |
|
| 63 | - throw SyntaxErrorException::unclosedString($reader->getPosition() - 1); |
|
| 64 | - } |
|
| 61 | + // check unclosed strings |
|
| 62 | + if (\strlen($match[0]) === $reader->getRemainingLength()) { |
|
| 63 | + throw SyntaxErrorException::unclosedString($reader->getPosition() - 1); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - // check quotes pairs validity |
|
| 67 | - if ($quote !== $reader->getSubstring(1, \strlen($match[0]))) { |
|
| 68 | - throw SyntaxErrorException::unclosedString($reader->getPosition() - 1); |
|
| 69 | - } |
|
| 66 | + // check quotes pairs validity |
|
| 67 | + if ($quote !== $reader->getSubstring(1, \strlen($match[0]))) { |
|
| 68 | + throw SyntaxErrorException::unclosedString($reader->getPosition() - 1); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - $string = $this->escaping->escapeUnicodeAndNewLine($match[0]); |
|
| 72 | - $stream->push(new Token(Token::TYPE_STRING, $string, $reader->getPosition())); |
|
| 73 | - $reader->moveForward(\strlen($match[0]) + 1); |
|
| 71 | + $string = $this->escaping->escapeUnicodeAndNewLine($match[0]); |
|
| 72 | + $stream->push(new Token(Token::TYPE_STRING, $string, $reader->getPosition())); |
|
| 73 | + $reader->moveForward(\strlen($match[0]) + 1); |
|
| 74 | 74 | |
| 75 | - return true; |
|
| 76 | - } |
|
| 75 | + return true; |
|
| 76 | + } |
|
| 77 | 77 | } |
@@ -29,30 +29,30 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | class IdentifierHandler implements HandlerInterface |
| 31 | 31 | { |
| 32 | - private $patterns; |
|
| 33 | - private $escaping; |
|
| 34 | - |
|
| 35 | - public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping) |
|
| 36 | - { |
|
| 37 | - $this->patterns = $patterns; |
|
| 38 | - $this->escaping = $escaping; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * {@inheritdoc} |
|
| 43 | - */ |
|
| 44 | - public function handle(Reader $reader, TokenStream $stream): bool |
|
| 45 | - { |
|
| 46 | - $match = $reader->findPattern($this->patterns->getIdentifierPattern()); |
|
| 47 | - |
|
| 48 | - if (!$match) { |
|
| 49 | - return false; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - $value = $this->escaping->escapeUnicode($match[0]); |
|
| 53 | - $stream->push(new Token(Token::TYPE_IDENTIFIER, $value, $reader->getPosition())); |
|
| 54 | - $reader->moveForward(\strlen($match[0])); |
|
| 55 | - |
|
| 56 | - return true; |
|
| 57 | - } |
|
| 32 | + private $patterns; |
|
| 33 | + private $escaping; |
|
| 34 | + |
|
| 35 | + public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping) |
|
| 36 | + { |
|
| 37 | + $this->patterns = $patterns; |
|
| 38 | + $this->escaping = $escaping; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * {@inheritdoc} |
|
| 43 | + */ |
|
| 44 | + public function handle(Reader $reader, TokenStream $stream): bool |
|
| 45 | + { |
|
| 46 | + $match = $reader->findPattern($this->patterns->getIdentifierPattern()); |
|
| 47 | + |
|
| 48 | + if (!$match) { |
|
| 49 | + return false; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + $value = $this->escaping->escapeUnicode($match[0]); |
|
| 53 | + $stream->push(new Token(Token::TYPE_IDENTIFIER, $value, $reader->getPosition())); |
|
| 54 | + $reader->moveForward(\strlen($match[0])); |
|
| 55 | + |
|
| 56 | + return true; |
|
| 57 | + } |
|
| 58 | 58 | } |
@@ -26,142 +26,142 @@ |
||
| 26 | 26 | */ |
| 27 | 27 | class TokenStream |
| 28 | 28 | { |
| 29 | - /** |
|
| 30 | - * @var Token[] |
|
| 31 | - */ |
|
| 32 | - private $tokens = []; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @var Token[] |
|
| 36 | - */ |
|
| 37 | - private $used = []; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @var int |
|
| 41 | - */ |
|
| 42 | - private $cursor = 0; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @var Token|null |
|
| 46 | - */ |
|
| 47 | - private $peeked; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @var bool |
|
| 51 | - */ |
|
| 52 | - private $peeking = false; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Pushes a token. |
|
| 56 | - * |
|
| 57 | - * @return $this |
|
| 58 | - */ |
|
| 59 | - public function push(Token $token): self |
|
| 60 | - { |
|
| 61 | - $this->tokens[] = $token; |
|
| 62 | - |
|
| 63 | - return $this; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Freezes stream. |
|
| 68 | - * |
|
| 69 | - * @return $this |
|
| 70 | - */ |
|
| 71 | - public function freeze(): self |
|
| 72 | - { |
|
| 73 | - return $this; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Returns next token. |
|
| 78 | - * |
|
| 79 | - * @throws InternalErrorException If there is no more token |
|
| 80 | - */ |
|
| 81 | - public function getNext(): Token |
|
| 82 | - { |
|
| 83 | - if ($this->peeking) { |
|
| 84 | - $this->peeking = false; |
|
| 85 | - $this->used[] = $this->peeked; |
|
| 86 | - |
|
| 87 | - return $this->peeked; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - if (!isset($this->tokens[$this->cursor])) { |
|
| 91 | - throw new InternalErrorException('Unexpected token stream end.'); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - return $this->tokens[$this->cursor++]; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Returns peeked token. |
|
| 99 | - */ |
|
| 100 | - public function getPeek(): Token |
|
| 101 | - { |
|
| 102 | - if (!$this->peeking) { |
|
| 103 | - $this->peeked = $this->getNext(); |
|
| 104 | - $this->peeking = true; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - return $this->peeked; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Returns used tokens. |
|
| 112 | - * |
|
| 113 | - * @return Token[] |
|
| 114 | - */ |
|
| 115 | - public function getUsed(): array |
|
| 116 | - { |
|
| 117 | - return $this->used; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Returns next identifier token. |
|
| 122 | - * |
|
| 123 | - * @throws SyntaxErrorException If next token is not an identifier |
|
| 124 | - */ |
|
| 125 | - public function getNextIdentifier(): string |
|
| 126 | - { |
|
| 127 | - $next = $this->getNext(); |
|
| 128 | - |
|
| 129 | - if (!$next->isIdentifier()) { |
|
| 130 | - throw SyntaxErrorException::unexpectedToken('identifier', $next); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - return $next->getValue(); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Returns next identifier or null if star delimiter token is found. |
|
| 138 | - * |
|
| 139 | - * @throws SyntaxErrorException If next token is not an identifier or a star delimiter |
|
| 140 | - */ |
|
| 141 | - public function getNextIdentifierOrStar(): ?string |
|
| 142 | - { |
|
| 143 | - $next = $this->getNext(); |
|
| 144 | - |
|
| 145 | - if ($next->isIdentifier()) { |
|
| 146 | - return $next->getValue(); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - if ($next->isDelimiter(['*'])) { |
|
| 150 | - return null; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - throw SyntaxErrorException::unexpectedToken('identifier or "*"', $next); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Skips next whitespace if any. |
|
| 158 | - */ |
|
| 159 | - public function skipWhitespace() |
|
| 160 | - { |
|
| 161 | - $peek = $this->getPeek(); |
|
| 162 | - |
|
| 163 | - if ($peek->isWhitespace()) { |
|
| 164 | - $this->getNext(); |
|
| 165 | - } |
|
| 166 | - } |
|
| 29 | + /** |
|
| 30 | + * @var Token[] |
|
| 31 | + */ |
|
| 32 | + private $tokens = []; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @var Token[] |
|
| 36 | + */ |
|
| 37 | + private $used = []; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @var int |
|
| 41 | + */ |
|
| 42 | + private $cursor = 0; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @var Token|null |
|
| 46 | + */ |
|
| 47 | + private $peeked; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @var bool |
|
| 51 | + */ |
|
| 52 | + private $peeking = false; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Pushes a token. |
|
| 56 | + * |
|
| 57 | + * @return $this |
|
| 58 | + */ |
|
| 59 | + public function push(Token $token): self |
|
| 60 | + { |
|
| 61 | + $this->tokens[] = $token; |
|
| 62 | + |
|
| 63 | + return $this; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Freezes stream. |
|
| 68 | + * |
|
| 69 | + * @return $this |
|
| 70 | + */ |
|
| 71 | + public function freeze(): self |
|
| 72 | + { |
|
| 73 | + return $this; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Returns next token. |
|
| 78 | + * |
|
| 79 | + * @throws InternalErrorException If there is no more token |
|
| 80 | + */ |
|
| 81 | + public function getNext(): Token |
|
| 82 | + { |
|
| 83 | + if ($this->peeking) { |
|
| 84 | + $this->peeking = false; |
|
| 85 | + $this->used[] = $this->peeked; |
|
| 86 | + |
|
| 87 | + return $this->peeked; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + if (!isset($this->tokens[$this->cursor])) { |
|
| 91 | + throw new InternalErrorException('Unexpected token stream end.'); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + return $this->tokens[$this->cursor++]; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Returns peeked token. |
|
| 99 | + */ |
|
| 100 | + public function getPeek(): Token |
|
| 101 | + { |
|
| 102 | + if (!$this->peeking) { |
|
| 103 | + $this->peeked = $this->getNext(); |
|
| 104 | + $this->peeking = true; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + return $this->peeked; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Returns used tokens. |
|
| 112 | + * |
|
| 113 | + * @return Token[] |
|
| 114 | + */ |
|
| 115 | + public function getUsed(): array |
|
| 116 | + { |
|
| 117 | + return $this->used; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Returns next identifier token. |
|
| 122 | + * |
|
| 123 | + * @throws SyntaxErrorException If next token is not an identifier |
|
| 124 | + */ |
|
| 125 | + public function getNextIdentifier(): string |
|
| 126 | + { |
|
| 127 | + $next = $this->getNext(); |
|
| 128 | + |
|
| 129 | + if (!$next->isIdentifier()) { |
|
| 130 | + throw SyntaxErrorException::unexpectedToken('identifier', $next); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + return $next->getValue(); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Returns next identifier or null if star delimiter token is found. |
|
| 138 | + * |
|
| 139 | + * @throws SyntaxErrorException If next token is not an identifier or a star delimiter |
|
| 140 | + */ |
|
| 141 | + public function getNextIdentifierOrStar(): ?string |
|
| 142 | + { |
|
| 143 | + $next = $this->getNext(); |
|
| 144 | + |
|
| 145 | + if ($next->isIdentifier()) { |
|
| 146 | + return $next->getValue(); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + if ($next->isDelimiter(['*'])) { |
|
| 150 | + return null; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + throw SyntaxErrorException::unexpectedToken('identifier or "*"', $next); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Skips next whitespace if any. |
|
| 158 | + */ |
|
| 159 | + public function skipWhitespace() |
|
| 160 | + { |
|
| 161 | + $peek = $this->getPeek(); |
|
| 162 | + |
|
| 163 | + if ($peek->isWhitespace()) { |
|
| 164 | + $this->getNext(); |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | 167 | } |