Define logic to mutate the state inside tokens

This commit is contained in:
Andrea Marco Sartori
2022-11-17 23:49:26 +10:00
parent acde9f4572
commit bf1ef5cad7
6 changed files with 43 additions and 34 deletions

View File

@ -4,7 +4,6 @@ namespace Cerbero\JsonParser;
use Cerbero\JsonParser\Pointers\Pointers; use Cerbero\JsonParser\Pointers\Pointers;
use Cerbero\JsonParser\Sources\Source; use Cerbero\JsonParser\Sources\Source;
use Cerbero\JsonParser\Tokens\Token;
use IteratorAggregate; use IteratorAggregate;
use Traversable; use Traversable;
@ -37,7 +36,6 @@ class Parser implements IteratorAggregate
public function __construct(protected Lexer $lexer, protected Config $config) public function __construct(protected Lexer $lexer, protected Config $config)
{ {
$this->state = new State(); $this->state = new State();
$this->pointers = new Pointers(...$config->pointers);
} }
/** /**
@ -58,10 +56,11 @@ class Parser implements IteratorAggregate
*/ */
public function getIterator(): Traversable public function getIterator(): Traversable
{ {
$this->pointers = new Pointers(...$this->config->pointers);
$this->state->matchPointer($this->pointers); $this->state->matchPointer($this->pointers);
foreach ($this->lexer as $token) { foreach ($this->lexer as $token) {
$this->handleToken($token); $token->mutateState($this->state);
$this->rematchPointer(); $this->rematchPointer();
if (!$token->endsChunk() || $this->state->treeIsDeep()) { 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 * Set the matching JSON pointer when the tree changes
* *

View File

@ -21,12 +21,12 @@ class Comma extends Token
} }
/** /**
* Mutate the given state * Update the given state
* *
* @param State $state * @param State $state
* @return void * @return void
*/ */
public function mutateState(State $state): void protected function updateState(State $state): void
{ {
if ($state->inObject()) { if ($state->inObject()) {
$state->expectKey(); $state->expectKey();

View File

@ -21,12 +21,12 @@ class CompoundBegin extends Token
} }
/** /**
* Mutate the given state * Update the given state
* *
* @param State $state * @param State $state
* @return void * @return void
*/ */
public function mutateState(State $state): void protected function updateState(State $state): void
{ {
$state->tree()->deepen(); $state->tree()->deepen();

View File

@ -21,12 +21,12 @@ class CompoundEnd extends Token
} }
/** /**
* Mutate the given state * Update the given state
* *
* @param State $state * @param State $state
* @return void * @return void
*/ */
public function mutateState(State $state): void protected function updateState(State $state): void
{ {
$state->tree()->emerge(); $state->tree()->emerge();

View File

@ -28,22 +28,18 @@ class ScalarString extends Token
} }
/** /**
* Mutate the given state * Update the given state
* *
* @param State $state * @param State $state
* @return void * @return void
*/ */
public function mutateState(State $state): void protected function updateState(State $state): void
{ {
if (!$this->isKey = $state->expectsKey()) { if (!$this->isKey = $state->expectsKey()) {
return; return;
} }
$state->doNotExpectKey(); $state->doNotExpectKey();
if ($state->treeIsShallow()) {
$state->traverseKey($this->value);
}
} }
/** /**

View File

@ -58,6 +58,16 @@ abstract class Token implements Stringable
return ($this->type() | Tokens::VALUE_SCALAR) == Tokens::VALUE_SCALAR; 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 * Mutate the given state
* *
@ -65,6 +75,29 @@ abstract class Token implements Stringable
* @return void * @return void
*/ */
public function mutateState(State $state): 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; return;
} }