final class Node
Public API: explicitly declared with @visibility public.

One nonterminal of the syntax tree, named after the grammar rule that built it.

The tree is concrete: every token of the SQL text is a leaf, and the nonterminals are those of the official grammar, so a node is named after the rule of the upstream grammar file that built it. The ordinal says which alternative of the rule matched.

Nothing of the text is left out. Every token carries the whitespace and comments written before it and the tree carries what follows its last token, so a tree writes back the text it was parsed from, byte for byte.

Reading the text a node coversdoctest
$sql = 'SELECT 1';
    $select = new \SqlParser\Lexer\Token(3, 'SELECT', 'SELECT', 0);
    $one = new \SqlParser\Lexer\Token(7, 'NUM', '1', 7);
    $tree = new \SqlParser\Parser\Node('statement', 0, [$select, new \SqlParser\Parser\Node('expr', 2, [$one])]);
    $tree->find('expr')[0]->text($sql) // => '1'
    $tree->text($sql) // => 'SELECT 1'
Writing the tree back as the text it was parsed fromdoctest
$select = new \SqlParser\Lexer\Token(3, 'SELECT', 'SELECT', 0);
    $one = new \SqlParser\Lexer\Token(7, 'NUM', '1', 9, '   ');
    $tree = new \SqlParser\Parser\Node('statement', 0, [$select, new \SqlParser\Parser\Node('expr', 2, [$one])], ' ');
    $tree->toString() // => 'SELECT   1 '

Properties§

public string $name
public int $ordinal
public list<Node|Token> $children
public string $trailing = ''

Methods§

public function __construct(
    public string $name,
    public int $ordinal,
    public list<Node|Token> $children,
    public string $trailing = '',
)

Parameters

$namestringNonterminal name as the grammar spells it
$ordinalintWhich alternative of the nonterminal matched, counted from zero
$childrenlist<Node|Token>What the alternative matched, in order
$trailingstringWhitespace and comments written after the last token, which only the root of a tree carries
public function isEmpty(): bool

Reports whether the node matched nothing at all.

Returns

bool True for an empty alternative
Test cases 1
public function tokens(): list<Token>

Answers every token under the node, in text order.

Returns

list<Token> The tokens
Test cases 2
Called from 19
Calls 1
public function find(string $name): list<Node>

Answers the nodes under this one with a given name, this one included, in text order.

Parameters

$namestringNonterminal name

Returns

list<Node> The matching nodes
Test cases 1
Called from 3
Calls 1
public function span(): array{int, int}|null

Answers the byte range of the text the node covers.

Returns

array{int, int}|null Start offset and end offset, or null for an empty node
Test cases 1
Called from 1
Calls 2
public function text(string $source): string

Answers the SQL text the node covers.

Parameters

$sourcestringThe SQL text the tree was parsed from

Returns

string The covered text, empty for an empty node
Test cases 1
Calls 2
public function toString(): string

Writes the node back as the SQL text it was parsed from, trivia included.

Returns

string The text, which for the root of a tree is the whole of what was parsed
Test cases 1

Test cases 38§

Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.

Dedicated tests 6
Other tests reaching this symbol 32

Relations§

Instantiated in 3
Method calls 21
Type checks 12
Type declarations 133