@@ -67,13 +67,16 @@ discard block |
||
| 67 | 67 | */ |
| 68 | 68 | public function parse(Buffer $src): \Generator |
| 69 | 69 | { |
| 70 | - while ($n = $src->next()) { |
|
| 71 | - if (!$n instanceof Byte) { |
|
| 70 | + while ($n = $src->next()) |
|
| 71 | + { |
|
| 72 | + if (!$n instanceof Byte) |
|
| 73 | + { |
|
| 72 | 74 | yield $n; |
| 73 | 75 | continue; |
| 74 | 76 | } |
| 75 | 77 | |
| 76 | - if ($n->char === DirectiveGrammar::DIRECTIVE_CHAR) { |
|
| 78 | + if ($n->char === DirectiveGrammar::DIRECTIVE_CHAR) |
|
| 79 | + { |
|
| 77 | 80 | if ( |
| 78 | 81 | $this->echo->nextToken($src) || |
| 79 | 82 | $this->raw->nextToken($src) || |
@@ -85,11 +88,15 @@ discard block |
||
| 85 | 88 | } |
| 86 | 89 | |
| 87 | 90 | $directive = new DirectiveGrammar(); |
| 88 | - if ($directive->parse($src, $n->offset)) { |
|
| 89 | - if (\strtolower($directive->getKeyword()) === self::DECLARE_DIRECTIVE) { |
|
| 91 | + if ($directive->parse($src, $n->offset)) |
|
| 92 | + { |
|
| 93 | + if (\strtolower($directive->getKeyword()) === self::DECLARE_DIRECTIVE) |
|
| 94 | + { |
|
| 90 | 95 | // configure braces syntax |
| 91 | 96 | $this->declare($directive->getBody()); |
| 92 | - } else { |
|
| 97 | + } |
|
| 98 | + else |
|
| 99 | + { |
|
| 93 | 100 | if ( |
| 94 | 101 | $this->directiveRenderer !== null |
| 95 | 102 | && !$this->directiveRenderer->hasDirective($directive->getKeyword()) |
@@ -107,7 +114,9 @@ discard block |
||
| 107 | 114 | |
| 108 | 115 | $src->replay($directive->getLastOffset()); |
| 109 | 116 | continue; |
| 110 | - } else { |
|
| 117 | + } |
|
| 118 | + else |
|
| 119 | + { |
|
| 111 | 120 | // When we found directive char but it's not a directive, we need to flush the replay buffer |
| 112 | 121 | // because it may contain extra tokens that we don't need to return back to the stream |
| 113 | 122 | $src->flushReplay(); |
@@ -118,15 +127,20 @@ discard block |
||
| 118 | 127 | |
| 119 | 128 | /** @var BracesGrammar|null $braces */ |
| 120 | 129 | $braces = null; |
| 121 | - if ($this->echo->starts($src, $n)) { |
|
| 130 | + if ($this->echo->starts($src, $n)) |
|
| 131 | + { |
|
| 122 | 132 | $braces = clone $this->echo; |
| 123 | - } elseif ($this->raw->starts($src, $n)) { |
|
| 133 | + } |
|
| 134 | + elseif ($this->raw->starts($src, $n)) |
|
| 135 | + { |
|
| 124 | 136 | $braces = clone $this->raw; |
| 125 | 137 | } |
| 126 | 138 | |
| 127 | - if ($braces !== null) { |
|
| 139 | + if ($braces !== null) |
|
| 140 | + { |
|
| 128 | 141 | $echo = $braces->parse($src, $n); |
| 129 | - if ($echo !== null) { |
|
| 142 | + if ($echo !== null) |
|
| 143 | + { |
|
| 130 | 144 | yield from $echo; |
| 131 | 145 | continue; |
| 132 | 146 | } |
@@ -160,18 +174,22 @@ discard block |
||
| 160 | 174 | |
| 161 | 175 | private function declare(?string $body): void |
| 162 | 176 | { |
| 163 | - if ($body === null) { |
|
| 177 | + if ($body === null) |
|
| 178 | + { |
|
| 164 | 179 | return; |
| 165 | 180 | } |
| 166 | 181 | |
| 167 | - foreach ($this->fetchOptions($body) as $option => $value) { |
|
| 182 | + foreach ($this->fetchOptions($body) as $option => $value) |
|
| 183 | + { |
|
| 168 | 184 | $value = \trim((string) $value, '\'" '); |
| 169 | - switch ($option) { |
|
| 185 | + switch ($option) |
|
| 186 | + { |
|
| 170 | 187 | case 'syntax': |
| 171 | 188 | $this->echo->setActive($value !== 'off'); |
| 172 | 189 | $this->raw->setActive($value !== 'off'); |
| 173 | 190 | |
| 174 | - if ($value === 'default') { |
|
| 191 | + if ($value === 'default') |
|
| 192 | + { |
|
| 175 | 193 | $this->echo->setStartSequence('{{'); |
| 176 | 194 | $this->echo->setEndSequence('}}'); |
| 177 | 195 | $this->raw->setStartSequence('{!!'); |
@@ -208,10 +226,13 @@ discard block |
||
| 208 | 226 | $keyword = null; |
| 209 | 227 | |
| 210 | 228 | /** @var Token $token */ |
| 211 | - foreach ($lexer->parse(new StringStream($body)) as $token) { |
|
| 212 | - switch ($token->type) { |
|
| 229 | + foreach ($lexer->parse(new StringStream($body)) as $token) |
|
| 230 | + { |
|
| 231 | + switch ($token->type) |
|
| 232 | + { |
|
| 213 | 233 | case DeclareGrammar::TYPE_KEYWORD: |
| 214 | - if ($keyword !== null) { |
|
| 234 | + if ($keyword !== null) |
|
| 235 | + { |
|
| 215 | 236 | $options[$keyword] = $token->content; |
| 216 | 237 | $keyword = null; |
| 217 | 238 | break; |
@@ -219,7 +240,8 @@ discard block |
||
| 219 | 240 | $keyword = $token->content; |
| 220 | 241 | break; |
| 221 | 242 | case DeclareGrammar::TYPE_QUOTED: |
| 222 | - if ($keyword !== null) { |
|
| 243 | + if ($keyword !== null) |
|
| 244 | + { |
|
| 223 | 245 | $options[$keyword] = \trim($token->content, $token->content[0]); |
| 224 | 246 | $keyword = null; |
| 225 | 247 | break; |
@@ -228,7 +250,8 @@ discard block |
||
| 228 | 250 | $keyword = \trim($token->content, $token->content[0]); |
| 229 | 251 | break; |
| 230 | 252 | case DeclareGrammar::TYPE_COMMA: |
| 231 | - if ($keyword !== null) { |
|
| 253 | + if ($keyword !== null) |
|
| 254 | + { |
|
| 232 | 255 | $options[$keyword] = null; |
| 233 | 256 | $keyword = null; |
| 234 | 257 | break; |
@@ -39,39 +39,48 @@ discard block |
||
| 39 | 39 | $this->body = null; |
| 40 | 40 | $hasWhitespace = false; |
| 41 | 41 | |
| 42 | - while ($n = $src->next()) { |
|
| 43 | - if (!$n instanceof Byte) { |
|
| 42 | + while ($n = $src->next()) |
|
| 43 | + { |
|
| 44 | + if (!$n instanceof Byte) |
|
| 45 | + { |
|
| 44 | 46 | // no other grammars are allowed |
| 45 | 47 | break; |
| 46 | 48 | } |
| 47 | 49 | |
| 48 | - switch ($n->char) { |
|
| 50 | + switch ($n->char) |
|
| 51 | + { |
|
| 49 | 52 | case '(': |
| 50 | 53 | $this->flushName(); |
| 51 | 54 | $this->tokens[] = new Token(DynamicGrammar::TYPE_BODY_OPEN, $n->offset, $n->char); |
| 52 | 55 | |
| 53 | 56 | return $this->parseBody($src); |
| 54 | 57 | default: |
| 55 | - if (\preg_match(self::REGEXP_WHITESPACE, $n->char)) { |
|
| 58 | + if (\preg_match(self::REGEXP_WHITESPACE, $n->char)) |
|
| 59 | + { |
|
| 56 | 60 | $hasWhitespace = true; |
| 57 | - if ($this->name !== []) { |
|
| 61 | + if ($this->name !== []) |
|
| 62 | + { |
|
| 58 | 63 | $this->flushName(); |
| 59 | 64 | $this->tokens[] = new Token(DynamicGrammar::TYPE_WHITESPACE, $n->offset, $n->char); |
| 60 | 65 | break; |
| 61 | 66 | } |
| 62 | 67 | |
| 63 | - if ($this->getLastToken()->type === DynamicGrammar::TYPE_WHITESPACE) { |
|
| 68 | + if ($this->getLastToken()->type === DynamicGrammar::TYPE_WHITESPACE) |
|
| 69 | + { |
|
| 64 | 70 | $this->getLastToken()->content .= $n->char; |
| 65 | 71 | break; |
| 66 | 72 | } |
| 67 | 73 | |
| 68 | 74 | // invalid directive |
| 69 | 75 | return false; |
| 70 | - } elseif ($hasWhitespace) { |
|
| 76 | + } |
|
| 77 | + elseif ($hasWhitespace) |
|
| 78 | + { |
|
| 71 | 79 | return $this->finalize(); |
| 72 | 80 | } |
| 73 | 81 | |
| 74 | - if (!\preg_match(self::REGEXP_KEYWORD, $n->char)) { |
|
| 82 | + if (!\preg_match(self::REGEXP_KEYWORD, $n->char)) |
|
| 83 | + { |
|
| 75 | 84 | $this->flushName(); |
| 76 | 85 | |
| 77 | 86 | return $this->finalize(); |
@@ -93,7 +102,8 @@ discard block |
||
| 93 | 102 | */ |
| 94 | 103 | public function getIterator(): \Generator |
| 95 | 104 | { |
| 96 | - if ($this->tokens === []) { |
|
| 105 | + if ($this->tokens === []) |
|
| 106 | + { |
|
| 97 | 107 | throw new \LogicException('Directive not parsed'); |
| 98 | 108 | } |
| 99 | 109 | |
@@ -113,8 +123,10 @@ discard block |
||
| 113 | 123 | */ |
| 114 | 124 | public function getKeyword(): string |
| 115 | 125 | { |
| 116 | - foreach ($this->tokens as $token) { |
|
| 117 | - if ($token->type === DynamicGrammar::TYPE_KEYWORD) { |
|
| 126 | + foreach ($this->tokens as $token) |
|
| 127 | + { |
|
| 128 | + if ($token->type === DynamicGrammar::TYPE_KEYWORD) |
|
| 129 | + { |
|
| 118 | 130 | return $token->content; |
| 119 | 131 | } |
| 120 | 132 | } |
@@ -127,8 +139,10 @@ discard block |
||
| 127 | 139 | */ |
| 128 | 140 | public function getBody(): ?string |
| 129 | 141 | { |
| 130 | - foreach ($this->tokens as $token) { |
|
| 131 | - if ($token->type === DynamicGrammar::TYPE_BODY) { |
|
| 142 | + foreach ($this->tokens as $token) |
|
| 143 | + { |
|
| 144 | + if ($token->type === DynamicGrammar::TYPE_BODY) |
|
| 145 | + { |
|
| 132 | 146 | return $token->content; |
| 133 | 147 | } |
| 134 | 148 | } |
@@ -141,7 +155,8 @@ discard block |
||
| 141 | 155 | */ |
| 142 | 156 | private function flushName(): void |
| 143 | 157 | { |
| 144 | - if ($this->name === []) { |
|
| 158 | + if ($this->name === []) |
|
| 159 | + { |
|
| 145 | 160 | return; |
| 146 | 161 | } |
| 147 | 162 | |
@@ -159,17 +174,22 @@ discard block |
||
| 159 | 174 | $this->body = []; |
| 160 | 175 | $level = 1; |
| 161 | 176 | |
| 162 | - while ($nn = $src->next()) { |
|
| 163 | - if (!$nn instanceof Byte) { |
|
| 177 | + while ($nn = $src->next()) |
|
| 178 | + { |
|
| 179 | + if (!$nn instanceof Byte) |
|
| 180 | + { |
|
| 164 | 181 | $this->flushBody(); |
| 165 | 182 | return $this->finalize(); |
| 166 | 183 | } |
| 167 | 184 | |
| 168 | - if (\in_array($nn->char, ['"', '"'])) { |
|
| 185 | + if (\in_array($nn->char, ['"', '"'])) |
|
| 186 | + { |
|
| 169 | 187 | $this->body[] = $nn; |
| 170 | - while ($nnn = $src->next()) { |
|
| 188 | + while ($nnn = $src->next()) |
|
| 189 | + { |
|
| 171 | 190 | $this->body[] = $nnn; |
| 172 | - if ($nnn instanceof Byte && $nnn->char === $nn->char) { |
|
| 191 | + if ($nnn instanceof Byte && $nnn->char === $nn->char) |
|
| 192 | + { |
|
| 173 | 193 | break; |
| 174 | 194 | } |
| 175 | 195 | } |
@@ -178,15 +198,18 @@ discard block |
||
| 178 | 198 | |
| 179 | 199 | $this->body[] = $nn; |
| 180 | 200 | |
| 181 | - if ($nn->char === '(') { |
|
| 201 | + if ($nn->char === '(') |
|
| 202 | + { |
|
| 182 | 203 | $level++; |
| 183 | 204 | continue; |
| 184 | 205 | } |
| 185 | 206 | |
| 186 | - if ($nn->char === ')') { |
|
| 207 | + if ($nn->char === ')') |
|
| 208 | + { |
|
| 187 | 209 | $level--; |
| 188 | 210 | |
| 189 | - if ($level === 0) { |
|
| 211 | + if ($level === 0) |
|
| 212 | + { |
|
| 190 | 213 | $n = \array_pop($this->body); |
| 191 | 214 | |
| 192 | 215 | $this->flushBody(); |
@@ -205,7 +228,8 @@ discard block |
||
| 205 | 228 | */ |
| 206 | 229 | private function flushBody(): void |
| 207 | 230 | { |
| 208 | - if ($this->body === []) { |
|
| 231 | + if ($this->body === []) |
|
| 232 | + { |
|
| 209 | 233 | return; |
| 210 | 234 | } |
| 211 | 235 | |
@@ -215,7 +239,8 @@ discard block |
||
| 215 | 239 | |
| 216 | 240 | private function getLastToken(): Token |
| 217 | 241 | { |
| 218 | - if ($this->tokens === []) { |
|
| 242 | + if ($this->tokens === []) |
|
| 243 | + { |
|
| 219 | 244 | throw new \LogicException('Directive not parsed'); |
| 220 | 245 | } |
| 221 | 246 | |
@@ -231,12 +256,15 @@ discard block |
||
| 231 | 256 | |
| 232 | 257 | // A directive must have at least one keyword |
| 233 | 258 | // Without it, it's just a char |
| 234 | - if (\count($tokens) === 1 && $tokens[0]->content === self::DIRECTIVE_CHAR) { |
|
| 259 | + if (\count($tokens) === 1 && $tokens[0]->content === self::DIRECTIVE_CHAR) |
|
| 260 | + { |
|
| 235 | 261 | return false; |
| 236 | 262 | } |
| 237 | 263 | |
| 238 | - foreach (\array_reverse($tokens, true) as $i => $t) { |
|
| 239 | - if ($t->type !== DynamicGrammar::TYPE_WHITESPACE) { |
|
| 264 | + foreach (\array_reverse($tokens, true) as $i => $t) |
|
| 265 | + { |
|
| 266 | + if ($t->type !== DynamicGrammar::TYPE_WHITESPACE) |
|
| 267 | + { |
|
| 240 | 268 | break; |
| 241 | 269 | } |
| 242 | 270 | |
@@ -244,18 +272,22 @@ discard block |
||
| 244 | 272 | } |
| 245 | 273 | |
| 246 | 274 | $body = null; |
| 247 | - foreach ($tokens as $t) { |
|
| 248 | - if ($t->type === DynamicGrammar::TYPE_BODY_OPEN) { |
|
| 275 | + foreach ($tokens as $t) |
|
| 276 | + { |
|
| 277 | + if ($t->type === DynamicGrammar::TYPE_BODY_OPEN) |
|
| 278 | + { |
|
| 249 | 279 | $body = false; |
| 250 | 280 | continue; |
| 251 | 281 | } |
| 252 | 282 | |
| 253 | - if ($t->type === DynamicGrammar::TYPE_BODY_CLOSE) { |
|
| 283 | + if ($t->type === DynamicGrammar::TYPE_BODY_CLOSE) |
|
| 284 | + { |
|
| 254 | 285 | $body = null; |
| 255 | 286 | } |
| 256 | 287 | } |
| 257 | 288 | |
| 258 | - if ($body !== null) { |
|
| 289 | + if ($body !== null) |
|
| 290 | + { |
|
| 259 | 291 | return false; |
| 260 | 292 | } |
| 261 | 293 | |