xp-framework / ast
AST for the XP Framework
Installs: 69 760
Dependents: 2
Suggesters: 0
Security: 0
Stars: 3
Watchers: 3
Forks: 0
Open Issues: 4
Requires
- php: >=7.4.0
- xp-framework/core: ^12.0 | ^11.0 | ^10.0
Requires (Dev)
- xp-framework/test: ^2.0 | ^1.0
- dev-master
- v11.3.1
- v11.3.0
- v11.2.1
- v11.2.0
- v11.1.0
- v11.0.1
- v11.0.0
- v10.3.1
- v10.3.0
- v10.2.3
- v10.2.2
- v10.2.1
- v10.2.0
- v10.1.0
- v10.0.0
- v9.2.7
- v9.2.6
- v9.2.5
- v9.2.4
- v9.2.3
- v9.2.2
- v9.2.1
- v9.2.0
- v9.1.0
- v9.0.0
- v8.2.0
- v8.1.0
- v8.0.1
- v8.0.0
- v7.7.2
- v7.7.1
- v7.7.0
- v7.6.2
- v7.6.1
- v7.6.0
- v7.5.1
- v7.5.0
- v7.4.0
- v7.3.0
- v7.2.0
- v7.1.1
- v7.1.0
- v7.0.4
- v7.0.3
- v7.0.2
- v7.0.1
- v7.0.0
- v6.1.0
- v6.0.0
- v5.4.0
- v5.3.0
- v5.2.0
- v5.1.0
- v5.0.0
- v4.0.0
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.0.0
- v1.4.0
- v1.3.0
- v1.2.0
- v1.1.0
- v1.0.1
- v1.0.0
- dev-feature/pipelines
- dev-feature/type-alias
- dev-feature/generic-methods
- dev-feature/unchecked-types
- dev-feature/using
This package is auto-updated.
Last update: 2024-12-02 12:14:26 UTC
README
Abstract syntax tree library used for XP Compiler.
Example
use lang\ast\{Language, Tokens}; $tree= Language::named('PHP')->parse(new Tokens('echo PHP_VERSION;'))->tree(); // lang.ast.ParseTree(source: (string))@{ // scope => lang.ast.Scope { // parent => null // package => null // imports => [] // types => [] // } // children => [lang.ast.nodes.EchoStatement { // kind => "echo" // expressions => [lang.ast.nodes.Literal { // kind => "literal" // expression => "PHP_VERSION" // line => 1 // }] // line => 1 // }] // }
Compile-time metaprogramming
Register transformations by creating classes inside the lang.ast.syntax.php
package - see xp-framework/rfc#327
namespace lang\ast\syntax\php; use lang\ast\Code; use lang\ast\nodes\{Method, Signature}; use lang\ast\syntax\Extension; use codegen\Getters; class CreateGetters implements Extension { public function setup($language, $emitter) { $emitter->transform('class', function($codegen, $class) { if ($class->annotation(Getters::class)) { foreach ($class->properties() as $property) { $class->declare(new Method( ['public'], $property->name, new Signature([], $property->type), [new Code('return $this->'.$property->name)] )); } } return $class; }); } }
When compiling the following sourcecode, getters for the id
and name
members will automatically be added.
use codegen\Getters; #[Getters] class Person { private int $id; private string $name; public function __construct(int $id, string $name) { $this->id= $id; $this->name= $name; } }