@@ -23,35 +23,35 @@ |
||
23 | 23 | */ |
24 | 24 | class ClassNode extends AbstractNode |
25 | 25 | { |
26 | - private $selector; |
|
27 | - private string $name; |
|
28 | - |
|
29 | - public function __construct(NodeInterface $selector, string $name) |
|
30 | - { |
|
31 | - $this->selector = $selector; |
|
32 | - $this->name = $name; |
|
33 | - } |
|
34 | - |
|
35 | - public function getSelector(): NodeInterface |
|
36 | - { |
|
37 | - return $this->selector; |
|
38 | - } |
|
39 | - |
|
40 | - public function getName(): string |
|
41 | - { |
|
42 | - return $this->name; |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * {@inheritdoc} |
|
47 | - */ |
|
48 | - public function getSpecificity(): Specificity |
|
49 | - { |
|
50 | - return $this->selector->getSpecificity()->plus(new Specificity(0, 1, 0)); |
|
51 | - } |
|
52 | - |
|
53 | - public function __toString(): string |
|
54 | - { |
|
55 | - return sprintf('%s[%s.%s]', $this->getNodeName(), $this->selector, $this->name); |
|
56 | - } |
|
26 | + private $selector; |
|
27 | + private string $name; |
|
28 | + |
|
29 | + public function __construct(NodeInterface $selector, string $name) |
|
30 | + { |
|
31 | + $this->selector = $selector; |
|
32 | + $this->name = $name; |
|
33 | + } |
|
34 | + |
|
35 | + public function getSelector(): NodeInterface |
|
36 | + { |
|
37 | + return $this->selector; |
|
38 | + } |
|
39 | + |
|
40 | + public function getName(): string |
|
41 | + { |
|
42 | + return $this->name; |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * {@inheritdoc} |
|
47 | + */ |
|
48 | + public function getSpecificity(): Specificity |
|
49 | + { |
|
50 | + return $this->selector->getSpecificity()->plus(new Specificity(0, 1, 0)); |
|
51 | + } |
|
52 | + |
|
53 | + public function __toString(): string |
|
54 | + { |
|
55 | + return sprintf('%s[%s.%s]', $this->getNodeName(), $this->selector, $this->name); |
|
56 | + } |
|
57 | 57 | } |
@@ -23,60 +23,60 @@ |
||
23 | 23 | */ |
24 | 24 | class AttributeNode extends AbstractNode |
25 | 25 | { |
26 | - private $selector; |
|
27 | - private ?string $namespace; |
|
28 | - private string $attribute; |
|
29 | - private string $operator; |
|
30 | - private ?string $value; |
|
26 | + private $selector; |
|
27 | + private ?string $namespace; |
|
28 | + private string $attribute; |
|
29 | + private string $operator; |
|
30 | + private ?string $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 ?string $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 ?string $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 string $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 string $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,28 +23,28 @@ |
||
23 | 23 | */ |
24 | 24 | class SyntaxErrorException extends ParseException |
25 | 25 | { |
26 | - public static function unexpectedToken(string $expectedValue, Token $foundToken): self |
|
27 | - { |
|
28 | - return new self(sprintf('Expected %s, but %s found.', $expectedValue, $foundToken)); |
|
29 | - } |
|
30 | - |
|
31 | - public static function pseudoElementFound(string $pseudoElement, string $unexpectedLocation): self |
|
32 | - { |
|
33 | - return new self(sprintf('Unexpected pseudo-element "::%s" found %s.', $pseudoElement, $unexpectedLocation)); |
|
34 | - } |
|
35 | - |
|
36 | - public static function unclosedString(int $position): self |
|
37 | - { |
|
38 | - return new self(sprintf('Unclosed/invalid string at %s.', $position)); |
|
39 | - } |
|
40 | - |
|
41 | - public static function nestedNot(): self |
|
42 | - { |
|
43 | - return new self('Got nested ::not().'); |
|
44 | - } |
|
45 | - |
|
46 | - public static function stringAsFunctionArgument(): self |
|
47 | - { |
|
48 | - return new self('String not allowed as function argument.'); |
|
49 | - } |
|
26 | + public static function unexpectedToken(string $expectedValue, Token $foundToken): self |
|
27 | + { |
|
28 | + return new self(sprintf('Expected %s, but %s found.', $expectedValue, $foundToken)); |
|
29 | + } |
|
30 | + |
|
31 | + public static function pseudoElementFound(string $pseudoElement, string $unexpectedLocation): self |
|
32 | + { |
|
33 | + return new self(sprintf('Unexpected pseudo-element "::%s" found %s.', $pseudoElement, $unexpectedLocation)); |
|
34 | + } |
|
35 | + |
|
36 | + public static function unclosedString(int $position): self |
|
37 | + { |
|
38 | + return new self(sprintf('Unclosed/invalid string at %s.', $position)); |
|
39 | + } |
|
40 | + |
|
41 | + public static function nestedNot(): self |
|
42 | + { |
|
43 | + return new self('Got nested ::not().'); |
|
44 | + } |
|
45 | + |
|
46 | + public static function stringAsFunctionArgument(): self |
|
47 | + { |
|
48 | + return new self('String not allowed as function argument.'); |
|
49 | + } |
|
50 | 50 | } |
@@ -23,61 +23,61 @@ |
||
23 | 23 | */ |
24 | 24 | class Reader |
25 | 25 | { |
26 | - private string $source; |
|
27 | - private int $length; |
|
28 | - private int $position = 0; |
|
26 | + private string $source; |
|
27 | + private int $length; |
|
28 | + private int $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 | - public function findPattern(string $pattern): array|false |
|
64 | - { |
|
65 | - $source = substr($this->source, $this->position); |
|
63 | + public function findPattern(string $pattern): array|false |
|
64 | + { |
|
65 | + $source = substr($this->source, $this->position); |
|
66 | 66 | |
67 | - if (preg_match($pattern, $source, $matches)) { |
|
68 | - return $matches; |
|
69 | - } |
|
67 | + if (preg_match($pattern, $source, $matches)) { |
|
68 | + return $matches; |
|
69 | + } |
|
70 | 70 | |
71 | - return false; |
|
72 | - } |
|
71 | + return false; |
|
72 | + } |
|
73 | 73 | |
74 | - public function moveForward(int $length) |
|
75 | - { |
|
76 | - $this->position += $length; |
|
77 | - } |
|
74 | + public function moveForward(int $length) |
|
75 | + { |
|
76 | + $this->position += $length; |
|
77 | + } |
|
78 | 78 | |
79 | - public function moveToEnd() |
|
80 | - { |
|
81 | - $this->position = $this->length; |
|
82 | - } |
|
79 | + public function moveToEnd() |
|
80 | + { |
|
81 | + $this->position = $this->length; |
|
82 | + } |
|
83 | 83 | } |
@@ -60,7 +60,7 @@ |
||
60 | 60 | return false === $position ? false : $position - $this->position; |
61 | 61 | } |
62 | 62 | |
63 | - public function findPattern(string $pattern): array|false |
|
63 | + public function findPattern(string $pattern): array | false |
|
64 | 64 | { |
65 | 65 | $source = substr($this->source, $this->position); |
66 | 66 |
@@ -26,131 +26,131 @@ |
||
26 | 26 | */ |
27 | 27 | class TokenStream |
28 | 28 | { |
29 | - /** |
|
30 | - * @var Token[] |
|
31 | - */ |
|
32 | - private array $tokens = []; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var Token[] |
|
36 | - */ |
|
37 | - private array $used = []; |
|
38 | - |
|
39 | - private int $cursor = 0; |
|
40 | - private $peeked; |
|
41 | - private bool $peeking = false; |
|
42 | - |
|
43 | - /** |
|
44 | - * Pushes a token. |
|
45 | - * |
|
46 | - * @return $this |
|
47 | - */ |
|
48 | - public function push(Token $token): static |
|
49 | - { |
|
50 | - $this->tokens[] = $token; |
|
51 | - |
|
52 | - return $this; |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * Freezes stream. |
|
57 | - * |
|
58 | - * @return $this |
|
59 | - */ |
|
60 | - public function freeze(): static |
|
61 | - { |
|
62 | - return $this; |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * Returns next token. |
|
67 | - * |
|
68 | - * @throws InternalErrorException If there is no more token |
|
69 | - */ |
|
70 | - public function getNext(): Token |
|
71 | - { |
|
72 | - if ($this->peeking) { |
|
73 | - $this->peeking = false; |
|
74 | - $this->used[] = $this->peeked; |
|
75 | - |
|
76 | - return $this->peeked; |
|
77 | - } |
|
78 | - |
|
79 | - if (!isset($this->tokens[$this->cursor])) { |
|
80 | - throw new InternalErrorException('Unexpected token stream end.'); |
|
81 | - } |
|
82 | - |
|
83 | - return $this->tokens[$this->cursor++]; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Returns peeked token. |
|
88 | - */ |
|
89 | - public function getPeek(): Token |
|
90 | - { |
|
91 | - if (!$this->peeking) { |
|
92 | - $this->peeked = $this->getNext(); |
|
93 | - $this->peeking = true; |
|
94 | - } |
|
95 | - |
|
96 | - return $this->peeked; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Returns used tokens. |
|
101 | - * |
|
102 | - * @return Token[] |
|
103 | - */ |
|
104 | - public function getUsed(): array |
|
105 | - { |
|
106 | - return $this->used; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Returns next identifier token. |
|
111 | - * |
|
112 | - * @throws SyntaxErrorException If next token is not an identifier |
|
113 | - */ |
|
114 | - public function getNextIdentifier(): string |
|
115 | - { |
|
116 | - $next = $this->getNext(); |
|
117 | - |
|
118 | - if (!$next->isIdentifier()) { |
|
119 | - throw SyntaxErrorException::unexpectedToken('identifier', $next); |
|
120 | - } |
|
121 | - |
|
122 | - return $next->getValue(); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Returns next identifier or null if star delimiter token is found. |
|
127 | - * |
|
128 | - * @throws SyntaxErrorException If next token is not an identifier or a star delimiter |
|
129 | - */ |
|
130 | - public function getNextIdentifierOrStar(): ?string |
|
131 | - { |
|
132 | - $next = $this->getNext(); |
|
133 | - |
|
134 | - if ($next->isIdentifier()) { |
|
135 | - return $next->getValue(); |
|
136 | - } |
|
137 | - |
|
138 | - if ($next->isDelimiter(['*'])) { |
|
139 | - return null; |
|
140 | - } |
|
141 | - |
|
142 | - throw SyntaxErrorException::unexpectedToken('identifier or "*"', $next); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Skips next whitespace if any. |
|
147 | - */ |
|
148 | - public function skipWhitespace() |
|
149 | - { |
|
150 | - $peek = $this->getPeek(); |
|
151 | - |
|
152 | - if ($peek->isWhitespace()) { |
|
153 | - $this->getNext(); |
|
154 | - } |
|
155 | - } |
|
29 | + /** |
|
30 | + * @var Token[] |
|
31 | + */ |
|
32 | + private array $tokens = []; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var Token[] |
|
36 | + */ |
|
37 | + private array $used = []; |
|
38 | + |
|
39 | + private int $cursor = 0; |
|
40 | + private $peeked; |
|
41 | + private bool $peeking = false; |
|
42 | + |
|
43 | + /** |
|
44 | + * Pushes a token. |
|
45 | + * |
|
46 | + * @return $this |
|
47 | + */ |
|
48 | + public function push(Token $token): static |
|
49 | + { |
|
50 | + $this->tokens[] = $token; |
|
51 | + |
|
52 | + return $this; |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * Freezes stream. |
|
57 | + * |
|
58 | + * @return $this |
|
59 | + */ |
|
60 | + public function freeze(): static |
|
61 | + { |
|
62 | + return $this; |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * Returns next token. |
|
67 | + * |
|
68 | + * @throws InternalErrorException If there is no more token |
|
69 | + */ |
|
70 | + public function getNext(): Token |
|
71 | + { |
|
72 | + if ($this->peeking) { |
|
73 | + $this->peeking = false; |
|
74 | + $this->used[] = $this->peeked; |
|
75 | + |
|
76 | + return $this->peeked; |
|
77 | + } |
|
78 | + |
|
79 | + if (!isset($this->tokens[$this->cursor])) { |
|
80 | + throw new InternalErrorException('Unexpected token stream end.'); |
|
81 | + } |
|
82 | + |
|
83 | + return $this->tokens[$this->cursor++]; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Returns peeked token. |
|
88 | + */ |
|
89 | + public function getPeek(): Token |
|
90 | + { |
|
91 | + if (!$this->peeking) { |
|
92 | + $this->peeked = $this->getNext(); |
|
93 | + $this->peeking = true; |
|
94 | + } |
|
95 | + |
|
96 | + return $this->peeked; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Returns used tokens. |
|
101 | + * |
|
102 | + * @return Token[] |
|
103 | + */ |
|
104 | + public function getUsed(): array |
|
105 | + { |
|
106 | + return $this->used; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Returns next identifier token. |
|
111 | + * |
|
112 | + * @throws SyntaxErrorException If next token is not an identifier |
|
113 | + */ |
|
114 | + public function getNextIdentifier(): string |
|
115 | + { |
|
116 | + $next = $this->getNext(); |
|
117 | + |
|
118 | + if (!$next->isIdentifier()) { |
|
119 | + throw SyntaxErrorException::unexpectedToken('identifier', $next); |
|
120 | + } |
|
121 | + |
|
122 | + return $next->getValue(); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Returns next identifier or null if star delimiter token is found. |
|
127 | + * |
|
128 | + * @throws SyntaxErrorException If next token is not an identifier or a star delimiter |
|
129 | + */ |
|
130 | + public function getNextIdentifierOrStar(): ?string |
|
131 | + { |
|
132 | + $next = $this->getNext(); |
|
133 | + |
|
134 | + if ($next->isIdentifier()) { |
|
135 | + return $next->getValue(); |
|
136 | + } |
|
137 | + |
|
138 | + if ($next->isDelimiter(['*'])) { |
|
139 | + return null; |
|
140 | + } |
|
141 | + |
|
142 | + throw SyntaxErrorException::unexpectedToken('identifier or "*"', $next); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Skips next whitespace if any. |
|
147 | + */ |
|
148 | + public function skipWhitespace() |
|
149 | + { |
|
150 | + $peek = $this->getPeek(); |
|
151 | + |
|
152 | + if ($peek->isWhitespace()) { |
|
153 | + $this->getNext(); |
|
154 | + } |
|
155 | + } |
|
156 | 156 | } |
@@ -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 ?string $type; |
|
35 | - private ?string $value; |
|
36 | - private ?int $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 ?string $type; |
|
35 | + private ?string $value; |
|
36 | + private ?int $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 string $unicodeEscapePattern; |
|
27 | - private string $simpleEscapePattern; |
|
28 | - private string $newLineEscapePattern; |
|
29 | - private string $escapePattern; |
|
30 | - private string $stringEscapePattern; |
|
31 | - private string $nonAsciiPattern; |
|
32 | - private string $nmCharPattern; |
|
33 | - private string $nmStartPattern; |
|
34 | - private string $identifierPattern; |
|
35 | - private string $hashPattern; |
|
36 | - private string $numberPattern; |
|
37 | - private string $quotedStringPattern; |
|
26 | + private string $unicodeEscapePattern; |
|
27 | + private string $simpleEscapePattern; |
|
28 | + private string $newLineEscapePattern; |
|
29 | + private string $escapePattern; |
|
30 | + private string $stringEscapePattern; |
|
31 | + private string $nonAsciiPattern; |
|
32 | + private string $nmCharPattern; |
|
33 | + private string $nmStartPattern; |
|
34 | + private string $identifierPattern; |
|
35 | + private string $hashPattern; |
|
36 | + private string $numberPattern; |
|
37 | + private string $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 | } |