Skip to content

Commit b20499f

Browse files
committed
Move native parser node logic to subclass
1 parent dce6e67 commit b20499f

3 files changed

Lines changed: 179 additions & 68 deletions

File tree

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?php
2+
3+
/**
4+
* Native-backed parser node.
5+
*
6+
* This subclass keeps the regular WP_Parser_Node API while delegating lazy AST
7+
* reads to the optional native MySQL parser extension. The base node remains a
8+
* plain PHP tree node for the polyfill parser.
9+
*/
10+
class WP_MySQL_Native_Parser_Node extends WP_Parser_Node {
11+
private $native_ast = null;
12+
private $native_node_index = null;
13+
14+
public function __construct( $rule_id, $rule_name, $native_ast = null, $native_node_index = null ) {
15+
parent::__construct( $rule_id, $rule_name );
16+
17+
$this->native_ast = $native_ast;
18+
$this->native_node_index = $native_node_index;
19+
}
20+
21+
/** @inheritDoc */
22+
public function append_child( $node ) {
23+
$this->materialize_native_children();
24+
parent::append_child( $node );
25+
}
26+
27+
/** @inheritDoc */
28+
public function merge_fragment( $node ) {
29+
$this->materialize_native_children();
30+
if ( $node instanceof self ) {
31+
$node->materialize_native_children();
32+
}
33+
parent::merge_fragment( $node );
34+
}
35+
36+
/** @inheritDoc */
37+
public function has_child(): bool {
38+
if ( $this->has_native_ast() ) {
39+
return wp_sqlite_mysql_native_ast_has_child( $this->native_ast, $this->native_node_index );
40+
}
41+
return parent::has_child();
42+
}
43+
44+
/** @inheritDoc */
45+
public function has_child_node( ?string $rule_name = null ): bool {
46+
if ( $this->has_native_ast() ) {
47+
return wp_sqlite_mysql_native_ast_has_child_node( $this->native_ast, $this->native_node_index, $rule_name );
48+
}
49+
return parent::has_child_node( $rule_name );
50+
}
51+
52+
/** @inheritDoc */
53+
public function has_child_token( ?int $token_id = null ): bool {
54+
if ( $this->has_native_ast() ) {
55+
return wp_sqlite_mysql_native_ast_has_child_token( $this->native_ast, $this->native_node_index, $token_id );
56+
}
57+
return parent::has_child_token( $token_id );
58+
}
59+
60+
/** @inheritDoc */
61+
public function get_first_child() {
62+
if ( $this->has_native_ast() ) {
63+
return wp_sqlite_mysql_native_ast_get_first_child( $this->native_ast, $this->native_node_index );
64+
}
65+
return parent::get_first_child();
66+
}
67+
68+
/** @inheritDoc */
69+
public function get_first_child_node( ?string $rule_name = null ): ?WP_Parser_Node {
70+
if ( $this->has_native_ast() ) {
71+
return wp_sqlite_mysql_native_ast_get_first_child_node( $this->native_ast, $this->native_node_index, $rule_name );
72+
}
73+
return parent::get_first_child_node( $rule_name );
74+
}
75+
76+
/** @inheritDoc */
77+
public function get_first_child_token( ?int $token_id = null ): ?WP_Parser_Token {
78+
if ( $this->has_native_ast() ) {
79+
return wp_sqlite_mysql_native_ast_get_first_child_token( $this->native_ast, $this->native_node_index, $token_id );
80+
}
81+
return parent::get_first_child_token( $token_id );
82+
}
83+
84+
/** @inheritDoc */
85+
public function get_first_descendant_node( ?string $rule_name = null ): ?WP_Parser_Node {
86+
if ( $this->has_native_ast() ) {
87+
return wp_sqlite_mysql_native_ast_get_first_descendant_node( $this->native_ast, $this->native_node_index, $rule_name );
88+
}
89+
return parent::get_first_descendant_node( $rule_name );
90+
}
91+
92+
/** @inheritDoc */
93+
public function get_first_descendant_token( ?int $token_id = null ): ?WP_Parser_Token {
94+
if ( $this->has_native_ast() ) {
95+
return wp_sqlite_mysql_native_ast_get_first_descendant_token( $this->native_ast, $this->native_node_index, $token_id );
96+
}
97+
return parent::get_first_descendant_token( $token_id );
98+
}
99+
100+
/** @inheritDoc */
101+
public function get_children(): array {
102+
if ( $this->has_native_ast() ) {
103+
return wp_sqlite_mysql_native_ast_get_children( $this->native_ast, $this->native_node_index );
104+
}
105+
return parent::get_children();
106+
}
107+
108+
/** @inheritDoc */
109+
public function get_child_nodes( ?string $rule_name = null ): array {
110+
if ( $this->has_native_ast() ) {
111+
return wp_sqlite_mysql_native_ast_get_child_nodes( $this->native_ast, $this->native_node_index, $rule_name );
112+
}
113+
return parent::get_child_nodes( $rule_name );
114+
}
115+
116+
/** @inheritDoc */
117+
public function get_child_tokens( ?int $token_id = null ): array {
118+
if ( $this->has_native_ast() ) {
119+
return wp_sqlite_mysql_native_ast_get_child_tokens( $this->native_ast, $this->native_node_index, $token_id );
120+
}
121+
return parent::get_child_tokens( $token_id );
122+
}
123+
124+
/** @inheritDoc */
125+
public function get_descendants(): array {
126+
if ( $this->has_native_ast() ) {
127+
return wp_sqlite_mysql_native_ast_get_descendants( $this->native_ast, $this->native_node_index );
128+
}
129+
return parent::get_descendants();
130+
}
131+
132+
/** @inheritDoc */
133+
public function get_descendant_nodes( ?string $rule_name = null ): array {
134+
if ( $this->has_native_ast() ) {
135+
return wp_sqlite_mysql_native_ast_get_descendant_nodes( $this->native_ast, $this->native_node_index, $rule_name );
136+
}
137+
return parent::get_descendant_nodes( $rule_name );
138+
}
139+
140+
/** @inheritDoc */
141+
public function get_descendant_tokens( ?int $token_id = null ): array {
142+
if ( $this->has_native_ast() ) {
143+
return wp_sqlite_mysql_native_ast_get_descendant_tokens( $this->native_ast, $this->native_node_index, $token_id );
144+
}
145+
return parent::get_descendant_tokens( $token_id );
146+
}
147+
148+
/** @inheritDoc */
149+
public function get_start(): int {
150+
if ( $this->has_native_ast() ) {
151+
return wp_sqlite_mysql_native_ast_get_start( $this->native_ast, $this->native_node_index );
152+
}
153+
return parent::get_start();
154+
}
155+
156+
/** @inheritDoc */
157+
public function get_length(): int {
158+
if ( $this->has_native_ast() ) {
159+
return wp_sqlite_mysql_native_ast_get_length( $this->native_ast, $this->native_node_index );
160+
}
161+
return parent::get_length();
162+
}
163+
164+
private function has_native_ast(): bool {
165+
return null !== $this->native_ast;
166+
}
167+
168+
private function materialize_native_children(): void {
169+
if ( ! $this->has_native_ast() ) {
170+
return;
171+
}
172+
173+
$this->children = wp_sqlite_mysql_native_ast_get_children( $this->native_ast, $this->native_node_index );
174+
$this->native_ast = null;
175+
$this->native_node_index = null;
176+
}
177+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_once __DIR__ . '/class-wp-mysql-polyfill-parser.php';
44

55
if ( class_exists( 'WP_MySQL_Native_Parser', false ) ) {
6+
require_once __DIR__ . '/class-wp-mysql-native-parser-node.php';
67
if ( ! function_exists( 'wp_sqlite_mysql_native_export_grammar' ) ) {
78
require_once __DIR__ . '/mysql-rust-bridge.php';
89
}

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

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,14 @@ class WP_Parser_Node {
1515
*/
1616
public $rule_id;
1717
public $rule_name;
18-
private $children = array();
19-
private $native_ast = null;
20-
private $native_node_index = null;
18+
protected $children = array();
2119

2220
public function __construct( $rule_id, $rule_name ) {
2321
$this->rule_id = $rule_id;
2422
$this->rule_name = $rule_name;
2523
}
2624

2725
public function append_child( $node ) {
28-
$this->materialize_native_children();
2926
$this->children[] = $node;
3027
}
3128

@@ -102,8 +99,6 @@ public function append_child( $node ) {
10299
* ]
103100
*/
104101
public function merge_fragment( $node ) {
105-
$this->materialize_native_children();
106-
$node->materialize_native_children();
107102
$this->children = array_merge( $this->children, $node->children );
108103
}
109104

@@ -113,9 +108,6 @@ public function merge_fragment( $node ) {
113108
* @return bool True if this node has any child nodes or tokens, false otherwise.
114109
*/
115110
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-
}
119111
return count( $this->children ) > 0;
120112
}
121113

@@ -126,9 +118,6 @@ public function has_child(): bool {
126118
* @return bool True if any child nodes are found, false otherwise.
127119
*/
128120
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-
}
132121
foreach ( $this->children as $child ) {
133122
if (
134123
$child instanceof WP_Parser_Node
@@ -147,9 +136,6 @@ public function has_child_node( ?string $rule_name = null ): bool {
147136
* @return bool True if any child tokens are found, false otherwise.
148137
*/
149138
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-
}
153139
foreach ( $this->children as $child ) {
154140
if (
155141
$child instanceof WP_Parser_Token
@@ -168,9 +154,6 @@ public function has_child_token( ?int $token_id = null ): bool {
168154
* null when no children are found.
169155
*/
170156
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-
}
174157
return $this->children[0] ?? null;
175158
}
176159

@@ -181,9 +164,6 @@ public function get_first_child() {
181164
* @return WP_Parser_Node|null The first matching child node; null when no children are found.
182165
*/
183166
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-
}
187167
foreach ( $this->children as $child ) {
188168
if (
189169
$child instanceof WP_Parser_Node
@@ -202,9 +182,6 @@ public function get_first_child_node( ?string $rule_name = null ): ?WP_Parser_No
202182
* @return WP_Parser_Token|null The first matching child token; null when no children are found.
203183
*/
204184
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-
}
208185
foreach ( $this->children as $child ) {
209186
if (
210187
$child instanceof WP_Parser_Token
@@ -226,9 +203,6 @@ public function get_first_child_token( ?int $token_id = null ): ?WP_Parser_Token
226203
* @return WP_Parser_Node|null The first matching descendant node; null when no descendants are found.
227204
*/
228205
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-
}
232206
for ( $i = 0; $i < count( $this->children ); $i++ ) {
233207
$child = $this->children[ $i ];
234208
if ( ! $child instanceof WP_Parser_Node ) {
@@ -255,9 +229,6 @@ public function get_first_descendant_node( ?string $rule_name = null ): ?WP_Pars
255229
* @return WP_Parser_Token|null The first matching descendant token; null when no descendants are found.
256230
*/
257231
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-
}
261232
for ( $i = 0; $i < count( $this->children ); $i++ ) {
262233
$child = $this->children[ $i ];
263234
if ( $child instanceof WP_Parser_Token ) {
@@ -280,9 +251,6 @@ public function get_first_descendant_token( ?int $token_id = null ): ?WP_Parser_
280251
* @return array<WP_Parser_Node|WP_Parser_Token> An array of all child nodes and tokens of this node.
281252
*/
282253
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-
}
286254
return $this->children;
287255
}
288256

@@ -293,9 +261,6 @@ public function get_children(): array {
293261
* @return WP_Parser_Node[] An array of all matching child nodes.
294262
*/
295263
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-
}
299264
$nodes = array();
300265
foreach ( $this->children as $child ) {
301266
if (
@@ -315,9 +280,6 @@ public function get_child_nodes( ?string $rule_name = null ): array {
315280
* @return WP_Parser_Token[] An array of all matching child tokens.
316281
*/
317282
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-
}
321283
$tokens = array();
322284
foreach ( $this->children as $child ) {
323285
if (
@@ -339,9 +301,6 @@ public function get_child_tokens( ?int $token_id = null ): array {
339301
* @return array<WP_Parser_Node|WP_Parser_Token> An array of all descendant nodes and tokens of this node.
340302
*/
341303
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-
}
345304
$descendants = array();
346305
foreach ( $this->children as $child ) {
347306
if ( $child instanceof WP_Parser_Node ) {
@@ -365,9 +324,6 @@ public function get_descendants(): array {
365324
* @return WP_Parser_Node[] An array of all matching descendant nodes.
366325
*/
367326
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-
}
371327
$nodes = array();
372328
foreach ( $this->children as $child ) {
373329
if ( ! $child instanceof WP_Parser_Node ) {
@@ -392,9 +348,6 @@ public function get_descendant_nodes( ?string $rule_name = null ): array {
392348
* @return WP_Parser_Token[] An array of all matching descendant tokens.
393349
*/
394350
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-
}
398351
$tokens = array();
399352
foreach ( $this->children as $child ) {
400353
if ( $child instanceof WP_Parser_Token ) {
@@ -414,9 +367,6 @@ public function get_descendant_tokens( ?int $token_id = null ): array {
414367
* @return int The byte offset in the input string where this node begins.
415368
*/
416369
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-
}
420370
return $this->get_first_descendant_token()->start;
421371
}
422372

@@ -426,26 +376,9 @@ public function get_start(): int {
426376
* @return int The byte length of this node in the input string.
427377
*/
428378
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-
}
432379
$tokens = $this->get_descendant_tokens();
433380
$first_token = $tokens[0];
434381
$last_token = $tokens[ count( $tokens ) - 1 ];
435382
return $last_token->start + $last_token->length - $first_token->start;
436383
}
437-
438-
private function has_native_ast(): bool {
439-
return null !== $this->native_ast;
440-
}
441-
442-
private function materialize_native_children(): void {
443-
if ( ! $this->has_native_ast() ) {
444-
return;
445-
}
446-
447-
$this->children = wp_sqlite_mysql_native_ast_get_children( $this->native_ast, $this->native_node_index );
448-
$this->native_ast = null;
449-
$this->native_node_index = null;
450-
}
451384
}

0 commit comments

Comments
 (0)