Skip to content

Commit 65d08ec

Browse files
committed
init
0 parents  commit 65d08ec

File tree

14 files changed

+483
-0
lines changed

14 files changed

+483
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
/composer.lock

composer.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "phpstan/type-parser",
3+
"license": "MIT",
4+
"autoload": {
5+
"classmap": ["src"]
6+
}
7+
}

doc/grammer.ebnf

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Type
2+
::= Atomic (Union | Intersection)?
3+
| Complement
4+
5+
Union
6+
::= ('|' Atomic)+
7+
8+
Intersection
9+
::= ('&' Atomic)+
10+
11+
Complement
12+
::= '~' Atomic
13+
14+
Atomic
15+
::= Identifier (Generic | Array)?
16+
| '(' Type ')' Array?
17+
18+
Generic
19+
::= '<' Type (',' Type)* '>'
20+
21+
Array
22+
::= ('[' ']')+
23+
24+
Identifier
25+
::= ('\'? Word)+
26+
27+
Word
28+
::= [a-zA-Z_#x7F-#x10FFFF][0-9a-zA-Z_#x7F-#x10FFFF]*

doc/grammer.peg

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Type
2+
= Atomic (Union / Intersection)?
3+
/ Complement
4+
5+
Union
6+
= ('|' Atomic)+
7+
8+
Intersection
9+
= ('&' Atomic)+
10+
11+
Complement
12+
= '~' Atomic
13+
14+
Atomic
15+
= Identifier (Generic / Array)?
16+
/ '(' Type ')' Array?
17+
18+
Generic
19+
= '<' Type (',' Type)* '>'
20+
21+
Array
22+
= ('[' ']')+
23+
24+
Identifier
25+
= ('\'? Word)+
26+
27+
Word
28+
= [a-zA-Z_\127-\255\\][a-zA-Z0-9_\127-\255\\]*

src/Ast/ArrayNode.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PhpStan\TypeParser\Ast;
4+
5+
6+
class ArrayNode implements Node
7+
{
8+
/** @var Node */
9+
public $type;
10+
11+
12+
public function __construct(Node $type)
13+
{
14+
$this->type = $type;
15+
}
16+
17+
18+
public function __toString(): string
19+
{
20+
return $this->type . '[]';
21+
}
22+
}

src/Ast/ComplementNode.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PhpStan\TypeParser\Ast;
4+
5+
6+
class ComplementNode implements Node
7+
{
8+
/** @var Node */
9+
public $type;
10+
11+
12+
public function __construct(Node $type)
13+
{
14+
$this->type = $type;
15+
}
16+
17+
18+
public function __toString(): string
19+
{
20+
return '~' . $this->type;
21+
}
22+
}

src/Ast/GenericNode.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PhpStan\TypeParser\Ast;
4+
5+
6+
class GenericNode implements Node
7+
{
8+
/** @var SimpleNode */
9+
public $type;
10+
11+
/** @var Node[] */
12+
public $genericTypes;
13+
14+
15+
public function __construct(SimpleNode $type, array $genericTypes)
16+
{
17+
$this->type = $type;
18+
$this->genericTypes = $genericTypes;
19+
}
20+
21+
22+
public function __toString(): string
23+
{
24+
return $this->type . '<' . implode(', ', $this->genericTypes) . '>';
25+
}
26+
}

src/Ast/IntersectionNode.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PhpStan\TypeParser\Ast;
4+
5+
6+
class IntersectionNode implements Node
7+
{
8+
/** @var Node[] */
9+
public $types;
10+
11+
12+
public function __construct(array $types)
13+
{
14+
$this->types = $types;
15+
}
16+
17+
18+
public function __toString(): string
19+
{
20+
return '(' . implode(' & ', $this->types) . ')';
21+
}
22+
}

src/Ast/Node.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PhpStan\TypeParser\Ast;
4+
5+
6+
interface Node
7+
{
8+
public function __toString(): string;
9+
}

src/Ast/SimpleNode.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PhpStan\TypeParser\Ast;
4+
5+
6+
class SimpleNode implements Node
7+
{
8+
/** @var string */
9+
public $name;
10+
11+
12+
public function __construct(string $name)
13+
{
14+
$this->name = $name;
15+
}
16+
17+
18+
public function __toString(): string
19+
{
20+
return $this->name;
21+
}
22+
}

0 commit comments

Comments
 (0)