Reduce instructions

This commit is contained in:
Andrea Marco Sartori 2023-06-04 17:18:43 +02:00
parent edf39f9233
commit dc21c4facb
3 changed files with 15 additions and 14 deletions

View File

@ -76,12 +76,14 @@ final class Pointers
$originalTree = $tree->original(); $originalTree = $tree->original();
foreach ($this->pointers as $pointer) { foreach ($this->pointers as $pointer) {
$referenceTokens = $pointer->referenceTokens(); if ($pointer->referenceTokens() == $originalTree) {
return $this->matching = $pointer;
}
foreach ($originalTree as $depth => $key) { foreach ($originalTree as $depth => $key) {
if (!$pointer->depthMatchesKey($depth, $key)) { if (!$pointer->depthMatchesKey($depth, $key)) {
continue 2; continue 2;
} elseif (!isset($pointers[$depth]) || $referenceTokens == $originalTree) { } elseif (!isset($pointers[$depth])) {
$pointers[$depth] = $pointer; $pointers[$depth] = $pointer;
} }
} }
@ -93,13 +95,15 @@ final class Pointers
/** /**
* Mark the given pointer as found * Mark the given pointer as found
* *
* @return void * @return Pointer
*/ */
public function markAsFound(): void public function markAsFound(): Pointer
{ {
if (!$this->matching->wasFound) { if (!$this->matching->wasFound) {
$this->found[(string) $this->matching] = $this->matching->wasFound = true; $this->found[(string) $this->matching] = $this->matching->wasFound = true;
} }
return $this->matching;
} }
/** /**

View File

@ -53,30 +53,27 @@ final class Lexer implements IteratorAggregate
{ {
$buffer = ''; $buffer = '';
$inString = $isEscaping = false; $inString = $isEscaping = false;
$tokenizer = Tokenizer::instance();
foreach ($this->source as $chunk) { foreach ($this->source as $chunk) {
for ($i = 0, $size = strlen($chunk); $i < $size; $i++, $this->position++) { for ($i = 0, $size = strlen($chunk); $i < $size; $i++, $this->position++) {
$character = $chunk[$i]; $character = $chunk[$i];
$inString = ($character == '"') != $inString || $isEscaping; $inString = ($character == '"') != $inString || $isEscaping;
$isEscaping = $character == '\\' && !$isEscaping; $isEscaping = $character == '\\' && !$isEscaping;
$shouldBuffer = $inString || !isset(Tokens::BOUNDARIES[$character]);
if ($shouldBuffer && $buffer == '' && !isset(Tokens::TYPES[$character])) { if ($inString || !isset(Tokens::BOUNDARIES[$character])) {
throw new SyntaxException($character); $buffer == '' && !isset(Tokens::TYPES[$character]) && throw new SyntaxException($character);
}
if ($shouldBuffer) {
$buffer .= $character; $buffer .= $character;
continue; continue;
} }
if ($buffer != '') { if ($buffer != '') {
yield Tokenizer::instance()->toToken($buffer); yield $tokenizer->toToken($buffer);
$buffer = ''; $buffer = '';
} }
if (isset(Tokens::DELIMITERS[$character])) { if (isset(Tokens::DELIMITERS[$character])) {
yield Tokenizer::instance()->toToken($character); yield $tokenizer->toToken($character);
} }
} }
} }

View File

@ -107,9 +107,9 @@ final class State
$this->tree->traverseToken($token, $this->expectsKey); $this->tree->traverseToken($token, $this->expectsKey);
if ($this->tree->isMatched() && ((!$this->expectsKey && $token->isValue()) || $this->tree->isDeep())) { if ($this->tree->isMatched() && ((!$this->expectsKey && $token->isValue()) || $this->tree->isDeep())) {
$this->pointers->markAsFound(); $pointer = $this->pointers->markAsFound();
if ($token instanceof CompoundBegin && $this->pointers->matching()->isLazy()) { if ($token instanceof CompoundBegin && $pointer->isLazy()) {
$this->buffer = ($this->lazyLoad)(); $this->buffer = ($this->lazyLoad)();
$token->shouldLazyLoad = true; $token->shouldLazyLoad = true;
} else { } else {