Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
Add more parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crisu83 committed Feb 23, 2018
1 parent 2eed29c commit 69cb802
Show file tree
Hide file tree
Showing 17 changed files with 636 additions and 91 deletions.
31 changes: 31 additions & 0 deletions src/Language/AST/Builder/EnumBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Digia\GraphQL\Language\AST\Builder;

use Digia\GraphQL\Language\AST\Node\Contract\NodeInterface;
use Digia\GraphQL\Language\AST\Node\EnumValueNode;
use Digia\GraphQL\Language\AST\Node\IntValueNode;
use Digia\GraphQL\Language\AST\NodeKindEnum;

class EnumBuilder extends AbstractBuilder
{

/**
* @inheritdoc
*/
public function build(array $ast): NodeInterface
{
return new EnumValueNode([
'value' => $this->getOne($ast, 'value'),
'location' => $this->createLocation($ast),
]);
}

/**
* @inheritdoc
*/
public function supportsKind(string $kind): bool
{
return $kind === NodeKindEnum::ENUM;
}
}
29 changes: 29 additions & 0 deletions src/Language/AST/Builder/NullBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Digia\GraphQL\Language\AST\Builder;

use Digia\GraphQL\Language\AST\Node\Contract\NodeInterface;
use Digia\GraphQL\Language\AST\Node\NullValueNode;
use Digia\GraphQL\Language\AST\NodeKindEnum;

class NullBuilder extends AbstractBuilder
{

/**
* @inheritdoc
*/
public function build(array $ast): NodeInterface
{
return new NullValueNode([
'location' => $this->createLocation($ast),
]);
}

/**
* @inheritdoc
*/
public function supportsKind(string $kind): bool
{
return $kind === NodeKindEnum::NULL;
}
}
11 changes: 10 additions & 1 deletion src/Language/AST/Node/Behavior/TypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace Digia\GraphQL\Language\AST\Node\Behavior;

use Digia\GraphQL\Contract\SerializationInterface;
use Digia\GraphQL\Language\AST\Node\Contract\TypeNodeInterface;

trait TypeTrait
{

/**
* @var TypeNodeInterface
* @var TypeNodeInterface|SerializationInterface
*/
protected $type;

Expand All @@ -19,4 +20,12 @@ public function getType(): TypeNodeInterface
{
return $this->type;
}

/**
* @return array
*/
public function getTypeAsArray(): array
{
return $this->type->toArray();
}
}
3 changes: 3 additions & 0 deletions src/Language/AST/Node/Behavior/ValueLiteralTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public function getValue(): ?ValueNodeInterface
return $this->value;
}

/**
* @return array
*/
public function getValueAsArray(): array
{
return null !== $this->value ? $this->value->toArray() : null;
Expand Down
7 changes: 7 additions & 0 deletions src/Language/AST/Node/Contract/NodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

namespace Digia\GraphQL\Language\AST\Node\Contract;

use Digia\GraphQL\Language\Location;

interface NodeInterface
{

/**
* @return string
*/
public function getKind(): string;

/**
* @return Location|null
*/
public function getLocation(): ?Location;
}
12 changes: 12 additions & 0 deletions src/Language/AST/Node/ListTypeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,16 @@ class ListTypeNode extends AbstractNode implements TypeNodeInterface
* @var string
*/
protected $kind = NodeKindEnum::LIST_TYPE;

/**
* @inheritdoc
*/
public function toArray(): array
{
return [
'kind' => $this->kind,
'loc' => $this->getLocationAsArray(),
'type' => $this->getTypeAsArray(),
];
}
}
35 changes: 33 additions & 2 deletions src/Language/AST/Node/ListValueNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Digia\GraphQL\Language\AST\Node;

use Digia\GraphQL\Language\AST\NodeKindEnum;
use Digia\GraphQL\Contract\SerializationInterface;
use Digia\GraphQL\Language\AST\Node\Contract\ValueNodeInterface;
use Digia\GraphQL\Language\AST\NodeKindEnum;

class ListValueNode extends AbstractNode implements ValueNodeInterface
{
Expand All @@ -14,7 +15,37 @@ class ListValueNode extends AbstractNode implements ValueNodeInterface
protected $kind = NodeKindEnum::LIST;

/**
* @var ValueNodeInterface[]
* @var array|ValueNodeInterface[]
*/
protected $values;

/**
* @return array|ValueNodeInterface[]
*/
public function getValues(): array
{
return $this->values;
}

/**
* @return array
*/
public function getValuesAsArray(): array
{
return array_map(function (SerializationInterface $node) {
return $node->toArray();
}, $this->values);
}

/**
* @inheritdoc
*/
public function toArray(): array
{
return [
'kind' => $this->kind,
'loc' => $this->getLocationAsArray(),
'values' => $this->getValuesAsArray(),
];
}
}
7 changes: 5 additions & 2 deletions src/Language/AST/Node/NameNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@

namespace Digia\GraphQL\Language\AST\Node;

use Digia\GraphQL\Language\AST\Node\Behavior\ValueLiteralTrait;
use Digia\GraphQL\Language\AST\Node\Behavior\ValueTrait;
use Digia\GraphQL\Language\AST\Node\Contract\NodeInterface;
use Digia\GraphQL\Language\AST\NodeKindEnum;

class NameNode extends AbstractNode implements NodeInterface
{

use ValueLiteralTrait;
use ValueTrait;

/**
* @var string
*/
protected $kind = NodeKindEnum::NAME;

/**
* @inheritdoc
*/
public function toArray(): array
{
return [
Expand Down
12 changes: 12 additions & 0 deletions src/Language/AST/Node/NamedTypeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,16 @@ class NamedTypeNode extends AbstractNode implements TypeNodeInterface
* @var string
*/
protected $kind = NodeKindEnum::NAMED_TYPE;

/**
* @inheritdoc
*/
public function toArray(): array
{
return [
'kind' => $this->kind,
'loc' => $this->getLocationAsArray(),
'name' => $this->getNameAsArray(),
];
}
}
12 changes: 12 additions & 0 deletions src/Language/AST/Node/NonNullTypeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,16 @@ class NonNullTypeNode extends AbstractNode implements TypeNodeInterface
* @var string
*/
protected $kind = NodeKindEnum::NON_NULL_TYPE;

/**
* @inheritdoc
*/
public function toArray(): array
{
return [
'kind' => $this->kind,
'loc' => $this->getLocationAsArray(),
'type' => $this->getTypeAsArray(),
];
}
}
4 changes: 2 additions & 2 deletions src/Language/ASTParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ protected function parseFragmentDefinition(Lexer $lexer): array
return [
'kind' => NodeKindEnum::FRAGMENT_DEFINITION,
'name' => $this->parseFragmentName($lexer),
'typeCondition' => $parseTypeCondition,
'typeCondition' => $parseTypeCondition($lexer),
'directives' => $this->parseDirectives($lexer, false),
'selectionSet' => $this->parseSelectionSet($lexer),
'loc' => $this->createLocation($lexer, $start),
Expand All @@ -719,7 +719,7 @@ protected function parseFragmentDefinition(Lexer $lexer): array
protected function parseFragmentName(Lexer $lexer): array
{
if ($lexer->getToken()->getValue() === 'on') {
$this->unexpected($lexer);
throw $this->unexpected($lexer);
}

return $this->parseName($lexer);
Expand Down
4 changes: 2 additions & 2 deletions src/Language/NodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ protected function parseFragmentDefinition(Lexer $lexer): FragmentDefinitionNode

return new FragmentDefinitionNode([
'name' => $this->parseFragmentName($lexer),
'typeCondition' => $parseTypeCondition,
'typeCondition' => $parseTypeCondition($lexer),
'directives' => $this->parseDirectives($lexer, false),
'selectionSet' => $this->parseSelectionSet($lexer),
'loc' => $this->createLocation($lexer, $start),
Expand All @@ -649,7 +649,7 @@ protected function parseFragmentDefinition(Lexer $lexer): FragmentDefinitionNode
protected function parseFragmentName(Lexer $lexer): NameNode
{
if ($lexer->getToken()->getValue() === 'on') {
$this->unexpected($lexer);
throw $this->unexpected($lexer);
}

return $this->parseName($lexer);
Expand Down
3 changes: 3 additions & 0 deletions src/Language/Reader/StringReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Digia\GraphQL\Error\SyntaxError;
use function Digia\GraphQL\Language\charCodeAt;
use function Digia\GraphQL\Language\chrUTF8;
use function Digia\GraphQL\Language\printCharCode;
use function Digia\GraphQL\Language\sliceString;
use Digia\GraphQL\Language\Token;
Expand Down Expand Up @@ -50,6 +51,7 @@ public function read(int $code, int $pos, int $line, int $col, Token $prev): Tok
++$pos;

if ($code === 92) {
// \
$value .= sliceString($body, $chunkStart, $pos + 1);
$code = charCodeAt($body, $pos);

Expand Down Expand Up @@ -79,6 +81,7 @@ public function read(int $code, int $pos, int $line, int $col, Token $prev): Tok
$value .= '\t';
break;
case 117:
// u
$charCode = uniCharCode(
charCodeAt($body, $pos + 1),
charCodeAt($body, $pos + 2),
Expand Down
Loading

0 comments on commit 69cb802

Please sign in to comment.