Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 55 additions & 23 deletions packages/view/src/Parser/TempestViewLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ public function lex(): TokenCollection
$tokens = [];

while ($this->current !== null) {
if ($this->comesNext('<?xml')) {
if ($this->comesNext('<?xml', 5)) {
$tokens[] = $this->lexXml();
} elseif ($this->comesNext('<?')) {
} elseif ($this->comesNext('<?', 2)) {
$tokens[] = $this->lexPhp();
} elseif ($this->comesNext('<!--')) {
} elseif ($this->comesNext('<!--', 4)) {
$tokens[] = $this->lexComment();
} elseif ($this->comesNext('<!doctype') || $this->comesNext('<!DOCTYPE')) {
} elseif ($this->comesNext('<!doctype', 9) || $this->comesNext('<!DOCTYPE', 9)) {
$tokens[] = $this->lexDocType();
} elseif ($this->comesNext('<![CDATA')) {
} elseif ($this->comesNext('<![CDATA', 8)) {
$tokens = [...$tokens, ...$this->lexCharacterData()];
} elseif ($this->comesNext('<')) {
} elseif ($this->comesNext('<', 1)) {
$tokens = [...$tokens, ...$this->lexTag()];
} elseif (str_contains(self::WHITESPACE, $this->current)) {
$tokens[] = $this->lexWhitespace();
Expand All @@ -46,13 +46,23 @@ public function lex(): TokenCollection
return new TokenCollection($tokens);
}

private function comesNext(string $search): bool
private function comesNext(string $search, ?int $length = null): bool
{
return $this->seek(strlen($search)) === $search;
$length ??= strlen($search);

if ($length === 1) {
return ($this->html[$this->position] ?? null) === $search;
}

return substr_compare($this->html, $search, $this->position, $length) === 0;
}

private function seek(int $length = 1, int $offset = 0): ?string
{
if ($length === 1) {
return $this->html[$this->position + $offset] ?? null;
}

$seek = substr($this->html, $this->position + $offset, $length);

if ($seek === '') {
Expand All @@ -62,18 +72,28 @@ private function seek(int $length = 1, int $offset = 0): ?string
return $seek;
}

private function seekIgnoringWhitespace(int $length = 1): ?string
private function seekIgnoringWhitespace(): ?string
{
$offset = strspn($this->html, self::WHITESPACE, $this->position);

return $this->seek(length: $length, offset: $offset);
return $this->seek(
// Whitespace offset
offset: strspn($this->html, self::WHITESPACE, $this->position),
);
}

private function consume(int $length = 1): string
{
if ($length === 0) {
return '';
}

if ($length === 1) {
$char = $this->html[$this->position++] ?? null;
$this->current = $this->html[$this->position] ?? null;
return $char ?? '';
}

$buffer = substr($this->html, $this->position, $length);
$this->position += $length;
$this->line += substr_count($buffer, "\n");
$this->current = $this->html[$this->position] ?? null;

return $buffer;
Expand Down Expand Up @@ -124,8 +144,15 @@ private function lexTag(): array
} else {
$tokens[] = $this->makeToken($tag, TokenType::OPEN_TAG_START, $tagLine);

while ($this->seek() !== null && $this->seekIgnoringWhitespace() !== '>' && $this->seekIgnoringWhitespace() !== '/') {
if ($this->seekIgnoringWhitespace(2) === '<?') {
while ($this->current !== null) {
$whitespaceOffset = strspn($this->html, self::WHITESPACE, $this->position);
$next = $this->seek(offset: $whitespaceOffset);

if ($next === '>' || $next === '/') {
break;
}

if ($next === '<' && $this->seek(length: 2, offset: $whitespaceOffset) === '<?') {
$tokens[] = $this->lexPhp();
continue;
}
Expand All @@ -135,7 +162,7 @@ private function lexTag(): array

$attributeName .= $this->consumeUntil(self::WHITESPACE . '=/>');

$hasValue = $this->seek() === '=';
$hasValue = $this->comesNext('=', 1);

if ($hasValue) {
$attributeName .= $this->consume();
Expand All @@ -148,7 +175,7 @@ private function lexTag(): array
);

if ($hasValue) {
$quote = $this->seek() === "'"
$quote = $this->comesNext("'", 1)
? "'"
: '"';

Expand All @@ -164,15 +191,20 @@ private function lexTag(): array
}
}

if ($this->seekIgnoringWhitespace() === '>') {
$next = $this->seek(
// Whitespace offset
offset: strspn($this->html, self::WHITESPACE, $this->position),
);

if ($next === '>') {
$openTagEndLine = $this->line;

$tokens[] = $this->makeToken(
content: $this->consumeIncluding('>'),
type: TokenType::OPEN_TAG_END,
line: $openTagEndLine,
);
} elseif ($this->seekIgnoringWhitespace() === '/') {
} elseif ($next === '/') {
$selfClosingTagEndLine = $this->line;

$tokens[] = $this->makeToken(
Expand All @@ -191,7 +223,7 @@ private function lexXml(): Token
$line = $this->line;
$buffer = '';

while ($this->seek(2) !== '?>' && $this->current !== null) {
while (! $this->comesNext('?>', 2) && $this->current !== null) {
$buffer .= $this->consume();
}

Expand All @@ -205,7 +237,7 @@ private function lexPhp(): Token
$line = $this->line;
$buffer = '';

while ($this->seek(2) !== '?>' && $this->current !== null) {
while (! $this->comesNext('?>', 2) && $this->current !== null) {
$buffer .= $this->consume();
}

Expand All @@ -227,7 +259,7 @@ private function lexComment(): Token
$line = $this->line;
$buffer = '';

while ($this->seek(3) !== '-->' && $this->current !== null) {
while (! $this->comesNext('-->', 3) && $this->current !== null) {
$buffer .= $this->consume();
}

Expand Down Expand Up @@ -264,7 +296,7 @@ private function lexCharacterData(): array

$contentLine = $this->line;

while ($this->seek(3) !== ']]>' && $this->current !== null) {
while (! $this->comesNext(']]>', 3) && $this->current !== null) {
$buffer .= $this->consume();
}

Expand Down
Loading