@@ -44,12 +44,12 @@ discard block |
||
44 | 44 | public function parse(StreamInterface $src): \Generator |
45 | 45 | { |
46 | 46 | $stream = new Buffer($this->generate($src)); |
47 | - foreach ($this->grammars as $grammar) { |
|
47 | + foreach ($this->grammars as $grammar){ |
|
48 | 48 | $stream = new Buffer($this->wrap(clone $grammar, $stream)); |
49 | 49 | } |
50 | 50 | |
51 | 51 | // always group raw bytes into raw tokens |
52 | - foreach ($this->wrap(new RawGrammar(), $stream) as $n) { |
|
52 | + foreach ($this->wrap(new RawGrammar(), $stream) as $n){ |
|
53 | 53 | yield $n; |
54 | 54 | } |
55 | 55 | } |
@@ -61,8 +61,8 @@ discard block |
||
61 | 61 | */ |
62 | 62 | private function wrap(GrammarInterface $grammar, Buffer $stream): \Generator |
63 | 63 | { |
64 | - foreach ($grammar->parse($stream) as $n) { |
|
65 | - if ($n instanceof Token && $n->grammar === null) { |
|
64 | + foreach ($grammar->parse($stream) as $n){ |
|
65 | + if ($n instanceof Token && $n->grammar === null){ |
|
66 | 66 | $n->grammar = get_class($grammar); |
67 | 67 | } |
68 | 68 | |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | */ |
79 | 79 | private function generate(StreamInterface $src) |
80 | 80 | { |
81 | - while (!$src->isEOI()) { |
|
81 | + while (!$src->isEOI()){ |
|
82 | 82 | yield new Byte($src->getOffset(), $src->peak()); |
83 | 83 | } |
84 | 84 | } |
@@ -44,12 +44,14 @@ discard block |
||
44 | 44 | public function parse(StreamInterface $src): \Generator |
45 | 45 | { |
46 | 46 | $stream = new Buffer($this->generate($src)); |
47 | - foreach ($this->grammars as $grammar) { |
|
47 | + foreach ($this->grammars as $grammar) |
|
48 | + { |
|
48 | 49 | $stream = new Buffer($this->wrap(clone $grammar, $stream)); |
49 | 50 | } |
50 | 51 | |
51 | 52 | // always group raw bytes into raw tokens |
52 | - foreach ($this->wrap(new RawGrammar(), $stream) as $n) { |
|
53 | + foreach ($this->wrap(new RawGrammar(), $stream) as $n) |
|
54 | + { |
|
53 | 55 | yield $n; |
54 | 56 | } |
55 | 57 | } |
@@ -61,8 +63,10 @@ discard block |
||
61 | 63 | */ |
62 | 64 | private function wrap(GrammarInterface $grammar, Buffer $stream): \Generator |
63 | 65 | { |
64 | - foreach ($grammar->parse($stream) as $n) { |
|
65 | - if ($n instanceof Token && $n->grammar === null) { |
|
66 | + foreach ($grammar->parse($stream) as $n) |
|
67 | + { |
|
68 | + if ($n instanceof Token && $n->grammar === null) |
|
69 | + { |
|
66 | 70 | $n->grammar = get_class($grammar); |
67 | 71 | } |
68 | 72 | |
@@ -78,7 +82,8 @@ discard block |
||
78 | 82 | */ |
79 | 83 | private function generate(StreamInterface $src) |
80 | 84 | { |
81 | - while (!$src->isEOI()) { |
|
85 | + while (!$src->isEOI()) |
|
86 | + { |
|
82 | 87 | yield new Byte($src->getOffset(), $src->peak()); |
83 | 88 | } |
84 | 89 | } |
@@ -53,7 +53,7 @@ |
||
53 | 53 | */ |
54 | 54 | public function peak(): ?string |
55 | 55 | { |
56 | - if ($this->offset + 1 > $this->length) { |
|
56 | + if ($this->offset + 1 > $this->length){ |
|
57 | 57 | return null; |
58 | 58 | } |
59 | 59 |
@@ -53,7 +53,8 @@ |
||
53 | 53 | */ |
54 | 54 | public function peak(): ?string |
55 | 55 | { |
56 | - if ($this->offset + 1 > $this->length) { |
|
56 | + if ($this->offset + 1 > $this->length) |
|
57 | + { |
|
57 | 58 | return null; |
58 | 59 | } |
59 | 60 |
@@ -58,11 +58,11 @@ |
||
58 | 58 | 'content' => $this->content |
59 | 59 | ]; |
60 | 60 | |
61 | - if ($this->grammar !== null) { |
|
61 | + if ($this->grammar !== null){ |
|
62 | 62 | $token['type'] = call_user_func([$this->grammar, 'tokenName'], $this->type); |
63 | 63 | } |
64 | 64 | |
65 | - if ($this->tokens !== []) { |
|
65 | + if ($this->tokens !== []){ |
|
66 | 66 | $token['tokens'] = $this->tokens; |
67 | 67 | } |
68 | 68 |
@@ -58,11 +58,13 @@ |
||
58 | 58 | 'content' => $this->content |
59 | 59 | ]; |
60 | 60 | |
61 | - if ($this->grammar !== null) { |
|
61 | + if ($this->grammar !== null) |
|
62 | + { |
|
62 | 63 | $token['type'] = call_user_func([$this->grammar, 'tokenName'], $this->type); |
63 | 64 | } |
64 | 65 | |
65 | - if ($this->tokens !== []) { |
|
66 | + if ($this->tokens !== []) |
|
67 | + { |
|
66 | 68 | $token['tokens'] = $this->tokens; |
67 | 69 | } |
68 | 70 |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | */ |
47 | 47 | public function getIterator() |
48 | 48 | { |
49 | - while ($n = $this->next()) { |
|
49 | + while ($n = $this->next()){ |
|
50 | 50 | yield $n; |
51 | 51 | } |
52 | 52 | } |
@@ -64,18 +64,18 @@ discard block |
||
64 | 64 | */ |
65 | 65 | public function next() |
66 | 66 | { |
67 | - if ($this->replay !== []) { |
|
67 | + if ($this->replay !== []){ |
|
68 | 68 | $n = array_shift($this->replay); |
69 | - } else { |
|
69 | + }else{ |
|
70 | 70 | $n = $this->generator->current(); |
71 | - if ($n === null) { |
|
71 | + if ($n === null){ |
|
72 | 72 | return null; |
73 | 73 | } |
74 | 74 | $this->generator->next(); |
75 | 75 | $this->buffer[] = $n; |
76 | 76 | } |
77 | 77 | |
78 | - if ($n !== null) { |
|
78 | + if ($n !== null){ |
|
79 | 79 | $this->offset = $n->offset; |
80 | 80 | } |
81 | 81 | |
@@ -90,10 +90,10 @@ discard block |
||
90 | 90 | public function nextBytes(): string |
91 | 91 | { |
92 | 92 | $result = ''; |
93 | - while ($n = $this->next()) { |
|
94 | - if ($n instanceof Byte) { |
|
93 | + while ($n = $this->next()){ |
|
94 | + if ($n instanceof Byte){ |
|
95 | 95 | $result .= $n->char; |
96 | - } else { |
|
96 | + }else{ |
|
97 | 97 | break; |
98 | 98 | } |
99 | 99 | } |
@@ -106,12 +106,12 @@ discard block |
||
106 | 106 | */ |
107 | 107 | public function lookahead() |
108 | 108 | { |
109 | - if ($this->replay !== []) { |
|
109 | + if ($this->replay !== []){ |
|
110 | 110 | return $this->replay[0]; |
111 | 111 | } |
112 | 112 | |
113 | 113 | $n = $this->next(); |
114 | - if ($n !== null) { |
|
114 | + if ($n !== null){ |
|
115 | 115 | array_unshift($this->replay, $n); |
116 | 116 | } |
117 | 117 | |
@@ -128,20 +128,20 @@ discard block |
||
128 | 128 | { |
129 | 129 | $result = ''; |
130 | 130 | $replay = []; |
131 | - for ($i = 0; $i < $size; $i++) { |
|
131 | + for ($i = 0; $i < $size; $i++){ |
|
132 | 132 | $n = $this->next(); |
133 | - if ($n !== null) { |
|
133 | + if ($n !== null){ |
|
134 | 134 | $replay[] = $n; |
135 | 135 | } |
136 | 136 | |
137 | - if (!$n instanceof Byte) { |
|
137 | + if (!$n instanceof Byte){ |
|
138 | 138 | break; |
139 | 139 | } |
140 | 140 | |
141 | 141 | $result .= $n->char; |
142 | 142 | } |
143 | 143 | |
144 | - foreach (array_reverse($replay) as $n) { |
|
144 | + foreach (array_reverse($replay) as $n){ |
|
145 | 145 | array_unshift($this->replay, $n); |
146 | 146 | } |
147 | 147 | |
@@ -155,8 +155,8 @@ discard block |
||
155 | 155 | */ |
156 | 156 | public function replay(int $offset): void |
157 | 157 | { |
158 | - foreach ($this->buffer as $n) { |
|
159 | - if ($n->offset > $offset) { |
|
158 | + foreach ($this->buffer as $n){ |
|
159 | + if ($n->offset > $offset){ |
|
160 | 160 | $this->replay[] = $n; |
161 | 161 | } |
162 | 162 | } |
@@ -46,7 +46,8 @@ discard block |
||
46 | 46 | */ |
47 | 47 | public function getIterator() |
48 | 48 | { |
49 | - while ($n = $this->next()) { |
|
49 | + while ($n = $this->next()) |
|
50 | + { |
|
50 | 51 | yield $n; |
51 | 52 | } |
52 | 53 | } |
@@ -64,18 +65,23 @@ discard block |
||
64 | 65 | */ |
65 | 66 | public function next() |
66 | 67 | { |
67 | - if ($this->replay !== []) { |
|
68 | + if ($this->replay !== []) |
|
69 | + { |
|
68 | 70 | $n = array_shift($this->replay); |
69 | - } else { |
|
71 | + } |
|
72 | + else |
|
73 | + { |
|
70 | 74 | $n = $this->generator->current(); |
71 | - if ($n === null) { |
|
75 | + if ($n === null) |
|
76 | + { |
|
72 | 77 | return null; |
73 | 78 | } |
74 | 79 | $this->generator->next(); |
75 | 80 | $this->buffer[] = $n; |
76 | 81 | } |
77 | 82 | |
78 | - if ($n !== null) { |
|
83 | + if ($n !== null) |
|
84 | + { |
|
79 | 85 | $this->offset = $n->offset; |
80 | 86 | } |
81 | 87 | |
@@ -90,10 +96,14 @@ discard block |
||
90 | 96 | public function nextBytes(): string |
91 | 97 | { |
92 | 98 | $result = ''; |
93 | - while ($n = $this->next()) { |
|
94 | - if ($n instanceof Byte) { |
|
99 | + while ($n = $this->next()) |
|
100 | + { |
|
101 | + if ($n instanceof Byte) |
|
102 | + { |
|
95 | 103 | $result .= $n->char; |
96 | - } else { |
|
104 | + } |
|
105 | + else |
|
106 | + { |
|
97 | 107 | break; |
98 | 108 | } |
99 | 109 | } |
@@ -106,12 +116,14 @@ discard block |
||
106 | 116 | */ |
107 | 117 | public function lookahead() |
108 | 118 | { |
109 | - if ($this->replay !== []) { |
|
119 | + if ($this->replay !== []) |
|
120 | + { |
|
110 | 121 | return $this->replay[0]; |
111 | 122 | } |
112 | 123 | |
113 | 124 | $n = $this->next(); |
114 | - if ($n !== null) { |
|
125 | + if ($n !== null) |
|
126 | + { |
|
115 | 127 | array_unshift($this->replay, $n); |
116 | 128 | } |
117 | 129 | |
@@ -128,20 +140,24 @@ discard block |
||
128 | 140 | { |
129 | 141 | $result = ''; |
130 | 142 | $replay = []; |
131 | - for ($i = 0; $i < $size; $i++) { |
|
143 | + for ($i = 0; $i < $size; $i++) |
|
144 | + { |
|
132 | 145 | $n = $this->next(); |
133 | - if ($n !== null) { |
|
146 | + if ($n !== null) |
|
147 | + { |
|
134 | 148 | $replay[] = $n; |
135 | 149 | } |
136 | 150 | |
137 | - if (!$n instanceof Byte) { |
|
151 | + if (!$n instanceof Byte) |
|
152 | + { |
|
138 | 153 | break; |
139 | 154 | } |
140 | 155 | |
141 | 156 | $result .= $n->char; |
142 | 157 | } |
143 | 158 | |
144 | - foreach (array_reverse($replay) as $n) { |
|
159 | + foreach (array_reverse($replay) as $n) |
|
160 | + { |
|
145 | 161 | array_unshift($this->replay, $n); |
146 | 162 | } |
147 | 163 | |
@@ -155,8 +171,10 @@ discard block |
||
155 | 171 | */ |
156 | 172 | public function replay(int $offset): void |
157 | 173 | { |
158 | - foreach ($this->buffer as $n) { |
|
159 | - if ($n->offset > $offset) { |
|
174 | + foreach ($this->buffer as $n) |
|
175 | + { |
|
176 | + if ($n->offset > $offset) |
|
177 | + { |
|
160 | 178 | $this->replay[] = $n; |
161 | 179 | } |
162 | 180 | } |
@@ -59,15 +59,15 @@ discard block |
||
59 | 59 | */ |
60 | 60 | public function parse(Buffer $src): \Generator |
61 | 61 | { |
62 | - while ($n = $src->next()) { |
|
63 | - if (!$n instanceof Byte || $n->char !== '<') { |
|
62 | + while ($n = $src->next()){ |
|
63 | + if (!$n instanceof Byte || $n->char !== '<'){ |
|
64 | 64 | yield $n; |
65 | 65 | continue; |
66 | 66 | } |
67 | 67 | |
68 | 68 | // work with isolated token stream! |
69 | 69 | $tag = (clone $this)->parseGrammar($src); |
70 | - if ($tag === null) { |
|
70 | + if ($tag === null){ |
|
71 | 71 | yield $n; |
72 | 72 | $src->replay($n->offset); |
73 | 73 | continue; |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | $tagName = $this->tagName($tag); |
77 | 77 | |
78 | 78 | // todo: add support for custom tag list |
79 | - if (in_array($tagName, self::VERBATIM_TAGS)) { |
|
79 | + if (in_array($tagName, self::VERBATIM_TAGS)){ |
|
80 | 80 | yield from $tag; |
81 | 81 | yield from $this->parseVerbatim($src, $tagName); |
82 | 82 | continue; |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | */ |
93 | 93 | public static function tokenName(int $token): string |
94 | 94 | { |
95 | - switch ($token) { |
|
95 | + switch ($token){ |
|
96 | 96 | case self::TYPE_RAW: |
97 | 97 | return 'HTML:RAW'; |
98 | 98 | case self::TYPE_KEYWORD: |
@@ -127,26 +127,26 @@ discard block |
||
127 | 127 | { |
128 | 128 | $chunks = []; |
129 | 129 | |
130 | - while ($n = $src->next()) { |
|
131 | - if ($n instanceof Token) { |
|
130 | + while ($n = $src->next()){ |
|
131 | + if ($n instanceof Token){ |
|
132 | 132 | $chunks[] = $n; |
133 | 133 | continue; |
134 | 134 | } |
135 | 135 | |
136 | - switch ($n->char) { |
|
136 | + switch ($n->char){ |
|
137 | 137 | case '"': |
138 | 138 | case "'": |
139 | 139 | case '`': |
140 | 140 | $chunks[] = $n; |
141 | 141 | |
142 | 142 | // language inclusions allow nested strings |
143 | - while ($nc = $src->next()) { |
|
143 | + while ($nc = $src->next()){ |
|
144 | 144 | $chunks[] = $nc; |
145 | - if ($nc instanceof Token) { |
|
145 | + if ($nc instanceof Token){ |
|
146 | 146 | continue; |
147 | 147 | } |
148 | 148 | |
149 | - if ($nc->char === $n->char) { |
|
149 | + if ($nc->char === $n->char){ |
|
150 | 150 | break; |
151 | 151 | } |
152 | 152 | } |
@@ -157,22 +157,22 @@ discard block |
||
157 | 157 | $chunks[] = $n; |
158 | 158 | |
159 | 159 | $multiline = false; |
160 | - if ($src->lookaheadByte(1) === '/' || $src->lookaheadByte(1) === '*') { |
|
161 | - if ($src->lookaheadByte(1) === '*') { |
|
160 | + if ($src->lookaheadByte(1) === '/' || $src->lookaheadByte(1) === '*'){ |
|
161 | + if ($src->lookaheadByte(1) === '*'){ |
|
162 | 162 | $multiline = true; |
163 | 163 | } |
164 | 164 | |
165 | 165 | $chunks[] = $src->next(); |
166 | 166 | |
167 | 167 | // language inclusions allow nested strings |
168 | - while ($nc = $src->next()) { |
|
169 | - if ($nc instanceof Token) { |
|
168 | + while ($nc = $src->next()){ |
|
169 | + if ($nc instanceof Token){ |
|
170 | 170 | continue; |
171 | 171 | } |
172 | 172 | |
173 | - if ($nc->char === '<') { |
|
173 | + if ($nc->char === '<'){ |
|
174 | 174 | $tag = (clone $this)->parseGrammar($src); |
175 | - if ($tag === null || $this->tagName($tag) !== $verbatim) { |
|
175 | + if ($tag === null || $this->tagName($tag) !== $verbatim){ |
|
176 | 176 | $src->replay($n->offset); |
177 | 177 | break; |
178 | 178 | } |
@@ -183,12 +183,12 @@ discard block |
||
183 | 183 | |
184 | 184 | $chunks[] = $nc; |
185 | 185 | |
186 | - if ($multiline) { |
|
187 | - if ($nc->char === '*' && $src->lookaheadByte(1) === '/') { |
|
186 | + if ($multiline){ |
|
187 | + if ($nc->char === '*' && $src->lookaheadByte(1) === '/'){ |
|
188 | 188 | $chunks[] = $src->next(); |
189 | 189 | break; |
190 | 190 | } |
191 | - } elseif ($nc->char === "\n") { |
|
191 | + } elseif ($nc->char === "\n"){ |
|
192 | 192 | break; |
193 | 193 | } |
194 | 194 | } |
@@ -199,7 +199,7 @@ discard block |
||
199 | 199 | case '<': |
200 | 200 | // tag beginning? |
201 | 201 | $tag = (clone $this)->parseGrammar($src); |
202 | - if ($tag === null || $this->tagName($tag) !== $verbatim) { |
|
202 | + if ($tag === null || $this->tagName($tag) !== $verbatim){ |
|
203 | 203 | $chunks[] = $n; |
204 | 204 | $src->replay($n->offset); |
205 | 205 | break; |
@@ -223,8 +223,8 @@ discard block |
||
223 | 223 | */ |
224 | 224 | private function tagName(array $tag): string |
225 | 225 | { |
226 | - foreach ($tag as $token) { |
|
227 | - if ($token->type === self::TYPE_KEYWORD) { |
|
226 | + foreach ($tag as $token){ |
|
227 | + if ($token->type === self::TYPE_KEYWORD){ |
|
228 | 228 | return strtolower($token->content); |
229 | 229 | } |
230 | 230 | } |
@@ -242,28 +242,28 @@ discard block |
||
242 | 242 | new Token(self::TYPE_OPEN, $src->getOffset(), '<') |
243 | 243 | ]; |
244 | 244 | |
245 | - if ($src->lookaheadByte() === '/') { |
|
245 | + if ($src->lookaheadByte() === '/'){ |
|
246 | 246 | $this->tokens[0]->type = self::TYPE_OPEN_SHORT; |
247 | 247 | $this->tokens[0]->content .= $src->next()->char; |
248 | 248 | } |
249 | 249 | |
250 | - while ($n = $src->next()) { |
|
251 | - if ($this->attribute !== []) { |
|
250 | + while ($n = $src->next()){ |
|
251 | + if ($this->attribute !== []){ |
|
252 | 252 | $this->attribute[] = $n; |
253 | 253 | |
254 | - if ($n instanceof Byte && $n->char === $this->attribute[0]->char) { |
|
254 | + if ($n instanceof Byte && $n->char === $this->attribute[0]->char){ |
|
255 | 255 | $this->flushAttribute(); |
256 | 256 | } |
257 | 257 | |
258 | 258 | continue; |
259 | 259 | } |
260 | 260 | |
261 | - if ($n instanceof Token) { |
|
261 | + if ($n instanceof Token){ |
|
262 | 262 | $this->keyword[] = $n; |
263 | 263 | continue; |
264 | 264 | } |
265 | 265 | |
266 | - switch ($n->char) { |
|
266 | + switch ($n->char){ |
|
267 | 267 | case '"': |
268 | 268 | case "'": |
269 | 269 | case '`': |
@@ -281,12 +281,12 @@ discard block |
||
281 | 281 | break; |
282 | 282 | |
283 | 283 | case '/': |
284 | - if ($src->lookaheadByte() === '>') { |
|
284 | + if ($src->lookaheadByte() === '>'){ |
|
285 | 285 | $this->flush(); |
286 | 286 | $this->tokens[] = new Token( |
287 | 287 | self::TYPE_CLOSE_SHORT, |
288 | 288 | $n->offset, |
289 | - $n->char . $src->next()->char |
|
289 | + $n->char.$src->next()->char |
|
290 | 290 | ); |
291 | 291 | |
292 | 292 | break 2; |
@@ -305,7 +305,7 @@ discard block |
||
305 | 305 | break 2; |
306 | 306 | |
307 | 307 | default: |
308 | - if (preg_match(self::REGEXP_WHITESPACE, $n->char)) { |
|
308 | + if (preg_match(self::REGEXP_WHITESPACE, $n->char)){ |
|
309 | 309 | $this->flushKeyword(); |
310 | 310 | $this->whitespace[] = $n; |
311 | 311 | break; |
@@ -313,7 +313,7 @@ discard block |
||
313 | 313 | $this->flushWhitespace(); |
314 | 314 | |
315 | 315 | |
316 | - if (!preg_match(self::REGEXP_KEYWORD, $n->char)) { |
|
316 | + if (!preg_match(self::REGEXP_KEYWORD, $n->char)){ |
|
317 | 317 | // unexpected char |
318 | 318 | return null; |
319 | 319 | } |
@@ -322,7 +322,7 @@ discard block |
||
322 | 322 | } |
323 | 323 | } |
324 | 324 | |
325 | - if (!$this->isValid()) { |
|
325 | + if (!$this->isValid()){ |
|
326 | 326 | return null; |
327 | 327 | } |
328 | 328 | |
@@ -335,17 +335,17 @@ discard block |
||
335 | 335 | private function isValid(): bool |
336 | 336 | { |
337 | 337 | // tag is too short or does not have name keyword |
338 | - if (count($this->tokens) < 3) { |
|
338 | + if (count($this->tokens) < 3){ |
|
339 | 339 | return false; |
340 | 340 | } |
341 | 341 | |
342 | 342 | $last = $this->tokens[count($this->tokens) - 1]; |
343 | - if ($last->type !== self::TYPE_CLOSE && $last->type !== self::TYPE_CLOSE_SHORT) { |
|
343 | + if ($last->type !== self::TYPE_CLOSE && $last->type !== self::TYPE_CLOSE_SHORT){ |
|
344 | 344 | return false; |
345 | 345 | } |
346 | 346 | |
347 | - foreach ($this->tokens as $token) { |
|
348 | - switch ($token->type) { |
|
347 | + foreach ($this->tokens as $token){ |
|
348 | + switch ($token->type){ |
|
349 | 349 | case self::TYPE_WHITESPACE: |
350 | 350 | // ignore |
351 | 351 | continue 2; |
@@ -376,7 +376,7 @@ discard block |
||
376 | 376 | */ |
377 | 377 | private function flushWhitespace(): void |
378 | 378 | { |
379 | - if ($this->whitespace === []) { |
|
379 | + if ($this->whitespace === []){ |
|
380 | 380 | return; |
381 | 381 | } |
382 | 382 | |
@@ -389,7 +389,7 @@ discard block |
||
389 | 389 | */ |
390 | 390 | private function flushKeyword(): void |
391 | 391 | { |
392 | - if ($this->keyword === []) { |
|
392 | + if ($this->keyword === []){ |
|
393 | 393 | return; |
394 | 394 | } |
395 | 395 | |
@@ -402,7 +402,7 @@ discard block |
||
402 | 402 | */ |
403 | 403 | private function flushAttribute(): void |
404 | 404 | { |
405 | - if ($this->attribute === []) { |
|
405 | + if ($this->attribute === []){ |
|
406 | 406 | return; |
407 | 407 | } |
408 | 408 |
@@ -59,15 +59,18 @@ discard block |
||
59 | 59 | */ |
60 | 60 | public function parse(Buffer $src): \Generator |
61 | 61 | { |
62 | - while ($n = $src->next()) { |
|
63 | - if (!$n instanceof Byte || $n->char !== '<') { |
|
62 | + while ($n = $src->next()) |
|
63 | + { |
|
64 | + if (!$n instanceof Byte || $n->char !== '<') |
|
65 | + { |
|
64 | 66 | yield $n; |
65 | 67 | continue; |
66 | 68 | } |
67 | 69 | |
68 | 70 | // work with isolated token stream! |
69 | 71 | $tag = (clone $this)->parseGrammar($src); |
70 | - if ($tag === null) { |
|
72 | + if ($tag === null) |
|
73 | + { |
|
71 | 74 | yield $n; |
72 | 75 | $src->replay($n->offset); |
73 | 76 | continue; |
@@ -76,7 +79,8 @@ discard block |
||
76 | 79 | $tagName = $this->tagName($tag); |
77 | 80 | |
78 | 81 | // todo: add support for custom tag list |
79 | - if (in_array($tagName, self::VERBATIM_TAGS)) { |
|
82 | + if (in_array($tagName, self::VERBATIM_TAGS)) |
|
83 | + { |
|
80 | 84 | yield from $tag; |
81 | 85 | yield from $this->parseVerbatim($src, $tagName); |
82 | 86 | continue; |
@@ -92,7 +96,8 @@ discard block |
||
92 | 96 | */ |
93 | 97 | public static function tokenName(int $token): string |
94 | 98 | { |
95 | - switch ($token) { |
|
99 | + switch ($token) |
|
100 | + { |
|
96 | 101 | case self::TYPE_RAW: |
97 | 102 | return 'HTML:RAW'; |
98 | 103 | case self::TYPE_KEYWORD: |
@@ -127,26 +132,32 @@ discard block |
||
127 | 132 | { |
128 | 133 | $chunks = []; |
129 | 134 | |
130 | - while ($n = $src->next()) { |
|
131 | - if ($n instanceof Token) { |
|
135 | + while ($n = $src->next()) |
|
136 | + { |
|
137 | + if ($n instanceof Token) |
|
138 | + { |
|
132 | 139 | $chunks[] = $n; |
133 | 140 | continue; |
134 | 141 | } |
135 | 142 | |
136 | - switch ($n->char) { |
|
143 | + switch ($n->char) |
|
144 | + { |
|
137 | 145 | case '"': |
138 | 146 | case "'": |
139 | 147 | case '`': |
140 | 148 | $chunks[] = $n; |
141 | 149 | |
142 | 150 | // language inclusions allow nested strings |
143 | - while ($nc = $src->next()) { |
|
151 | + while ($nc = $src->next()) |
|
152 | + { |
|
144 | 153 | $chunks[] = $nc; |
145 | - if ($nc instanceof Token) { |
|
154 | + if ($nc instanceof Token) |
|
155 | + { |
|
146 | 156 | continue; |
147 | 157 | } |
148 | 158 | |
149 | - if ($nc->char === $n->char) { |
|
159 | + if ($nc->char === $n->char) |
|
160 | + { |
|
150 | 161 | break; |
151 | 162 | } |
152 | 163 | } |
@@ -157,22 +168,28 @@ discard block |
||
157 | 168 | $chunks[] = $n; |
158 | 169 | |
159 | 170 | $multiline = false; |
160 | - if ($src->lookaheadByte(1) === '/' || $src->lookaheadByte(1) === '*') { |
|
161 | - if ($src->lookaheadByte(1) === '*') { |
|
171 | + if ($src->lookaheadByte(1) === '/' || $src->lookaheadByte(1) === '*') |
|
172 | + { |
|
173 | + if ($src->lookaheadByte(1) === '*') |
|
174 | + { |
|
162 | 175 | $multiline = true; |
163 | 176 | } |
164 | 177 | |
165 | 178 | $chunks[] = $src->next(); |
166 | 179 | |
167 | 180 | // language inclusions allow nested strings |
168 | - while ($nc = $src->next()) { |
|
169 | - if ($nc instanceof Token) { |
|
181 | + while ($nc = $src->next()) |
|
182 | + { |
|
183 | + if ($nc instanceof Token) |
|
184 | + { |
|
170 | 185 | continue; |
171 | 186 | } |
172 | 187 | |
173 | - if ($nc->char === '<') { |
|
188 | + if ($nc->char === '<') |
|
189 | + { |
|
174 | 190 | $tag = (clone $this)->parseGrammar($src); |
175 | - if ($tag === null || $this->tagName($tag) !== $verbatim) { |
|
191 | + if ($tag === null || $this->tagName($tag) !== $verbatim) |
|
192 | + { |
|
176 | 193 | $src->replay($n->offset); |
177 | 194 | break; |
178 | 195 | } |
@@ -183,12 +200,16 @@ discard block |
||
183 | 200 | |
184 | 201 | $chunks[] = $nc; |
185 | 202 | |
186 | - if ($multiline) { |
|
187 | - if ($nc->char === '*' && $src->lookaheadByte(1) === '/') { |
|
203 | + if ($multiline) |
|
204 | + { |
|
205 | + if ($nc->char === '*' && $src->lookaheadByte(1) === '/') |
|
206 | + { |
|
188 | 207 | $chunks[] = $src->next(); |
189 | 208 | break; |
190 | 209 | } |
191 | - } elseif ($nc->char === "\n") { |
|
210 | + } |
|
211 | + elseif ($nc->char === "\n") |
|
212 | + { |
|
192 | 213 | break; |
193 | 214 | } |
194 | 215 | } |
@@ -199,7 +220,8 @@ discard block |
||
199 | 220 | case '<': |
200 | 221 | // tag beginning? |
201 | 222 | $tag = (clone $this)->parseGrammar($src); |
202 | - if ($tag === null || $this->tagName($tag) !== $verbatim) { |
|
223 | + if ($tag === null || $this->tagName($tag) !== $verbatim) |
|
224 | + { |
|
203 | 225 | $chunks[] = $n; |
204 | 226 | $src->replay($n->offset); |
205 | 227 | break; |
@@ -223,8 +245,10 @@ discard block |
||
223 | 245 | */ |
224 | 246 | private function tagName(array $tag): string |
225 | 247 | { |
226 | - foreach ($tag as $token) { |
|
227 | - if ($token->type === self::TYPE_KEYWORD) { |
|
248 | + foreach ($tag as $token) |
|
249 | + { |
|
250 | + if ($token->type === self::TYPE_KEYWORD) |
|
251 | + { |
|
228 | 252 | return strtolower($token->content); |
229 | 253 | } |
230 | 254 | } |
@@ -242,28 +266,34 @@ discard block |
||
242 | 266 | new Token(self::TYPE_OPEN, $src->getOffset(), '<') |
243 | 267 | ]; |
244 | 268 | |
245 | - if ($src->lookaheadByte() === '/') { |
|
269 | + if ($src->lookaheadByte() === '/') |
|
270 | + { |
|
246 | 271 | $this->tokens[0]->type = self::TYPE_OPEN_SHORT; |
247 | 272 | $this->tokens[0]->content .= $src->next()->char; |
248 | 273 | } |
249 | 274 | |
250 | - while ($n = $src->next()) { |
|
251 | - if ($this->attribute !== []) { |
|
275 | + while ($n = $src->next()) |
|
276 | + { |
|
277 | + if ($this->attribute !== []) |
|
278 | + { |
|
252 | 279 | $this->attribute[] = $n; |
253 | 280 | |
254 | - if ($n instanceof Byte && $n->char === $this->attribute[0]->char) { |
|
281 | + if ($n instanceof Byte && $n->char === $this->attribute[0]->char) |
|
282 | + { |
|
255 | 283 | $this->flushAttribute(); |
256 | 284 | } |
257 | 285 | |
258 | 286 | continue; |
259 | 287 | } |
260 | 288 | |
261 | - if ($n instanceof Token) { |
|
289 | + if ($n instanceof Token) |
|
290 | + { |
|
262 | 291 | $this->keyword[] = $n; |
263 | 292 | continue; |
264 | 293 | } |
265 | 294 | |
266 | - switch ($n->char) { |
|
295 | + switch ($n->char) |
|
296 | + { |
|
267 | 297 | case '"': |
268 | 298 | case "'": |
269 | 299 | case '`': |
@@ -281,7 +311,8 @@ discard block |
||
281 | 311 | break; |
282 | 312 | |
283 | 313 | case '/': |
284 | - if ($src->lookaheadByte() === '>') { |
|
314 | + if ($src->lookaheadByte() === '>') |
|
315 | + { |
|
285 | 316 | $this->flush(); |
286 | 317 | $this->tokens[] = new Token( |
287 | 318 | self::TYPE_CLOSE_SHORT, |
@@ -305,7 +336,8 @@ discard block |
||
305 | 336 | break 2; |
306 | 337 | |
307 | 338 | default: |
308 | - if (preg_match(self::REGEXP_WHITESPACE, $n->char)) { |
|
339 | + if (preg_match(self::REGEXP_WHITESPACE, $n->char)) |
|
340 | + { |
|
309 | 341 | $this->flushKeyword(); |
310 | 342 | $this->whitespace[] = $n; |
311 | 343 | break; |
@@ -313,7 +345,8 @@ discard block |
||
313 | 345 | $this->flushWhitespace(); |
314 | 346 | |
315 | 347 | |
316 | - if (!preg_match(self::REGEXP_KEYWORD, $n->char)) { |
|
348 | + if (!preg_match(self::REGEXP_KEYWORD, $n->char)) |
|
349 | + { |
|
317 | 350 | // unexpected char |
318 | 351 | return null; |
319 | 352 | } |
@@ -322,7 +355,8 @@ discard block |
||
322 | 355 | } |
323 | 356 | } |
324 | 357 | |
325 | - if (!$this->isValid()) { |
|
358 | + if (!$this->isValid()) |
|
359 | + { |
|
326 | 360 | return null; |
327 | 361 | } |
328 | 362 | |
@@ -335,17 +369,21 @@ discard block |
||
335 | 369 | private function isValid(): bool |
336 | 370 | { |
337 | 371 | // tag is too short or does not have name keyword |
338 | - if (count($this->tokens) < 3) { |
|
372 | + if (count($this->tokens) < 3) |
|
373 | + { |
|
339 | 374 | return false; |
340 | 375 | } |
341 | 376 | |
342 | 377 | $last = $this->tokens[count($this->tokens) - 1]; |
343 | - if ($last->type !== self::TYPE_CLOSE && $last->type !== self::TYPE_CLOSE_SHORT) { |
|
378 | + if ($last->type !== self::TYPE_CLOSE && $last->type !== self::TYPE_CLOSE_SHORT) |
|
379 | + { |
|
344 | 380 | return false; |
345 | 381 | } |
346 | 382 | |
347 | - foreach ($this->tokens as $token) { |
|
348 | - switch ($token->type) { |
|
383 | + foreach ($this->tokens as $token) |
|
384 | + { |
|
385 | + switch ($token->type) |
|
386 | + { |
|
349 | 387 | case self::TYPE_WHITESPACE: |
350 | 388 | // ignore |
351 | 389 | continue 2; |
@@ -376,7 +414,8 @@ discard block |
||
376 | 414 | */ |
377 | 415 | private function flushWhitespace(): void |
378 | 416 | { |
379 | - if ($this->whitespace === []) { |
|
417 | + if ($this->whitespace === []) |
|
418 | + { |
|
380 | 419 | return; |
381 | 420 | } |
382 | 421 | |
@@ -389,7 +428,8 @@ discard block |
||
389 | 428 | */ |
390 | 429 | private function flushKeyword(): void |
391 | 430 | { |
392 | - if ($this->keyword === []) { |
|
431 | + if ($this->keyword === []) |
|
432 | + { |
|
393 | 433 | return; |
394 | 434 | } |
395 | 435 | |
@@ -402,7 +442,8 @@ discard block |
||
402 | 442 | */ |
403 | 443 | private function flushAttribute(): void |
404 | 444 | { |
405 | - if ($this->attribute === []) { |
|
445 | + if ($this->attribute === []) |
|
446 | + { |
|
406 | 447 | return; |
407 | 448 | } |
408 | 449 |
@@ -48,14 +48,14 @@ discard block |
||
48 | 48 | */ |
49 | 49 | public function parse(Buffer $src): \Generator |
50 | 50 | { |
51 | - while ($n = $src->next()) { |
|
52 | - if (!$n instanceof Byte || $n->char !== '$' || $src->lookaheadByte() !== '{') { |
|
51 | + while ($n = $src->next()){ |
|
52 | + if (!$n instanceof Byte || $n->char !== '$' || $src->lookaheadByte() !== '{'){ |
|
53 | 53 | yield $n; |
54 | 54 | continue; |
55 | 55 | } |
56 | 56 | |
57 | 57 | $binding = (clone $this)->parseGrammar($src, $n->offset); |
58 | - if ($binding === null) { |
|
58 | + if ($binding === null){ |
|
59 | 59 | yield $n; |
60 | 60 | $src->replay($n->offset); |
61 | 61 | continue; |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | */ |
72 | 72 | public static function tokenName(int $token): string |
73 | 73 | { |
74 | - switch ($token) { |
|
74 | + switch ($token){ |
|
75 | 75 | case self::TYPE_OPEN_TAG: |
76 | 76 | return 'INLINE:OPEN_TAG'; |
77 | 77 | case self::TYPE_CLOSE_TAG: |
@@ -95,27 +95,27 @@ discard block |
||
95 | 95 | private function parseGrammar(Buffer $src, int $offset): ?array |
96 | 96 | { |
97 | 97 | $this->tokens = [ |
98 | - new Token(self::TYPE_OPEN_TAG, $offset, '$' . $src->next()->char) |
|
98 | + new Token(self::TYPE_OPEN_TAG, $offset, '$'.$src->next()->char) |
|
99 | 99 | ]; |
100 | 100 | |
101 | - while ($n = $src->next()) { |
|
102 | - if (!$n instanceof Byte) { |
|
101 | + while ($n = $src->next()){ |
|
102 | + if (!$n instanceof Byte){ |
|
103 | 103 | // no other grammars are allowed |
104 | 104 | return null; |
105 | 105 | } |
106 | 106 | |
107 | - switch ($n->char) { |
|
107 | + switch ($n->char){ |
|
108 | 108 | case '"': |
109 | 109 | case "'": |
110 | - if ($this->default === null) { |
|
110 | + if ($this->default === null){ |
|
111 | 111 | // " and ' not allowed in names |
112 | 112 | return null; |
113 | 113 | } |
114 | 114 | |
115 | 115 | $this->default[] = $n; |
116 | - while ($nn = $src->next()) { |
|
116 | + while ($nn = $src->next()){ |
|
117 | 117 | $this->default[] = $nn; |
118 | - if ($nn instanceof Byte && $nn->char === $n->char) { |
|
118 | + if ($nn instanceof Byte && $nn->char === $n->char){ |
|
119 | 119 | break; |
120 | 120 | } |
121 | 121 | } |
@@ -148,17 +148,17 @@ discard block |
||
148 | 148 | break; |
149 | 149 | |
150 | 150 | default: |
151 | - if ($this->default !== null) { |
|
151 | + if ($this->default !== null){ |
|
152 | 152 | // default allows spaces |
153 | 153 | $this->default[] = $n; |
154 | 154 | break; |
155 | 155 | } |
156 | 156 | |
157 | - if (preg_match(self::REGEXP_WHITESPACE, $n->char)) { |
|
157 | + if (preg_match(self::REGEXP_WHITESPACE, $n->char)){ |
|
158 | 158 | break; |
159 | 159 | } |
160 | 160 | |
161 | - if (preg_match(self::REGEXP_KEYWORD, $n->char)) { |
|
161 | + if (preg_match(self::REGEXP_KEYWORD, $n->char)){ |
|
162 | 162 | $this->name[] = $n; |
163 | 163 | break; |
164 | 164 | } |
@@ -167,7 +167,7 @@ discard block |
||
167 | 167 | } |
168 | 168 | } |
169 | 169 | |
170 | - if (!$this->isValid()) { |
|
170 | + if (!$this->isValid()){ |
|
171 | 171 | return null; |
172 | 172 | } |
173 | 173 | |
@@ -179,25 +179,25 @@ discard block |
||
179 | 179 | */ |
180 | 180 | private function isValid(): bool |
181 | 181 | { |
182 | - if (count($this->tokens) < 3) { |
|
182 | + if (count($this->tokens) < 3){ |
|
183 | 183 | return false; |
184 | 184 | } |
185 | 185 | |
186 | 186 | $hasName = false; |
187 | 187 | $hasDefault = null; |
188 | - foreach ($this->tokens as $token) { |
|
189 | - if ($token->type === self::TYPE_NAME) { |
|
188 | + foreach ($this->tokens as $token){ |
|
189 | + if ($token->type === self::TYPE_NAME){ |
|
190 | 190 | $hasName = true; |
191 | 191 | continue; |
192 | 192 | } |
193 | 193 | |
194 | - if ($token->type === self::TYPE_SEPARATOR && $hasDefault === null) { |
|
194 | + if ($token->type === self::TYPE_SEPARATOR && $hasDefault === null){ |
|
195 | 195 | $hasDefault = false; |
196 | 196 | continue; |
197 | 197 | } |
198 | 198 | |
199 | - if ($token->type === self::TYPE_DEFAULT) { |
|
200 | - if ($hasDefault === true) { |
|
199 | + if ($token->type === self::TYPE_DEFAULT){ |
|
200 | + if ($hasDefault === true){ |
|
201 | 201 | // multiple default value |
202 | 202 | return false; |
203 | 203 | } |
@@ -214,7 +214,7 @@ discard block |
||
214 | 214 | */ |
215 | 215 | private function flushName(): void |
216 | 216 | { |
217 | - if ($this->name === []) { |
|
217 | + if ($this->name === []){ |
|
218 | 218 | return; |
219 | 219 | } |
220 | 220 | |
@@ -227,7 +227,7 @@ discard block |
||
227 | 227 | */ |
228 | 228 | private function flushDefault(): void |
229 | 229 | { |
230 | - if ($this->default === [] || $this->default === null) { |
|
230 | + if ($this->default === [] || $this->default === null){ |
|
231 | 231 | return; |
232 | 232 | } |
233 | 233 |
@@ -48,14 +48,17 @@ discard block |
||
48 | 48 | */ |
49 | 49 | public function parse(Buffer $src): \Generator |
50 | 50 | { |
51 | - while ($n = $src->next()) { |
|
52 | - if (!$n instanceof Byte || $n->char !== '$' || $src->lookaheadByte() !== '{') { |
|
51 | + while ($n = $src->next()) |
|
52 | + { |
|
53 | + if (!$n instanceof Byte || $n->char !== '$' || $src->lookaheadByte() !== '{') |
|
54 | + { |
|
53 | 55 | yield $n; |
54 | 56 | continue; |
55 | 57 | } |
56 | 58 | |
57 | 59 | $binding = (clone $this)->parseGrammar($src, $n->offset); |
58 | - if ($binding === null) { |
|
60 | + if ($binding === null) |
|
61 | + { |
|
59 | 62 | yield $n; |
60 | 63 | $src->replay($n->offset); |
61 | 64 | continue; |
@@ -71,7 +74,8 @@ discard block |
||
71 | 74 | */ |
72 | 75 | public static function tokenName(int $token): string |
73 | 76 | { |
74 | - switch ($token) { |
|
77 | + switch ($token) |
|
78 | + { |
|
75 | 79 | case self::TYPE_OPEN_TAG: |
76 | 80 | return 'INLINE:OPEN_TAG'; |
77 | 81 | case self::TYPE_CLOSE_TAG: |
@@ -98,24 +102,30 @@ discard block |
||
98 | 102 | new Token(self::TYPE_OPEN_TAG, $offset, '$' . $src->next()->char) |
99 | 103 | ]; |
100 | 104 | |
101 | - while ($n = $src->next()) { |
|
102 | - if (!$n instanceof Byte) { |
|
105 | + while ($n = $src->next()) |
|
106 | + { |
|
107 | + if (!$n instanceof Byte) |
|
108 | + { |
|
103 | 109 | // no other grammars are allowed |
104 | 110 | return null; |
105 | 111 | } |
106 | 112 | |
107 | - switch ($n->char) { |
|
113 | + switch ($n->char) |
|
114 | + { |
|
108 | 115 | case '"': |
109 | 116 | case "'": |
110 | - if ($this->default === null) { |
|
117 | + if ($this->default === null) |
|
118 | + { |
|
111 | 119 | // " and ' not allowed in names |
112 | 120 | return null; |
113 | 121 | } |
114 | 122 | |
115 | 123 | $this->default[] = $n; |
116 | - while ($nn = $src->next()) { |
|
124 | + while ($nn = $src->next()) |
|
125 | + { |
|
117 | 126 | $this->default[] = $nn; |
118 | - if ($nn instanceof Byte && $nn->char === $n->char) { |
|
127 | + if ($nn instanceof Byte && $nn->char === $n->char) |
|
128 | + { |
|
119 | 129 | break; |
120 | 130 | } |
121 | 131 | } |
@@ -148,17 +158,20 @@ discard block |
||
148 | 158 | break; |
149 | 159 | |
150 | 160 | default: |
151 | - if ($this->default !== null) { |
|
161 | + if ($this->default !== null) |
|
162 | + { |
|
152 | 163 | // default allows spaces |
153 | 164 | $this->default[] = $n; |
154 | 165 | break; |
155 | 166 | } |
156 | 167 | |
157 | - if (preg_match(self::REGEXP_WHITESPACE, $n->char)) { |
|
168 | + if (preg_match(self::REGEXP_WHITESPACE, $n->char)) |
|
169 | + { |
|
158 | 170 | break; |
159 | 171 | } |
160 | 172 | |
161 | - if (preg_match(self::REGEXP_KEYWORD, $n->char)) { |
|
173 | + if (preg_match(self::REGEXP_KEYWORD, $n->char)) |
|
174 | + { |
|
162 | 175 | $this->name[] = $n; |
163 | 176 | break; |
164 | 177 | } |
@@ -167,7 +180,8 @@ discard block |
||
167 | 180 | } |
168 | 181 | } |
169 | 182 | |
170 | - if (!$this->isValid()) { |
|
183 | + if (!$this->isValid()) |
|
184 | + { |
|
171 | 185 | return null; |
172 | 186 | } |
173 | 187 | |
@@ -179,25 +193,31 @@ discard block |
||
179 | 193 | */ |
180 | 194 | private function isValid(): bool |
181 | 195 | { |
182 | - if (count($this->tokens) < 3) { |
|
196 | + if (count($this->tokens) < 3) |
|
197 | + { |
|
183 | 198 | return false; |
184 | 199 | } |
185 | 200 | |
186 | 201 | $hasName = false; |
187 | 202 | $hasDefault = null; |
188 | - foreach ($this->tokens as $token) { |
|
189 | - if ($token->type === self::TYPE_NAME) { |
|
203 | + foreach ($this->tokens as $token) |
|
204 | + { |
|
205 | + if ($token->type === self::TYPE_NAME) |
|
206 | + { |
|
190 | 207 | $hasName = true; |
191 | 208 | continue; |
192 | 209 | } |
193 | 210 | |
194 | - if ($token->type === self::TYPE_SEPARATOR && $hasDefault === null) { |
|
211 | + if ($token->type === self::TYPE_SEPARATOR && $hasDefault === null) |
|
212 | + { |
|
195 | 213 | $hasDefault = false; |
196 | 214 | continue; |
197 | 215 | } |
198 | 216 | |
199 | - if ($token->type === self::TYPE_DEFAULT) { |
|
200 | - if ($hasDefault === true) { |
|
217 | + if ($token->type === self::TYPE_DEFAULT) |
|
218 | + { |
|
219 | + if ($hasDefault === true) |
|
220 | + { |
|
201 | 221 | // multiple default value |
202 | 222 | return false; |
203 | 223 | } |
@@ -214,7 +234,8 @@ discard block |
||
214 | 234 | */ |
215 | 235 | private function flushName(): void |
216 | 236 | { |
217 | - if ($this->name === []) { |
|
237 | + if ($this->name === []) |
|
238 | + { |
|
218 | 239 | return; |
219 | 240 | } |
220 | 241 | |
@@ -227,7 +248,8 @@ discard block |
||
227 | 248 | */ |
228 | 249 | private function flushDefault(): void |
229 | 250 | { |
230 | - if ($this->default === [] || $this->default === null) { |
|
251 | + if ($this->default === [] || $this->default === null) |
|
252 | + { |
|
231 | 253 | return; |
232 | 254 | } |
233 | 255 |
@@ -26,14 +26,14 @@ discard block |
||
26 | 26 | */ |
27 | 27 | public function parse(Buffer $src): \Generator |
28 | 28 | { |
29 | - while ($n = $src->next()) { |
|
30 | - if (!$n instanceof Byte || $n->char !== '<' || $src->lookaheadByte() !== '?') { |
|
29 | + while ($n = $src->next()){ |
|
30 | + if (!$n instanceof Byte || $n->char !== '<' || $src->lookaheadByte() !== '?'){ |
|
31 | 31 | yield $n; |
32 | 32 | continue; |
33 | 33 | } |
34 | 34 | |
35 | - $php = $this->parseGrammar($n->char . $src->nextBytes(), $n->offset); |
|
36 | - if ($php === null) { |
|
35 | + $php = $this->parseGrammar($n->char.$src->nextBytes(), $n->offset); |
|
36 | + if ($php === null){ |
|
37 | 37 | yield $n; |
38 | 38 | $src->replay($n->offset); |
39 | 39 | continue; |
@@ -61,27 +61,27 @@ discard block |
||
61 | 61 | private function parseGrammar(string $content, int $offset): ?Token |
62 | 62 | { |
63 | 63 | $tokens = null; |
64 | - foreach (token_get_all($content) as $token) { |
|
65 | - if ($tokens === null) { |
|
66 | - if (!$this->is($token, [T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO])) { |
|
64 | + foreach (token_get_all($content) as $token){ |
|
65 | + if ($tokens === null){ |
|
66 | + if (!$this->is($token, [T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO])){ |
|
67 | 67 | // not php |
68 | 68 | return null; |
69 | 69 | } |
70 | 70 | } |
71 | 71 | |
72 | 72 | $tokens[] = $token; |
73 | - if ($this->is($token, [T_CLOSE_TAG])) { |
|
73 | + if ($this->is($token, [T_CLOSE_TAG])){ |
|
74 | 74 | break; |
75 | 75 | } |
76 | 76 | } |
77 | 77 | |
78 | - if ($tokens === null) { |
|
78 | + if ($tokens === null){ |
|
79 | 79 | return null; |
80 | 80 | } |
81 | 81 | |
82 | 82 | $buffer = ''; |
83 | - foreach ($tokens as $token) { |
|
84 | - if (!is_array($token)) { |
|
83 | + foreach ($tokens as $token){ |
|
84 | + if (!is_array($token)){ |
|
85 | 85 | $buffer .= $token; |
86 | 86 | continue; |
87 | 87 | } |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | */ |
102 | 102 | private function is($token, array $type): bool |
103 | 103 | { |
104 | - if (!is_array($token)) { |
|
104 | + if (!is_array($token)){ |
|
105 | 105 | return false; |
106 | 106 | } |
107 | 107 |
@@ -26,14 +26,17 @@ discard block |
||
26 | 26 | */ |
27 | 27 | public function parse(Buffer $src): \Generator |
28 | 28 | { |
29 | - while ($n = $src->next()) { |
|
30 | - if (!$n instanceof Byte || $n->char !== '<' || $src->lookaheadByte() !== '?') { |
|
29 | + while ($n = $src->next()) |
|
30 | + { |
|
31 | + if (!$n instanceof Byte || $n->char !== '<' || $src->lookaheadByte() !== '?') |
|
32 | + { |
|
31 | 33 | yield $n; |
32 | 34 | continue; |
33 | 35 | } |
34 | 36 | |
35 | 37 | $php = $this->parseGrammar($n->char . $src->nextBytes(), $n->offset); |
36 | - if ($php === null) { |
|
38 | + if ($php === null) |
|
39 | + { |
|
37 | 40 | yield $n; |
38 | 41 | $src->replay($n->offset); |
39 | 42 | continue; |
@@ -61,27 +64,34 @@ discard block |
||
61 | 64 | private function parseGrammar(string $content, int $offset): ?Token |
62 | 65 | { |
63 | 66 | $tokens = null; |
64 | - foreach (token_get_all($content) as $token) { |
|
65 | - if ($tokens === null) { |
|
66 | - if (!$this->is($token, [T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO])) { |
|
67 | + foreach (token_get_all($content) as $token) |
|
68 | + { |
|
69 | + if ($tokens === null) |
|
70 | + { |
|
71 | + if (!$this->is($token, [T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO])) |
|
72 | + { |
|
67 | 73 | // not php |
68 | 74 | return null; |
69 | 75 | } |
70 | 76 | } |
71 | 77 | |
72 | 78 | $tokens[] = $token; |
73 | - if ($this->is($token, [T_CLOSE_TAG])) { |
|
79 | + if ($this->is($token, [T_CLOSE_TAG])) |
|
80 | + { |
|
74 | 81 | break; |
75 | 82 | } |
76 | 83 | } |
77 | 84 | |
78 | - if ($tokens === null) { |
|
85 | + if ($tokens === null) |
|
86 | + { |
|
79 | 87 | return null; |
80 | 88 | } |
81 | 89 | |
82 | 90 | $buffer = ''; |
83 | - foreach ($tokens as $token) { |
|
84 | - if (!is_array($token)) { |
|
91 | + foreach ($tokens as $token) |
|
92 | + { |
|
93 | + if (!is_array($token)) |
|
94 | + { |
|
85 | 95 | $buffer .= $token; |
86 | 96 | continue; |
87 | 97 | } |
@@ -101,7 +111,8 @@ discard block |
||
101 | 111 | */ |
102 | 112 | private function is($token, array $type): bool |
103 | 113 | { |
104 | - if (!is_array($token)) { |
|
114 | + if (!is_array($token)) |
|
115 | + { |
|
105 | 116 | return false; |
106 | 117 | } |
107 | 118 |
@@ -32,11 +32,11 @@ discard block |
||
32 | 32 | $buffer = null; |
33 | 33 | $bufferOffset = 0; |
34 | 34 | |
35 | - foreach ($inner as $n) { |
|
35 | + foreach ($inner as $n){ |
|
36 | 36 | $token->offset = $token->offset ?? $n->offset; |
37 | 37 | |
38 | - if ($n instanceof Byte) { |
|
39 | - if ($buffer === null) { |
|
38 | + if ($n instanceof Byte){ |
|
39 | + if ($buffer === null){ |
|
40 | 40 | $buffer = ''; |
41 | 41 | $bufferOffset = $n->offset; |
42 | 42 | } |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | continue; |
48 | 48 | } |
49 | 49 | |
50 | - if ($buffer !== null) { |
|
50 | + if ($buffer !== null){ |
|
51 | 51 | $token->tokens[] = new Token( |
52 | 52 | Token::TYPE_RAW, |
53 | 53 | $bufferOffset, |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | $token->tokens[] = $n; |
62 | 62 | } |
63 | 63 | |
64 | - if ($buffer !== null) { |
|
64 | + if ($buffer !== null){ |
|
65 | 65 | $token->tokens[] = new Token( |
66 | 66 | Token::TYPE_RAW, |
67 | 67 | $bufferOffset, |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | ); |
71 | 71 | } |
72 | 72 | |
73 | - if (count($token->tokens) === 1 && $token->tokens[0]->type === Token::TYPE_RAW) { |
|
73 | + if (count($token->tokens) === 1 && $token->tokens[0]->type === Token::TYPE_RAW){ |
|
74 | 74 | $token->tokens = []; |
75 | 75 | } |
76 | 76 |
@@ -32,11 +32,14 @@ discard block |
||
32 | 32 | $buffer = null; |
33 | 33 | $bufferOffset = 0; |
34 | 34 | |
35 | - foreach ($inner as $n) { |
|
35 | + foreach ($inner as $n) |
|
36 | + { |
|
36 | 37 | $token->offset = $token->offset ?? $n->offset; |
37 | 38 | |
38 | - if ($n instanceof Byte) { |
|
39 | - if ($buffer === null) { |
|
39 | + if ($n instanceof Byte) |
|
40 | + { |
|
41 | + if ($buffer === null) |
|
42 | + { |
|
40 | 43 | $buffer = ''; |
41 | 44 | $bufferOffset = $n->offset; |
42 | 45 | } |
@@ -47,7 +50,8 @@ discard block |
||
47 | 50 | continue; |
48 | 51 | } |
49 | 52 | |
50 | - if ($buffer !== null) { |
|
53 | + if ($buffer !== null) |
|
54 | + { |
|
51 | 55 | $token->tokens[] = new Token( |
52 | 56 | Token::TYPE_RAW, |
53 | 57 | $bufferOffset, |
@@ -61,7 +65,8 @@ discard block |
||
61 | 65 | $token->tokens[] = $n; |
62 | 66 | } |
63 | 67 | |
64 | - if ($buffer !== null) { |
|
68 | + if ($buffer !== null) |
|
69 | + { |
|
65 | 70 | $token->tokens[] = new Token( |
66 | 71 | Token::TYPE_RAW, |
67 | 72 | $bufferOffset, |
@@ -70,7 +75,8 @@ discard block |
||
70 | 75 | ); |
71 | 76 | } |
72 | 77 | |
73 | - if (count($token->tokens) === 1 && $token->tokens[0]->type === Token::TYPE_RAW) { |
|
78 | + if (count($token->tokens) === 1 && $token->tokens[0]->type === Token::TYPE_RAW) |
|
79 | + { |
|
74 | 80 | $token->tokens = []; |
75 | 81 | } |
76 | 82 |
@@ -84,33 +84,33 @@ discard block |
||
84 | 84 | */ |
85 | 85 | public function parse(Buffer $src): \Generator |
86 | 86 | { |
87 | - while ($n = $src->next()) { |
|
88 | - if (!$n instanceof Byte) { |
|
87 | + while ($n = $src->next()){ |
|
88 | + if (!$n instanceof Byte){ |
|
89 | 89 | yield $n; |
90 | 90 | continue; |
91 | 91 | } |
92 | 92 | |
93 | - if ($n->char === DirectiveGrammar::DIRECTIVE_CHAR) { |
|
93 | + if ($n->char === DirectiveGrammar::DIRECTIVE_CHAR){ |
|
94 | 94 | if ( |
95 | 95 | $this->echo->nextToken($src) || |
96 | 96 | $this->raw->nextToken($src) || |
97 | 97 | $src->lookaheadByte() === DirectiveGrammar::DIRECTIVE_CHAR |
98 | - ) { |
|
98 | + ){ |
|
99 | 99 | // escaped echo sequence, hide directive byte |
100 | 100 | yield $src->next(); |
101 | 101 | continue; |
102 | 102 | } |
103 | 103 | |
104 | 104 | $directive = new DirectiveGrammar(); |
105 | - if ($directive->parse($src, $n->offset)) { |
|
106 | - if (strtolower($directive->getKeyword()) === self::DECLARE_DIRECTIVE) { |
|
105 | + if ($directive->parse($src, $n->offset)){ |
|
106 | + if (strtolower($directive->getKeyword()) === self::DECLARE_DIRECTIVE){ |
|
107 | 107 | // configure braces syntax |
108 | 108 | $this->declare($directive->getBody()); |
109 | - } else { |
|
109 | + }else{ |
|
110 | 110 | if ( |
111 | 111 | $this->directiveRenderer !== null |
112 | 112 | && !$this->directiveRenderer->hasDirective($directive->getKeyword()) |
113 | - ) { |
|
113 | + ){ |
|
114 | 114 | // directive opening char |
115 | 115 | yield $n; |
116 | 116 | |
@@ -131,15 +131,15 @@ discard block |
||
131 | 131 | |
132 | 132 | /** @var BracesGrammar|null $braces */ |
133 | 133 | $braces = null; |
134 | - if ($this->echo->starts($src, $n)) { |
|
134 | + if ($this->echo->starts($src, $n)){ |
|
135 | 135 | $braces = clone $this->echo; |
136 | - } elseif ($this->raw->starts($src, $n)) { |
|
136 | + } elseif ($this->raw->starts($src, $n)){ |
|
137 | 137 | $braces = clone $this->raw; |
138 | 138 | } |
139 | 139 | |
140 | - if ($braces !== null) { |
|
140 | + if ($braces !== null){ |
|
141 | 141 | $echo = $braces->parse($src, $n); |
142 | - if ($echo !== null) { |
|
142 | + if ($echo !== null){ |
|
143 | 143 | yield from $echo; |
144 | 144 | continue; |
145 | 145 | } |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | */ |
160 | 160 | public static function tokenName(int $token): string |
161 | 161 | { |
162 | - switch ($token) { |
|
162 | + switch ($token){ |
|
163 | 163 | case self::TYPE_OPEN_TAG: |
164 | 164 | return 'DYNAMIC:OPEN_TAG'; |
165 | 165 | case self::TYPE_CLOSE_TAG: |
@@ -184,20 +184,20 @@ discard block |
||
184 | 184 | /** |
185 | 185 | * @param string $body |
186 | 186 | */ |
187 | - private function declare(?string $body): void |
|
187 | + private function declare(?string$body) : void |
|
188 | 188 | { |
189 | - if ($body === null) { |
|
189 | + if ($body === null){ |
|
190 | 190 | return; |
191 | 191 | } |
192 | 192 | |
193 | - foreach ($this->fetchOptions($body) as $option => $value) { |
|
193 | + foreach ($this->fetchOptions($body) as $option => $value){ |
|
194 | 194 | $value = trim($value, '\'" '); |
195 | - switch ($option) { |
|
195 | + switch ($option){ |
|
196 | 196 | case 'syntax': |
197 | 197 | $this->echo->setActive($value !== 'off'); |
198 | 198 | $this->raw->setActive($value !== 'off'); |
199 | 199 | |
200 | - if ($value === 'default') { |
|
200 | + if ($value === 'default'){ |
|
201 | 201 | $this->echo->setStartSequence('{{'); |
202 | 202 | $this->echo->setEndSequence('}}'); |
203 | 203 | $this->raw->setStartSequence('{!!'); |
@@ -238,10 +238,10 @@ discard block |
||
238 | 238 | $keyword = null; |
239 | 239 | |
240 | 240 | /** @var Token $token */ |
241 | - foreach ($lexer->parse(new StringStream($body)) as $token) { |
|
242 | - switch ($token->type) { |
|
241 | + foreach ($lexer->parse(new StringStream($body)) as $token){ |
|
242 | + switch ($token->type){ |
|
243 | 243 | case DeclareGrammar::TYPE_KEYWORD: |
244 | - if ($keyword !== null) { |
|
244 | + if ($keyword !== null){ |
|
245 | 245 | $options[$keyword] = $token->content; |
246 | 246 | $keyword = null; |
247 | 247 | break; |
@@ -249,7 +249,7 @@ discard block |
||
249 | 249 | $keyword = $token->content; |
250 | 250 | break; |
251 | 251 | case DeclareGrammar::TYPE_QUOTED: |
252 | - if ($keyword !== null) { |
|
252 | + if ($keyword !== null){ |
|
253 | 253 | $options[$keyword] = trim($token->content, $token->content[0]); |
254 | 254 | $keyword = null; |
255 | 255 | break; |
@@ -258,7 +258,7 @@ discard block |
||
258 | 258 | $keyword = trim($token->content, $token->content[0]); |
259 | 259 | break; |
260 | 260 | case DeclareGrammar::TYPE_COMMA: |
261 | - if ($keyword !== null) { |
|
261 | + if ($keyword !== null){ |
|
262 | 262 | $options[$keyword] = null; |
263 | 263 | $keyword = null; |
264 | 264 | break; |
@@ -84,13 +84,16 @@ discard block |
||
84 | 84 | */ |
85 | 85 | public function parse(Buffer $src): \Generator |
86 | 86 | { |
87 | - while ($n = $src->next()) { |
|
88 | - if (!$n instanceof Byte) { |
|
87 | + while ($n = $src->next()) |
|
88 | + { |
|
89 | + if (!$n instanceof Byte) |
|
90 | + { |
|
89 | 91 | yield $n; |
90 | 92 | continue; |
91 | 93 | } |
92 | 94 | |
93 | - if ($n->char === DirectiveGrammar::DIRECTIVE_CHAR) { |
|
95 | + if ($n->char === DirectiveGrammar::DIRECTIVE_CHAR) |
|
96 | + { |
|
94 | 97 | if ( |
95 | 98 | $this->echo->nextToken($src) || |
96 | 99 | $this->raw->nextToken($src) || |
@@ -102,11 +105,15 @@ discard block |
||
102 | 105 | } |
103 | 106 | |
104 | 107 | $directive = new DirectiveGrammar(); |
105 | - if ($directive->parse($src, $n->offset)) { |
|
106 | - if (strtolower($directive->getKeyword()) === self::DECLARE_DIRECTIVE) { |
|
108 | + if ($directive->parse($src, $n->offset)) |
|
109 | + { |
|
110 | + if (strtolower($directive->getKeyword()) === self::DECLARE_DIRECTIVE) |
|
111 | + { |
|
107 | 112 | // configure braces syntax |
108 | 113 | $this->declare($directive->getBody()); |
109 | - } else { |
|
114 | + } |
|
115 | + else |
|
116 | + { |
|
110 | 117 | if ( |
111 | 118 | $this->directiveRenderer !== null |
112 | 119 | && !$this->directiveRenderer->hasDirective($directive->getKeyword()) |
@@ -131,15 +138,20 @@ discard block |
||
131 | 138 | |
132 | 139 | /** @var BracesGrammar|null $braces */ |
133 | 140 | $braces = null; |
134 | - if ($this->echo->starts($src, $n)) { |
|
141 | + if ($this->echo->starts($src, $n)) |
|
142 | + { |
|
135 | 143 | $braces = clone $this->echo; |
136 | - } elseif ($this->raw->starts($src, $n)) { |
|
144 | + } |
|
145 | + elseif ($this->raw->starts($src, $n)) |
|
146 | + { |
|
137 | 147 | $braces = clone $this->raw; |
138 | 148 | } |
139 | 149 | |
140 | - if ($braces !== null) { |
|
150 | + if ($braces !== null) |
|
151 | + { |
|
141 | 152 | $echo = $braces->parse($src, $n); |
142 | - if ($echo !== null) { |
|
153 | + if ($echo !== null) |
|
154 | + { |
|
143 | 155 | yield from $echo; |
144 | 156 | continue; |
145 | 157 | } |
@@ -159,7 +171,8 @@ discard block |
||
159 | 171 | */ |
160 | 172 | public static function tokenName(int $token): string |
161 | 173 | { |
162 | - switch ($token) { |
|
174 | + switch ($token) |
|
175 | + { |
|
163 | 176 | case self::TYPE_OPEN_TAG: |
164 | 177 | return 'DYNAMIC:OPEN_TAG'; |
165 | 178 | case self::TYPE_CLOSE_TAG: |
@@ -186,18 +199,22 @@ discard block |
||
186 | 199 | */ |
187 | 200 | private function declare(?string $body): void |
188 | 201 | { |
189 | - if ($body === null) { |
|
202 | + if ($body === null) |
|
203 | + { |
|
190 | 204 | return; |
191 | 205 | } |
192 | 206 | |
193 | - foreach ($this->fetchOptions($body) as $option => $value) { |
|
207 | + foreach ($this->fetchOptions($body) as $option => $value) |
|
208 | + { |
|
194 | 209 | $value = trim($value, '\'" '); |
195 | - switch ($option) { |
|
210 | + switch ($option) |
|
211 | + { |
|
196 | 212 | case 'syntax': |
197 | 213 | $this->echo->setActive($value !== 'off'); |
198 | 214 | $this->raw->setActive($value !== 'off'); |
199 | 215 | |
200 | - if ($value === 'default') { |
|
216 | + if ($value === 'default') |
|
217 | + { |
|
201 | 218 | $this->echo->setStartSequence('{{'); |
202 | 219 | $this->echo->setEndSequence('}}'); |
203 | 220 | $this->raw->setStartSequence('{!!'); |
@@ -238,10 +255,13 @@ discard block |
||
238 | 255 | $keyword = null; |
239 | 256 | |
240 | 257 | /** @var Token $token */ |
241 | - foreach ($lexer->parse(new StringStream($body)) as $token) { |
|
242 | - switch ($token->type) { |
|
258 | + foreach ($lexer->parse(new StringStream($body)) as $token) |
|
259 | + { |
|
260 | + switch ($token->type) |
|
261 | + { |
|
243 | 262 | case DeclareGrammar::TYPE_KEYWORD: |
244 | - if ($keyword !== null) { |
|
263 | + if ($keyword !== null) |
|
264 | + { |
|
245 | 265 | $options[$keyword] = $token->content; |
246 | 266 | $keyword = null; |
247 | 267 | break; |
@@ -249,7 +269,8 @@ discard block |
||
249 | 269 | $keyword = $token->content; |
250 | 270 | break; |
251 | 271 | case DeclareGrammar::TYPE_QUOTED: |
252 | - if ($keyword !== null) { |
|
272 | + if ($keyword !== null) |
|
273 | + { |
|
253 | 274 | $options[$keyword] = trim($token->content, $token->content[0]); |
254 | 275 | $keyword = null; |
255 | 276 | break; |
@@ -258,7 +279,8 @@ discard block |
||
258 | 279 | $keyword = trim($token->content, $token->content[0]); |
259 | 280 | break; |
260 | 281 | case DeclareGrammar::TYPE_COMMA: |
261 | - if ($keyword !== null) { |
|
282 | + if ($keyword !== null) |
|
283 | + { |
|
262 | 284 | $options[$keyword] = null; |
263 | 285 | $keyword = null; |
264 | 286 | break; |