phptl / parser
Library for parsing and validating TypeLang syntax and converting it into AST nodes
Requires
- php: ^8.1
- phplrt/exception: ^4.2.3
- phplrt/lexer: ^4.2.3
- phplrt/lexer-contracts: ^4.2.3
- phplrt/parser: ^4.2.3
- phplrt/position: ^4.2.3
- phplrt/position-contracts: ^4.2.3
- phplrt/position-factory-contracts: ^4.2.3
- phplrt/source: ^4.2.3
- phplrt/source-contracts: ^4.2.3
- phplrt/source-factory-contracts: ^4.2.3
- type-lang/types: 2.0.0-beta10
Requires (Dev)
- jetbrains/phpstorm-attributes: ^1.3
- phplrt/compiler: ^4.2.3
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^10.5.36|^11.0|^12.0|^13.0
- type-lang/printer: 2.0.0-beta10
Suggests
None
Provides
None
Conflicts
None
Replaces
None
- dev-master / 2.x-dev
- 2.0.0-beta10
- 2.0.0-beta9
- 2.0.0-beta8
- 2.0.0-beta7
- 2.0.0-beta6
- 2.0.0-beta5
- 2.0.0-beta4
- 2.0.0-beta3
- 2.0.0-beta2
- 2.0.0-beta1
- 1.6.0
- 1.5.0
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- 1.0.0-RC1
- 1.0.0-beta7
- 1.0.0-beta6
- 1.0.0-beta5
- 1.0.0-beta4
- 1.0.0-beta3
- 1.0.0-beta2
- 1.0.0-beta1
This package is auto-updated.
Last update: 2026-11-30 11:30:25 UTC
README
The reference parser for TypeLang — a declarative type language inspired by static analyzers like PHPStan and Psalm.
It reads a type declaration string and builds an AST of TypeLang\Type\* nodes,
checking the grammar along the way. The node classes themselves live in the
separate type-lang/types package.
- Full documentation is available at typelang.dev.
- Language specification is available here.
Installation
Install the package via Composer:
composer require type-lang/parser
Requirements:
- PHP 8.1+
Usage
TypeLang\Parser\TypeParser is the entry point. Its parse() method turns a
type declaration into a TypeLang\Type\TypeNode:
$parser = new TypeLang\Parser\TypeParser(); $type = $parser->parse('array{ key: int }'); var_dump($type); // object(TypeLang\Type\NamedTypeNode) { // ["name"] => "array" // ["fields"] => object(TypeLang\Type\Shape\FieldListNode) { ... } // ... // }
Every node exposes an $offset (byte offset in the source) plus a handful of
properties describing its kind.
Strict vs. Tolerant Parsing
parse(): TypeNode— strict mode; requires the whole input to be a valid type and throws aParserExceptionInterfaceon the first error.partial(): ParsedResult— parses as much as it can and reports how far it consumed the source. Useful for phpdoc, where a type is followed by a free-text description.
$result = $parser->partial('int and some trailing description'); $result->type; // TypeLang\Type\NamedTypeNode ("int") $result->offset; // offset where parsing stopped
See the documentation for feature toggling, visitors and name resolution.