Skip to content

Commit 6fedb02

Browse files
committed
Cache native AST child materialization
1 parent 48db7c5 commit 6fedb02

1 file changed

Lines changed: 16 additions & 48 deletions

File tree

packages/mysql-on-sqlite/src/parser/class-wp-parser-node.php

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ public function merge_fragment( $node ) {
113113
* @return bool True if this node has any child nodes or tokens, false otherwise.
114114
*/
115115
public function has_child(): bool {
116-
if ( $this->has_native_ast() ) {
117-
return wp_sqlite_mysql_native_ast_has_child( $this->native_ast, $this->native_node_index );
118-
}
116+
$this->materialize_native_children();
119117
return count( $this->children ) > 0;
120118
}
121119

@@ -126,9 +124,7 @@ public function has_child(): bool {
126124
* @return bool True if any child nodes are found, false otherwise.
127125
*/
128126
public function has_child_node( ?string $rule_name = null ): bool {
129-
if ( $this->has_native_ast() ) {
130-
return wp_sqlite_mysql_native_ast_has_child_node( $this->native_ast, $this->native_node_index, $rule_name );
131-
}
127+
$this->materialize_native_children();
132128
foreach ( $this->children as $child ) {
133129
if (
134130
$child instanceof WP_Parser_Node
@@ -147,9 +143,7 @@ public function has_child_node( ?string $rule_name = null ): bool {
147143
* @return bool True if any child tokens are found, false otherwise.
148144
*/
149145
public function has_child_token( ?int $token_id = null ): bool {
150-
if ( $this->has_native_ast() ) {
151-
return wp_sqlite_mysql_native_ast_has_child_token( $this->native_ast, $this->native_node_index, $token_id );
152-
}
146+
$this->materialize_native_children();
153147
foreach ( $this->children as $child ) {
154148
if (
155149
$child instanceof WP_Parser_Token
@@ -168,9 +162,7 @@ public function has_child_token( ?int $token_id = null ): bool {
168162
* null when no children are found.
169163
*/
170164
public function get_first_child() {
171-
if ( $this->has_native_ast() ) {
172-
return wp_sqlite_mysql_native_ast_get_first_child( $this->native_ast, $this->native_node_index );
173-
}
165+
$this->materialize_native_children();
174166
return $this->children[0] ?? null;
175167
}
176168

@@ -181,9 +173,7 @@ public function get_first_child() {
181173
* @return WP_Parser_Node|null The first matching child node; null when no children are found.
182174
*/
183175
public function get_first_child_node( ?string $rule_name = null ): ?WP_Parser_Node {
184-
if ( $this->has_native_ast() ) {
185-
return wp_sqlite_mysql_native_ast_get_first_child_node( $this->native_ast, $this->native_node_index, $rule_name );
186-
}
176+
$this->materialize_native_children();
187177
foreach ( $this->children as $child ) {
188178
if (
189179
$child instanceof WP_Parser_Node
@@ -202,9 +192,7 @@ public function get_first_child_node( ?string $rule_name = null ): ?WP_Parser_No
202192
* @return WP_Parser_Token|null The first matching child token; null when no children are found.
203193
*/
204194
public function get_first_child_token( ?int $token_id = null ): ?WP_Parser_Token {
205-
if ( $this->has_native_ast() ) {
206-
return wp_sqlite_mysql_native_ast_get_first_child_token( $this->native_ast, $this->native_node_index, $token_id );
207-
}
195+
$this->materialize_native_children();
208196
foreach ( $this->children as $child ) {
209197
if (
210198
$child instanceof WP_Parser_Token
@@ -226,9 +214,7 @@ public function get_first_child_token( ?int $token_id = null ): ?WP_Parser_Token
226214
* @return WP_Parser_Node|null The first matching descendant node; null when no descendants are found.
227215
*/
228216
public function get_first_descendant_node( ?string $rule_name = null ): ?WP_Parser_Node {
229-
if ( $this->has_native_ast() ) {
230-
return wp_sqlite_mysql_native_ast_get_first_descendant_node( $this->native_ast, $this->native_node_index, $rule_name );
231-
}
217+
$this->materialize_native_children();
232218
for ( $i = 0; $i < count( $this->children ); $i++ ) {
233219
$child = $this->children[ $i ];
234220
if ( ! $child instanceof WP_Parser_Node ) {
@@ -255,9 +241,7 @@ public function get_first_descendant_node( ?string $rule_name = null ): ?WP_Pars
255241
* @return WP_Parser_Token|null The first matching descendant token; null when no descendants are found.
256242
*/
257243
public function get_first_descendant_token( ?int $token_id = null ): ?WP_Parser_Token {
258-
if ( $this->has_native_ast() ) {
259-
return wp_sqlite_mysql_native_ast_get_first_descendant_token( $this->native_ast, $this->native_node_index, $token_id );
260-
}
244+
$this->materialize_native_children();
261245
for ( $i = 0; $i < count( $this->children ); $i++ ) {
262246
$child = $this->children[ $i ];
263247
if ( $child instanceof WP_Parser_Token ) {
@@ -280,9 +264,7 @@ public function get_first_descendant_token( ?int $token_id = null ): ?WP_Parser_
280264
* @return array<WP_Parser_Node|WP_Parser_Token> An array of all child nodes and tokens of this node.
281265
*/
282266
public function get_children(): array {
283-
if ( $this->has_native_ast() ) {
284-
return wp_sqlite_mysql_native_ast_get_children( $this->native_ast, $this->native_node_index );
285-
}
267+
$this->materialize_native_children();
286268
return $this->children;
287269
}
288270

@@ -293,9 +275,7 @@ public function get_children(): array {
293275
* @return WP_Parser_Node[] An array of all matching child nodes.
294276
*/
295277
public function get_child_nodes( ?string $rule_name = null ): array {
296-
if ( $this->has_native_ast() ) {
297-
return wp_sqlite_mysql_native_ast_get_child_nodes( $this->native_ast, $this->native_node_index, $rule_name );
298-
}
278+
$this->materialize_native_children();
299279
$nodes = array();
300280
foreach ( $this->children as $child ) {
301281
if (
@@ -315,9 +295,7 @@ public function get_child_nodes( ?string $rule_name = null ): array {
315295
* @return WP_Parser_Token[] An array of all matching child tokens.
316296
*/
317297
public function get_child_tokens( ?int $token_id = null ): array {
318-
if ( $this->has_native_ast() ) {
319-
return wp_sqlite_mysql_native_ast_get_child_tokens( $this->native_ast, $this->native_node_index, $token_id );
320-
}
298+
$this->materialize_native_children();
321299
$tokens = array();
322300
foreach ( $this->children as $child ) {
323301
if (
@@ -339,9 +317,7 @@ public function get_child_tokens( ?int $token_id = null ): array {
339317
* @return array<WP_Parser_Node|WP_Parser_Token> An array of all descendant nodes and tokens of this node.
340318
*/
341319
public function get_descendants(): array {
342-
if ( $this->has_native_ast() ) {
343-
return wp_sqlite_mysql_native_ast_get_descendants( $this->native_ast, $this->native_node_index );
344-
}
320+
$this->materialize_native_children();
345321
$descendants = array();
346322
foreach ( $this->children as $child ) {
347323
if ( $child instanceof WP_Parser_Node ) {
@@ -365,9 +341,7 @@ public function get_descendants(): array {
365341
* @return WP_Parser_Node[] An array of all matching descendant nodes.
366342
*/
367343
public function get_descendant_nodes( ?string $rule_name = null ): array {
368-
if ( $this->has_native_ast() ) {
369-
return wp_sqlite_mysql_native_ast_get_descendant_nodes( $this->native_ast, $this->native_node_index, $rule_name );
370-
}
344+
$this->materialize_native_children();
371345
$nodes = array();
372346
foreach ( $this->children as $child ) {
373347
if ( ! $child instanceof WP_Parser_Node ) {
@@ -392,9 +366,7 @@ public function get_descendant_nodes( ?string $rule_name = null ): array {
392366
* @return WP_Parser_Token[] An array of all matching descendant tokens.
393367
*/
394368
public function get_descendant_tokens( ?int $token_id = null ): array {
395-
if ( $this->has_native_ast() ) {
396-
return wp_sqlite_mysql_native_ast_get_descendant_tokens( $this->native_ast, $this->native_node_index, $token_id );
397-
}
369+
$this->materialize_native_children();
398370
$tokens = array();
399371
foreach ( $this->children as $child ) {
400372
if ( $child instanceof WP_Parser_Token ) {
@@ -414,9 +386,7 @@ public function get_descendant_tokens( ?int $token_id = null ): array {
414386
* @return int The byte offset in the input string where this node begins.
415387
*/
416388
public function get_start(): int {
417-
if ( $this->has_native_ast() ) {
418-
return wp_sqlite_mysql_native_ast_get_start( $this->native_ast, $this->native_node_index );
419-
}
389+
$this->materialize_native_children();
420390
return $this->get_first_descendant_token()->start;
421391
}
422392

@@ -426,9 +396,7 @@ public function get_start(): int {
426396
* @return int The byte length of this node in the input string.
427397
*/
428398
public function get_length(): int {
429-
if ( $this->has_native_ast() ) {
430-
return wp_sqlite_mysql_native_ast_get_length( $this->native_ast, $this->native_node_index );
431-
}
399+
$this->materialize_native_children();
432400
$tokens = $this->get_descendant_tokens();
433401
$first_token = $tokens[0];
434402
$last_token = $tokens[ count( $tokens ) - 1 ];

0 commit comments

Comments
 (0)