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