@@ -23,60 +23,60 @@ |
||
23 | 23 | */ |
24 | 24 | class AttributeNode extends AbstractNode |
25 | 25 | { |
26 | - private $selector; |
|
27 | - private $namespace; |
|
28 | - private $attribute; |
|
29 | - private $operator; |
|
30 | - private $value; |
|
26 | + private $selector; |
|
27 | + private $namespace; |
|
28 | + private $attribute; |
|
29 | + private $operator; |
|
30 | + private $value; |
|
31 | 31 | |
32 | - public function __construct(NodeInterface $selector, ?string $namespace, string $attribute, string $operator, ?string $value) |
|
33 | - { |
|
34 | - $this->selector = $selector; |
|
35 | - $this->namespace = $namespace; |
|
36 | - $this->attribute = $attribute; |
|
37 | - $this->operator = $operator; |
|
38 | - $this->value = $value; |
|
39 | - } |
|
32 | + public function __construct(NodeInterface $selector, ?string $namespace, string $attribute, string $operator, ?string $value) |
|
33 | + { |
|
34 | + $this->selector = $selector; |
|
35 | + $this->namespace = $namespace; |
|
36 | + $this->attribute = $attribute; |
|
37 | + $this->operator = $operator; |
|
38 | + $this->value = $value; |
|
39 | + } |
|
40 | 40 | |
41 | - public function getSelector(): NodeInterface |
|
42 | - { |
|
43 | - return $this->selector; |
|
44 | - } |
|
41 | + public function getSelector(): NodeInterface |
|
42 | + { |
|
43 | + return $this->selector; |
|
44 | + } |
|
45 | 45 | |
46 | - public function getNamespace(): ?string |
|
47 | - { |
|
48 | - return $this->namespace; |
|
49 | - } |
|
46 | + public function getNamespace(): ?string |
|
47 | + { |
|
48 | + return $this->namespace; |
|
49 | + } |
|
50 | 50 | |
51 | - public function getAttribute(): string |
|
52 | - { |
|
53 | - return $this->attribute; |
|
54 | - } |
|
51 | + public function getAttribute(): string |
|
52 | + { |
|
53 | + return $this->attribute; |
|
54 | + } |
|
55 | 55 | |
56 | - public function getOperator(): string |
|
57 | - { |
|
58 | - return $this->operator; |
|
59 | - } |
|
56 | + public function getOperator(): string |
|
57 | + { |
|
58 | + return $this->operator; |
|
59 | + } |
|
60 | 60 | |
61 | - public function getValue(): ?string |
|
62 | - { |
|
63 | - return $this->value; |
|
64 | - } |
|
61 | + public function getValue(): ?string |
|
62 | + { |
|
63 | + return $this->value; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * {@inheritdoc} |
|
68 | - */ |
|
69 | - public function getSpecificity(): Specificity |
|
70 | - { |
|
71 | - return $this->selector->getSpecificity()->plus(new Specificity(0, 1, 0)); |
|
72 | - } |
|
66 | + /** |
|
67 | + * {@inheritdoc} |
|
68 | + */ |
|
69 | + public function getSpecificity(): Specificity |
|
70 | + { |
|
71 | + return $this->selector->getSpecificity()->plus(new Specificity(0, 1, 0)); |
|
72 | + } |
|
73 | 73 | |
74 | - public function __toString(): string |
|
75 | - { |
|
76 | - $attribute = $this->namespace ? $this->namespace.'|'.$this->attribute : $this->attribute; |
|
74 | + public function __toString(): string |
|
75 | + { |
|
76 | + $attribute = $this->namespace ? $this->namespace.'|'.$this->attribute : $this->attribute; |
|
77 | 77 | |
78 | - return 'exists' === $this->operator |
|
79 | - ? sprintf('%s[%s[%s]]', $this->getNodeName(), $this->selector, $attribute) |
|
80 | - : sprintf("%s[%s[%s %s '%s']]", $this->getNodeName(), $this->selector, $attribute, $this->operator, $this->value); |
|
81 | - } |
|
78 | + return 'exists' === $this->operator |
|
79 | + ? sprintf('%s[%s[%s]]', $this->getNodeName(), $this->selector, $attribute) |
|
80 | + : sprintf("%s[%s[%s %s '%s']]", $this->getNodeName(), $this->selector, $attribute, $this->operator, $this->value); |
|
81 | + } |
|
82 | 82 | } |
@@ -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 | } |
@@ -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 | } |
@@ -23,89 +23,89 @@ |
||
23 | 23 | */ |
24 | 24 | class Token |
25 | 25 | { |
26 | - public const TYPE_FILE_END = 'eof'; |
|
27 | - public const TYPE_DELIMITER = 'delimiter'; |
|
28 | - public const TYPE_WHITESPACE = 'whitespace'; |
|
29 | - public const TYPE_IDENTIFIER = 'identifier'; |
|
30 | - public const TYPE_HASH = 'hash'; |
|
31 | - public const TYPE_NUMBER = 'number'; |
|
32 | - public const TYPE_STRING = 'string'; |
|
33 | - |
|
34 | - private $type; |
|
35 | - private $value; |
|
36 | - private $position; |
|
37 | - |
|
38 | - public function __construct(?string $type, ?string $value, ?int $position) |
|
39 | - { |
|
40 | - $this->type = $type; |
|
41 | - $this->value = $value; |
|
42 | - $this->position = $position; |
|
43 | - } |
|
44 | - |
|
45 | - public function getType(): ?int |
|
46 | - { |
|
47 | - return $this->type; |
|
48 | - } |
|
49 | - |
|
50 | - public function getValue(): ?string |
|
51 | - { |
|
52 | - return $this->value; |
|
53 | - } |
|
54 | - |
|
55 | - public function getPosition(): ?int |
|
56 | - { |
|
57 | - return $this->position; |
|
58 | - } |
|
59 | - |
|
60 | - public function isFileEnd(): bool |
|
61 | - { |
|
62 | - return self::TYPE_FILE_END === $this->type; |
|
63 | - } |
|
64 | - |
|
65 | - public function isDelimiter(array $values = []): bool |
|
66 | - { |
|
67 | - if (self::TYPE_DELIMITER !== $this->type) { |
|
68 | - return false; |
|
69 | - } |
|
70 | - |
|
71 | - if (empty($values)) { |
|
72 | - return true; |
|
73 | - } |
|
74 | - |
|
75 | - return \in_array($this->value, $values); |
|
76 | - } |
|
77 | - |
|
78 | - public function isWhitespace(): bool |
|
79 | - { |
|
80 | - return self::TYPE_WHITESPACE === $this->type; |
|
81 | - } |
|
82 | - |
|
83 | - public function isIdentifier(): bool |
|
84 | - { |
|
85 | - return self::TYPE_IDENTIFIER === $this->type; |
|
86 | - } |
|
87 | - |
|
88 | - public function isHash(): bool |
|
89 | - { |
|
90 | - return self::TYPE_HASH === $this->type; |
|
91 | - } |
|
92 | - |
|
93 | - public function isNumber(): bool |
|
94 | - { |
|
95 | - return self::TYPE_NUMBER === $this->type; |
|
96 | - } |
|
97 | - |
|
98 | - public function isString(): bool |
|
99 | - { |
|
100 | - return self::TYPE_STRING === $this->type; |
|
101 | - } |
|
102 | - |
|
103 | - public function __toString(): string |
|
104 | - { |
|
105 | - if ($this->value) { |
|
106 | - return sprintf('<%s "%s" at %s>', $this->type, $this->value, $this->position); |
|
107 | - } |
|
108 | - |
|
109 | - return sprintf('<%s at %s>', $this->type, $this->position); |
|
110 | - } |
|
26 | + public const TYPE_FILE_END = 'eof'; |
|
27 | + public const TYPE_DELIMITER = 'delimiter'; |
|
28 | + public const TYPE_WHITESPACE = 'whitespace'; |
|
29 | + public const TYPE_IDENTIFIER = 'identifier'; |
|
30 | + public const TYPE_HASH = 'hash'; |
|
31 | + public const TYPE_NUMBER = 'number'; |
|
32 | + public const TYPE_STRING = 'string'; |
|
33 | + |
|
34 | + private $type; |
|
35 | + private $value; |
|
36 | + private $position; |
|
37 | + |
|
38 | + public function __construct(?string $type, ?string $value, ?int $position) |
|
39 | + { |
|
40 | + $this->type = $type; |
|
41 | + $this->value = $value; |
|
42 | + $this->position = $position; |
|
43 | + } |
|
44 | + |
|
45 | + public function getType(): ?int |
|
46 | + { |
|
47 | + return $this->type; |
|
48 | + } |
|
49 | + |
|
50 | + public function getValue(): ?string |
|
51 | + { |
|
52 | + return $this->value; |
|
53 | + } |
|
54 | + |
|
55 | + public function getPosition(): ?int |
|
56 | + { |
|
57 | + return $this->position; |
|
58 | + } |
|
59 | + |
|
60 | + public function isFileEnd(): bool |
|
61 | + { |
|
62 | + return self::TYPE_FILE_END === $this->type; |
|
63 | + } |
|
64 | + |
|
65 | + public function isDelimiter(array $values = []): bool |
|
66 | + { |
|
67 | + if (self::TYPE_DELIMITER !== $this->type) { |
|
68 | + return false; |
|
69 | + } |
|
70 | + |
|
71 | + if (empty($values)) { |
|
72 | + return true; |
|
73 | + } |
|
74 | + |
|
75 | + return \in_array($this->value, $values); |
|
76 | + } |
|
77 | + |
|
78 | + public function isWhitespace(): bool |
|
79 | + { |
|
80 | + return self::TYPE_WHITESPACE === $this->type; |
|
81 | + } |
|
82 | + |
|
83 | + public function isIdentifier(): bool |
|
84 | + { |
|
85 | + return self::TYPE_IDENTIFIER === $this->type; |
|
86 | + } |
|
87 | + |
|
88 | + public function isHash(): bool |
|
89 | + { |
|
90 | + return self::TYPE_HASH === $this->type; |
|
91 | + } |
|
92 | + |
|
93 | + public function isNumber(): bool |
|
94 | + { |
|
95 | + return self::TYPE_NUMBER === $this->type; |
|
96 | + } |
|
97 | + |
|
98 | + public function isString(): bool |
|
99 | + { |
|
100 | + return self::TYPE_STRING === $this->type; |
|
101 | + } |
|
102 | + |
|
103 | + public function __toString(): string |
|
104 | + { |
|
105 | + if ($this->value) { |
|
106 | + return sprintf('<%s "%s" at %s>', $this->type, $this->value, $this->position); |
|
107 | + } |
|
108 | + |
|
109 | + return sprintf('<%s at %s>', $this->type, $this->position); |
|
110 | + } |
|
111 | 111 | } |
@@ -23,67 +23,67 @@ |
||
23 | 23 | */ |
24 | 24 | class TokenizerPatterns |
25 | 25 | { |
26 | - private $unicodeEscapePattern; |
|
27 | - private $simpleEscapePattern; |
|
28 | - private $newLineEscapePattern; |
|
29 | - private $escapePattern; |
|
30 | - private $stringEscapePattern; |
|
31 | - private $nonAsciiPattern; |
|
32 | - private $nmCharPattern; |
|
33 | - private $nmStartPattern; |
|
34 | - private $identifierPattern; |
|
35 | - private $hashPattern; |
|
36 | - private $numberPattern; |
|
37 | - private $quotedStringPattern; |
|
26 | + private $unicodeEscapePattern; |
|
27 | + private $simpleEscapePattern; |
|
28 | + private $newLineEscapePattern; |
|
29 | + private $escapePattern; |
|
30 | + private $stringEscapePattern; |
|
31 | + private $nonAsciiPattern; |
|
32 | + private $nmCharPattern; |
|
33 | + private $nmStartPattern; |
|
34 | + private $identifierPattern; |
|
35 | + private $hashPattern; |
|
36 | + private $numberPattern; |
|
37 | + private $quotedStringPattern; |
|
38 | 38 | |
39 | - public function __construct() |
|
40 | - { |
|
41 | - $this->unicodeEscapePattern = '\\\\([0-9a-f]{1,6})(?:\r\n|[ \n\r\t\f])?'; |
|
42 | - $this->simpleEscapePattern = '\\\\(.)'; |
|
43 | - $this->newLineEscapePattern = '\\\\(?:\n|\r\n|\r|\f)'; |
|
44 | - $this->escapePattern = $this->unicodeEscapePattern.'|\\\\[^\n\r\f0-9a-f]'; |
|
45 | - $this->stringEscapePattern = $this->newLineEscapePattern.'|'.$this->escapePattern; |
|
46 | - $this->nonAsciiPattern = '[^\x00-\x7F]'; |
|
47 | - $this->nmCharPattern = '[_a-z0-9-]|'.$this->escapePattern.'|'.$this->nonAsciiPattern; |
|
48 | - $this->nmStartPattern = '[_a-z]|'.$this->escapePattern.'|'.$this->nonAsciiPattern; |
|
49 | - $this->identifierPattern = '-?(?:'.$this->nmStartPattern.')(?:'.$this->nmCharPattern.')*'; |
|
50 | - $this->hashPattern = '#((?:'.$this->nmCharPattern.')+)'; |
|
51 | - $this->numberPattern = '[+-]?(?:[0-9]*\.[0-9]+|[0-9]+)'; |
|
52 | - $this->quotedStringPattern = '([^\n\r\f%s]|'.$this->stringEscapePattern.')*'; |
|
53 | - } |
|
39 | + public function __construct() |
|
40 | + { |
|
41 | + $this->unicodeEscapePattern = '\\\\([0-9a-f]{1,6})(?:\r\n|[ \n\r\t\f])?'; |
|
42 | + $this->simpleEscapePattern = '\\\\(.)'; |
|
43 | + $this->newLineEscapePattern = '\\\\(?:\n|\r\n|\r|\f)'; |
|
44 | + $this->escapePattern = $this->unicodeEscapePattern.'|\\\\[^\n\r\f0-9a-f]'; |
|
45 | + $this->stringEscapePattern = $this->newLineEscapePattern.'|'.$this->escapePattern; |
|
46 | + $this->nonAsciiPattern = '[^\x00-\x7F]'; |
|
47 | + $this->nmCharPattern = '[_a-z0-9-]|'.$this->escapePattern.'|'.$this->nonAsciiPattern; |
|
48 | + $this->nmStartPattern = '[_a-z]|'.$this->escapePattern.'|'.$this->nonAsciiPattern; |
|
49 | + $this->identifierPattern = '-?(?:'.$this->nmStartPattern.')(?:'.$this->nmCharPattern.')*'; |
|
50 | + $this->hashPattern = '#((?:'.$this->nmCharPattern.')+)'; |
|
51 | + $this->numberPattern = '[+-]?(?:[0-9]*\.[0-9]+|[0-9]+)'; |
|
52 | + $this->quotedStringPattern = '([^\n\r\f%s]|'.$this->stringEscapePattern.')*'; |
|
53 | + } |
|
54 | 54 | |
55 | - public function getNewLineEscapePattern(): string |
|
56 | - { |
|
57 | - return '~^'.$this->newLineEscapePattern.'~'; |
|
58 | - } |
|
55 | + public function getNewLineEscapePattern(): string |
|
56 | + { |
|
57 | + return '~^'.$this->newLineEscapePattern.'~'; |
|
58 | + } |
|
59 | 59 | |
60 | - public function getSimpleEscapePattern(): string |
|
61 | - { |
|
62 | - return '~^'.$this->simpleEscapePattern.'~'; |
|
63 | - } |
|
60 | + public function getSimpleEscapePattern(): string |
|
61 | + { |
|
62 | + return '~^'.$this->simpleEscapePattern.'~'; |
|
63 | + } |
|
64 | 64 | |
65 | - public function getUnicodeEscapePattern(): string |
|
66 | - { |
|
67 | - return '~^'.$this->unicodeEscapePattern.'~i'; |
|
68 | - } |
|
65 | + public function getUnicodeEscapePattern(): string |
|
66 | + { |
|
67 | + return '~^'.$this->unicodeEscapePattern.'~i'; |
|
68 | + } |
|
69 | 69 | |
70 | - public function getIdentifierPattern(): string |
|
71 | - { |
|
72 | - return '~^'.$this->identifierPattern.'~i'; |
|
73 | - } |
|
70 | + public function getIdentifierPattern(): string |
|
71 | + { |
|
72 | + return '~^'.$this->identifierPattern.'~i'; |
|
73 | + } |
|
74 | 74 | |
75 | - public function getHashPattern(): string |
|
76 | - { |
|
77 | - return '~^'.$this->hashPattern.'~i'; |
|
78 | - } |
|
75 | + public function getHashPattern(): string |
|
76 | + { |
|
77 | + return '~^'.$this->hashPattern.'~i'; |
|
78 | + } |
|
79 | 79 | |
80 | - public function getNumberPattern(): string |
|
81 | - { |
|
82 | - return '~^'.$this->numberPattern.'~'; |
|
83 | - } |
|
80 | + public function getNumberPattern(): string |
|
81 | + { |
|
82 | + return '~^'.$this->numberPattern.'~'; |
|
83 | + } |
|
84 | 84 | |
85 | - public function getQuotedStringPattern(string $quote): string |
|
86 | - { |
|
87 | - return '~^'.sprintf($this->quotedStringPattern, $quote).'~i'; |
|
88 | - } |
|
85 | + public function getQuotedStringPattern(string $quote): string |
|
86 | + { |
|
87 | + return '~^'.sprintf($this->quotedStringPattern, $quote).'~i'; |
|
88 | + } |
|
89 | 89 | } |
@@ -28,46 +28,46 @@ |
||
28 | 28 | */ |
29 | 29 | class Tokenizer |
30 | 30 | { |
31 | - /** |
|
32 | - * @var Handler\HandlerInterface[] |
|
33 | - */ |
|
34 | - private $handlers; |
|
31 | + /** |
|
32 | + * @var Handler\HandlerInterface[] |
|
33 | + */ |
|
34 | + private $handlers; |
|
35 | 35 | |
36 | - public function __construct() |
|
37 | - { |
|
38 | - $patterns = new TokenizerPatterns(); |
|
39 | - $escaping = new TokenizerEscaping($patterns); |
|
36 | + public function __construct() |
|
37 | + { |
|
38 | + $patterns = new TokenizerPatterns(); |
|
39 | + $escaping = new TokenizerEscaping($patterns); |
|
40 | 40 | |
41 | - $this->handlers = [ |
|
42 | - new Handler\WhitespaceHandler(), |
|
43 | - new Handler\IdentifierHandler($patterns, $escaping), |
|
44 | - new Handler\HashHandler($patterns, $escaping), |
|
45 | - new Handler\StringHandler($patterns, $escaping), |
|
46 | - new Handler\NumberHandler($patterns), |
|
47 | - new Handler\CommentHandler(), |
|
48 | - ]; |
|
49 | - } |
|
41 | + $this->handlers = [ |
|
42 | + new Handler\WhitespaceHandler(), |
|
43 | + new Handler\IdentifierHandler($patterns, $escaping), |
|
44 | + new Handler\HashHandler($patterns, $escaping), |
|
45 | + new Handler\StringHandler($patterns, $escaping), |
|
46 | + new Handler\NumberHandler($patterns), |
|
47 | + new Handler\CommentHandler(), |
|
48 | + ]; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Tokenize selector source code. |
|
53 | - */ |
|
54 | - public function tokenize(Reader $reader): TokenStream |
|
55 | - { |
|
56 | - $stream = new TokenStream(); |
|
51 | + /** |
|
52 | + * Tokenize selector source code. |
|
53 | + */ |
|
54 | + public function tokenize(Reader $reader): TokenStream |
|
55 | + { |
|
56 | + $stream = new TokenStream(); |
|
57 | 57 | |
58 | - while (!$reader->isEOF()) { |
|
59 | - foreach ($this->handlers as $handler) { |
|
60 | - if ($handler->handle($reader, $stream)) { |
|
61 | - continue 2; |
|
62 | - } |
|
63 | - } |
|
58 | + while (!$reader->isEOF()) { |
|
59 | + foreach ($this->handlers as $handler) { |
|
60 | + if ($handler->handle($reader, $stream)) { |
|
61 | + continue 2; |
|
62 | + } |
|
63 | + } |
|
64 | 64 | |
65 | - $stream->push(new Token(Token::TYPE_DELIMITER, $reader->getSubstring(1), $reader->getPosition())); |
|
66 | - $reader->moveForward(1); |
|
67 | - } |
|
65 | + $stream->push(new Token(Token::TYPE_DELIMITER, $reader->getSubstring(1), $reader->getPosition())); |
|
66 | + $reader->moveForward(1); |
|
67 | + } |
|
68 | 68 | |
69 | - return $stream |
|
70 | - ->push(new Token(Token::TYPE_FILE_END, null, $reader->getPosition())) |
|
71 | - ->freeze(); |
|
72 | - } |
|
69 | + return $stream |
|
70 | + ->push(new Token(Token::TYPE_FILE_END, null, $reader->getPosition())) |
|
71 | + ->freeze(); |
|
72 | + } |
|
73 | 73 | } |