mirror of
https://github.com/cerbero90/json-parser.git
synced 2025-01-17 04:58:15 +01:00
Define logic to mutate the state inside tokens
This commit is contained in:
parent
acde9f4572
commit
bf1ef5cad7
@ -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
|
||||
*
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user