Skip to content

Commit 8eace1a

Browse files
committed
Rename WP_Parser_Tree to WP_Parser_Node and document it
1 parent 3de76b0 commit 8eace1a

File tree

6 files changed

+23
-11
lines changed

6 files changed

+23
-11
lines changed

tests/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
require_once __DIR__ . '/../wp-includes/mysql/class-wp-mysql-lexer.php';
66
require_once __DIR__ . '/../wp-includes/parser/class-wp-parser-grammar.php';
77
require_once __DIR__ . '/../wp-includes/parser/class-wp-parser.php';
8-
require_once __DIR__ . '/../wp-includes/parser/class-wp-parser-tree.php';
8+
require_once __DIR__ . '/../wp-includes/parser/class-wp-parser-node.php';
99
require_once __DIR__ . '/../wp-includes/mysql/class-wp-mysql-parser.php';
1010
require_once __DIR__ . '/../wp-includes/sqlite/class-wp-sqlite-query-rewriter.php';
1111
require_once __DIR__ . '/../wp-includes/sqlite/class-wp-sqlite-lexer.php';

tests/tools/run-parser-benchmark.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function ( $severity, $message, $file, $line ) {
1616
require_once __DIR__ . '/../../wp-includes/mysql/class-wp-mysql-token.php';
1717
require_once __DIR__ . '/../../wp-includes/mysql/class-wp-mysql-lexer.php';
1818
require_once __DIR__ . '/../../wp-includes/parser/class-wp-parser-grammar.php';
19-
require_once __DIR__ . '/../../wp-includes/parser/class-wp-parser-tree.php';
19+
require_once __DIR__ . '/../../wp-includes/parser/class-wp-parser-node.php';
2020
require_once __DIR__ . '/../../wp-includes/parser/class-wp-parser.php';
2121
require_once __DIR__ . '/../../wp-includes/mysql/class-wp-mysql-parser.php';
2222

tests/tools/run-parser-test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function ( $severity, $message, $file, $line ) {
1616
require_once __DIR__ . '/../../wp-includes/mysql/class-wp-mysql-lexer.php';
1717
require_once __DIR__ . '/../../wp-includes/parser/class-wp-parser.php';
1818
require_once __DIR__ . '/../../wp-includes/parser/class-wp-parser-grammar.php';
19-
require_once __DIR__ . '/../../wp-includes/parser/class-wp-parser-tree.php';
19+
require_once __DIR__ . '/../../wp-includes/parser/class-wp-parser-node.php';
2020
require_once __DIR__ . '/../../wp-includes/mysql/class-wp-mysql-parser.php';
2121

2222
$grammar_data = include __DIR__ . '/../../wp-includes/mysql/mysql-grammar.php';

wip/run-mysql-driver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private function translate_query( $parse_tree ) {
150150
}
151151
}
152152

153-
if ( ! ( $parse_tree instanceof WP_Parser_Tree ) ) {
153+
if ( ! ( $parse_tree instanceof WP_Parser_Node ) ) {
154154
throw new Exception( 'translateQuery only accepts MySQLToken and ParseTree instances' );
155155
}
156156

wp-includes/parser/class-wp-parser-tree.php renamed to wp-includes/parser/class-wp-parser-node.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
<?php
22

3-
class WP_Parser_Tree {
3+
/**
4+
* A node in parse tree.
5+
*
6+
* This class represents a node in the parse tree that is produced by WP_Parser.
7+
* A node corresponds to the related grammar rule that was matched by the parser.
8+
* Each node can contain children, consisting of other nodes and grammar tokens.
9+
* In this way, a parser node constitutes a recursive structure that represents
10+
* a parse (sub)tree at each level of the full grammar tree.
11+
*/
12+
class WP_Parser_Node {
13+
/**
14+
* @TODO: Review and document these properties and their visibility.
15+
*/
416
public $rule_id;
517
public $rule_name;
618
public $children = array();
@@ -20,7 +32,7 @@ public function append_child( $node ) {
2032
*
2133
* What are rule fragments?
2234
*
23-
* When we initially parse the BNF grammar file, it has compound rules such
35+
* When we initially parse the grammar file, it has compound rules such
2436
* as this one:
2537
*
2638
* query ::= EOF | ((simpleStatement | beginWork) ((SEMICOLON_SYMBOL EOF?) | EOF))
@@ -35,7 +47,7 @@ public function append_child( $node ) {
3547
* %%query02 ::= SEMICOLON_SYMBOL EOF_zero_or_one | EOF
3648
* EOF_zero_or_one ::= EOF | ε
3749
*
38-
* This factorization happens in 1-ebnf-to-json.js.
50+
* This factorization happens in "convert-grammar.php".
3951
*
4052
* "Fragments" are intermediate artifacts whose names are not in the original grammar.
4153
* They are extremely useful for the parser, but the API consumer should never have to
@@ -92,7 +104,7 @@ public function merge_fragment( $node ) {
92104

93105
public function has_child( $rule_name ) {
94106
foreach ( $this->children as $child ) {
95-
if ( ( $child instanceof WP_Parser_Tree && $child->rule_name === $rule_name ) ) {
107+
if ( ( $child instanceof WP_Parser_Node && $child->rule_name === $rule_name ) ) {
96108
return true;
97109
}
98110
}
@@ -125,7 +137,7 @@ public function get_token( $token_id = null ) {
125137

126138
public function get_child( $rule_name = null ) {
127139
foreach ( $this->children as $child ) {
128-
if ( $child instanceof WP_Parser_Tree && (
140+
if ( $child instanceof WP_Parser_Node && (
129141
$child->rule_name === $rule_name ||
130142
null === $rule_name
131143
) ) {
@@ -160,7 +172,7 @@ public function get_descendants( $rule_name ) {
160172
public function get_children( $rule_name = null ) {
161173
$matches = array();
162174
foreach ( $this->children as $child ) {
163-
if ( $child instanceof WP_Parser_Tree && (
175+
if ( $child instanceof WP_Parser_Node && (
164176
null === $rule_name ||
165177
$child->rule_name === $rule_name
166178
) ) {

wp-includes/parser/class-wp-parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private function parse_recursive( $rule_id ) {
6464
$starting_position = $this->position;
6565
foreach ( $rule as $branch ) {
6666
$this->position = $starting_position;
67-
$node = new WP_Parser_Tree( $rule_id, $rule_name );
67+
$node = new WP_Parser_Node( $rule_id, $rule_name );
6868
$branch_matches = true;
6969
foreach ( $branch as $subrule_id ) {
7070
$subnode = $this->parse_recursive( $subrule_id );

0 commit comments

Comments
 (0)