xp-framework / ast
XP Framework 的抽象语法树(AST)
v11.3.0
2024-08-26 19:28 UTC
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.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/asymmetric-visibility
- 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-08-26 19:29:08 UTC
README
用于 XP 编译器 的抽象语法树库。
示例
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 // }] // }
编译时元编程
通过在 lang.ast.syntax.php
包内部创建类来注册转换 - 查看 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; }); } }
在编译以下源代码时,将自动添加对 id
和 name
成员的获取器。
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; } }