Skip to content

Commit f573dd7

Browse files
committed
Add more ("generics", "union", "intersection" and "list" types) parser configuration options
1 parent f6f441a commit f573dd7

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

resources/grammar.pp2

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ LogicalType
6363

6464
UnionType -> {
6565
if (\count($children) === 2) {
66+
if ($this->union === false) {
67+
throw FeatureNotAllowedException::fromFeature('union types', $offset);
68+
}
69+
6670
return new Node\Stmt\UnionTypeNode($children[0], $children[1]);
6771
}
6872

@@ -73,6 +77,10 @@ UnionType -> {
7377

7478
IntersectionType -> {
7579
if (\count($children) === 2) {
80+
if ($this->intersection === false) {
81+
throw FeatureNotAllowedException::fromFeature('intersection types', $offset);
82+
}
83+
7684
return new Node\Stmt\IntersectionTypeNode($children[0], $children[1]);
7785
}
7886

@@ -114,6 +122,10 @@ TypesList -> {
114122
$statement = \array_shift($children);
115123

116124
for ($i = 0, $length = \count($children); $i < $length; ++$i) {
125+
if ($this->list === false) {
126+
throw FeatureNotAllowedException::fromFeature('square bracket list types', $offset);
127+
}
128+
117129
$statement = new Node\Stmt\TypesListNode($statement);
118130
$statement->offset = $children[$i]->getOffset();
119131
}

resources/grammar/template-arguments.pp2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11

22
TemplateArguments -> {
3+
if ($this->generics === false) {
4+
throw FeatureNotAllowedException::fromFeature('template arguments', $offset);
5+
}
6+
37
return new Node\Stmt\Template\ArgumentsListNode($children);
48
}
59
: ::T_ANGLE_BRACKET_OPEN::

src/Exception/FeatureNotAllowedException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class FeatureNotAllowedException extends SemanticException
1414
*/
1515
public static function fromFeature(string $name, int $offset = 0): self
1616
{
17-
$message = \sprintf('%s not allowed by parser configuration', $name);
17+
$message = \sprintf('%s not allowed', $name);
1818

1919
return new static($offset, $message);
2020
}

src/Parser.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public function __construct(
7373
public readonly bool $shapes = true,
7474
public readonly bool $callables = true,
7575
public readonly bool $literals = true,
76+
public readonly bool $generics = true,
77+
public readonly bool $union = true,
78+
public readonly bool $intersection = true,
79+
public readonly bool $list = true,
7680
private readonly SourceFactoryInterface $sources = new SourceFactory(),
7781
) {
7882
/** @psalm-var GrammarConfigArray $grammar */

0 commit comments

Comments
 (0)