Skip to content

Commit 8c11f76

Browse files
committed
Maintain end-of-input sentinel in reset_tokens()
Trunk's WP_MySQL_Parser::reset_tokens() lets the parser be reused across queries by swapping in a new token array. The performance branch's parser relies on an end-of-input sentinel token (id = EMPTY_RULE_ID) appended at $tokens[$token_count] so the hot path can read $tokens[$pos]->id without a range check; reset_tokens() must reproduce that invariant or the next parse() walks off the end. Append the sentinel and update $token_count in reset_tokens(), matching WP_Parser::__construct().
1 parent 873bed5 commit 8c11f76

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class WP_MySQL_Parser extends WP_Parser {
1414
* @param array<WP_Parser_Token> $tokens The parser tokens.
1515
*/
1616
public function reset_tokens( array $tokens ): void {
17+
$this->token_count = count( $tokens );
18+
// Maintain the end-of-input sentinel that parse_recursive() relies on.
19+
// See WP_Parser::__construct for the invariants.
20+
$tokens[] = new WP_Parser_Token( WP_Parser_Grammar::EMPTY_RULE_ID, 0, 0, '' );
1721
$this->tokens = $tokens;
1822
$this->position = 0;
1923
$this->current_ast = null;

0 commit comments

Comments
 (0)