@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | |
| 73 | 73 | public function renderCase(Directive $directive): string |
| 74 | 74 | { |
| 75 | - if ($this->firstSwitchCase) { |
|
| 75 | + if ($this->firstSwitchCase){ |
|
| 76 | 76 | $this->firstSwitchCase = false; |
| 77 | 77 | |
| 78 | 78 | return \sprintf('case (%s): ?>', $directive->body); |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | |
| 84 | 84 | public function renderDefault(Directive $directive): string |
| 85 | 85 | { |
| 86 | - if ($this->firstSwitchCase) { |
|
| 86 | + if ($this->firstSwitchCase){ |
|
| 87 | 87 | $this->firstSwitchCase = false; |
| 88 | 88 | |
| 89 | 89 | return 'default: ?>'; |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | |
| 100 | 100 | public function renderBreak(Directive $directive): string |
| 101 | 101 | { |
| 102 | - if (isset($directive->values[0])) { |
|
| 102 | + if (isset($directive->values[0])){ |
|
| 103 | 103 | return \sprintf('<?php break %s; ?>', $directive->values[0]); |
| 104 | 104 | } |
| 105 | 105 | |
@@ -72,7 +72,8 @@ discard block |
||
| 72 | 72 | |
| 73 | 73 | public function renderCase(Directive $directive): string |
| 74 | 74 | { |
| 75 | - if ($this->firstSwitchCase) { |
|
| 75 | + if ($this->firstSwitchCase) |
|
| 76 | + { |
|
| 76 | 77 | $this->firstSwitchCase = false; |
| 77 | 78 | |
| 78 | 79 | return \sprintf('case (%s): ?>', $directive->body); |
@@ -83,7 +84,8 @@ discard block |
||
| 83 | 84 | |
| 84 | 85 | public function renderDefault(Directive $directive): string |
| 85 | 86 | { |
| 86 | - if ($this->firstSwitchCase) { |
|
| 87 | + if ($this->firstSwitchCase) |
|
| 88 | + { |
|
| 87 | 89 | $this->firstSwitchCase = false; |
| 88 | 90 | |
| 89 | 91 | return 'default: ?>'; |
@@ -99,7 +101,8 @@ discard block |
||
| 99 | 101 | |
| 100 | 102 | public function renderBreak(Directive $directive): string |
| 101 | 103 | { |
| 102 | - if (isset($directive->values[0])) { |
|
| 104 | + if (isset($directive->values[0])) |
|
| 105 | + { |
|
| 103 | 106 | return \sprintf('<?php break %s; ?>', $directive->values[0]); |
| 104 | 107 | } |
| 105 | 108 | |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | |
| 34 | 34 | public function renderBreak(Directive $directive): string |
| 35 | 35 | { |
| 36 | - if (isset($directive->values[0])) { |
|
| 36 | + if (isset($directive->values[0])){ |
|
| 37 | 37 | return \sprintf('<?php break %s; ?>', $directive->values[0]); |
| 38 | 38 | } |
| 39 | 39 | |
@@ -42,7 +42,7 @@ discard block |
||
| 42 | 42 | |
| 43 | 43 | public function renderContinue(Directive $directive): string |
| 44 | 44 | { |
| 45 | - if (isset($directive->values[0])) { |
|
| 45 | + if (isset($directive->values[0])){ |
|
| 46 | 46 | return \sprintf('<?php continue %s; ?>', $directive->values[0]); |
| 47 | 47 | } |
| 48 | 48 | |
@@ -33,7 +33,8 @@ discard block |
||
| 33 | 33 | |
| 34 | 34 | public function renderBreak(Directive $directive): string |
| 35 | 35 | { |
| 36 | - if (isset($directive->values[0])) { |
|
| 36 | + if (isset($directive->values[0])) |
|
| 37 | + { |
|
| 37 | 38 | return \sprintf('<?php break %s; ?>', $directive->values[0]); |
| 38 | 39 | } |
| 39 | 40 | |
@@ -42,7 +43,8 @@ discard block |
||
| 42 | 43 | |
| 43 | 44 | public function renderContinue(Directive $directive): string |
| 44 | 45 | { |
| 45 | - if (isset($directive->values[0])) { |
|
| 46 | + if (isset($directive->values[0])) |
|
| 47 | + { |
|
| 46 | 48 | return \sprintf('<?php continue %s; ?>', $directive->values[0]); |
| 47 | 49 | } |
| 48 | 50 | |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | public ?int $offset, |
| 17 | 17 | public string $content, |
| 18 | 18 | public ?string $grammar = null |
| 19 | - ) { |
|
| 19 | + ){ |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -32,11 +32,11 @@ discard block |
||
| 32 | 32 | 'content' => $this->content, |
| 33 | 33 | ]; |
| 34 | 34 | |
| 35 | - if ($this->grammar !== null) { |
|
| 35 | + if ($this->grammar !== null){ |
|
| 36 | 36 | $token['type'] = \call_user_func([$this->grammar, 'tokenName'], $this->type); |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - if ($this->tokens !== []) { |
|
| 39 | + if ($this->tokens !== []){ |
|
| 40 | 40 | $token['tokens'] = $this->tokens; |
| 41 | 41 | } |
| 42 | 42 | |
@@ -32,11 +32,13 @@ |
||
| 32 | 32 | 'content' => $this->content, |
| 33 | 33 | ]; |
| 34 | 34 | |
| 35 | - if ($this->grammar !== null) { |
|
| 35 | + if ($this->grammar !== null) |
|
| 36 | + { |
|
| 36 | 37 | $token['type'] = \call_user_func([$this->grammar, 'tokenName'], $this->type); |
| 37 | 38 | } |
| 38 | 39 | |
| 39 | - if ($this->tokens !== []) { |
|
| 40 | + if ($this->tokens !== []) |
|
| 41 | + { |
|
| 40 | 42 | $token['tokens'] = $this->tokens; |
| 41 | 43 | } |
| 42 | 44 | |
@@ -20,11 +20,11 @@ discard block |
||
| 20 | 20 | $buffer = null; |
| 21 | 21 | $bufferOffset = 0; |
| 22 | 22 | |
| 23 | - foreach ($inner as $n) { |
|
| 23 | + foreach ($inner as $n){ |
|
| 24 | 24 | $token->offset ??= $n->offset; |
| 25 | 25 | |
| 26 | - if ($n instanceof Byte) { |
|
| 27 | - if ($buffer === null) { |
|
| 26 | + if ($n instanceof Byte){ |
|
| 27 | + if ($buffer === null){ |
|
| 28 | 28 | $buffer = ''; |
| 29 | 29 | $bufferOffset = $n->offset; |
| 30 | 30 | } |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | continue; |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - if ($buffer !== null) { |
|
| 38 | + if ($buffer !== null){ |
|
| 39 | 39 | $token->tokens[] = new Token( |
| 40 | 40 | Token::TYPE_RAW, |
| 41 | 41 | $bufferOffset, |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | $token->tokens[] = $n; |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | - if ($buffer !== null) { |
|
| 52 | + if ($buffer !== null){ |
|
| 53 | 53 | $token->tokens[] = new Token( |
| 54 | 54 | Token::TYPE_RAW, |
| 55 | 55 | $bufferOffset, |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | ); |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | - if (\count($token->tokens) === 1 && $token->tokens[0]->type === Token::TYPE_RAW) { |
|
| 61 | + if (\count($token->tokens) === 1 && $token->tokens[0]->type === Token::TYPE_RAW){ |
|
| 62 | 62 | $token->tokens = []; |
| 63 | 63 | } |
| 64 | 64 | |
@@ -20,11 +20,14 @@ discard block |
||
| 20 | 20 | $buffer = null; |
| 21 | 21 | $bufferOffset = 0; |
| 22 | 22 | |
| 23 | - foreach ($inner as $n) { |
|
| 23 | + foreach ($inner as $n) |
|
| 24 | + { |
|
| 24 | 25 | $token->offset ??= $n->offset; |
| 25 | 26 | |
| 26 | - if ($n instanceof Byte) { |
|
| 27 | - if ($buffer === null) { |
|
| 27 | + if ($n instanceof Byte) |
|
| 28 | + { |
|
| 29 | + if ($buffer === null) |
|
| 30 | + { |
|
| 28 | 31 | $buffer = ''; |
| 29 | 32 | $bufferOffset = $n->offset; |
| 30 | 33 | } |
@@ -35,7 +38,8 @@ discard block |
||
| 35 | 38 | continue; |
| 36 | 39 | } |
| 37 | 40 | |
| 38 | - if ($buffer !== null) { |
|
| 41 | + if ($buffer !== null) |
|
| 42 | + { |
|
| 39 | 43 | $token->tokens[] = new Token( |
| 40 | 44 | Token::TYPE_RAW, |
| 41 | 45 | $bufferOffset, |
@@ -49,7 +53,8 @@ discard block |
||
| 49 | 53 | $token->tokens[] = $n; |
| 50 | 54 | } |
| 51 | 55 | |
| 52 | - if ($buffer !== null) { |
|
| 56 | + if ($buffer !== null) |
|
| 57 | + { |
|
| 53 | 58 | $token->tokens[] = new Token( |
| 54 | 59 | Token::TYPE_RAW, |
| 55 | 60 | $bufferOffset, |
@@ -58,7 +63,8 @@ discard block |
||
| 58 | 63 | ); |
| 59 | 64 | } |
| 60 | 65 | |
| 61 | - if (\count($token->tokens) === 1 && $token->tokens[0]->type === Token::TYPE_RAW) { |
|
| 66 | + if (\count($token->tokens) === 1 && $token->tokens[0]->type === Token::TYPE_RAW) |
|
| 67 | + { |
|
| 62 | 68 | $token->tokens = []; |
| 63 | 69 | } |
| 64 | 70 | |
@@ -35,19 +35,19 @@ discard block |
||
| 35 | 35 | public function parse(Buffer $src): \Generator |
| 36 | 36 | { |
| 37 | 37 | $quoted = []; |
| 38 | - while ($n = $src->next()) { |
|
| 39 | - switch ($n->char) { |
|
| 38 | + while ($n = $src->next()){ |
|
| 39 | + switch ($n->char){ |
|
| 40 | 40 | case '"': |
| 41 | 41 | case "'": |
| 42 | - if ($this->keyword !== []) { |
|
| 42 | + if ($this->keyword !== []){ |
|
| 43 | 43 | yield $this->packToken($this->keyword, self::TYPE_KEYWORD); |
| 44 | 44 | $this->keyword = []; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | $quoted[] = $n; |
| 48 | - while ($nn = $src->next()) { |
|
| 48 | + while ($nn = $src->next()){ |
|
| 49 | 49 | $quoted[] = $nn; |
| 50 | - if ($nn instanceof Byte && $nn->char === $n->char) { |
|
| 50 | + if ($nn instanceof Byte && $nn->char === $n->char){ |
|
| 51 | 51 | break; |
| 52 | 52 | } |
| 53 | 53 | } |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | |
| 58 | 58 | break; |
| 59 | 59 | case '=': |
| 60 | - if ($this->keyword !== []) { |
|
| 60 | + if ($this->keyword !== []){ |
|
| 61 | 61 | yield $this->packToken($this->keyword, self::TYPE_KEYWORD); |
| 62 | 62 | $this->keyword = []; |
| 63 | 63 | } |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | yield new Token(self::TYPE_EQUAL, $n->offset, '='); |
| 66 | 66 | break; |
| 67 | 67 | case ',': |
| 68 | - if ($this->keyword !== []) { |
|
| 68 | + if ($this->keyword !== []){ |
|
| 69 | 69 | yield $this->packToken($this->keyword, self::TYPE_KEYWORD); |
| 70 | 70 | $this->keyword = []; |
| 71 | 71 | } |
@@ -73,8 +73,8 @@ discard block |
||
| 73 | 73 | yield new Token(self::TYPE_COMMA, $n->offset, ','); |
| 74 | 74 | break; |
| 75 | 75 | default: |
| 76 | - if (\preg_match(self::REGEXP_WHITESPACE, $n->char)) { |
|
| 77 | - if ($this->keyword !== []) { |
|
| 76 | + if (\preg_match(self::REGEXP_WHITESPACE, $n->char)){ |
|
| 77 | + if ($this->keyword !== []){ |
|
| 78 | 78 | yield $this->packToken($this->keyword, self::TYPE_KEYWORD); |
| 79 | 79 | $this->keyword = []; |
| 80 | 80 | } |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | } |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | - if ($this->keyword !== []) { |
|
| 89 | + if ($this->keyword !== []){ |
|
| 90 | 90 | yield $this->packToken($this->keyword, self::TYPE_KEYWORD); |
| 91 | 91 | } |
| 92 | 92 | } |
@@ -35,19 +35,24 @@ discard block |
||
| 35 | 35 | public function parse(Buffer $src): \Generator |
| 36 | 36 | { |
| 37 | 37 | $quoted = []; |
| 38 | - while ($n = $src->next()) { |
|
| 39 | - switch ($n->char) { |
|
| 38 | + while ($n = $src->next()) |
|
| 39 | + { |
|
| 40 | + switch ($n->char) |
|
| 41 | + { |
|
| 40 | 42 | case '"': |
| 41 | 43 | case "'": |
| 42 | - if ($this->keyword !== []) { |
|
| 44 | + if ($this->keyword !== []) |
|
| 45 | + { |
|
| 43 | 46 | yield $this->packToken($this->keyword, self::TYPE_KEYWORD); |
| 44 | 47 | $this->keyword = []; |
| 45 | 48 | } |
| 46 | 49 | |
| 47 | 50 | $quoted[] = $n; |
| 48 | - while ($nn = $src->next()) { |
|
| 51 | + while ($nn = $src->next()) |
|
| 52 | + { |
|
| 49 | 53 | $quoted[] = $nn; |
| 50 | - if ($nn instanceof Byte && $nn->char === $n->char) { |
|
| 54 | + if ($nn instanceof Byte && $nn->char === $n->char) |
|
| 55 | + { |
|
| 51 | 56 | break; |
| 52 | 57 | } |
| 53 | 58 | } |
@@ -57,7 +62,8 @@ discard block |
||
| 57 | 62 | |
| 58 | 63 | break; |
| 59 | 64 | case '=': |
| 60 | - if ($this->keyword !== []) { |
|
| 65 | + if ($this->keyword !== []) |
|
| 66 | + { |
|
| 61 | 67 | yield $this->packToken($this->keyword, self::TYPE_KEYWORD); |
| 62 | 68 | $this->keyword = []; |
| 63 | 69 | } |
@@ -65,7 +71,8 @@ discard block |
||
| 65 | 71 | yield new Token(self::TYPE_EQUAL, $n->offset, '='); |
| 66 | 72 | break; |
| 67 | 73 | case ',': |
| 68 | - if ($this->keyword !== []) { |
|
| 74 | + if ($this->keyword !== []) |
|
| 75 | + { |
|
| 69 | 76 | yield $this->packToken($this->keyword, self::TYPE_KEYWORD); |
| 70 | 77 | $this->keyword = []; |
| 71 | 78 | } |
@@ -73,8 +80,10 @@ discard block |
||
| 73 | 80 | yield new Token(self::TYPE_COMMA, $n->offset, ','); |
| 74 | 81 | break; |
| 75 | 82 | default: |
| 76 | - if (\preg_match(self::REGEXP_WHITESPACE, $n->char)) { |
|
| 77 | - if ($this->keyword !== []) { |
|
| 83 | + if (\preg_match(self::REGEXP_WHITESPACE, $n->char)) |
|
| 84 | + { |
|
| 85 | + if ($this->keyword !== []) |
|
| 86 | + { |
|
| 78 | 87 | yield $this->packToken($this->keyword, self::TYPE_KEYWORD); |
| 79 | 88 | $this->keyword = []; |
| 80 | 89 | } |
@@ -86,7 +95,8 @@ discard block |
||
| 86 | 95 | } |
| 87 | 96 | } |
| 88 | 97 | |
| 89 | - if ($this->keyword !== []) { |
|
| 98 | + if ($this->keyword !== []) |
|
| 99 | + { |
|
| 90 | 100 | yield $this->packToken($this->keyword, self::TYPE_KEYWORD); |
| 91 | 101 | } |
| 92 | 102 | } |
@@ -36,39 +36,39 @@ discard block |
||
| 36 | 36 | $this->body = null; |
| 37 | 37 | $hasWhitespace = false; |
| 38 | 38 | |
| 39 | - while ($n = $src->next()) { |
|
| 40 | - if (!$n instanceof Byte) { |
|
| 39 | + while ($n = $src->next()){ |
|
| 40 | + if (!$n instanceof Byte){ |
|
| 41 | 41 | // no other grammars are allowed |
| 42 | 42 | break; |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | - switch ($n->char) { |
|
| 45 | + switch ($n->char){ |
|
| 46 | 46 | case '(': |
| 47 | 47 | $this->flushName(); |
| 48 | 48 | $this->tokens[] = new Token(DynamicGrammar::TYPE_BODY_OPEN, $n->offset, $n->char); |
| 49 | 49 | |
| 50 | 50 | return $this->parseBody($src); |
| 51 | 51 | default: |
| 52 | - if (\preg_match(self::REGEXP_WHITESPACE, $n->char)) { |
|
| 52 | + if (\preg_match(self::REGEXP_WHITESPACE, $n->char)){ |
|
| 53 | 53 | $hasWhitespace = true; |
| 54 | - if ($this->name !== []) { |
|
| 54 | + if ($this->name !== []){ |
|
| 55 | 55 | $this->flushName(); |
| 56 | 56 | $this->tokens[] = new Token(DynamicGrammar::TYPE_WHITESPACE, $n->offset, $n->char); |
| 57 | 57 | break; |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - if ($this->getLastToken()->type === DynamicGrammar::TYPE_WHITESPACE) { |
|
| 60 | + if ($this->getLastToken()->type === DynamicGrammar::TYPE_WHITESPACE){ |
|
| 61 | 61 | $this->getLastToken()->content .= $n->char; |
| 62 | 62 | break; |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | // invalid directive |
| 66 | 66 | return false; |
| 67 | - } elseif ($hasWhitespace) { |
|
| 67 | + } elseif ($hasWhitespace){ |
|
| 68 | 68 | return $this->finalize(); |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - if (!\preg_match(self::REGEXP_KEYWORD, $n->char)) { |
|
| 71 | + if (!\preg_match(self::REGEXP_KEYWORD, $n->char)){ |
|
| 72 | 72 | $this->flushName(); |
| 73 | 73 | |
| 74 | 74 | return $this->finalize(); |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | */ |
| 91 | 91 | public function getIterator(): \Generator |
| 92 | 92 | { |
| 93 | - if ($this->tokens === []) { |
|
| 93 | + if ($this->tokens === []){ |
|
| 94 | 94 | throw new \LogicException('Directive not parsed'); |
| 95 | 95 | } |
| 96 | 96 | |
@@ -110,8 +110,8 @@ discard block |
||
| 110 | 110 | */ |
| 111 | 111 | public function getKeyword(): string |
| 112 | 112 | { |
| 113 | - foreach ($this->tokens as $token) { |
|
| 114 | - if ($token->type === DynamicGrammar::TYPE_KEYWORD) { |
|
| 113 | + foreach ($this->tokens as $token){ |
|
| 114 | + if ($token->type === DynamicGrammar::TYPE_KEYWORD){ |
|
| 115 | 115 | return $token->content; |
| 116 | 116 | } |
| 117 | 117 | } |
@@ -124,8 +124,8 @@ discard block |
||
| 124 | 124 | */ |
| 125 | 125 | public function getBody(): ?string |
| 126 | 126 | { |
| 127 | - foreach ($this->tokens as $token) { |
|
| 128 | - if ($token->type === DynamicGrammar::TYPE_BODY) { |
|
| 127 | + foreach ($this->tokens as $token){ |
|
| 128 | + if ($token->type === DynamicGrammar::TYPE_BODY){ |
|
| 129 | 129 | return $token->content; |
| 130 | 130 | } |
| 131 | 131 | } |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | */ |
| 139 | 139 | private function flushName(): void |
| 140 | 140 | { |
| 141 | - if ($this->name === []) { |
|
| 141 | + if ($this->name === []){ |
|
| 142 | 142 | return; |
| 143 | 143 | } |
| 144 | 144 | |
@@ -156,17 +156,17 @@ discard block |
||
| 156 | 156 | $this->body = []; |
| 157 | 157 | $level = 1; |
| 158 | 158 | |
| 159 | - while ($nn = $src->next()) { |
|
| 160 | - if (!$nn instanceof Byte) { |
|
| 159 | + while ($nn = $src->next()){ |
|
| 160 | + if (!$nn instanceof Byte){ |
|
| 161 | 161 | $this->flushBody(); |
| 162 | 162 | return $this->finalize(); |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | - if (\in_array($nn->char, ['"', '"'])) { |
|
| 165 | + if (\in_array($nn->char, ['"', '"'])){ |
|
| 166 | 166 | $this->body[] = $nn; |
| 167 | - while ($nnn = $src->next()) { |
|
| 167 | + while ($nnn = $src->next()){ |
|
| 168 | 168 | $this->body[] = $nnn; |
| 169 | - if ($nnn instanceof Byte && $nnn->char === $nn->char) { |
|
| 169 | + if ($nnn instanceof Byte && $nnn->char === $nn->char){ |
|
| 170 | 170 | break; |
| 171 | 171 | } |
| 172 | 172 | } |
@@ -175,15 +175,15 @@ discard block |
||
| 175 | 175 | |
| 176 | 176 | $this->body[] = $nn; |
| 177 | 177 | |
| 178 | - if ($nn->char === '(') { |
|
| 178 | + if ($nn->char === '('){ |
|
| 179 | 179 | $level++; |
| 180 | 180 | continue; |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | - if ($nn->char === ')') { |
|
| 183 | + if ($nn->char === ')'){ |
|
| 184 | 184 | $level--; |
| 185 | 185 | |
| 186 | - if ($level === 0) { |
|
| 186 | + if ($level === 0){ |
|
| 187 | 187 | $n = \array_pop($this->body); |
| 188 | 188 | |
| 189 | 189 | $this->flushBody(); |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | */ |
| 203 | 203 | private function flushBody(): void |
| 204 | 204 | { |
| 205 | - if ($this->body === []) { |
|
| 205 | + if ($this->body === []){ |
|
| 206 | 206 | return; |
| 207 | 207 | } |
| 208 | 208 | |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | |
| 213 | 213 | private function getLastToken(): Token |
| 214 | 214 | { |
| 215 | - if ($this->tokens === []) { |
|
| 215 | + if ($this->tokens === []){ |
|
| 216 | 216 | throw new \LogicException('Directive not parsed'); |
| 217 | 217 | } |
| 218 | 218 | |
@@ -226,8 +226,8 @@ discard block |
||
| 226 | 226 | { |
| 227 | 227 | $tokens = $this->tokens; |
| 228 | 228 | |
| 229 | - foreach (\array_reverse($tokens, true) as $i => $t) { |
|
| 230 | - if ($t->type !== DynamicGrammar::TYPE_WHITESPACE) { |
|
| 229 | + foreach (\array_reverse($tokens, true) as $i => $t){ |
|
| 230 | + if ($t->type !== DynamicGrammar::TYPE_WHITESPACE){ |
|
| 231 | 231 | break; |
| 232 | 232 | } |
| 233 | 233 | |
@@ -235,18 +235,18 @@ discard block |
||
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | $body = null; |
| 238 | - foreach ($tokens as $t) { |
|
| 239 | - if ($t->type === DynamicGrammar::TYPE_BODY_OPEN) { |
|
| 238 | + foreach ($tokens as $t){ |
|
| 239 | + if ($t->type === DynamicGrammar::TYPE_BODY_OPEN){ |
|
| 240 | 240 | $body = false; |
| 241 | 241 | continue; |
| 242 | 242 | } |
| 243 | 243 | |
| 244 | - if ($t->type === DynamicGrammar::TYPE_BODY_CLOSE) { |
|
| 244 | + if ($t->type === DynamicGrammar::TYPE_BODY_CLOSE){ |
|
| 245 | 245 | $body = null; |
| 246 | 246 | } |
| 247 | 247 | } |
| 248 | 248 | |
| 249 | - if ($body !== null) { |
|
| 249 | + if ($body !== null){ |
|
| 250 | 250 | return false; |
| 251 | 251 | } |
| 252 | 252 | |
@@ -36,39 +36,48 @@ discard block |
||
| 36 | 36 | $this->body = null; |
| 37 | 37 | $hasWhitespace = false; |
| 38 | 38 | |
| 39 | - while ($n = $src->next()) { |
|
| 40 | - if (!$n instanceof Byte) { |
|
| 39 | + while ($n = $src->next()) |
|
| 40 | + { |
|
| 41 | + if (!$n instanceof Byte) |
|
| 42 | + { |
|
| 41 | 43 | // no other grammars are allowed |
| 42 | 44 | break; |
| 43 | 45 | } |
| 44 | 46 | |
| 45 | - switch ($n->char) { |
|
| 47 | + switch ($n->char) |
|
| 48 | + { |
|
| 46 | 49 | case '(': |
| 47 | 50 | $this->flushName(); |
| 48 | 51 | $this->tokens[] = new Token(DynamicGrammar::TYPE_BODY_OPEN, $n->offset, $n->char); |
| 49 | 52 | |
| 50 | 53 | return $this->parseBody($src); |
| 51 | 54 | default: |
| 52 | - if (\preg_match(self::REGEXP_WHITESPACE, $n->char)) { |
|
| 55 | + if (\preg_match(self::REGEXP_WHITESPACE, $n->char)) |
|
| 56 | + { |
|
| 53 | 57 | $hasWhitespace = true; |
| 54 | - if ($this->name !== []) { |
|
| 58 | + if ($this->name !== []) |
|
| 59 | + { |
|
| 55 | 60 | $this->flushName(); |
| 56 | 61 | $this->tokens[] = new Token(DynamicGrammar::TYPE_WHITESPACE, $n->offset, $n->char); |
| 57 | 62 | break; |
| 58 | 63 | } |
| 59 | 64 | |
| 60 | - if ($this->getLastToken()->type === DynamicGrammar::TYPE_WHITESPACE) { |
|
| 65 | + if ($this->getLastToken()->type === DynamicGrammar::TYPE_WHITESPACE) |
|
| 66 | + { |
|
| 61 | 67 | $this->getLastToken()->content .= $n->char; |
| 62 | 68 | break; |
| 63 | 69 | } |
| 64 | 70 | |
| 65 | 71 | // invalid directive |
| 66 | 72 | return false; |
| 67 | - } elseif ($hasWhitespace) { |
|
| 73 | + } |
|
| 74 | + elseif ($hasWhitespace) |
|
| 75 | + { |
|
| 68 | 76 | return $this->finalize(); |
| 69 | 77 | } |
| 70 | 78 | |
| 71 | - if (!\preg_match(self::REGEXP_KEYWORD, $n->char)) { |
|
| 79 | + if (!\preg_match(self::REGEXP_KEYWORD, $n->char)) |
|
| 80 | + { |
|
| 72 | 81 | $this->flushName(); |
| 73 | 82 | |
| 74 | 83 | return $this->finalize(); |
@@ -90,7 +99,8 @@ discard block |
||
| 90 | 99 | */ |
| 91 | 100 | public function getIterator(): \Generator |
| 92 | 101 | { |
| 93 | - if ($this->tokens === []) { |
|
| 102 | + if ($this->tokens === []) |
|
| 103 | + { |
|
| 94 | 104 | throw new \LogicException('Directive not parsed'); |
| 95 | 105 | } |
| 96 | 106 | |
@@ -110,8 +120,10 @@ discard block |
||
| 110 | 120 | */ |
| 111 | 121 | public function getKeyword(): string |
| 112 | 122 | { |
| 113 | - foreach ($this->tokens as $token) { |
|
| 114 | - if ($token->type === DynamicGrammar::TYPE_KEYWORD) { |
|
| 123 | + foreach ($this->tokens as $token) |
|
| 124 | + { |
|
| 125 | + if ($token->type === DynamicGrammar::TYPE_KEYWORD) |
|
| 126 | + { |
|
| 115 | 127 | return $token->content; |
| 116 | 128 | } |
| 117 | 129 | } |
@@ -124,8 +136,10 @@ discard block |
||
| 124 | 136 | */ |
| 125 | 137 | public function getBody(): ?string |
| 126 | 138 | { |
| 127 | - foreach ($this->tokens as $token) { |
|
| 128 | - if ($token->type === DynamicGrammar::TYPE_BODY) { |
|
| 139 | + foreach ($this->tokens as $token) |
|
| 140 | + { |
|
| 141 | + if ($token->type === DynamicGrammar::TYPE_BODY) |
|
| 142 | + { |
|
| 129 | 143 | return $token->content; |
| 130 | 144 | } |
| 131 | 145 | } |
@@ -138,7 +152,8 @@ discard block |
||
| 138 | 152 | */ |
| 139 | 153 | private function flushName(): void |
| 140 | 154 | { |
| 141 | - if ($this->name === []) { |
|
| 155 | + if ($this->name === []) |
|
| 156 | + { |
|
| 142 | 157 | return; |
| 143 | 158 | } |
| 144 | 159 | |
@@ -156,17 +171,22 @@ discard block |
||
| 156 | 171 | $this->body = []; |
| 157 | 172 | $level = 1; |
| 158 | 173 | |
| 159 | - while ($nn = $src->next()) { |
|
| 160 | - if (!$nn instanceof Byte) { |
|
| 174 | + while ($nn = $src->next()) |
|
| 175 | + { |
|
| 176 | + if (!$nn instanceof Byte) |
|
| 177 | + { |
|
| 161 | 178 | $this->flushBody(); |
| 162 | 179 | return $this->finalize(); |
| 163 | 180 | } |
| 164 | 181 | |
| 165 | - if (\in_array($nn->char, ['"', '"'])) { |
|
| 182 | + if (\in_array($nn->char, ['"', '"'])) |
|
| 183 | + { |
|
| 166 | 184 | $this->body[] = $nn; |
| 167 | - while ($nnn = $src->next()) { |
|
| 185 | + while ($nnn = $src->next()) |
|
| 186 | + { |
|
| 168 | 187 | $this->body[] = $nnn; |
| 169 | - if ($nnn instanceof Byte && $nnn->char === $nn->char) { |
|
| 188 | + if ($nnn instanceof Byte && $nnn->char === $nn->char) |
|
| 189 | + { |
|
| 170 | 190 | break; |
| 171 | 191 | } |
| 172 | 192 | } |
@@ -175,15 +195,18 @@ discard block |
||
| 175 | 195 | |
| 176 | 196 | $this->body[] = $nn; |
| 177 | 197 | |
| 178 | - if ($nn->char === '(') { |
|
| 198 | + if ($nn->char === '(') |
|
| 199 | + { |
|
| 179 | 200 | $level++; |
| 180 | 201 | continue; |
| 181 | 202 | } |
| 182 | 203 | |
| 183 | - if ($nn->char === ')') { |
|
| 204 | + if ($nn->char === ')') |
|
| 205 | + { |
|
| 184 | 206 | $level--; |
| 185 | 207 | |
| 186 | - if ($level === 0) { |
|
| 208 | + if ($level === 0) |
|
| 209 | + { |
|
| 187 | 210 | $n = \array_pop($this->body); |
| 188 | 211 | |
| 189 | 212 | $this->flushBody(); |
@@ -202,7 +225,8 @@ discard block |
||
| 202 | 225 | */ |
| 203 | 226 | private function flushBody(): void |
| 204 | 227 | { |
| 205 | - if ($this->body === []) { |
|
| 228 | + if ($this->body === []) |
|
| 229 | + { |
|
| 206 | 230 | return; |
| 207 | 231 | } |
| 208 | 232 | |
@@ -212,7 +236,8 @@ discard block |
||
| 212 | 236 | |
| 213 | 237 | private function getLastToken(): Token |
| 214 | 238 | { |
| 215 | - if ($this->tokens === []) { |
|
| 239 | + if ($this->tokens === []) |
|
| 240 | + { |
|
| 216 | 241 | throw new \LogicException('Directive not parsed'); |
| 217 | 242 | } |
| 218 | 243 | |
@@ -226,8 +251,10 @@ discard block |
||
| 226 | 251 | { |
| 227 | 252 | $tokens = $this->tokens; |
| 228 | 253 | |
| 229 | - foreach (\array_reverse($tokens, true) as $i => $t) { |
|
| 230 | - if ($t->type !== DynamicGrammar::TYPE_WHITESPACE) { |
|
| 254 | + foreach (\array_reverse($tokens, true) as $i => $t) |
|
| 255 | + { |
|
| 256 | + if ($t->type !== DynamicGrammar::TYPE_WHITESPACE) |
|
| 257 | + { |
|
| 231 | 258 | break; |
| 232 | 259 | } |
| 233 | 260 | |
@@ -235,18 +262,22 @@ discard block |
||
| 235 | 262 | } |
| 236 | 263 | |
| 237 | 264 | $body = null; |
| 238 | - foreach ($tokens as $t) { |
|
| 239 | - if ($t->type === DynamicGrammar::TYPE_BODY_OPEN) { |
|
| 265 | + foreach ($tokens as $t) |
|
| 266 | + { |
|
| 267 | + if ($t->type === DynamicGrammar::TYPE_BODY_OPEN) |
|
| 268 | + { |
|
| 240 | 269 | $body = false; |
| 241 | 270 | continue; |
| 242 | 271 | } |
| 243 | 272 | |
| 244 | - if ($t->type === DynamicGrammar::TYPE_BODY_CLOSE) { |
|
| 273 | + if ($t->type === DynamicGrammar::TYPE_BODY_CLOSE) |
|
| 274 | + { |
|
| 245 | 275 | $body = null; |
| 246 | 276 | } |
| 247 | 277 | } |
| 248 | 278 | |
| 249 | - if ($body !== null) { |
|
| 279 | + if ($body !== null) |
|
| 280 | + { |
|
| 250 | 281 | return false; |
| 251 | 282 | } |
| 252 | 283 | |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | string $endSequence, |
| 28 | 28 | private readonly int $startToken, |
| 29 | 29 | private readonly int $endToken |
| 30 | - ) { |
|
| 30 | + ){ |
|
| 31 | 31 | $this->setStartSequence($startSequence); |
| 32 | 32 | $this->setEndSequence($endSequence); |
| 33 | 33 | } |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | |
| 69 | 69 | public function nextToken(Buffer $src): bool |
| 70 | 70 | { |
| 71 | - return $this->active && $src->lookaheadByte(\strlen((string) $this->startSequence)) === $this->startSequence; |
|
| 71 | + return $this->active && $src->lookaheadByte(\strlen((string)$this->startSequence)) === $this->startSequence; |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | /** |
@@ -76,11 +76,11 @@ discard block |
||
| 76 | 76 | */ |
| 77 | 77 | public function starts(Buffer $src, Byte $n): bool |
| 78 | 78 | { |
| 79 | - if (!$this->active) { |
|
| 79 | + if (!$this->active){ |
|
| 80 | 80 | return false; |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | - return $this->startSequence === ($n->char . $src->lookaheadByte(\strlen((string) $this->startSequence) - 1)); |
|
| 83 | + return $this->startSequence === ($n->char.$src->lookaheadByte(\strlen((string)$this->startSequence) - 1)); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | /** |
@@ -92,30 +92,30 @@ discard block |
||
| 92 | 92 | new Token( |
| 93 | 93 | $this->startToken, |
| 94 | 94 | $n->offset, |
| 95 | - $n->char . $this->nextBytes($src, \strlen((string) $this->startSequence) - 1) |
|
| 95 | + $n->char.$this->nextBytes($src, \strlen((string)$this->startSequence) - 1) |
|
| 96 | 96 | ), |
| 97 | 97 | ]; |
| 98 | 98 | |
| 99 | - while ($n = $src->next()) { |
|
| 100 | - if (!$n instanceof Byte) { |
|
| 99 | + while ($n = $src->next()){ |
|
| 100 | + if (!$n instanceof Byte){ |
|
| 101 | 101 | // no other grammars are allowed |
| 102 | 102 | break; |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | - switch ($n->char) { |
|
| 105 | + switch ($n->char){ |
|
| 106 | 106 | case '"': |
| 107 | 107 | case "'": |
| 108 | 108 | $this->body[] = $n; |
| 109 | - while ($nn = $src->next()) { |
|
| 109 | + while ($nn = $src->next()){ |
|
| 110 | 110 | $this->body[] = $nn; |
| 111 | - if ($nn instanceof Byte && $nn->char === $n->char) { |
|
| 111 | + if ($nn instanceof Byte && $nn->char === $n->char){ |
|
| 112 | 112 | break; |
| 113 | 113 | } |
| 114 | 114 | } |
| 115 | 115 | break; |
| 116 | 116 | |
| 117 | 117 | case $this->endSequence[0]: |
| 118 | - if (!$this->ends($src, $n)) { |
|
| 118 | + if (!$this->ends($src, $n)){ |
|
| 119 | 119 | // still part of body |
| 120 | 120 | $this->body[] = $n; |
| 121 | 121 | break; |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | $this->tokens[] = new Token( |
| 126 | 126 | $this->endToken, |
| 127 | 127 | $n->offset, |
| 128 | - $n->char . $this->nextBytes($src, \strlen((string) $this->endSequence) - 1) |
|
| 128 | + $n->char.$this->nextBytes($src, \strlen((string)$this->endSequence) - 1) |
|
| 129 | 129 | ); |
| 130 | 130 | |
| 131 | 131 | break 2; |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | } |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | - if (\count($this->tokens) !== 3) { |
|
| 137 | + if (\count($this->tokens) !== 3){ |
|
| 138 | 138 | return null; |
| 139 | 139 | } |
| 140 | 140 | |
@@ -146,7 +146,7 @@ discard block |
||
| 146 | 146 | */ |
| 147 | 147 | private function ends(Buffer $src, Byte $n): bool |
| 148 | 148 | { |
| 149 | - return $this->endSequence === ($n->char . $src->lookaheadByte(\strlen((string) $this->endSequence) - 1)); |
|
| 149 | + return $this->endSequence === ($n->char.$src->lookaheadByte(\strlen((string)$this->endSequence) - 1)); |
|
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | /** |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | private function nextBytes(Buffer $src, int $size): string |
| 162 | 162 | { |
| 163 | 163 | $result = ''; |
| 164 | - for ($i = 0; $i < $size; $i++) { |
|
| 164 | + for ($i = 0; $i < $size; $i++){ |
|
| 165 | 165 | $result .= $src->next()->char; |
| 166 | 166 | } |
| 167 | 167 | |
@@ -173,7 +173,7 @@ discard block |
||
| 173 | 173 | */ |
| 174 | 174 | private function flushBody(): void |
| 175 | 175 | { |
| 176 | - if ($this->body === []) { |
|
| 176 | + if ($this->body === []){ |
|
| 177 | 177 | return; |
| 178 | 178 | } |
| 179 | 179 | |
@@ -76,7 +76,8 @@ discard block |
||
| 76 | 76 | */ |
| 77 | 77 | public function starts(Buffer $src, Byte $n): bool |
| 78 | 78 | { |
| 79 | - if (!$this->active) { |
|
| 79 | + if (!$this->active) |
|
| 80 | + { |
|
| 80 | 81 | return false; |
| 81 | 82 | } |
| 82 | 83 | |
@@ -96,26 +97,32 @@ discard block |
||
| 96 | 97 | ), |
| 97 | 98 | ]; |
| 98 | 99 | |
| 99 | - while ($n = $src->next()) { |
|
| 100 | - if (!$n instanceof Byte) { |
|
| 100 | + while ($n = $src->next()) |
|
| 101 | + { |
|
| 102 | + if (!$n instanceof Byte) |
|
| 103 | + { |
|
| 101 | 104 | // no other grammars are allowed |
| 102 | 105 | break; |
| 103 | 106 | } |
| 104 | 107 | |
| 105 | - switch ($n->char) { |
|
| 108 | + switch ($n->char) |
|
| 109 | + { |
|
| 106 | 110 | case '"': |
| 107 | 111 | case "'": |
| 108 | 112 | $this->body[] = $n; |
| 109 | - while ($nn = $src->next()) { |
|
| 113 | + while ($nn = $src->next()) |
|
| 114 | + { |
|
| 110 | 115 | $this->body[] = $nn; |
| 111 | - if ($nn instanceof Byte && $nn->char === $n->char) { |
|
| 116 | + if ($nn instanceof Byte && $nn->char === $n->char) |
|
| 117 | + { |
|
| 112 | 118 | break; |
| 113 | 119 | } |
| 114 | 120 | } |
| 115 | 121 | break; |
| 116 | 122 | |
| 117 | 123 | case $this->endSequence[0]: |
| 118 | - if (!$this->ends($src, $n)) { |
|
| 124 | + if (!$this->ends($src, $n)) |
|
| 125 | + { |
|
| 119 | 126 | // still part of body |
| 120 | 127 | $this->body[] = $n; |
| 121 | 128 | break; |
@@ -134,7 +141,8 @@ discard block |
||
| 134 | 141 | } |
| 135 | 142 | } |
| 136 | 143 | |
| 137 | - if (\count($this->tokens) !== 3) { |
|
| 144 | + if (\count($this->tokens) !== 3) |
|
| 145 | + { |
|
| 138 | 146 | return null; |
| 139 | 147 | } |
| 140 | 148 | |
@@ -161,7 +169,8 @@ discard block |
||
| 161 | 169 | private function nextBytes(Buffer $src, int $size): string |
| 162 | 170 | { |
| 163 | 171 | $result = ''; |
| 164 | - for ($i = 0; $i < $size; $i++) { |
|
| 172 | + for ($i = 0; $i < $size; $i++) |
|
| 173 | + { |
|
| 165 | 174 | $result .= $src->next()->char; |
| 166 | 175 | } |
| 167 | 176 | |
@@ -173,7 +182,8 @@ discard block |
||
| 173 | 182 | */ |
| 174 | 183 | private function flushBody(): void |
| 175 | 184 | { |
| 176 | - if ($this->body === []) { |
|
| 185 | + if ($this->body === []) |
|
| 186 | + { |
|
| 177 | 187 | return; |
| 178 | 188 | } |
| 179 | 189 | |
@@ -38,14 +38,14 @@ discard block |
||
| 38 | 38 | |
| 39 | 39 | public function parse(Buffer $src): \Generator |
| 40 | 40 | { |
| 41 | - while ($n = $src->next()) { |
|
| 42 | - if (!$n instanceof Byte || $n->char !== '$' || $src->lookaheadByte() !== '{') { |
|
| 41 | + while ($n = $src->next()){ |
|
| 42 | + if (!$n instanceof Byte || $n->char !== '$' || $src->lookaheadByte() !== '{'){ |
|
| 43 | 43 | yield $n; |
| 44 | 44 | continue; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | $binding = (clone $this)->parseGrammar($src, $n->offset); |
| 48 | - if ($binding === null) { |
|
| 48 | + if ($binding === null){ |
|
| 49 | 49 | yield $n; |
| 50 | 50 | $src->replay($n->offset); |
| 51 | 51 | continue; |
@@ -80,27 +80,27 @@ discard block |
||
| 80 | 80 | private function parseGrammar(Buffer $src, int $offset): ?array |
| 81 | 81 | { |
| 82 | 82 | $this->tokens = [ |
| 83 | - new Token(self::TYPE_OPEN_TAG, $offset, '$' . $src->next()->char), |
|
| 83 | + new Token(self::TYPE_OPEN_TAG, $offset, '$'.$src->next()->char), |
|
| 84 | 84 | ]; |
| 85 | 85 | |
| 86 | - while ($n = $src->next()) { |
|
| 87 | - if (!$n instanceof Byte) { |
|
| 86 | + while ($n = $src->next()){ |
|
| 87 | + if (!$n instanceof Byte){ |
|
| 88 | 88 | // no other grammars are allowed |
| 89 | 89 | return null; |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - switch ($n->char) { |
|
| 92 | + switch ($n->char){ |
|
| 93 | 93 | case '"': |
| 94 | 94 | case "'": |
| 95 | - if ($this->default === null) { |
|
| 95 | + if ($this->default === null){ |
|
| 96 | 96 | // " and ' not allowed in names |
| 97 | 97 | return null; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | $this->default[] = $n; |
| 101 | - while ($nn = $src->next()) { |
|
| 101 | + while ($nn = $src->next()){ |
|
| 102 | 102 | $this->default[] = $nn; |
| 103 | - if ($nn instanceof Byte && $nn->char === $n->char) { |
|
| 103 | + if ($nn instanceof Byte && $nn->char === $n->char){ |
|
| 104 | 104 | break; |
| 105 | 105 | } |
| 106 | 106 | } |
@@ -133,17 +133,17 @@ discard block |
||
| 133 | 133 | break; |
| 134 | 134 | |
| 135 | 135 | default: |
| 136 | - if ($this->default !== null) { |
|
| 136 | + if ($this->default !== null){ |
|
| 137 | 137 | // default allows spaces |
| 138 | 138 | $this->default[] = $n; |
| 139 | 139 | break; |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | - if (\preg_match(self::REGEXP_WHITESPACE, $n->char)) { |
|
| 142 | + if (\preg_match(self::REGEXP_WHITESPACE, $n->char)){ |
|
| 143 | 143 | break; |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | - if (\preg_match(self::REGEXP_KEYWORD, $n->char)) { |
|
| 146 | + if (\preg_match(self::REGEXP_KEYWORD, $n->char)){ |
|
| 147 | 147 | $this->name[] = $n; |
| 148 | 148 | break; |
| 149 | 149 | } |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | } |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | - if (!$this->isValid()) { |
|
| 155 | + if (!$this->isValid()){ |
|
| 156 | 156 | return null; |
| 157 | 157 | } |
| 158 | 158 | |
@@ -161,25 +161,25 @@ discard block |
||
| 161 | 161 | |
| 162 | 162 | private function isValid(): bool |
| 163 | 163 | { |
| 164 | - if (\count($this->tokens) < 3) { |
|
| 164 | + if (\count($this->tokens) < 3){ |
|
| 165 | 165 | return false; |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | $hasName = false; |
| 169 | 169 | $hasDefault = null; |
| 170 | - foreach ($this->tokens as $token) { |
|
| 171 | - if ($token->type === self::TYPE_NAME) { |
|
| 170 | + foreach ($this->tokens as $token){ |
|
| 171 | + if ($token->type === self::TYPE_NAME){ |
|
| 172 | 172 | $hasName = true; |
| 173 | 173 | continue; |
| 174 | 174 | } |
| 175 | 175 | |
| 176 | - if ($token->type === self::TYPE_SEPARATOR && $hasDefault === null) { |
|
| 176 | + if ($token->type === self::TYPE_SEPARATOR && $hasDefault === null){ |
|
| 177 | 177 | $hasDefault = false; |
| 178 | 178 | continue; |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | - if ($token->type === self::TYPE_DEFAULT) { |
|
| 182 | - if ($hasDefault === true) { |
|
| 181 | + if ($token->type === self::TYPE_DEFAULT){ |
|
| 182 | + if ($hasDefault === true){ |
|
| 183 | 183 | // multiple default value |
| 184 | 184 | return false; |
| 185 | 185 | } |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | */ |
| 197 | 197 | private function flushName(): void |
| 198 | 198 | { |
| 199 | - if ($this->name === []) { |
|
| 199 | + if ($this->name === []){ |
|
| 200 | 200 | return; |
| 201 | 201 | } |
| 202 | 202 | |
@@ -209,7 +209,7 @@ discard block |
||
| 209 | 209 | */ |
| 210 | 210 | private function flushDefault(): void |
| 211 | 211 | { |
| 212 | - if ($this->default === [] || $this->default === null) { |
|
| 212 | + if ($this->default === [] || $this->default === null){ |
|
| 213 | 213 | return; |
| 214 | 214 | } |
| 215 | 215 | |
@@ -38,14 +38,17 @@ discard block |
||
| 38 | 38 | |
| 39 | 39 | public function parse(Buffer $src): \Generator |
| 40 | 40 | { |
| 41 | - while ($n = $src->next()) { |
|
| 42 | - if (!$n instanceof Byte || $n->char !== '$' || $src->lookaheadByte() !== '{') { |
|
| 41 | + while ($n = $src->next()) |
|
| 42 | + { |
|
| 43 | + if (!$n instanceof Byte || $n->char !== '$' || $src->lookaheadByte() !== '{') |
|
| 44 | + { |
|
| 43 | 45 | yield $n; |
| 44 | 46 | continue; |
| 45 | 47 | } |
| 46 | 48 | |
| 47 | 49 | $binding = (clone $this)->parseGrammar($src, $n->offset); |
| 48 | - if ($binding === null) { |
|
| 50 | + if ($binding === null) |
|
| 51 | + { |
|
| 49 | 52 | yield $n; |
| 50 | 53 | $src->replay($n->offset); |
| 51 | 54 | continue; |
@@ -83,24 +86,30 @@ discard block |
||
| 83 | 86 | new Token(self::TYPE_OPEN_TAG, $offset, '$' . $src->next()->char), |
| 84 | 87 | ]; |
| 85 | 88 | |
| 86 | - while ($n = $src->next()) { |
|
| 87 | - if (!$n instanceof Byte) { |
|
| 89 | + while ($n = $src->next()) |
|
| 90 | + { |
|
| 91 | + if (!$n instanceof Byte) |
|
| 92 | + { |
|
| 88 | 93 | // no other grammars are allowed |
| 89 | 94 | return null; |
| 90 | 95 | } |
| 91 | 96 | |
| 92 | - switch ($n->char) { |
|
| 97 | + switch ($n->char) |
|
| 98 | + { |
|
| 93 | 99 | case '"': |
| 94 | 100 | case "'": |
| 95 | - if ($this->default === null) { |
|
| 101 | + if ($this->default === null) |
|
| 102 | + { |
|
| 96 | 103 | // " and ' not allowed in names |
| 97 | 104 | return null; |
| 98 | 105 | } |
| 99 | 106 | |
| 100 | 107 | $this->default[] = $n; |
| 101 | - while ($nn = $src->next()) { |
|
| 108 | + while ($nn = $src->next()) |
|
| 109 | + { |
|
| 102 | 110 | $this->default[] = $nn; |
| 103 | - if ($nn instanceof Byte && $nn->char === $n->char) { |
|
| 111 | + if ($nn instanceof Byte && $nn->char === $n->char) |
|
| 112 | + { |
|
| 104 | 113 | break; |
| 105 | 114 | } |
| 106 | 115 | } |
@@ -133,17 +142,20 @@ discard block |
||
| 133 | 142 | break; |
| 134 | 143 | |
| 135 | 144 | default: |
| 136 | - if ($this->default !== null) { |
|
| 145 | + if ($this->default !== null) |
|
| 146 | + { |
|
| 137 | 147 | // default allows spaces |
| 138 | 148 | $this->default[] = $n; |
| 139 | 149 | break; |
| 140 | 150 | } |
| 141 | 151 | |
| 142 | - if (\preg_match(self::REGEXP_WHITESPACE, $n->char)) { |
|
| 152 | + if (\preg_match(self::REGEXP_WHITESPACE, $n->char)) |
|
| 153 | + { |
|
| 143 | 154 | break; |
| 144 | 155 | } |
| 145 | 156 | |
| 146 | - if (\preg_match(self::REGEXP_KEYWORD, $n->char)) { |
|
| 157 | + if (\preg_match(self::REGEXP_KEYWORD, $n->char)) |
|
| 158 | + { |
|
| 147 | 159 | $this->name[] = $n; |
| 148 | 160 | break; |
| 149 | 161 | } |
@@ -152,7 +164,8 @@ discard block |
||
| 152 | 164 | } |
| 153 | 165 | } |
| 154 | 166 | |
| 155 | - if (!$this->isValid()) { |
|
| 167 | + if (!$this->isValid()) |
|
| 168 | + { |
|
| 156 | 169 | return null; |
| 157 | 170 | } |
| 158 | 171 | |
@@ -161,25 +174,31 @@ discard block |
||
| 161 | 174 | |
| 162 | 175 | private function isValid(): bool |
| 163 | 176 | { |
| 164 | - if (\count($this->tokens) < 3) { |
|
| 177 | + if (\count($this->tokens) < 3) |
|
| 178 | + { |
|
| 165 | 179 | return false; |
| 166 | 180 | } |
| 167 | 181 | |
| 168 | 182 | $hasName = false; |
| 169 | 183 | $hasDefault = null; |
| 170 | - foreach ($this->tokens as $token) { |
|
| 171 | - if ($token->type === self::TYPE_NAME) { |
|
| 184 | + foreach ($this->tokens as $token) |
|
| 185 | + { |
|
| 186 | + if ($token->type === self::TYPE_NAME) |
|
| 187 | + { |
|
| 172 | 188 | $hasName = true; |
| 173 | 189 | continue; |
| 174 | 190 | } |
| 175 | 191 | |
| 176 | - if ($token->type === self::TYPE_SEPARATOR && $hasDefault === null) { |
|
| 192 | + if ($token->type === self::TYPE_SEPARATOR && $hasDefault === null) |
|
| 193 | + { |
|
| 177 | 194 | $hasDefault = false; |
| 178 | 195 | continue; |
| 179 | 196 | } |
| 180 | 197 | |
| 181 | - if ($token->type === self::TYPE_DEFAULT) { |
|
| 182 | - if ($hasDefault === true) { |
|
| 198 | + if ($token->type === self::TYPE_DEFAULT) |
|
| 199 | + { |
|
| 200 | + if ($hasDefault === true) |
|
| 201 | + { |
|
| 183 | 202 | // multiple default value |
| 184 | 203 | return false; |
| 185 | 204 | } |
@@ -196,7 +215,8 @@ discard block |
||
| 196 | 215 | */ |
| 197 | 216 | private function flushName(): void |
| 198 | 217 | { |
| 199 | - if ($this->name === []) { |
|
| 218 | + if ($this->name === []) |
|
| 219 | + { |
|
| 200 | 220 | return; |
| 201 | 221 | } |
| 202 | 222 | |
@@ -209,7 +229,8 @@ discard block |
||
| 209 | 229 | */ |
| 210 | 230 | private function flushDefault(): void |
| 211 | 231 | { |
| 212 | - if ($this->default === [] || $this->default === null) { |
|
| 232 | + if ($this->default === [] || $this->default === null) |
|
| 233 | + { |
|
| 213 | 234 | return; |
| 214 | 235 | } |
| 215 | 236 | |
@@ -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 | |
@@ -114,15 +114,15 @@ discard block |
||
| 114 | 114 | |
| 115 | 115 | /** @var BracesGrammar|null $braces */ |
| 116 | 116 | $braces = null; |
| 117 | - if ($this->echo->starts($src, $n)) { |
|
| 117 | + if ($this->echo->starts($src, $n)){ |
|
| 118 | 118 | $braces = clone $this->echo; |
| 119 | - } elseif ($this->raw->starts($src, $n)) { |
|
| 119 | + } elseif ($this->raw->starts($src, $n)){ |
|
| 120 | 120 | $braces = clone $this->raw; |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | - if ($braces !== null) { |
|
| 123 | + if ($braces !== null){ |
|
| 124 | 124 | $echo = $braces->parse($src, $n); |
| 125 | - if ($echo !== null) { |
|
| 125 | + if ($echo !== null){ |
|
| 126 | 126 | yield from $echo; |
| 127 | 127 | continue; |
| 128 | 128 | } |
@@ -154,20 +154,20 @@ discard block |
||
| 154 | 154 | }; |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | - private function declare(?string $body): void |
|
| 157 | + private function declare(?string$body) : void |
|
| 158 | 158 | { |
| 159 | - if ($body === null) { |
|
| 159 | + if ($body === null){ |
|
| 160 | 160 | return; |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | - foreach ($this->fetchOptions($body) as $option => $value) { |
|
| 163 | + foreach ($this->fetchOptions($body) as $option => $value){ |
|
| 164 | 164 | $value = \trim($value, '\'" '); |
| 165 | - switch ($option) { |
|
| 165 | + switch ($option){ |
|
| 166 | 166 | case 'syntax': |
| 167 | 167 | $this->echo->setActive($value !== 'off'); |
| 168 | 168 | $this->raw->setActive($value !== 'off'); |
| 169 | 169 | |
| 170 | - if ($value === 'default') { |
|
| 170 | + if ($value === 'default'){ |
|
| 171 | 171 | $this->echo->setStartSequence('{{'); |
| 172 | 172 | $this->echo->setEndSequence('}}'); |
| 173 | 173 | $this->raw->setStartSequence('{!!'); |
@@ -204,10 +204,10 @@ discard block |
||
| 204 | 204 | $keyword = null; |
| 205 | 205 | |
| 206 | 206 | /** @var Token $token */ |
| 207 | - foreach ($lexer->parse(new StringStream($body)) as $token) { |
|
| 208 | - switch ($token->type) { |
|
| 207 | + foreach ($lexer->parse(new StringStream($body)) as $token){ |
|
| 208 | + switch ($token->type){ |
|
| 209 | 209 | case DeclareGrammar::TYPE_KEYWORD: |
| 210 | - if ($keyword !== null) { |
|
| 210 | + if ($keyword !== null){ |
|
| 211 | 211 | $options[$keyword] = $token->content; |
| 212 | 212 | $keyword = null; |
| 213 | 213 | break; |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | $keyword = $token->content; |
| 216 | 216 | break; |
| 217 | 217 | case DeclareGrammar::TYPE_QUOTED: |
| 218 | - if ($keyword !== null) { |
|
| 218 | + if ($keyword !== null){ |
|
| 219 | 219 | $options[$keyword] = \trim($token->content, $token->content[0]); |
| 220 | 220 | $keyword = null; |
| 221 | 221 | break; |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | $keyword = \trim($token->content, $token->content[0]); |
| 225 | 225 | break; |
| 226 | 226 | case DeclareGrammar::TYPE_COMMA: |
| 227 | - if ($keyword !== null) { |
|
| 227 | + if ($keyword !== null){ |
|
| 228 | 228 | $options[$keyword] = null; |
| 229 | 229 | $keyword = null; |
| 230 | 230 | 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()) |
@@ -114,15 +121,20 @@ discard block |
||
| 114 | 121 | |
| 115 | 122 | /** @var BracesGrammar|null $braces */ |
| 116 | 123 | $braces = null; |
| 117 | - if ($this->echo->starts($src, $n)) { |
|
| 124 | + if ($this->echo->starts($src, $n)) |
|
| 125 | + { |
|
| 118 | 126 | $braces = clone $this->echo; |
| 119 | - } elseif ($this->raw->starts($src, $n)) { |
|
| 127 | + } |
|
| 128 | + elseif ($this->raw->starts($src, $n)) |
|
| 129 | + { |
|
| 120 | 130 | $braces = clone $this->raw; |
| 121 | 131 | } |
| 122 | 132 | |
| 123 | - if ($braces !== null) { |
|
| 133 | + if ($braces !== null) |
|
| 134 | + { |
|
| 124 | 135 | $echo = $braces->parse($src, $n); |
| 125 | - if ($echo !== null) { |
|
| 136 | + if ($echo !== null) |
|
| 137 | + { |
|
| 126 | 138 | yield from $echo; |
| 127 | 139 | continue; |
| 128 | 140 | } |
@@ -156,18 +168,22 @@ discard block |
||
| 156 | 168 | |
| 157 | 169 | private function declare(?string $body): void |
| 158 | 170 | { |
| 159 | - if ($body === null) { |
|
| 171 | + if ($body === null) |
|
| 172 | + { |
|
| 160 | 173 | return; |
| 161 | 174 | } |
| 162 | 175 | |
| 163 | - foreach ($this->fetchOptions($body) as $option => $value) { |
|
| 176 | + foreach ($this->fetchOptions($body) as $option => $value) |
|
| 177 | + { |
|
| 164 | 178 | $value = \trim($value, '\'" '); |
| 165 | - switch ($option) { |
|
| 179 | + switch ($option) |
|
| 180 | + { |
|
| 166 | 181 | case 'syntax': |
| 167 | 182 | $this->echo->setActive($value !== 'off'); |
| 168 | 183 | $this->raw->setActive($value !== 'off'); |
| 169 | 184 | |
| 170 | - if ($value === 'default') { |
|
| 185 | + if ($value === 'default') |
|
| 186 | + { |
|
| 171 | 187 | $this->echo->setStartSequence('{{'); |
| 172 | 188 | $this->echo->setEndSequence('}}'); |
| 173 | 189 | $this->raw->setStartSequence('{!!'); |
@@ -204,10 +220,13 @@ discard block |
||
| 204 | 220 | $keyword = null; |
| 205 | 221 | |
| 206 | 222 | /** @var Token $token */ |
| 207 | - foreach ($lexer->parse(new StringStream($body)) as $token) { |
|
| 208 | - switch ($token->type) { |
|
| 223 | + foreach ($lexer->parse(new StringStream($body)) as $token) |
|
| 224 | + { |
|
| 225 | + switch ($token->type) |
|
| 226 | + { |
|
| 209 | 227 | case DeclareGrammar::TYPE_KEYWORD: |
| 210 | - if ($keyword !== null) { |
|
| 228 | + if ($keyword !== null) |
|
| 229 | + { |
|
| 211 | 230 | $options[$keyword] = $token->content; |
| 212 | 231 | $keyword = null; |
| 213 | 232 | break; |
@@ -215,7 +234,8 @@ discard block |
||
| 215 | 234 | $keyword = $token->content; |
| 216 | 235 | break; |
| 217 | 236 | case DeclareGrammar::TYPE_QUOTED: |
| 218 | - if ($keyword !== null) { |
|
| 237 | + if ($keyword !== null) |
|
| 238 | + { |
|
| 219 | 239 | $options[$keyword] = \trim($token->content, $token->content[0]); |
| 220 | 240 | $keyword = null; |
| 221 | 241 | break; |
@@ -224,7 +244,8 @@ discard block |
||
| 224 | 244 | $keyword = \trim($token->content, $token->content[0]); |
| 225 | 245 | break; |
| 226 | 246 | case DeclareGrammar::TYPE_COMMA: |
| 227 | - if ($keyword !== null) { |
|
| 247 | + if ($keyword !== null) |
|
| 248 | + { |
|
| 228 | 249 | $options[$keyword] = null; |
| 229 | 250 | $keyword = null; |
| 230 | 251 | break; |