-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[AssetMapper] Javascript sequence parser #58999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Symfony/Component/AssetMapper/Compiler/Parser/CodeSequence.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\AssetMapper\Compiler\Parser; | ||
|
||
/** | ||
* Represents a sequence of code (e.g. a string, a comment, a block of code). | ||
* | ||
* @author Simon André <smn.andre@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
class CodeSequence implements \Stringable | ||
{ | ||
public function __construct( | ||
private readonly string $type, | ||
private readonly int $start, | ||
private readonly int $end, | ||
) { | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return $this->type; | ||
} | ||
|
||
public function getStart(): int | ||
{ | ||
return $this->start; | ||
} | ||
|
||
public function getEnd(): int | ||
{ | ||
return $this->end; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return sprintf('%s [%d:%d]', $this->type, $this->start, $this->end); | ||
} | ||
} |
186 changes: 186 additions & 0 deletions
186
src/Symfony/Component/AssetMapper/Compiler/Parser/JavascriptParser.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\AssetMapper\Compiler\Parser; | ||
|
||
/** | ||
* Parses JavaScript content to identify sequences of strings, comments, etc. | ||
* | ||
* @author Simon André <smn.andre@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
class JavascriptParser | ||
{ | ||
private const STATE_DEFAULT = 'DEFAULT'; | ||
private const STATE_COMMENT = 'COMMENT'; | ||
private const STATE_STRING = 'STRING'; | ||
|
||
private int $cursor = 0; | ||
|
||
private int $contentEnd; | ||
|
||
private string $pattern; | ||
|
||
private ?CodeSequence $currentSequence = null; | ||
|
||
/** | ||
* @var CodeSequence[] | ||
*/ | ||
private array $sequences = []; | ||
|
||
private function __construct( | ||
private string $content, | ||
) { | ||
$this->contentEnd = \strlen($content); | ||
|
||
$chars = [ | ||
'/*', // Multi-line comment | ||
'//', // Single-line comment | ||
'"', // Double quote | ||
'\'', // Single quote | ||
'`', // Backtick | ||
]; | ||
$this->pattern = '/'.implode('|', array_map(fn ($ch) => preg_quote($ch, '/'), $chars)).'/'; | ||
} | ||
|
||
public static function create(string $content): self | ||
{ | ||
return new self($content); | ||
} | ||
|
||
/** | ||
* @return CodeSequence[] | ||
*/ | ||
public function getSequences(): array | ||
{ | ||
return $this->sequences; | ||
} | ||
|
||
public function getCurrentSequence(): ?CodeSequence | ||
{ | ||
return $this->currentSequence; | ||
} | ||
|
||
public function isExecutable(): bool | ||
{ | ||
return self::STATE_DEFAULT === $this->currentSequence?->getType(); | ||
} | ||
|
||
public function parseUntil(int $position): void | ||
{ | ||
if ($position > $this->contentEnd) { | ||
throw new \InvalidArgumentException('Cannot parse beyond the end of the content.'); | ||
} | ||
if ($position < $this->cursor) { | ||
throw new \InvalidArgumentException('Cannot parse backwards.'); | ||
} | ||
|
||
while ($this->cursor <= $position) { | ||
// Current CodeSequence ? | ||
if (null !== $this->currentSequence) { | ||
if ($this->currentSequence->getEnd() > $position) { | ||
$this->cursor = $position; | ||
|
||
return; | ||
} | ||
|
||
$this->cursor = $this->currentSequence->getEnd(); | ||
$this->currentSequence = null; | ||
} | ||
|
||
preg_match($this->pattern, $this->content, $matches, \PREG_OFFSET_CAPTURE, $this->cursor); | ||
if (!$matches) { | ||
$this->cursor = $position; | ||
$this->pushSequence(self::STATE_DEFAULT, $this->cursor, $this->contentEnd); | ||
|
||
return; | ||
} | ||
|
||
$matchPos = (int) $matches[0][1]; | ||
$matchChar = $matches[0][0]; | ||
|
||
if ($matchPos > $position) { | ||
$this->pushSequence(self::STATE_DEFAULT, $this->cursor, $matchPos - 1); | ||
$this->cursor = $position; | ||
|
||
return; | ||
} | ||
|
||
// Multi-line comment | ||
if ('/*' === $matchChar) { | ||
if (false === $endPos = strpos($this->content, '*/', $matchPos + 2)) { | ||
$this->cursor = $position; | ||
$this->pushSequence(self::STATE_COMMENT, $matchPos, $this->contentEnd); | ||
|
||
return; | ||
} | ||
|
||
$this->cursor = min($endPos + 2, $position); | ||
$this->pushSequence(self::STATE_COMMENT, $matchPos, $endPos + 2); | ||
continue; | ||
} | ||
|
||
// Single-line comment | ||
if ('//' === $matchChar) { | ||
if (false === $endPos = strpos($this->content, "\n", $matchPos + 2)) { | ||
$this->cursor = $position; | ||
$this->pushSequence(self::STATE_COMMENT, $matchPos, $this->contentEnd); | ||
|
||
return; | ||
} | ||
|
||
$this->cursor = min($endPos + 1, $position); | ||
$this->pushSequence(self::STATE_COMMENT, $matchPos, $endPos + 1); | ||
continue; | ||
} | ||
|
||
// Single-line string | ||
if ('"' === $matchChar || "'" === $matchChar) { | ||
if (false === $endPos = strpos($this->content, $matchChar, $matchPos + 1)) { | ||
$this->cursor = $position; | ||
$this->pushSequence(self::STATE_STRING, $matchPos, $this->contentEnd); | ||
|
||
return; | ||
} | ||
while (false !== $endPos && '\\' == $this->content[$endPos - 1]) { | ||
$endPos = strpos($this->content, $matchChar, $endPos + 1); | ||
} | ||
|
||
$this->cursor = min($endPos + 1, $position); | ||
$this->pushSequence(self::STATE_STRING, $matchPos, $endPos + 1); | ||
continue; | ||
} | ||
|
||
// Multi-line string | ||
if ('`' === $matchChar) { | ||
if (false === $endPos = strpos($this->content, $matchChar, $matchPos + 1)) { | ||
$this->cursor = $position; | ||
$this->pushSequence(self::STATE_STRING, $matchPos, $this->contentEnd); | ||
|
||
return; | ||
} | ||
while (false !== $endPos && '\\' == $this->content[$endPos - 1]) { | ||
$endPos = strpos($this->content, $matchChar, $endPos + 1); | ||
} | ||
|
||
$this->cursor = min($endPos + 1, $position); | ||
$this->pushSequence(self::STATE_STRING, $matchPos, $endPos + 1); | ||
continue; | ||
} | ||
} | ||
} | ||
|
||
private function pushSequence(string $type, int $start, int $end): void | ||
{ | ||
$this->sequences[] = $this->currentSequence = new CodeSequence($type, $start, $end); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.