Skip to content

Commit

Permalink
Merge pull request #30 from pcrov/cleanup
Browse files Browse the repository at this point in the history
Yay, const visibility
  • Loading branch information
pcrov authored Nov 21, 2021
2 parents dc8550c + 0fed667 commit 4b282c9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/InputStream/Psr7Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

final class Psr7Stream implements InputStream
{
const CHUNK_SIZE = 8192;
public const CHUNK_SIZE = 8192;

private $stream;

Expand Down
2 changes: 1 addition & 1 deletion src/InputStream/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

final class Stream implements InputStream
{
const CHUNK_SIZE = 8192;
public const CHUNK_SIZE = 8192;

private $handle;

Expand Down
65 changes: 31 additions & 34 deletions src/Parser/JsonParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

final class JsonParser implements Parser
{
/**
* @var array Map of token to node types.
*/
private static $tokenTypeMap = [
private const TOKEN_TYPE_MAP = [
Tokenizer::T_STRING => JsonReader::STRING,
Tokenizer::T_NUMBER => JsonReader::NUMBER,
Tokenizer::T_TRUE => JsonReader::BOOL,
Expand All @@ -21,15 +18,15 @@ final class JsonParser implements Parser
Tokenizer::T_END_OBJECT => JsonReader::END_OBJECT
];

private static $stateDocumentEnd = 0;
private static $stateDocumentStart = 1;
private static $stateAfterArrayStart = 2;
private static $stateAfterArrayMember = 3;
private static $stateAfterObjectStart = 4;
private static $stateAfterObjectMember = 5;
private const STATE_DOCUMENT_END = 0;
private const STATE_DOCUMENT_START = 1;
private const STATE_AFTER_ARRAY_START = 2;
private const STATE_AFTER_ARRAY_MEMBER = 3;
private const STATE_AFTER_OBJECT_START = 4;
private const STATE_AFTER_OBJECT_MEMBER = 5;

private static $inArray = 1;
private static $inObject = 2;
private const IN_ARRAY = 1;
private const IN_OBJECT = 2;

/**
* @var Tokenizer
Expand All @@ -44,7 +41,7 @@ final class JsonParser implements Parser
public function __construct(Tokenizer $tokenizer)
{
$this->tokenizer = $tokenizer;
$this->state = self::$stateDocumentStart;
$this->state = self::STATE_DOCUMENT_START;
}

/**
Expand All @@ -61,16 +58,16 @@ public function read()
$token = $tokenizer->read();

switch ($state) {
case self::$stateAfterArrayStart:
case self::STATE_AFTER_ARRAY_START:
if ($token[0] === Tokenizer::T_END_ARRAY) {
goto end_of_array_or_object;
}

$names[$depth] = null;
$state = self::$stateAfterArrayMember;
$state = self::STATE_AFTER_ARRAY_MEMBER;
goto value;

case self::$stateAfterArrayMember:
case self::STATE_AFTER_ARRAY_MEMBER:
if ($token[0] === Tokenizer::T_END_ARRAY) {
goto end_of_array_or_object;
}
Expand All @@ -82,15 +79,15 @@ public function read()
$token = $tokenizer->read();
goto value;

case self::$stateAfterObjectStart:
case self::STATE_AFTER_OBJECT_START:
if ($token[0] === Tokenizer::T_END_OBJECT) {
goto end_of_array_or_object;
}

$state = self::$stateAfterObjectMember;
$state = self::STATE_AFTER_OBJECT_MEMBER;
goto object_member;

case self::$stateAfterObjectMember:
case self::STATE_AFTER_OBJECT_MEMBER:
if ($token[0] === Tokenizer::T_END_OBJECT) {
goto end_of_array_or_object;
}
Expand All @@ -102,11 +99,11 @@ public function read()
$token = $tokenizer->read();
goto object_member;

case self::$stateDocumentStart:
$state = self::$stateDocumentEnd;
case self::STATE_DOCUMENT_START:
$state = self::STATE_DOCUMENT_END;
goto value;

case self::$stateDocumentEnd:
case self::STATE_DOCUMENT_END:
$names = [null];
if ($token[0] !== Tokenizer::T_EOF) {
throw new ParseException($this->getExceptionMessage($token));
Expand All @@ -133,16 +130,16 @@ public function read()
\array_pop($stack);
$depth--;
switch (\end($stack)) {
case self::$inArray:
$state = self::$stateAfterArrayMember;
case self::IN_ARRAY:
$state = self::STATE_AFTER_ARRAY_MEMBER;
break;
case self::$inObject:
$state = self::$stateAfterObjectMember;
case self::IN_OBJECT:
$state = self::STATE_AFTER_OBJECT_MEMBER;
break;
default:
$state = self::$stateDocumentEnd;
$state = self::STATE_DOCUMENT_END;
}
return [self::$tokenTypeMap[$token[0]], $names[$depth], $token[1], $depth];
return [self::TOKEN_TYPE_MAP[$token[0]], $names[$depth], $token[1], $depth];
}

value: {
Expand All @@ -155,25 +152,25 @@ public function read()
case Tokenizer::T_NULL:
break;
case Tokenizer::T_BEGIN_ARRAY:
$state = self::$stateAfterArrayStart;
$stack[] = self::$inArray;
$state = self::STATE_AFTER_ARRAY_START;
$stack[] = self::IN_ARRAY;
$depth++;
break;
case Tokenizer::T_BEGIN_OBJECT:
$state = self::$stateAfterObjectStart;
$stack[] = self::$inObject;
$state = self::STATE_AFTER_OBJECT_START;
$stack[] = self::IN_OBJECT;
$depth++;
break;
default:
throw new ParseException($this->getExceptionMessage($token));
}
return [self::$tokenTypeMap[$token[0]], $names[$currentDepth], $token[1], $currentDepth];
return [self::TOKEN_TYPE_MAP[$token[0]], $names[$currentDepth], $token[1], $currentDepth];
}
}

private function getExceptionMessage(array $token): string
{
list ($tokenType, , $tokenLine) = $token;
[$tokenType, , $tokenLine] = $token;

if ($tokenType === Tokenizer::T_EOF) {
return \sprintf(
Expand Down
24 changes: 12 additions & 12 deletions src/Parser/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

interface Tokenizer
{
const T_STRING = "T_STRING";
const T_NUMBER = "T_NUMBER";
const T_TRUE = "T_TRUE";
const T_FALSE = "T_FALSE";
const T_NULL = "T_NULL";
const T_COLON = "T_COLON";
const T_COMMA = "T_COMMA";
const T_BEGIN_ARRAY = "T_BEGIN_ARRAY";
const T_END_ARRAY = "T_END_ARRAY";
const T_BEGIN_OBJECT = "T_BEGIN_OBJECT";
const T_END_OBJECT = "T_END_OBJECT";
const T_EOF = "T_EOF";
public const T_STRING = "T_STRING";
public const T_NUMBER = "T_NUMBER";
public const T_TRUE = "T_TRUE";
public const T_FALSE = "T_FALSE";
public const T_NULL = "T_NULL";
public const T_COLON = "T_COLON";
public const T_COMMA = "T_COMMA";
public const T_BEGIN_ARRAY = "T_BEGIN_ARRAY";
public const T_END_ARRAY = "T_END_ARRAY";
public const T_BEGIN_OBJECT = "T_BEGIN_OBJECT";
public const T_END_OBJECT = "T_END_OBJECT";
public const T_EOF = "T_EOF";

/**
* @return array Tuples in the form of [string type, mixed value, int line]
Expand Down

0 comments on commit 4b282c9

Please sign in to comment.