|
3 | 3 | /** |
4 | 4 | * Parser node backed by a native (Rust) AST. |
5 | 5 | * |
6 | | - * Instances of this class are constructed exclusively by the native MySQL |
7 | | - * parser extension: when the extension parses a query, it produces a tree of |
8 | | - * `WP_MySQL_Native_Parser_Node` objects whose `$native_ast` and |
9 | | - * `$native_node_index` fields point into a Rust-owned AST buffer. Read methods |
10 | | - * (`get_start`, `has_child`, `get_children`, ...) delegate to the extension so |
11 | | - * children are never materialized into PHP arrays unless something actually |
12 | | - * asks for them. |
13 | | - * |
14 | | - * The hedge in those methods (`if ( $this->was_mutated() )`) is NOT a runtime |
15 | | - * check for whether the native extension is loaded — if this class is in use, |
16 | | - * the extension is loaded by definition. It checks whether THIS specific node |
17 | | - * has been mutated from PHP. A node loses its native backing the first time |
18 | | - * `append_child()` or `merge_fragment()` is called on it: those overrides |
19 | | - * invoke `materialize_native_children()`, which copies the native children |
20 | | - * into the inherited `$children` array and drops the native AST reference. |
21 | | - * From that point on, the node is a plain PHP-backed `WP_Parser_Node` and the |
22 | | - * read methods fall through to the parent implementation. |
23 | | - * |
24 | | - * Mutation from PHP is real and intentional — query rewriters in |
25 | | - * `WP_PDO_MySQL_On_SQLite` (e.g. building synthetic `count(*)` expressions) |
26 | | - * call `append_child()` on parsed nodes. The lazy-then-materialize design |
27 | | - * keeps the fast path (read-only traversal) cheap while still allowing |
28 | | - * mutation when callers need it. |
| 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. |
29 | 9 | */ |
30 | 10 | class WP_MySQL_Native_Parser_Node extends WP_Parser_Node { |
31 | 11 | private $native_ast = null; |
@@ -56,158 +36,138 @@ public function merge_fragment( $node ) { |
56 | 36 |
|
57 | 37 | /** @inheritDoc */ |
58 | 38 | public function has_child(): bool { |
59 | | - if ( $this->was_mutated() ) { |
60 | | - return parent::has_child(); |
| 39 | + if ( $this->has_native_ast() ) { |
| 40 | + return wp_sqlite_mysql_native_ast_has_child( $this->native_ast, $this->native_node_index ); |
61 | 41 | } |
62 | 42 | return wp_sqlite_mysql_native_ast_has_child( $this->native_ast, $this->native_node_index ); |
63 | 43 | } |
64 | 44 |
|
65 | 45 | /** @inheritDoc */ |
66 | 46 | public function has_child_node( ?string $rule_name = null ): bool { |
67 | | - if ( $this->was_mutated() ) { |
68 | | - return parent::has_child_node( $rule_name ); |
| 47 | + if ( $this->has_native_ast() ) { |
| 48 | + return wp_sqlite_mysql_native_ast_has_child_node( $this->native_ast, $this->native_node_index, $rule_name ); |
69 | 49 | } |
70 | 50 | return wp_sqlite_mysql_native_ast_has_child_node( $this->native_ast, $this->native_node_index, $rule_name ); |
71 | 51 | } |
72 | 52 |
|
73 | 53 | /** @inheritDoc */ |
74 | 54 | public function has_child_token( ?int $token_id = null ): bool { |
75 | | - if ( $this->was_mutated() ) { |
76 | | - return parent::has_child_token( $token_id ); |
| 55 | + if ( $this->has_native_ast() ) { |
| 56 | + return wp_sqlite_mysql_native_ast_has_child_token( $this->native_ast, $this->native_node_index, $token_id ); |
77 | 57 | } |
78 | 58 | return wp_sqlite_mysql_native_ast_has_child_token( $this->native_ast, $this->native_node_index, $token_id ); |
79 | 59 | } |
80 | 60 |
|
81 | 61 | /** @inheritDoc */ |
82 | 62 | public function get_first_child() { |
83 | | - if ( $this->was_mutated() ) { |
84 | | - return parent::get_first_child(); |
| 63 | + if ( $this->has_native_ast() ) { |
| 64 | + return wp_sqlite_mysql_native_ast_get_first_child( $this->native_ast, $this->native_node_index ); |
85 | 65 | } |
86 | 66 | return wp_sqlite_mysql_native_ast_get_first_child( $this->native_ast, $this->native_node_index ); |
87 | 67 | } |
88 | 68 |
|
89 | 69 | /** @inheritDoc */ |
90 | 70 | public function get_first_child_node( ?string $rule_name = null ): ?WP_Parser_Node { |
91 | | - if ( $this->was_mutated() ) { |
92 | | - return parent::get_first_child_node( $rule_name ); |
| 71 | + if ( $this->has_native_ast() ) { |
| 72 | + return wp_sqlite_mysql_native_ast_get_first_child_node( $this->native_ast, $this->native_node_index, $rule_name ); |
93 | 73 | } |
94 | 74 | return wp_sqlite_mysql_native_ast_get_first_child_node( $this->native_ast, $this->native_node_index, $rule_name ); |
95 | 75 | } |
96 | 76 |
|
97 | 77 | /** @inheritDoc */ |
98 | 78 | public function get_first_child_token( ?int $token_id = null ): ?WP_Parser_Token { |
99 | | - if ( $this->was_mutated() ) { |
100 | | - return parent::get_first_child_token( $token_id ); |
| 79 | + if ( $this->has_native_ast() ) { |
| 80 | + return wp_sqlite_mysql_native_ast_get_first_child_token( $this->native_ast, $this->native_node_index, $token_id ); |
101 | 81 | } |
102 | 82 | return wp_sqlite_mysql_native_ast_get_first_child_token( $this->native_ast, $this->native_node_index, $token_id ); |
103 | 83 | } |
104 | 84 |
|
105 | 85 | /** @inheritDoc */ |
106 | 86 | public function get_first_descendant_node( ?string $rule_name = null ): ?WP_Parser_Node { |
107 | | - if ( $this->was_mutated() ) { |
108 | | - return parent::get_first_descendant_node( $rule_name ); |
| 87 | + if ( $this->has_native_ast() ) { |
| 88 | + return wp_sqlite_mysql_native_ast_get_first_descendant_node( $this->native_ast, $this->native_node_index, $rule_name ); |
109 | 89 | } |
110 | 90 | return wp_sqlite_mysql_native_ast_get_first_descendant_node( $this->native_ast, $this->native_node_index, $rule_name ); |
111 | 91 | } |
112 | 92 |
|
113 | 93 | /** @inheritDoc */ |
114 | 94 | public function get_first_descendant_token( ?int $token_id = null ): ?WP_Parser_Token { |
115 | | - if ( $this->was_mutated() ) { |
116 | | - return parent::get_first_descendant_token( $token_id ); |
| 95 | + if ( $this->has_native_ast() ) { |
| 96 | + return wp_sqlite_mysql_native_ast_get_first_descendant_token( $this->native_ast, $this->native_node_index, $token_id ); |
117 | 97 | } |
118 | 98 | return wp_sqlite_mysql_native_ast_get_first_descendant_token( $this->native_ast, $this->native_node_index, $token_id ); |
119 | 99 | } |
120 | 100 |
|
121 | 101 | /** @inheritDoc */ |
122 | 102 | public function get_children(): array { |
123 | | - if ( $this->was_mutated() ) { |
124 | | - return parent::get_children(); |
| 103 | + if ( $this->has_native_ast() ) { |
| 104 | + return wp_sqlite_mysql_native_ast_get_children( $this->native_ast, $this->native_node_index ); |
125 | 105 | } |
126 | 106 | return wp_sqlite_mysql_native_ast_get_children( $this->native_ast, $this->native_node_index ); |
127 | 107 | } |
128 | 108 |
|
129 | 109 | /** @inheritDoc */ |
130 | 110 | public function get_child_nodes( ?string $rule_name = null ): array { |
131 | | - if ( $this->was_mutated() ) { |
132 | | - return parent::get_child_nodes( $rule_name ); |
| 111 | + if ( $this->has_native_ast() ) { |
| 112 | + return wp_sqlite_mysql_native_ast_get_child_nodes( $this->native_ast, $this->native_node_index, $rule_name ); |
133 | 113 | } |
134 | 114 | return wp_sqlite_mysql_native_ast_get_child_nodes( $this->native_ast, $this->native_node_index, $rule_name ); |
135 | 115 | } |
136 | 116 |
|
137 | 117 | /** @inheritDoc */ |
138 | 118 | public function get_child_tokens( ?int $token_id = null ): array { |
139 | | - if ( $this->was_mutated() ) { |
140 | | - return parent::get_child_tokens( $token_id ); |
| 119 | + if ( $this->has_native_ast() ) { |
| 120 | + return wp_sqlite_mysql_native_ast_get_child_tokens( $this->native_ast, $this->native_node_index, $token_id ); |
141 | 121 | } |
142 | 122 | return wp_sqlite_mysql_native_ast_get_child_tokens( $this->native_ast, $this->native_node_index, $token_id ); |
143 | 123 | } |
144 | 124 |
|
145 | 125 | /** @inheritDoc */ |
146 | 126 | public function get_descendants(): array { |
147 | | - if ( $this->was_mutated() ) { |
148 | | - return parent::get_descendants(); |
| 127 | + if ( $this->has_native_ast() ) { |
| 128 | + return wp_sqlite_mysql_native_ast_get_descendants( $this->native_ast, $this->native_node_index ); |
149 | 129 | } |
150 | 130 | return wp_sqlite_mysql_native_ast_get_descendants( $this->native_ast, $this->native_node_index ); |
151 | 131 | } |
152 | 132 |
|
153 | 133 | /** @inheritDoc */ |
154 | 134 | public function get_descendant_nodes( ?string $rule_name = null ): array { |
155 | | - if ( $this->was_mutated() ) { |
156 | | - return parent::get_descendant_nodes( $rule_name ); |
| 135 | + if ( $this->has_native_ast() ) { |
| 136 | + return wp_sqlite_mysql_native_ast_get_descendant_nodes( $this->native_ast, $this->native_node_index, $rule_name ); |
157 | 137 | } |
158 | 138 | return wp_sqlite_mysql_native_ast_get_descendant_nodes( $this->native_ast, $this->native_node_index, $rule_name ); |
159 | 139 | } |
160 | 140 |
|
161 | 141 | /** @inheritDoc */ |
162 | 142 | public function get_descendant_tokens( ?int $token_id = null ): array { |
163 | | - if ( $this->was_mutated() ) { |
164 | | - return parent::get_descendant_tokens( $token_id ); |
| 143 | + if ( $this->has_native_ast() ) { |
| 144 | + return wp_sqlite_mysql_native_ast_get_descendant_tokens( $this->native_ast, $this->native_node_index, $token_id ); |
165 | 145 | } |
166 | 146 | return wp_sqlite_mysql_native_ast_get_descendant_tokens( $this->native_ast, $this->native_node_index, $token_id ); |
167 | 147 | } |
168 | 148 |
|
169 | 149 | /** @inheritDoc */ |
170 | 150 | public function get_start(): int { |
171 | | - if ( $this->was_mutated() ) { |
172 | | - return parent::get_start(); |
| 151 | + if ( $this->has_native_ast() ) { |
| 152 | + return wp_sqlite_mysql_native_ast_get_start( $this->native_ast, $this->native_node_index ); |
173 | 153 | } |
174 | 154 | return wp_sqlite_mysql_native_ast_get_start( $this->native_ast, $this->native_node_index ); |
175 | 155 | } |
176 | 156 |
|
177 | 157 | /** @inheritDoc */ |
178 | 158 | public function get_length(): int { |
179 | | - if ( $this->was_mutated() ) { |
180 | | - return parent::get_length(); |
| 159 | + if ( $this->has_native_ast() ) { |
| 160 | + return wp_sqlite_mysql_native_ast_get_length( $this->native_ast, $this->native_node_index ); |
181 | 161 | } |
182 | 162 | return wp_sqlite_mysql_native_ast_get_length( $this->native_ast, $this->native_node_index ); |
183 | 163 | } |
184 | 164 |
|
185 | | - /** |
186 | | - * Indicates whether this node has been mutated from PHP. |
187 | | - * |
188 | | - * Returns false for freshly-parsed nodes whose children still live in the |
189 | | - * Rust-owned AST buffer; returns true once `append_child()` or |
190 | | - * `merge_fragment()` has copied the children into the inherited |
191 | | - * `$children` array and dropped the native AST reference. |
192 | | - * |
193 | | - * This is a per-instance state check, not a check for whether the native |
194 | | - * extension is loaded. |
195 | | - */ |
196 | | - private function was_mutated(): bool { |
197 | | - return $this->was_mutated; |
198 | | - } |
199 | | - |
200 | | - /** |
201 | | - * Copies native children into the inherited PHP $children array and drops |
202 | | - * the native AST reference for this node. |
203 | | - * |
204 | | - * Called before any mutation (append_child, merge_fragment) so the node's |
205 | | - * authoritative state lives in PHP from that point on. After this runs, |
206 | | - * was_mutated() returns true and read methods fall through to the parent |
207 | | - * WP_Parser_Node implementation. |
208 | | - */ |
| 165 | + private function has_native_ast(): bool { |
| 166 | + return null !== $this->native_ast; |
| 167 | + } |
| 168 | + |
209 | 169 | private function materialize_native_children(): void { |
210 | | - if ( $this->was_mutated ) { |
| 170 | + if ( ! $this->has_native_ast() ) { |
211 | 171 | return; |
212 | 172 | } |
213 | 173 |
|
|
0 commit comments