@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | |
| 47 | 47 | public function __construct( |
| 48 | 48 | private readonly ?DirectiveRendererInterface $directiveRenderer = null, |
| 49 | - ) { |
|
| 49 | + ){ |
|
| 50 | 50 | $this->echo = new BracesGrammar( |
| 51 | 51 | '{{', |
| 52 | 52 | '}}', |
@@ -67,33 +67,33 @@ 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 | + if (!$n instanceof Byte){ |
|
| 72 | 72 | yield $n; |
| 73 | 73 | continue; |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | - if ($n->char === DirectiveGrammar::DIRECTIVE_CHAR) { |
|
| 76 | + if ($n->char === DirectiveGrammar::DIRECTIVE_CHAR){ |
|
| 77 | 77 | if ( |
| 78 | 78 | $this->echo->nextToken($src) || |
| 79 | 79 | $this->raw->nextToken($src) || |
| 80 | 80 | $src->lookaheadByte() === DirectiveGrammar::DIRECTIVE_CHAR |
| 81 | - ) { |
|
| 81 | + ){ |
|
| 82 | 82 | // escaped echo sequence, hide directive byte |
| 83 | 83 | yield $src->next(); |
| 84 | 84 | continue; |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | $directive = new DirectiveGrammar(); |
| 88 | - if ($directive->parse($src, $n->offset)) { |
|
| 89 | - if (\strtolower($directive->getKeyword()) === self::DECLARE_DIRECTIVE) { |
|
| 88 | + if ($directive->parse($src, $n->offset)){ |
|
| 89 | + if (\strtolower($directive->getKeyword()) === self::DECLARE_DIRECTIVE){ |
|
| 90 | 90 | // configure braces syntax |
| 91 | 91 | $this->declare($directive->getBody()); |
| 92 | - } else { |
|
| 92 | + }else{ |
|
| 93 | 93 | if ( |
| 94 | 94 | $this->directiveRenderer !== null |
| 95 | 95 | && !$this->directiveRenderer->hasDirective($directive->getKeyword()) |
| 96 | - ) { |
|
| 96 | + ){ |
|
| 97 | 97 | // directive opening char |
| 98 | 98 | yield $n; |
| 99 | 99 | |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | |
| 108 | 108 | $src->replay($directive->getLastOffset()); |
| 109 | 109 | continue; |
| 110 | - } else { |
|
| 110 | + }else{ |
|
| 111 | 111 | // When we found directive char but it's not a directive, we need to flush the replay buffer |
| 112 | 112 | // because it may contain extra tokens that we don't need to return back to the stream |
| 113 | 113 | $src->flushReplay(); |
@@ -118,15 +118,15 @@ discard block |
||
| 118 | 118 | |
| 119 | 119 | /** @var BracesGrammar|null $braces */ |
| 120 | 120 | $braces = null; |
| 121 | - if ($this->echo->starts($src, $n)) { |
|
| 121 | + if ($this->echo->starts($src, $n)){ |
|
| 122 | 122 | $braces = clone $this->echo; |
| 123 | - } elseif ($this->raw->starts($src, $n)) { |
|
| 123 | + } elseif ($this->raw->starts($src, $n)){ |
|
| 124 | 124 | $braces = clone $this->raw; |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | - if ($braces !== null) { |
|
| 127 | + if ($braces !== null){ |
|
| 128 | 128 | $echo = $braces->parse($src, $n); |
| 129 | - if ($echo !== null) { |
|
| 129 | + if ($echo !== null){ |
|
| 130 | 130 | yield from $echo; |
| 131 | 131 | continue; |
| 132 | 132 | } |
@@ -158,20 +158,20 @@ discard block |
||
| 158 | 158 | }; |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | - private function declare(?string $body): void |
|
| 161 | + private function declare(?string$body) : void |
|
| 162 | 162 | { |
| 163 | - if ($body === null) { |
|
| 163 | + if ($body === null){ |
|
| 164 | 164 | return; |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - foreach ($this->fetchOptions($body) as $option => $value) { |
|
| 168 | - $value = \trim((string) $value, '\'" '); |
|
| 169 | - switch ($option) { |
|
| 167 | + foreach ($this->fetchOptions($body) as $option => $value){ |
|
| 168 | + $value = \trim((string)$value, '\'" '); |
|
| 169 | + switch ($option){ |
|
| 170 | 170 | case 'syntax': |
| 171 | 171 | $this->echo->setActive($value !== 'off'); |
| 172 | 172 | $this->raw->setActive($value !== 'off'); |
| 173 | 173 | |
| 174 | - if ($value === 'default') { |
|
| 174 | + if ($value === 'default'){ |
|
| 175 | 175 | $this->echo->setStartSequence('{{'); |
| 176 | 176 | $this->echo->setEndSequence('}}'); |
| 177 | 177 | $this->raw->setStartSequence('{!!'); |
@@ -208,10 +208,10 @@ discard block |
||
| 208 | 208 | $keyword = null; |
| 209 | 209 | |
| 210 | 210 | /** @var Token $token */ |
| 211 | - foreach ($lexer->parse(new StringStream($body)) as $token) { |
|
| 212 | - switch ($token->type) { |
|
| 211 | + foreach ($lexer->parse(new StringStream($body)) as $token){ |
|
| 212 | + switch ($token->type){ |
|
| 213 | 213 | case DeclareGrammar::TYPE_KEYWORD: |
| 214 | - if ($keyword !== null) { |
|
| 214 | + if ($keyword !== null){ |
|
| 215 | 215 | $options[$keyword] = $token->content; |
| 216 | 216 | $keyword = null; |
| 217 | 217 | break; |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | $keyword = $token->content; |
| 220 | 220 | break; |
| 221 | 221 | case DeclareGrammar::TYPE_QUOTED: |
| 222 | - if ($keyword !== null) { |
|
| 222 | + if ($keyword !== null){ |
|
| 223 | 223 | $options[$keyword] = \trim($token->content, $token->content[0]); |
| 224 | 224 | $keyword = null; |
| 225 | 225 | break; |
@@ -228,7 +228,7 @@ discard block |
||
| 228 | 228 | $keyword = \trim($token->content, $token->content[0]); |
| 229 | 229 | break; |
| 230 | 230 | case DeclareGrammar::TYPE_COMMA: |
| 231 | - if ($keyword !== null) { |
|
| 231 | + if ($keyword !== null){ |
|
| 232 | 232 | $options[$keyword] = null; |
| 233 | 233 | $keyword = null; |
| 234 | 234 | break; |
@@ -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,39 @@ 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 | + if (!$n instanceof Byte){ |
|
| 44 | 44 | // no other grammars are allowed |
| 45 | 45 | break; |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | - switch ($n->char) { |
|
| 48 | + switch ($n->char){ |
|
| 49 | 49 | case '(': |
| 50 | 50 | $this->flushName(); |
| 51 | 51 | $this->tokens[] = new Token(DynamicGrammar::TYPE_BODY_OPEN, $n->offset, $n->char); |
| 52 | 52 | |
| 53 | 53 | return $this->parseBody($src); |
| 54 | 54 | default: |
| 55 | - if (\preg_match(self::REGEXP_WHITESPACE, $n->char)) { |
|
| 55 | + if (\preg_match(self::REGEXP_WHITESPACE, $n->char)){ |
|
| 56 | 56 | $hasWhitespace = true; |
| 57 | - if ($this->name !== []) { |
|
| 57 | + if ($this->name !== []){ |
|
| 58 | 58 | $this->flushName(); |
| 59 | 59 | $this->tokens[] = new Token(DynamicGrammar::TYPE_WHITESPACE, $n->offset, $n->char); |
| 60 | 60 | break; |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | - if ($this->getLastToken()->type === DynamicGrammar::TYPE_WHITESPACE) { |
|
| 63 | + if ($this->getLastToken()->type === DynamicGrammar::TYPE_WHITESPACE){ |
|
| 64 | 64 | $this->getLastToken()->content .= $n->char; |
| 65 | 65 | break; |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | // invalid directive |
| 69 | 69 | return false; |
| 70 | - } elseif ($hasWhitespace) { |
|
| 70 | + } elseif ($hasWhitespace){ |
|
| 71 | 71 | return $this->finalize(); |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - if (!\preg_match(self::REGEXP_KEYWORD, $n->char)) { |
|
| 74 | + if (!\preg_match(self::REGEXP_KEYWORD, $n->char)){ |
|
| 75 | 75 | $this->flushName(); |
| 76 | 76 | |
| 77 | 77 | return $this->finalize(); |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | */ |
| 94 | 94 | public function getIterator(): \Generator |
| 95 | 95 | { |
| 96 | - if ($this->tokens === []) { |
|
| 96 | + if ($this->tokens === []){ |
|
| 97 | 97 | throw new \LogicException('Directive not parsed'); |
| 98 | 98 | } |
| 99 | 99 | |
@@ -113,8 +113,8 @@ discard block |
||
| 113 | 113 | */ |
| 114 | 114 | public function getKeyword(): string |
| 115 | 115 | { |
| 116 | - foreach ($this->tokens as $token) { |
|
| 117 | - if ($token->type === DynamicGrammar::TYPE_KEYWORD) { |
|
| 116 | + foreach ($this->tokens as $token){ |
|
| 117 | + if ($token->type === DynamicGrammar::TYPE_KEYWORD){ |
|
| 118 | 118 | return $token->content; |
| 119 | 119 | } |
| 120 | 120 | } |
@@ -127,8 +127,8 @@ discard block |
||
| 127 | 127 | */ |
| 128 | 128 | public function getBody(): ?string |
| 129 | 129 | { |
| 130 | - foreach ($this->tokens as $token) { |
|
| 131 | - if ($token->type === DynamicGrammar::TYPE_BODY) { |
|
| 130 | + foreach ($this->tokens as $token){ |
|
| 131 | + if ($token->type === DynamicGrammar::TYPE_BODY){ |
|
| 132 | 132 | return $token->content; |
| 133 | 133 | } |
| 134 | 134 | } |
@@ -141,7 +141,7 @@ discard block |
||
| 141 | 141 | */ |
| 142 | 142 | private function flushName(): void |
| 143 | 143 | { |
| 144 | - if ($this->name === []) { |
|
| 144 | + if ($this->name === []){ |
|
| 145 | 145 | return; |
| 146 | 146 | } |
| 147 | 147 | |
@@ -159,17 +159,17 @@ discard block |
||
| 159 | 159 | $this->body = []; |
| 160 | 160 | $level = 1; |
| 161 | 161 | |
| 162 | - while ($nn = $src->next()) { |
|
| 163 | - if (!$nn instanceof Byte) { |
|
| 162 | + while ($nn = $src->next()){ |
|
| 163 | + if (!$nn instanceof Byte){ |
|
| 164 | 164 | $this->flushBody(); |
| 165 | 165 | return $this->finalize(); |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | - if (\in_array($nn->char, ['"', '"'])) { |
|
| 168 | + if (\in_array($nn->char, ['"', '"'])){ |
|
| 169 | 169 | $this->body[] = $nn; |
| 170 | - while ($nnn = $src->next()) { |
|
| 170 | + while ($nnn = $src->next()){ |
|
| 171 | 171 | $this->body[] = $nnn; |
| 172 | - if ($nnn instanceof Byte && $nnn->char === $nn->char) { |
|
| 172 | + if ($nnn instanceof Byte && $nnn->char === $nn->char){ |
|
| 173 | 173 | break; |
| 174 | 174 | } |
| 175 | 175 | } |
@@ -178,15 +178,15 @@ discard block |
||
| 178 | 178 | |
| 179 | 179 | $this->body[] = $nn; |
| 180 | 180 | |
| 181 | - if ($nn->char === '(') { |
|
| 181 | + if ($nn->char === '('){ |
|
| 182 | 182 | $level++; |
| 183 | 183 | continue; |
| 184 | 184 | } |
| 185 | 185 | |
| 186 | - if ($nn->char === ')') { |
|
| 186 | + if ($nn->char === ')'){ |
|
| 187 | 187 | $level--; |
| 188 | 188 | |
| 189 | - if ($level === 0) { |
|
| 189 | + if ($level === 0){ |
|
| 190 | 190 | $n = \array_pop($this->body); |
| 191 | 191 | |
| 192 | 192 | $this->flushBody(); |
@@ -205,7 +205,7 @@ discard block |
||
| 205 | 205 | */ |
| 206 | 206 | private function flushBody(): void |
| 207 | 207 | { |
| 208 | - if ($this->body === []) { |
|
| 208 | + if ($this->body === []){ |
|
| 209 | 209 | return; |
| 210 | 210 | } |
| 211 | 211 | |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | |
| 216 | 216 | private function getLastToken(): Token |
| 217 | 217 | { |
| 218 | - if ($this->tokens === []) { |
|
| 218 | + if ($this->tokens === []){ |
|
| 219 | 219 | throw new \LogicException('Directive not parsed'); |
| 220 | 220 | } |
| 221 | 221 | |
@@ -231,12 +231,12 @@ discard block |
||
| 231 | 231 | |
| 232 | 232 | // A directive must have at least one keyword |
| 233 | 233 | // Without it, it's just a char |
| 234 | - if (\count($tokens) === 1 && $tokens[0]->content === self::DIRECTIVE_CHAR) { |
|
| 234 | + if (\count($tokens) === 1 && $tokens[0]->content === self::DIRECTIVE_CHAR){ |
|
| 235 | 235 | return false; |
| 236 | 236 | } |
| 237 | 237 | |
| 238 | - foreach (\array_reverse($tokens, true) as $i => $t) { |
|
| 239 | - if ($t->type !== DynamicGrammar::TYPE_WHITESPACE) { |
|
| 238 | + foreach (\array_reverse($tokens, true) as $i => $t){ |
|
| 239 | + if ($t->type !== DynamicGrammar::TYPE_WHITESPACE){ |
|
| 240 | 240 | break; |
| 241 | 241 | } |
| 242 | 242 | |
@@ -244,18 +244,18 @@ discard block |
||
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | $body = null; |
| 247 | - foreach ($tokens as $t) { |
|
| 248 | - if ($t->type === DynamicGrammar::TYPE_BODY_OPEN) { |
|
| 247 | + foreach ($tokens as $t){ |
|
| 248 | + if ($t->type === DynamicGrammar::TYPE_BODY_OPEN){ |
|
| 249 | 249 | $body = false; |
| 250 | 250 | continue; |
| 251 | 251 | } |
| 252 | 252 | |
| 253 | - if ($t->type === DynamicGrammar::TYPE_BODY_CLOSE) { |
|
| 253 | + if ($t->type === DynamicGrammar::TYPE_BODY_CLOSE){ |
|
| 254 | 254 | $body = null; |
| 255 | 255 | } |
| 256 | 256 | } |
| 257 | 257 | |
| 258 | - if ($body !== null) { |
|
| 258 | + if ($body !== null){ |
|
| 259 | 259 | return false; |
| 260 | 260 | } |
| 261 | 261 | |
@@ -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 | |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | /** @internal */ |
| 22 | 22 | private readonly \Generator $generator, |
| 23 | 23 | private int $offset = 0, |
| 24 | - ) { |
|
| 24 | + ){ |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | /** |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | */ |
| 33 | 33 | public function getIterator(): \Traversable |
| 34 | 34 | { |
| 35 | - while ($n = $this->next()) { |
|
| 35 | + while ($n = $this->next()){ |
|
| 36 | 36 | yield $n; |
| 37 | 37 | } |
| 38 | 38 | } |
@@ -42,20 +42,20 @@ discard block |
||
| 42 | 42 | return $this->offset; |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | - public function next(): Byte|Token|null |
|
| 45 | + public function next(): Byte | Token | null |
|
| 46 | 46 | { |
| 47 | - if ($this->replay !== []) { |
|
| 47 | + if ($this->replay !== []){ |
|
| 48 | 48 | $n = \array_shift($this->replay); |
| 49 | - } else { |
|
| 49 | + }else{ |
|
| 50 | 50 | $n = $this->generator->current(); |
| 51 | - if ($n === null) { |
|
| 51 | + if ($n === null){ |
|
| 52 | 52 | return null; |
| 53 | 53 | } |
| 54 | 54 | $this->generator->next(); |
| 55 | 55 | $this->buffer[] = $n; |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | - if ($n !== null && $n->offset !== null) { |
|
| 58 | + if ($n !== null && $n->offset !== null){ |
|
| 59 | 59 | $this->offset = $n->offset; |
| 60 | 60 | } |
| 61 | 61 | |
@@ -68,10 +68,10 @@ discard block |
||
| 68 | 68 | public function nextBytes(): string |
| 69 | 69 | { |
| 70 | 70 | $result = ''; |
| 71 | - while ($n = $this->next()) { |
|
| 72 | - if ($n instanceof Byte) { |
|
| 71 | + while ($n = $this->next()){ |
|
| 72 | + if ($n instanceof Byte){ |
|
| 73 | 73 | $result .= $n->char; |
| 74 | - } else { |
|
| 74 | + }else{ |
|
| 75 | 75 | break; |
| 76 | 76 | } |
| 77 | 77 | } |
@@ -82,14 +82,14 @@ discard block |
||
| 82 | 82 | /** |
| 83 | 83 | * Get next generator value without advancing the position. |
| 84 | 84 | */ |
| 85 | - public function lookahead(): Byte|Token|null |
|
| 85 | + public function lookahead(): Byte | Token | null |
|
| 86 | 86 | { |
| 87 | - if ($this->replay !== []) { |
|
| 87 | + if ($this->replay !== []){ |
|
| 88 | 88 | return $this->replay[0]; |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | $n = $this->next(); |
| 92 | - if ($n !== null) { |
|
| 92 | + if ($n !== null){ |
|
| 93 | 93 | \array_unshift($this->replay, $n); |
| 94 | 94 | } |
| 95 | 95 | |
@@ -105,20 +105,20 @@ discard block |
||
| 105 | 105 | { |
| 106 | 106 | $result = ''; |
| 107 | 107 | $replay = []; |
| 108 | - for ($i = 0; $i < $size; $i++) { |
|
| 108 | + for ($i = 0; $i < $size; $i++){ |
|
| 109 | 109 | $n = $this->next(); |
| 110 | - if ($n !== null) { |
|
| 110 | + if ($n !== null){ |
|
| 111 | 111 | $replay[] = $n; |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | - if (!$n instanceof Byte) { |
|
| 114 | + if (!$n instanceof Byte){ |
|
| 115 | 115 | break; |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | $result .= $n->char; |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - foreach (\array_reverse($replay) as $n) { |
|
| 121 | + foreach (\array_reverse($replay) as $n){ |
|
| 122 | 122 | \array_unshift($this->replay, $n); |
| 123 | 123 | } |
| 124 | 124 | |
@@ -130,8 +130,8 @@ discard block |
||
| 130 | 130 | */ |
| 131 | 131 | public function replay(int $offset): void |
| 132 | 132 | { |
| 133 | - foreach ($this->buffer as $n) { |
|
| 134 | - if ($n->offset > $offset) { |
|
| 133 | + foreach ($this->buffer as $n){ |
|
| 134 | + if ($n->offset > $offset){ |
|
| 135 | 135 | $this->replay[] = $n; |
| 136 | 136 | } |
| 137 | 137 | } |