|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\AssetMapper\Compiler\Parser; |
| 13 | + |
| 14 | +/** |
| 15 | + * Parses JavaScript content to identify sequences of strings, comments, etc. |
| 16 | + * |
| 17 | + * @author Simon André <[email protected]> |
| 18 | + * |
| 19 | + * @internal |
| 20 | + */ |
| 21 | +final class JavascriptSequenceParser |
| 22 | +{ |
| 23 | + private const STATE_DEFAULT = 0; |
| 24 | + private const STATE_COMMENT = 1; |
| 25 | + private const STATE_STRING = 2; |
| 26 | + |
| 27 | + private int $cursor = 0; |
| 28 | + |
| 29 | + private int $contentEnd; |
| 30 | + |
| 31 | + private string $pattern; |
| 32 | + |
| 33 | + private int $currentSequenceType = self::STATE_DEFAULT; |
| 34 | + |
| 35 | + private ?int $currentSequenceEnd = null; |
| 36 | + |
| 37 | + private const COMMENT_SEPARATORS = [ |
| 38 | + '/*', // Multi-line comment |
| 39 | + '//', // Single-line comment |
| 40 | + '"', // Double quote |
| 41 | + '\'', // Single quote |
| 42 | + '`', // Backtick |
| 43 | + ]; |
| 44 | + |
| 45 | + public function __construct( |
| 46 | + private readonly string $content, |
| 47 | + ) { |
| 48 | + $this->contentEnd = \strlen($content); |
| 49 | + |
| 50 | + $this->pattern ??= '/'.implode('|', array_map( |
| 51 | + fn (string $ch): string => preg_quote($ch, '/'), |
| 52 | + self::COMMENT_SEPARATORS |
| 53 | + )).'/'; |
| 54 | + } |
| 55 | + |
| 56 | + public function isString(): bool |
| 57 | + { |
| 58 | + return self::STATE_STRING === $this->currentSequenceType; |
| 59 | + } |
| 60 | + |
| 61 | + public function isExecutable(): bool |
| 62 | + { |
| 63 | + return self::STATE_DEFAULT === $this->currentSequenceType; |
| 64 | + } |
| 65 | + |
| 66 | + public function isComment(): bool |
| 67 | + { |
| 68 | + return self::STATE_COMMENT === $this->currentSequenceType; |
| 69 | + } |
| 70 | + |
| 71 | + public function parseUntil(int $position): void |
| 72 | + { |
| 73 | + if ($position > $this->contentEnd) { |
| 74 | + throw new \RuntimeException('Cannot parse beyond the end of the content.'); |
| 75 | + } |
| 76 | + if ($position < $this->cursor) { |
| 77 | + throw new \RuntimeException('Cannot parse backwards.'); |
| 78 | + } |
| 79 | + |
| 80 | + while ($this->cursor <= $position) { |
| 81 | + // Current CodeSequence ? |
| 82 | + if (null !== $this->currentSequenceEnd) { |
| 83 | + if ($this->currentSequenceEnd > $position) { |
| 84 | + $this->cursor = $position; |
| 85 | + |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + $this->cursor = $this->currentSequenceEnd; |
| 90 | + $this->setSequence(self::STATE_DEFAULT, null); |
| 91 | + } |
| 92 | + |
| 93 | + preg_match($this->pattern, $this->content, $matches, \PREG_OFFSET_CAPTURE, $this->cursor); |
| 94 | + if (!$matches) { |
| 95 | + $this->endsWithSequence(self::STATE_DEFAULT, $position); |
| 96 | + |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + $matchPos = (int) $matches[0][1]; |
| 101 | + $matchChar = $matches[0][0]; |
| 102 | + |
| 103 | + if ($matchPos > $position) { |
| 104 | + $this->setSequence(self::STATE_DEFAULT, $matchPos - 1); |
| 105 | + $this->cursor = $position; |
| 106 | + |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + // Multi-line comment |
| 111 | + if ('/*' === $matchChar) { |
| 112 | + if (false === $endPos = strpos($this->content, '*/', $matchPos + 2)) { |
| 113 | + $this->endsWithSequence(self::STATE_COMMENT, $position); |
| 114 | + |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + $this->cursor = min($endPos + 2, $position); |
| 119 | + $this->setSequence(self::STATE_COMMENT, $endPos + 2); |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + // Single-line comment |
| 124 | + if ('//' === $matchChar) { |
| 125 | + if (false === $endPos = strpos($this->content, "\n", $matchPos + 2)) { |
| 126 | + $this->endsWithSequence(self::STATE_COMMENT, $position); |
| 127 | + |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + $this->cursor = min($endPos + 1, $position); |
| 132 | + $this->setSequence(self::STATE_COMMENT, $endPos + 1); |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + // Single-line string |
| 137 | + if ('"' === $matchChar || "'" === $matchChar) { |
| 138 | + if (false === $endPos = strpos($this->content, $matchChar, $matchPos + 1)) { |
| 139 | + $this->endsWithSequence(self::STATE_STRING, $position); |
| 140 | + |
| 141 | + return; |
| 142 | + } |
| 143 | + while (false !== $endPos && '\\' == $this->content[$endPos - 1]) { |
| 144 | + $endPos = strpos($this->content, $matchChar, $endPos + 1); |
| 145 | + } |
| 146 | + |
| 147 | + $this->cursor = min($endPos + 1, $position); |
| 148 | + $this->setSequence(self::STATE_STRING, $endPos + 1); |
| 149 | + continue; |
| 150 | + } |
| 151 | + |
| 152 | + // Multi-line string |
| 153 | + if ('`' === $matchChar) { |
| 154 | + if (false === $endPos = strpos($this->content, $matchChar, $matchPos + 1)) { |
| 155 | + $this->endsWithSequence(self::STATE_STRING, $position); |
| 156 | + |
| 157 | + return; |
| 158 | + } |
| 159 | + while (false !== $endPos && '\\' == $this->content[$endPos - 1]) { |
| 160 | + $endPos = strpos($this->content, $matchChar, $endPos + 1); |
| 161 | + } |
| 162 | + |
| 163 | + $this->cursor = min($endPos + 1, $position); |
| 164 | + $this->setSequence(self::STATE_STRING, $endPos + 1); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * @param int<self::STATE_*> $type |
| 171 | + */ |
| 172 | + private function endsWithSequence(int $type, int $cursor): void |
| 173 | + { |
| 174 | + $this->cursor = $cursor; |
| 175 | + $this->currentSequenceType = $type; |
| 176 | + $this->currentSequenceEnd = $this->contentEnd; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * @param int<self::STATE_*> $type |
| 181 | + */ |
| 182 | + private function setSequence(int $type, ?int $end = null): void |
| 183 | + { |
| 184 | + $this->currentSequenceType = $type; |
| 185 | + $this->currentSequenceEnd = $end; |
| 186 | + } |
| 187 | +} |
0 commit comments