Skip to content

Commit

Permalink
Add more ("generics", "union", "intersection" and "list" types) parse…
Browse files Browse the repository at this point in the history
…r configuration options
  • Loading branch information
SerafimArts committed Apr 3, 2024
1 parent f6f441a commit f573dd7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
12 changes: 12 additions & 0 deletions resources/grammar.pp2
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ LogicalType
UnionType -> {
if (\count($children) === 2) {
if ($this->union === false) {
throw FeatureNotAllowedException::fromFeature('union types', $offset);
}
return new Node\Stmt\UnionTypeNode($children[0], $children[1]);
}
Expand All @@ -73,6 +77,10 @@ UnionType -> {
IntersectionType -> {
if (\count($children) === 2) {
if ($this->intersection === false) {
throw FeatureNotAllowedException::fromFeature('intersection types', $offset);
}
return new Node\Stmt\IntersectionTypeNode($children[0], $children[1]);
}
Expand Down Expand Up @@ -114,6 +122,10 @@ TypesList -> {
$statement = \array_shift($children);
for ($i = 0, $length = \count($children); $i < $length; ++$i) {
if ($this->list === false) {
throw FeatureNotAllowedException::fromFeature('square bracket list types', $offset);
}
$statement = new Node\Stmt\TypesListNode($statement);
$statement->offset = $children[$i]->getOffset();
}
Expand Down
4 changes: 4 additions & 0 deletions resources/grammar/template-arguments.pp2
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
TemplateArguments -> {
if ($this->generics === false) {
throw FeatureNotAllowedException::fromFeature('template arguments', $offset);
}
return new Node\Stmt\Template\ArgumentsListNode($children);
}
: ::T_ANGLE_BRACKET_OPEN::
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/FeatureNotAllowedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class FeatureNotAllowedException extends SemanticException
*/
public static function fromFeature(string $name, int $offset = 0): self
{
$message = \sprintf('%s not allowed by parser configuration', $name);
$message = \sprintf('%s not allowed', $name);

return new static($offset, $message);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public function __construct(
public readonly bool $shapes = true,
public readonly bool $callables = true,
public readonly bool $literals = true,
public readonly bool $generics = true,
public readonly bool $union = true,
public readonly bool $intersection = true,
public readonly bool $list = true,
private readonly SourceFactoryInterface $sources = new SourceFactory(),
) {
/** @psalm-var GrammarConfigArray $grammar */
Expand Down

0 comments on commit f573dd7

Please sign in to comment.