GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - php-type-language/parser: [READ ONLY] 🧱 Library for parsing and validating TypeLang syntax and converting it into AST nodes

BranchesTags
Open more actions menu

Latest commit

 

History

296 Commits
296 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
bin
bin
 
 
resources
resources
 
 
src
src
 
 
tests
tests
 
 
.editorconfig
.editorconfig
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
composer.json
composer.json
 
 
phpstan.neon
phpstan.neon
 
 
phpunit.xml
phpunit.xml
 
 

Repository files navigation

PHP 8.4+ Latest Stable Version Latest Unstable Version License MIT


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\FieldsListNode) { ... }
//   ...
// }

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 a ParserExceptionInterface on the first error.
  • parseTolerant(): 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 = $result->parseTolerant('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.

About

[READ ONLY] 🧱 Library for parsing and validating TypeLang syntax and converting it into AST nodes

typelang.dev

Topics

definitiondocblockgrammarparserphpphpdocphpdocumentorphpstanpsalmstricttagstype-systemtypes

Resources

Readme
MIT license

Code of conduct

Code of conduct
Activity
Custom properties

Stars

51 stars

Watchers

2 watching

Forks

2 forks
Report repository

Releases

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.