Type Parser Component
The parser component analyzes a TypeLang type declaration string and builds an AST (Abstract Syntax Tree) out of it, checking the grammar along the way.
Installation
Requirements:
PHP >= 8.4
Quick Start
The TypeLang\Parser\TypeParser class is the entry point of the component. Its parse() method turns a type declaration string into a TypeLang\Type\TypeNode instance.
Every node exposes an $offset (byte offset in the source where the node starts) and, depending on its kind, a handful of other public properties. The node classes themselves (TypeLang\Type\*) belong to the separate type-lang/types package — plain AST Nodes.
Strict vs. Tolerant Parsing
TypeParser implements two parsing strategies, both declared on TypeParserInterface:
parse(): TypeNode— strict mode. Requires the whole input to be a syntactically valid type statement; throws aParserExceptionInterfaceon the first error.parseTolerant(): ParsedResult— tolerant mode. Parses as much of the input as it can and returns aTypeLang\Parser\ParsedResultobject containing the (possibly partial) type and the offset up to which the source was actually consumed — regardless of what follows. Useful for phpdoc/docblock parsing where a type declaration is followed by a free-text description. See Tolerant mode.
Parser Arguments
The parse()/parseTolerant() methods accept the source code in any of the following forms:
What's Next
- Feature toggling
Enable or disable individual language constructs (generics, shapes, unions, ...) — see Feature Toggling.
- Tolerant mode
Parse a type declaration embedded in free-form text, such as a phpdoc annotation — see Tolerant Mode.
- Visitors
Traverse, search, and rewrite a parsed AST — see Visitors.
- Name resolution
Resolve short/relative names in an AST against
usestatements or any other custom rule — see Name Resolution.