-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
ParserTrait.php
140 lines (122 loc) · 3.59 KB
/
ParserTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
declare(strict_types=1);
namespace Cucumber\Gherkin\Parser;
use Cucumber\Gherkin\ParserException;
use Cucumber\Gherkin\Token;
use Cucumber\Gherkin\TokenMatcher;
use Cucumber\Gherkin\ParserException\CompositeParserException;
/**
* Contains code for Cucumber\Gherkin\Parser that does not need to be part of code generation
*
* @template T
*/
trait ParserTrait
{
/**
* @param Builder<T> $builder
*/
public function __construct(
private Builder $builder,
) {
}
/**
* @return T
*/
public function parse(string $uri, TokenScanner $tokenScanner, TokenMatcher $tokenMatcher): mixed
{
$this->builder->reset($uri);
$tokenMatcher->reset();
$context = new ParserContext(
$tokenScanner,
$tokenMatcher,
[],
[],
);
$this->startRule($context, RuleType::GherkinDocument);
$state = 0;
do {
$token = $this->readToken($context);
$state = $this->matchToken($state, $token, $context);
} while (!$token->isEof());
$this->endRule($context, RuleType::GherkinDocument);
if (count($context->errors) > 0) {
throw new CompositeParserException($context->errors);
}
return $this->builder->getResult();
}
private function addError(ParserContext $context, ParserException $error): void
{
$newErrorMessage = $error->getMessage();
foreach ($context->errors as $e) {
if ($e->getMessage() == $newErrorMessage) {
return;
}
}
$context->errors[] = $error;
if (count($context->errors) > 10) {
throw new CompositeParserException($context->errors);
}
}
/**
* @template U
* @param callable() : U $action
*/
private function handleAstError(ParserContext $context, callable $action): void
{
$this->handleExternalError($context, $action, null);
}
/**
* @template U
* @param callable() : U $action
* @param U $defaultValue
*
* @return U
*/
private function handleExternalError(ParserContext $context, callable $action, mixed $defaultValue): mixed
{
try {
return $action();
} catch (CompositeParserException $compositeParserException) {
foreach ($compositeParserException->errors as $error) {
$this->addError($context, $error);
}
} catch (ParserException $error) {
$this->addError($context, $error);
}
return $defaultValue;
}
private function build(ParserContext $context, Token $token): void
{
$this->handleAstError(
$context,
function () use ($token) {
$this->builder->build($token);
},
);
}
private function startRule(ParserContext $context, RuleType $ruleType): void
{
$this->handleAstError(
$context,
function () use ($ruleType) {
$this->builder->startRule($ruleType);
return null;
},
);
}
private function endRule(ParserContext $context, RuleType $ruleType): void
{
$this->handleAstError(
$context,
function () use ($ruleType) {
$this->builder->endRule($ruleType);
},
);
}
private function readToken(ParserContext $context): Token
{
return (count($context->tokenQueue) > 0)
? array_shift($context->tokenQueue)
: $context->tokenScanner->read();
}
}