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