Skip to content

Commit cde7da8

Browse files
committed
Store and expose byte start and length in tokens and nodes
1 parent 69381d8 commit cde7da8

3 files changed

Lines changed: 55 additions & 6 deletions

File tree

wp-includes/mysql/class-wp-mysql-lexer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2247,7 +2247,12 @@ public function get_token(): ?WP_MySQL_Token {
22472247
if ( null === $this->token_type ) {
22482248
return null;
22492249
}
2250-
return new WP_MySQL_Token( $this->token_type, $this->get_current_token_bytes() );
2250+
return new WP_MySQL_Token(
2251+
$this->token_type,
2252+
$this->get_current_token_bytes(),
2253+
$this->token_starts_at,
2254+
$this->bytes_already_read - $this->token_starts_at
2255+
);
22512256
}
22522257

22532258
/**

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,27 @@ public function get_descendant_tokens( ?int $token_id = null ): array {
263263
return $all_descendants;
264264
}
265265

266+
/**
267+
* Get the byte offset in the input where the node begins.
268+
*
269+
* @return int
270+
*/
271+
public function get_start(): int {
272+
return $this->get_first_descendant_token()->start;
273+
}
274+
275+
/**
276+
* Get the byte length of the node in the input.
277+
*
278+
* @return int
279+
*/
280+
public function get_length(): int {
281+
$tokens = $this->get_descendant_tokens();
282+
$last_token = end( $tokens );
283+
$start = $this->get_start();
284+
return $last_token->start + $last_token->length - $start;
285+
}
286+
266287
/*
267288
* @TODO: Let's implement a more powerful AST-querying API.
268289
* See: https://github.com/WordPress/sqlite-database-integration/pull/164#discussion_r1855230501

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,37 @@ class WP_Parser_Token {
2323
*/
2424
public $value;
2525

26+
/**
27+
* Byte offset in the input where the token begins.
28+
*
29+
* @var int
30+
*/
31+
public $start;
32+
33+
/**
34+
* Byte length of the token in the input.
35+
*
36+
* @var int
37+
*/
38+
public $length;
39+
2640
/**
2741
* Constructor.
2842
*
29-
* @param int $id Token type.
30-
* @param string $value Token value.
43+
* @param int $id Token type.
44+
* @param string $value Token value.
45+
* @param int $start Byte offset in the input where the token begins.
46+
* @param int $length Byte length of the token in the input.
3147
*/
32-
public function __construct( int $id, string $value ) {
33-
$this->id = $id;
34-
$this->value = $value;
48+
public function __construct(
49+
int $id,
50+
string $value,
51+
int $start,
52+
int $length
53+
) {
54+
$this->id = $id;
55+
$this->value = $value;
56+
$this->start = $start;
57+
$this->length = $length;
3558
}
3659
}

0 commit comments

Comments
 (0)