Skip to content

Commit 8af0ce5

Browse files
committed
Add unit tests for grammar build-time transforms
Cover epsilon stripping, single-branch fragment inlining (including cyclic-fragment termination), per-token branch selectors with FIRST/ NULLABLE propagation, single-candidate classification, and the merge_sorted helper. Add an invariant check over the real MySQL grammar that no branch retains an epsilon marker and that every single-candidate rule maps each token to exactly one branch sequence.
1 parent 56f1374 commit 8af0ce5

1 file changed

Lines changed: 183 additions & 0 deletions

File tree

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../../src/parser/class-wp-parser-grammar.php';
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
/**
8+
* Unit tests for the grammar build-time transforms that the parser hot path
9+
* depends on: epsilon stripping, single-branch fragment inlining, per-token
10+
* branch selectors (FIRST/NULLABLE sets), single-candidate classification,
11+
* and the sorted-merge helper.
12+
*
13+
* These transforms are exercised end-to-end by the parser corpus, but the
14+
* tests below lock their output shape directly so a regression surfaces here
15+
* rather than as an opaque parse failure.
16+
*/
17+
class WP_Parser_Grammar_Tests extends TestCase {
18+
19+
/**
20+
* Build a grammar from a compact definition.
21+
*
22+
* Terminals are ids below $rules_offset; non-terminals are
23+
* $rules_offset + the rule's index. EMPTY_RULE_ID (0) is the epsilon marker.
24+
*
25+
* @param int $rules_offset Lowest non-terminal id.
26+
* @param string[] $names Rule names by index (id = index + offset).
27+
* @param array $grammar Branches by index; each branch is an int[].
28+
*/
29+
private function build_grammar( int $rules_offset, array $names, array $grammar ): WP_Parser_Grammar {
30+
return new WP_Parser_Grammar(
31+
array(
32+
'rules_offset' => $rules_offset,
33+
'rules_names' => $names,
34+
'grammar' => $grammar,
35+
)
36+
);
37+
}
38+
39+
public function test_strip_epsilon_markers_and_nullable_fallback(): void {
40+
// opt ::= A ε | ε (A = 1)
41+
$g = $this->build_grammar(
42+
10,
43+
array( 'opt' ),
44+
array(
45+
array( array( 1, 0 ), array( 0 ) ),
46+
)
47+
);
48+
49+
// Epsilon markers are removed; the pure-epsilon branch becomes empty.
50+
$this->assertSame( array( array( 1 ), array() ), $g->rules[10] );
51+
52+
// The rule is nullable (it has an empty branch).
53+
$this->assertArrayHasKey( 10, $g->nullable_branches );
54+
55+
// Token A selects both branches: the A-led one and the nullable one.
56+
$this->assertSame( array( array( 1 ), array() ), $g->branches_for_token[10][1] );
57+
58+
// Two candidate branches for token A, so it is not single-candidate.
59+
$this->assertArrayNotHasKey( 10, $g->single_candidate_rules );
60+
}
61+
62+
public function test_inline_single_branch_fragment(): void {
63+
// r ::= %f C ; %f ::= A B (A=1, B=2, C=3)
64+
$g = $this->build_grammar(
65+
10,
66+
array( 'r', '%f' ),
67+
array(
68+
array( array( 11, 3 ) ),
69+
array( array( 1, 2 ) ),
70+
)
71+
);
72+
73+
// The single-branch fragment is expanded in place.
74+
$this->assertSame( array( array( 1, 2, 3 ) ), $g->rules[10] );
75+
76+
// The fragment rule itself is left intact.
77+
$this->assertSame( array( array( 1, 2 ) ), $g->rules[11] );
78+
79+
// Only token A (the inlined first symbol) starts the rule.
80+
$this->assertSame( array( 1 ), array_keys( $g->branches_for_token[10] ) );
81+
$this->assertSame( array( array( 1, 2, 3 ) ), $g->branches_for_token[10][1] );
82+
$this->assertArrayHasKey( 10, $g->single_candidate_rules );
83+
}
84+
85+
public function test_multi_candidate_rule_is_not_single_candidate(): void {
86+
// top ::= A B | A C (both branches start with A)
87+
$g = $this->build_grammar(
88+
10,
89+
array( 'top', 'alt' ),
90+
array(
91+
array( array( 1, 2 ), array( 1, 3 ) ),
92+
array( array( 1 ) ),
93+
)
94+
);
95+
96+
$this->assertSame( array( array( 1, 2 ), array( 1, 3 ) ), $g->branches_for_token[10][1] );
97+
$this->assertArrayNotHasKey( 10, $g->single_candidate_rules );
98+
99+
// The single-branch rule is single-candidate.
100+
$this->assertArrayHasKey( 11, $g->single_candidate_rules );
101+
}
102+
103+
public function test_first_set_propagates_through_non_terminal(): void {
104+
// top ::= child ; child ::= A | B
105+
$g = $this->build_grammar(
106+
10,
107+
array( 'top', 'child' ),
108+
array(
109+
array( array( 11 ) ),
110+
array( array( 1 ), array( 2 ) ),
111+
)
112+
);
113+
114+
// FIRST(child) = {A, B} flows up into top's selector.
115+
$this->assertSame( array( 1, 2 ), array_keys( $g->branches_for_token[10] ) );
116+
$this->assertSame( array( array( 11 ) ), $g->branches_for_token[10][1] );
117+
$this->assertSame( array( array( 11 ) ), $g->branches_for_token[10][2] );
118+
$this->assertArrayHasKey( 10, $g->single_candidate_rules );
119+
120+
$this->assertSame( array( array( 1 ) ), $g->branches_for_token[11][1] );
121+
$this->assertSame( array( array( 2 ) ), $g->branches_for_token[11][2] );
122+
}
123+
124+
public function test_inlining_terminates_on_cyclic_fragments(): void {
125+
// r ::= %a ; %a ::= %b ; %b ::= %a (mutually recursive fragments)
126+
// The inliner must detect the cycle and leave a reference in place
127+
// instead of recursing forever.
128+
$g = $this->build_grammar(
129+
10,
130+
array( 'r', '%a', '%b' ),
131+
array(
132+
array( array( 11 ) ),
133+
array( array( 12 ) ),
134+
array( array( 11 ) ),
135+
)
136+
);
137+
138+
$this->assertSame( array( array( 11 ) ), $g->rules[10] );
139+
}
140+
141+
public function test_merge_sorted_dedupes_and_preserves_ascending_order(): void {
142+
$merge = new ReflectionMethod( WP_Parser_Grammar::class, 'merge_sorted' );
143+
// setAccessible() is required on PHP < 8.1 and deprecated (no-op) from 8.5.
144+
if ( PHP_VERSION_ID < 80100 ) {
145+
$merge->setAccessible( true );
146+
}
147+
148+
$this->assertSame( array( 1, 2, 3 ), $merge->invoke( null, array( 1, 3 ), array( 2, 3 ) ) );
149+
$this->assertSame( array( 2 ), $merge->invoke( null, array(), array( 2 ) ) );
150+
$this->assertSame( array( 1, 2 ), $merge->invoke( null, array( 1, 2 ), array() ) );
151+
$this->assertSame( array( 0, 1 ), $merge->invoke( null, array( 0, 1 ), array( 1 ) ) );
152+
}
153+
154+
public function test_real_mysql_grammar_invariants(): void {
155+
$g = new WP_Parser_Grammar( require __DIR__ . '/../../src/mysql/mysql-grammar.php' );
156+
157+
// Epsilon markers are fully stripped from every branch. The parser's
158+
// end-of-input sentinel relies on no real branch symbol being 0.
159+
foreach ( $g->rules as $rule_id => $branches ) {
160+
foreach ( $branches as $branch ) {
161+
$this->assertNotContains(
162+
WP_Parser_Grammar::EMPTY_RULE_ID,
163+
$branch,
164+
"Rule {$rule_id} still contains an epsilon marker."
165+
);
166+
}
167+
}
168+
169+
// Every single-candidate rule has a selector, and each of its token
170+
// entries points to exactly one branch sequence (what the fast path
171+
// assumes when it reads $candidate_branches[0]).
172+
foreach ( array_keys( $g->single_candidate_rules ) as $rule_id ) {
173+
$this->assertArrayHasKey( $rule_id, $g->branches_for_token );
174+
foreach ( $g->branches_for_token[ $rule_id ] as $token_id => $sequences ) {
175+
$this->assertCount(
176+
1,
177+
$sequences,
178+
"Single-candidate rule {$rule_id} has multiple branches for token {$token_id}."
179+
);
180+
}
181+
}
182+
}
183+
}

0 commit comments

Comments
 (0)