diff --git a/src/Parser.php b/src/Parser.php index 00e7c53..2de62cc 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -4,7 +4,6 @@ namespace Cerbero\JsonParser; use Cerbero\JsonParser\Pointers\Pointers; use Cerbero\JsonParser\Sources\Source; -use Cerbero\JsonParser\Tokens\Token; use IteratorAggregate; use Traversable; @@ -37,7 +36,6 @@ class Parser implements IteratorAggregate public function __construct(protected Lexer $lexer, protected Config $config) { $this->state = new State(); - $this->pointers = new Pointers(...$config->pointers); } /** @@ -58,10 +56,11 @@ class Parser implements IteratorAggregate */ public function getIterator(): Traversable { + $this->pointers = new Pointers(...$this->config->pointers); $this->state->matchPointer($this->pointers); foreach ($this->lexer as $token) { - $this->handleToken($token); + $token->mutateState($this->state); $this->rematchPointer(); if (!$token->endsChunk() || $this->state->treeIsDeep()) { @@ -82,25 +81,6 @@ class Parser implements IteratorAggregate } } - /** - * Handle the given token - * - * @param Token $token - * @return void - */ - protected function handleToken(Token $token): void - { - if ($token->isValue() && !$this->state->inObject() && $this->state->treeIsShallow()) { - $this->state->traverseArray(); - } - - if ($this->state->inRoot() && $this->state->shouldBufferToken($token)) { - $this->state->bufferToken($token); - } - - $token->mutateState($this->state); - } - /** * Set the matching JSON pointer when the tree changes * diff --git a/src/Tokens/Comma.php b/src/Tokens/Comma.php index 6884fa5..cd6694c 100644 --- a/src/Tokens/Comma.php +++ b/src/Tokens/Comma.php @@ -21,12 +21,12 @@ class Comma extends Token } /** - * Mutate the given state + * Update the given state * * @param State $state * @return void */ - public function mutateState(State $state): void + protected function updateState(State $state): void { if ($state->inObject()) { $state->expectKey(); diff --git a/src/Tokens/CompoundBegin.php b/src/Tokens/CompoundBegin.php index eefbec9..0130ecc 100644 --- a/src/Tokens/CompoundBegin.php +++ b/src/Tokens/CompoundBegin.php @@ -21,12 +21,12 @@ class CompoundBegin extends Token } /** - * Mutate the given state + * Update the given state * * @param State $state * @return void */ - public function mutateState(State $state): void + protected function updateState(State $state): void { $state->tree()->deepen(); diff --git a/src/Tokens/CompoundEnd.php b/src/Tokens/CompoundEnd.php index 99c7202..fea88ce 100644 --- a/src/Tokens/CompoundEnd.php +++ b/src/Tokens/CompoundEnd.php @@ -21,12 +21,12 @@ class CompoundEnd extends Token } /** - * Mutate the given state + * Update the given state * * @param State $state * @return void */ - public function mutateState(State $state): void + protected function updateState(State $state): void { $state->tree()->emerge(); diff --git a/src/Tokens/ScalarString.php b/src/Tokens/ScalarString.php index 9dce3c9..c0cf392 100644 --- a/src/Tokens/ScalarString.php +++ b/src/Tokens/ScalarString.php @@ -28,22 +28,18 @@ class ScalarString extends Token } /** - * Mutate the given state + * Update the given state * * @param State $state * @return void */ - public function mutateState(State $state): void + protected function updateState(State $state): void { if (!$this->isKey = $state->expectsKey()) { return; } $state->doNotExpectKey(); - - if ($state->treeIsShallow()) { - $state->traverseKey($this->value); - } } /** diff --git a/src/Tokens/Token.php b/src/Tokens/Token.php index 92b89dd..b83cfb8 100644 --- a/src/Tokens/Token.php +++ b/src/Tokens/Token.php @@ -58,6 +58,16 @@ abstract class Token implements Stringable return ($this->type() | Tokens::VALUE_SCALAR) == Tokens::VALUE_SCALAR; } + /** + * Determine whether the token is a string + * + * @return bool + */ + public function isString(): bool + { + return ($this->type() | Tokens::SCALAR_STRING) == Tokens::SCALAR_STRING; + } + /** * Mutate the given state * @@ -65,6 +75,29 @@ abstract class Token implements Stringable * @return void */ public function mutateState(State $state): void + { + if ($this->isValue() && !$state->inObject() && $state->treeIsShallow()) { + $state->traverseArray(); + } + + if ($this->isString() && $state->expectsKey() && $state->treeIsShallow()) { + $state->traverseKey($this); + } + + if ($state->inRoot() && $state->shouldBufferToken($this)) { + $state->bufferToken($this); + } + + $this->updateState($state); + } + + /** + * Update the given state + * + * @param State $state + * @return void + */ + protected function updateState(State $state): void { return; }