|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Language; |
|
4
|
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\SyntaxErrorException; |
|
6
|
|
|
use Digia\GraphQL\Language\ASTBuilder\ASTDirectorInterface; |
|
7
|
|
|
use Digia\GraphQL\Language\ASTBuilder\ASTKindEnum; |
|
8
|
|
|
use Digia\GraphQL\Language\Node\NodeInterface; |
|
9
|
|
|
use Digia\GraphQL\Language\NodeBuilder\NodeDirectorInterface; |
|
10
|
|
|
|
|
11
|
|
|
class Parser implements ParserInterface |
|
12
|
|
|
{ |
|
13
|
|
|
use LexerUtilsTrait; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var ASTDirectorInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $astDirector; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var NodeDirectorInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $nodeDirector; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Parser constructor. |
|
27
|
|
|
* @param ASTDirectorInterface $astDirector |
|
28
|
|
|
* @param NodeDirectorInterface $nodeDirector |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(ASTDirectorInterface $astDirector, NodeDirectorInterface $nodeDirector) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->astDirector = $astDirector; |
|
33
|
|
|
$this->nodeDirector = $nodeDirector; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @inheritdoc |
|
38
|
|
|
* @return NodeInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
public function parse(LexerInterface $lexer): NodeInterface |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->nodeDirector->build($this->parseAST($lexer)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @inheritdoc |
|
47
|
|
|
* @return NodeInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
public function parseValue(LexerInterface $lexer): NodeInterface |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->nodeDirector->build($this->parseValueAST($lexer)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @inheritdoc |
|
56
|
|
|
* @return NodeInterface |
|
57
|
|
|
*/ |
|
58
|
|
|
public function parseType(LexerInterface $lexer): NodeInterface |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->nodeDirector->build($this->parseTypeAST($lexer)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param LexerInterface $lexer |
|
65
|
|
|
* @return array |
|
66
|
|
|
* @throws SyntaxErrorException |
|
67
|
|
|
* @throws \ReflectionException |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function parseAST(LexerInterface $lexer): array |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->astDirector->build(ASTKindEnum::DOCUMENT, $lexer); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param LexerInterface $lexer |
|
76
|
|
|
* @return array |
|
77
|
|
|
* @throws SyntaxErrorException |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function parseValueAST(LexerInterface $lexer): array |
|
80
|
|
|
{ |
|
81
|
|
|
$this->expect($lexer, TokenKindEnum::SOF); |
|
82
|
|
|
$value = $this->astDirector->build(ASTKindEnum::VALUE_LITERAL, $lexer, ['isConst' => false]); |
|
83
|
|
|
$this->expect($lexer, TokenKindEnum::EOF); |
|
84
|
|
|
|
|
85
|
|
|
return $value; |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param LexerInterface $lexer |
|
90
|
|
|
* @return array |
|
91
|
|
|
* @throws SyntaxErrorException |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function parseTypeAST(LexerInterface $lexer): array |
|
94
|
|
|
{ |
|
95
|
|
|
$this->expect($lexer, TokenKindEnum::SOF); |
|
96
|
|
|
$type = $this->astDirector->build(ASTKindEnum::TYPE_REFERENCE, $lexer); |
|
97
|
|
|
$this->expect($lexer, TokenKindEnum::EOF); |
|
98
|
|
|
|
|
99
|
|
|
return $type; |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|