Skip to content

Commit e8eb365

Browse files
author
Kirill Nesmeyanov
committed
Improve descriptions of parser constructor options
1 parent f573dd7 commit e8eb365

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

resources/grammar.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,14 @@
313313
$children[0] ?? null,
314314
);
315315
},
316-
50 => static function (\Phplrt\Parser\Context $ctx, $children) {
316+
50 => function (\Phplrt\Parser\Context $ctx, $children) {
317+
// The "$offset" variable is an auto-generated
318+
$offset = $ctx->lastProcessedToken->getOffset();
319+
320+
if ($this->generics === false) {
321+
throw FeatureNotAllowedException::fromFeature('template arguments', $offset);
322+
}
323+
317324
return new Node\Stmt\Template\ArgumentsListNode($children);
318325
},
319326
54 => static function (\Phplrt\Parser\Context $ctx, $children) {
@@ -511,15 +518,29 @@
511518
$children[4],
512519
);
513520
},
514-
123 => static function (\Phplrt\Parser\Context $ctx, $children) {
521+
123 => function (\Phplrt\Parser\Context $ctx, $children) {
522+
// The "$offset" variable is an auto-generated
523+
$offset = $ctx->lastProcessedToken->getOffset();
524+
515525
if (\count($children) === 2) {
526+
if ($this->union === false) {
527+
throw FeatureNotAllowedException::fromFeature('union types', $offset);
528+
}
529+
516530
return new Node\Stmt\UnionTypeNode($children[0], $children[1]);
517531
}
518532

519533
return $children;
520534
},
521-
124 => static function (\Phplrt\Parser\Context $ctx, $children) {
535+
124 => function (\Phplrt\Parser\Context $ctx, $children) {
536+
// The "$offset" variable is an auto-generated
537+
$offset = $ctx->lastProcessedToken->getOffset();
538+
522539
if (\count($children) === 2) {
540+
if ($this->intersection === false) {
541+
throw FeatureNotAllowedException::fromFeature('intersection types', $offset);
542+
}
543+
523544
return new Node\Stmt\IntersectionTypeNode($children[0], $children[1]);
524545
}
525546

@@ -532,10 +553,17 @@
532553

533554
return $children;
534555
},
535-
133 => static function (\Phplrt\Parser\Context $ctx, $children) {
556+
133 => function (\Phplrt\Parser\Context $ctx, $children) {
557+
// The "$offset" variable is an auto-generated
558+
$offset = $ctx->lastProcessedToken->getOffset();
559+
536560
$statement = \array_shift($children);
537561

538562
for ($i = 0, $length = \count($children); $i < $length; ++$i) {
563+
if ($this->list === false) {
564+
throw FeatureNotAllowedException::fromFeature('square bracket list types', $offset);
565+
}
566+
539567
$statement = new Node\Stmt\TypesListNode($statement);
540568
$statement->offset = $children[$i]->getOffset();
541569
}

src/Parser.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,29 @@ final class Parser implements ParserInterface
6767
*/
6868
public int $lastProcessedTokenOffset = 0;
6969

70+
/**
71+
* @param bool $tolerant Enables or disables tolerant type recognition. If
72+
* the option is {@see true}, the parser allows arbitrary text after
73+
* the type definition. This mode allows you to recognize types
74+
* specified in DocBlocks.
75+
* @param bool $conditional Enables or disables support for
76+
* dependent/conditional types such as `T ? X : Y`.
77+
* @param bool $shapes Enables or disables support for type shapes
78+
* such as `T{key: X}`.
79+
* @param bool $callables Enables or disables support for callable types
80+
* such as `(X, Y): T`.
81+
* @param bool $literals Enables or disables support for literal types such
82+
* as `42` or `"string"`.
83+
* @param bool $generics Enables or disables support for template arguments
84+
* such as `T<X, Y>`.
85+
* @param bool $union Enables or disables support for logical union types
86+
* such as `T | X`.
87+
* @param bool $intersection Enables or disables support for logical
88+
* intersection types such as `T & X`.
89+
* @param bool $list Enables or disables support for square bracket list
90+
* types such as `T[]`.
91+
* @param SourceFactoryInterface $sources
92+
*/
7093
public function __construct(
7194
public readonly bool $tolerant = false,
7295
public readonly bool $conditional = true,

0 commit comments

Comments
 (0)