final class SqliteTokenizer
Restricted visibility: declared "@visibility root". Code outside that scope must not name this declaration.

Reads SQLite text back into the parser tokens the server would produce.

This is the inverse of realization and the reason realization can be trusted: SQL written from a terminal sequence is read back here, and the two sequences must agree. SQLite's rules are its own — an identifier may be quoted four ways, a digit-separated number is a token of its own, and a parameter may be written with any of five sigils — so a tokenizer that merely looked plausible would agree with a generator that was equally wrong.

Methods§

public function __construct(private array<string, string> $keywordTokens)

Parameters

$keywordTokensarray<string, string>Terminal name by upper-cased keyword
Test cases 41
public function tokenize(string $sql): list<string>

Reads SQL text into the tokens SQLite's own lexer would produce.

Parameters

$sqlstringText to read

Returns

list<string> Parser token names, in order

Throws

LexicalException When the text holds something the lexer cannot read
Test cases 29
Called from 1
Calls 5
public function tokenAt(string $sql, int &$offset): string|null

Reads the one token that starts at the offset, advancing past it.

Parameters

$sqlstringText being read
$offsetintWhere to read from; moved past what was consumed

Returns

string|null The token name, or null when nothing here is a token

Throws

LexicalException When a quoted or bracketed run never closes
Test cases 28
Called from 1
Calls 4
public function quotedTokenAt(string $sql, int &$offset): string|null

Reads a quoted string or a quoted identifier in any of SQLite's spellings.

Parameters

$sqlstringText being read
$offsetintWhere to read from; moved past what was consumed

Returns

string|null The token name, or null when no quoted run opens here

Throws

LexicalException When the run never closes
Test cases 29
Called from 1
Calls 3
public function literalTokenAt(string $sql, int &$offset): string|null

Reads a blob, a bound parameter, or a numeric literal.

Parameters

$sqlstringText being read
$offsetintWhere to read from; moved past what was consumed

Returns

string|null The token name, or null when none of these start here
Test cases 24
Called from 1
Calls 3
public function wordTokenAt(string $sql, int &$offset): string|null

Reads a keyword or an unquoted identifier.

Parameters

$sqlstringText being read
$offsetintWhere to read from; moved past what was consumed

Returns

string|null The token name, or null when no word starts here
Test cases 13
Called from 1
Calls 3
public function skipTrivia(string $sql, int &$offset): bool

Consumes whitespace or one comment.

Parameters

$sqlstringText being read
$offsetintWhere to read from; moved past what was consumed

Returns

bool True when something was skipped

Throws

LexicalException When a block comment never closes
Test cases 29
Called from 1
Calls 5
public function normalizedSourceTokens(list<string> $tokens): list<string>

Strips the TK_ prefix and the spacing tokens SQLite's own lexer emits.

Witnesses record what SQLite's lexer produced, which names tokens with a prefix and reports whitespace as a token of its own. Neither belongs in the parser-token sequence this tokenizer is compared against.

Parameters

$tokenslist<string>Tokens as the upstream lexer named them

Returns

list<string> The same tokens as the parser names them
Test cases 2
Calls 2

Private surface 3§

Implementation details, listed for orientation only.

private const OPERATORS = ['->>' => 'PTR', '->' => 'PTR', '||' => 'CONCAT', '==' => 'EQ', '<=' => 'LE', '…
private const PUNCTUATION = ['(' => 'LP', ')' => 'RP', ';' => 'SEMI', ',' => 'COMMA', '.' => 'DOT', '=' => …
private array<string, string> $keywordTokens

Test cases 48§

Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.

Dedicated tests 42
Other tests reaching this symbol 6

Relations§

Instantiated in 1
Method calls 1
Type declarations 1