Core\Ast::parse
Core\Ast::parse(string $source): Core\Ast\NodeCore\Ast::parse( string $source, ): Core\Ast\Node
Reads Novis source code and returns a tree that describes it.
You pass the text of a program, starting with <?nvs the way a file does. You get back one node for the whole file. Every node says what kind of part it is, such as ClassDecl, Echo or Int, and where that part starts. From a node you can walk down to the parts inside it.
This is the same parser the nvs command uses. Source the compiler accepts is accepted here. Source it rejects throws a ParseError naming the line and column of the first problem.
Good to know: the tree is data. Nothing in it runs the code you parsed, and no node gives you the source text back.
The examples below show the parts of a small program, a check for valid source, and a project rule that forbids exit.
Parameters
$sourcestringNovis source code. As in a file, the code starts after the<?nvsopening tag.
Return value
Returns Core\Ast\Node. The root node of the file. Its kind() is File, and its children are the top-level statements. The tree is only data, and none of its code runs.
Errors
ParseError$sourcedoes not parse. The message has the first error with its line and column. Any source that the compiler rejects throws this error here too.
Examples
The parts of a program
<?nvs// Shows what a piece of Novis source is made of. `Core\Ast::parse` returns one// node for the whole file, and `children` gives the parts directly inside it.
string $source = <<<'NVS'<?nvsclass Cart { public int $total;}
int $count = 2;echo "items: ", $count, "\n";NVS;
Core\Ast\Node $file = Core\Ast::parse($source);// The whole file is one node. This prints `File`.echo $file->kind(), "\n";
// Each part of the file, with the line it starts on.foreach ($file->children() as Core\Ast\Node $part) { echo $part->kind(), ' on line ', $part->line(), "\n";}FileClassDecl on line 2LocalDecl on line 6Echo on line 7Checking that source is valid
<?nvs// Shows how to check that a piece of source is valid, as an editor plugin does// before it saves a snippet. `Core\Ast::parse` throws a `ParseError` when the// text does not parse.
array<string> $snippets = [ '<?nvs echo 1 + 2;', '<?nvs echo 1 +;',];
foreach ($snippets as string $snippet) { try { Core\Ast::parse($snippet); echo "valid\n"; } catch (ParseError $bad) { // The message names the line and column of the first problem. echo 'not valid: ', $bad->message, "\n"; }}validnot valid: Core\Ast::parse(): line 1, column 15: expected an expressionA project rule that forbids exit
<?nvs// Shows a project rule of your own: no file may stop the program with `exit`.// `nodes` gives every node in the file, so a call deep inside a method is found// too. Each one reports the place a person can go and fix.
string $source = <<<'NVS'<?nvsclass Report { public static function run(int $rows): void { if ($rows == 0) { exit(1); } echo "rows: ", $rows, "\n"; }}
Report::run(0);NVS;
Core\Ast\Node $file = Core\Ast::parse($source);int $found = 0;foreach ($file->nodes() as Core\Ast\Node $node) { if ($node->kind() == 'Exit') { $found = $found + 1; echo 'exit at line ', $node->line(), ', column ', $node->column(), "\n"; }}echo $found == 0 ? 'the file follows the rule' : 'the file breaks the rule', "\n";exit at line 5, column 13the file breaks the rule