type-lang/parser

Library for parsing and validating TypeLang syntax and converting it into AST nodes

1.2.1 2024-09-27 03:02 UTC

README

PHP 8.1+ Latest Stable Version Latest Unstable Version License MIT

Reference implementation for TypeLang Parser.

TypeLang is a declarative type language inspired by static analyzers like PHPStan and Psalm.

Read documentation pages for more information.

Installation

TypeLang Parser is available as Composer repository and can be installed using the following command in a root of your project:

composer require type-lang/parser

Quick Start

$parser = new \TypeLang\Parser\Parser();

$type = $parser->parse(<<<'PHP'
    array{
        key: callable(Example, int): mixed,
        ...
    }
    PHP);

var_dump($type);

Expected Output:

TypeLang\Parser\Node\Stmt\NamedTypeNode {
  +offset: 0
  +name: TypeLang\Parser\Node\Name {
    +offset: 0
    -parts: array:1 [
      0 => TypeLang\Parser\Node\Identifier {
        +offset: 0
        +value: "array"
      }
    ]
  }
  +arguments: null
  +fields: TypeLang\Parser\Node\Stmt\Shape\FieldsListNode {
    +offset: 11
    +items: array:1 [
      0 => TypeLang\Parser\Node\Stmt\Shape\NamedFieldNode {
        +offset: 11
        +type: TypeLang\Parser\Node\Stmt\CallableTypeNode {
          +offset: 16
          +name: TypeLang\Parser\Node\Name {
            +offset: 16
            -parts: array:1 [
              0 => TypeLang\Parser\Node\Identifier {
                +offset: 16
                +value: "callable"
              }
            ]
          }
          +parameters: TypeLang\Parser\Node\Stmt\Callable\ParametersListNode {
            +offset: 25
            +items: array:2 [
              0 => TypeLang\Parser\Node\Stmt\Callable\ParameterNode {
                +offset: 25
                +type: TypeLang\Parser\Node\Stmt\NamedTypeNode {
                  +offset: 25
                  +name: TypeLang\Parser\Node\Name {
                    +offset: 25
                    -parts: array:1 [
                      0 => TypeLang\Parser\Node\Identifier {
                        +offset: 25
                        +value: "Example"
                      }
                    ]
                  }
                  +arguments: null
                  +fields: null
                }
                +name: null
                +output: false
                +variadic: false
                +optional: false
              }
              1 => TypeLang\Parser\Node\Stmt\Callable\ParameterNode {
                +offset: 34
                +type: TypeLang\Parser\Node\Stmt\NamedTypeNode {
                  +offset: 34
                  +name: TypeLang\Parser\Node\Name {
                    +offset: 34
                    -parts: array:1 [
                      0 => TypeLang\Parser\Node\Identifier {
                        +offset: 34
                        +value: "int"
                      }
                    ]
                  }
                  +arguments: null
                  +fields: null
                }
                +name: null
                +output: false
                +variadic: false
                +optional: false
              }
            ]
          }
          +type: TypeLang\Parser\Node\Stmt\NamedTypeNode {
            +offset: 40
            +name: TypeLang\Parser\Node\Name {
              +offset: 40
              -parts: array:1 [
                0 => TypeLang\Parser\Node\Identifier {
                  +offset: 40
                  +value: "mixed"
                }
              ]
            }
            +arguments: null
            +fields: null
          }
        }
        +optional: false
        +key: TypeLang\Parser\Node\Identifier {
          +offset: 11
          +value: "key"
        }
      }
    ]
    +sealed: false
  }
}