|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Lsp\Feature\Translation; |
| 4 | + |
| 5 | +use Symfony\Lsp\Parser\Php\PhpStringLiteralDecoder; |
| 6 | + |
| 7 | +final class PhpTranslationCatalogParser |
| 8 | +{ |
| 9 | + /** |
| 10 | + * @return list<array{key: string, message: string, keyOffset: int, keyLength: int}> |
| 11 | + */ |
| 12 | + public function parse(string $source): array |
| 13 | + { |
| 14 | + $tokens = $this->tokens($source); |
| 15 | + foreach ($tokens as $index => $token) { |
| 16 | + if (\T_RETURN !== $token['id']) { |
| 17 | + continue; |
| 18 | + } |
| 19 | + $opening = $this->nextSignificant($tokens, $index + 1); |
| 20 | + if (null === $opening) { |
| 21 | + continue; |
| 22 | + } |
| 23 | + if ('[' === $tokens[$opening]['text']) { |
| 24 | + return $this->arrayEntries($tokens, $opening, ']'); |
| 25 | + } |
| 26 | + if (\T_ARRAY === $tokens[$opening]['id']) { |
| 27 | + $opening = $this->nextSignificant($tokens, $opening + 1); |
| 28 | + if (null !== $opening && '(' === $tokens[$opening]['text']) { |
| 29 | + return $this->arrayEntries($tokens, $opening, ')'); |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return []; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @param list<array{id: int|null, text: string, offset: int}> $tokens |
| 39 | + * |
| 40 | + * @return list<array{key: string, message: string, keyOffset: int, keyLength: int}> |
| 41 | + */ |
| 42 | + private function arrayEntries(array $tokens, int $opening, string $closing): array |
| 43 | + { |
| 44 | + $entries = []; |
| 45 | + $start = $opening + 1; |
| 46 | + $stack = [$closing]; |
| 47 | + for ($index = $start; isset($tokens[$index]); ++$index) { |
| 48 | + $text = $tokens[$index]['text']; |
| 49 | + if (\in_array($text, ['[', '(', '{'], true)) { |
| 50 | + $stack[] = match ($text) { |
| 51 | + '[' => ']', |
| 52 | + '(' => ')', |
| 53 | + '{' => '}', |
| 54 | + }; |
| 55 | + |
| 56 | + continue; |
| 57 | + } |
| 58 | + if ($text === $stack[array_key_last($stack)]) { |
| 59 | + array_pop($stack); |
| 60 | + if ([] === $stack) { |
| 61 | + if (null !== $entry = $this->entry($tokens, $start, $index)) { |
| 62 | + $entries[] = $entry; |
| 63 | + } |
| 64 | + |
| 65 | + break; |
| 66 | + } |
| 67 | + |
| 68 | + continue; |
| 69 | + } |
| 70 | + if (1 === \count($stack) && ',' === $text) { |
| 71 | + if (null !== $entry = $this->entry($tokens, $start, $index)) { |
| 72 | + $entries[] = $entry; |
| 73 | + } |
| 74 | + $start = $index + 1; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return $entries; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @param list<array{id: int|null, text: string, offset: int}> $tokens |
| 83 | + * |
| 84 | + * @return array{key: string, message: string, keyOffset: int, keyLength: int}|null |
| 85 | + */ |
| 86 | + private function entry(array $tokens, int $start, int $end): ?array |
| 87 | + { |
| 88 | + $keyIndex = $this->nextSignificant($tokens, $start, $end); |
| 89 | + if (null === $keyIndex || \T_CONSTANT_ENCAPSED_STRING !== $tokens[$keyIndex]['id']) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + $arrowIndex = $this->nextSignificant($tokens, $keyIndex + 1, $end); |
| 93 | + if (null === $arrowIndex || \T_DOUBLE_ARROW !== $tokens[$arrowIndex]['id']) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + $valueIndex = $this->nextSignificant($tokens, $arrowIndex + 1, $end); |
| 97 | + if (null === $valueIndex) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + $keyToken = $tokens[$keyIndex]; |
| 102 | + $key = $this->quotedString($keyToken['text']); |
| 103 | + if (null === $key) { |
| 104 | + return null; |
| 105 | + } |
| 106 | + |
| 107 | + return [ |
| 108 | + 'key' => $key, |
| 109 | + 'message' => $this->message($tokens, $valueIndex, $end), |
| 110 | + 'keyOffset' => $keyToken['offset'] + 1, |
| 111 | + 'keyLength' => max(0, \strlen($keyToken['text']) - 2), |
| 112 | + ]; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @param list<array{id: int|null, text: string, offset: int}> $tokens |
| 117 | + */ |
| 118 | + private function message(array $tokens, int $start, int $end): string |
| 119 | + { |
| 120 | + $token = $tokens[$start]; |
| 121 | + if (\T_CONSTANT_ENCAPSED_STRING === $token['id'] && null === $this->nextSignificant($tokens, $start + 1, $end)) { |
| 122 | + return $this->quotedString($token['text']) ?? ''; |
| 123 | + } |
| 124 | + if (\T_START_HEREDOC !== $token['id']) { |
| 125 | + return ''; |
| 126 | + } |
| 127 | + |
| 128 | + $content = ''; |
| 129 | + for ($index = $start + 1; $index < $end; ++$index) { |
| 130 | + if (\T_END_HEREDOC === $tokens[$index]['id']) { |
| 131 | + if (null !== $this->nextSignificant($tokens, $index + 1, $end)) { |
| 132 | + return ''; |
| 133 | + } |
| 134 | + $content = $this->stripHeredocIndentation($content, $tokens[$index]['text']); |
| 135 | + $content = preg_replace('/\r?\n$/D', '', $content) ?? $content; |
| 136 | + |
| 137 | + return str_starts_with($token['text'], "<<<'") |
| 138 | + ? $content |
| 139 | + : PhpStringLiteralDecoder::decodeDoubleQuoted($content); |
| 140 | + } |
| 141 | + if (\T_ENCAPSED_AND_WHITESPACE !== $tokens[$index]['id']) { |
| 142 | + return ''; |
| 143 | + } |
| 144 | + $content .= $tokens[$index]['text']; |
| 145 | + } |
| 146 | + |
| 147 | + return ''; |
| 148 | + } |
| 149 | + |
| 150 | + private function quotedString(string $literal): ?string |
| 151 | + { |
| 152 | + $quote = $literal[0] ?? null; |
| 153 | + if (!\in_array($quote, ["'", '"'], true) || !str_ends_with($literal, $quote)) { |
| 154 | + return null; |
| 155 | + } |
| 156 | + |
| 157 | + return PhpStringLiteralDecoder::decode($quote, substr($literal, 1, -1)); |
| 158 | + } |
| 159 | + |
| 160 | + private function stripHeredocIndentation(string $content, string $end): string |
| 161 | + { |
| 162 | + if (1 !== preg_match('/^([ \t]*)[A-Za-z_\x80-\xff][A-Za-z0-9_\x80-\xff]*$/D', $end, $matches) || '' === $matches[1]) { |
| 163 | + return $content; |
| 164 | + } |
| 165 | + |
| 166 | + return preg_replace('/^'.preg_quote($matches[1], '/').'/m', '', $content) ?? $content; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * @return list<array{id: int|null, text: string, offset: int}> |
| 171 | + */ |
| 172 | + private function tokens(string $source): array |
| 173 | + { |
| 174 | + $result = []; |
| 175 | + $offset = 0; |
| 176 | + foreach (token_get_all($source) as $token) { |
| 177 | + $text = \is_array($token) ? $token[1] : $token; |
| 178 | + $result[] = [ |
| 179 | + 'id' => \is_array($token) ? $token[0] : null, |
| 180 | + 'text' => $text, |
| 181 | + 'offset' => $offset, |
| 182 | + ]; |
| 183 | + $offset += \strlen($text); |
| 184 | + } |
| 185 | + |
| 186 | + return $result; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * @param list<array{id: int|null, text: string, offset: int}> $tokens |
| 191 | + */ |
| 192 | + private function nextSignificant(array $tokens, int $start, ?int $end = null): ?int |
| 193 | + { |
| 194 | + $end ??= \count($tokens); |
| 195 | + for ($index = $start; $index < $end; ++$index) { |
| 196 | + if (!\in_array($tokens[$index]['id'], [\T_WHITESPACE, \T_COMMENT, \T_DOC_COMMENT], true)) { |
| 197 | + return $index; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + return null; |
| 202 | + } |
| 203 | +} |
0 commit comments