Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

535 Commits

Repository files navigation

XP AST

Build status on GitHubXP Framework ModuleBSD LicenceRequires PHP 7.4+Supports PHP 8.0+Latest Stable Version

Abstract syntax tree library used for XP Compiler.

Example

uselang\ast\{Language, Tokens};
$tree= Language::named('PHP')->parse(newTokens('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

namespacelang\ast\syntax\php;
uselang\ast\Code;
uselang\ast\nodes\{Method, Signature};
uselang\ast\syntax\Extension;
usecodegen\Getters;
class CreateGetters implements Extension {
publicfunctionsetup($language, $emitter) {
$emitter->transform('class', function($codegen, $class) {
if ($class->annotation(Getters::class)) {
foreach ($class->properties() as$property) {
$class->declare(newMethod(
['public'],
$property->name,
newSignature([], $property->type),
[newCode('return $this->'.$property->name)]
));
}
}
return$class;
});
}
}

When compiling the following sourcecode, getters for the id and name members will automatically be added.

usecodegen\Getters;
#[Getters]
class Person {
privateint$id;
privatestring$name;
publicfunction__construct(int$id, string$name) {
$this->id= $id;
$this->name= $name;
}
}

Releases

Packages

Used by

Contributors

Languages