Core\Ast\Node::kind
$node->kind(): stringThe name of the construct this node is, such as If, Echo or ClassDecl.
Every part of a parsed file is one construct of the language, and kind is its name. The name comes from the grammar, not from the text of the program, so two files that write the same construct differently still give the same name. The node you get back from Core\Ast::parse is the whole file, and its kind is File.
This is what you branch on when you walk a tree. A tool that treats loops one way and declarations another reads kind at every node and decides from there. The set of names is fixed and known, so a name your walk does not handle is a construct you have not covered yet.
Good to know: the name says what the construct is, never what it says. Use line, column and offset to find the part in the source when you need the text itself.
The examples below show what each part of a small program is, how to count the loops in a file, and a check that a file declares classes and runs nothing else.
Return value
Returns string. The name of the node kind. It is the same name the language documentation uses.
Examples
What each part of a program is
<?nvs// Shows what each part of a program is. `kind` returns the name of the// construct, and `children` gives the parts one step down from the file.
string $source = <<<'NVS'<?nvsint $count = 2;if ($count > 1) { echo "many\n";}NVS;
// This prints LocalDecl, then If.foreach (Core\Ast::parse($source)->children() as Core\Ast\Node $part) { echo $part->kind(), "\n";}LocalDeclIfCounting the loops in a file
<?nvs// Shows how to count the loops in a file, which is one way to find the files// worth reading closely. `nodes` gives every node of the tree, however deep it// sits, and `kind` says which construct each one is.
string $source = <<<'NVS'<?nvsforeach ([1, 2] as int $n) { while ($n > 0) { $n = $n - 1; }}for (int $i = 0; $i < 3; $i = $i + 1) { echo $i;}NVS;
// The four names the language uses for its loops.array<string> $loops = ['For', 'Foreach', 'While', 'DoWhile'];uint $found = 0;
foreach (Core\Ast::parse($source)->nodes() as Core\Ast\Node $node) { if (Core\Arr::contains($loops, $node->kind())) { $found = $found + 1; }}
// This prints 3.echo 'loops in this file: ', $found, "\n";loops in this file: 3Checking a file holds only classes
<?nvs// Shows how to check that a file only declares classes. A project that loads a// file by the class name inside it needs that: every other part of the file// runs as soon as the file is loaded.
class LoadCheck{ public static function describe(string $source): string { foreach (Core\Ast::parse($source)->children() as Core\Ast\Node $part) { if ($part->kind() != 'ClassDecl') { return 'also holds ' . $part->kind() . ', which runs when the file is loaded'; } } return 'declares classes only'; }}
string $clean = <<<'NVS'<?nvsclass Invoice { public static function total(int $a): int { return $a; }}NVS;
string $mixed = <<<'NVS'<?nvsclass Report {}echo "building the report\n";NVS;
echo LoadCheck::describe($clean), "\n";echo LoadCheck::describe($mixed), "\n";declares classes onlyalso holds Echo, which runs when the file is loaded