classMySqlTokenizer
final class MySqlTokenizer
Reads MySQL text back into the parser tokens the server would produce.
This diagnostic tokenizer supports the declared generated spellings and selected scanner contracts. It is not a substitute for an independent native parser verdict.
Methods§
public function __construct(
private array<string, string> $symbolTokens,
private array<string, string> $functionTokens,
private bool $dollarQuotedStrings,
)Parameters
$symbolTokens | array<string, string> | Terminal name by upper-cased keyword or operator |
$functionTokens | array<string, string> | Terminal name by upper-cased function name |
$dollarQuotedStrings | bool | Whether the server build accepts `$$ ... $$` strings |
public function tokenize(string $sql): list<string>Reads SQL text into the tokens MySQL's own lexer would produce.
Parameters
$sql | string | Text to read |
Returns
list<string> Parser token names, in orderThrows
LexicalException When the text holds something the lexer cannot readTest cases 4
Called from 1
Calls 6
- function-call
strlenline 48 - method-call MySqlTokenizer::skipTrivia() line 53
- static-call LexicalException::noProgress() line 55
- method-call MySqlTokenizer::tokenAt() line 61
- static-call LexicalException::unsupportedInput() line 63
- method-call MySqlTokenizer::merged() line 73
public function tokenAt(string $sql, int &$offset, list<string> $emitted): string|nullReads the one token that starts at the offset, advancing past it.
Parameters
$sql | string | Text being read |
$offset | int | Where to read from; moved past what was consumed |
$emitted | list<string> | Tokens read so far, for the rules that depend on them |
Returns
string|null The token name, or null when nothing here is a tokenThrows
LexicalException When a quoted or dollar-quoted run never closesTest cases 1
Called from 1
Calls 5
- method-call MySqlTokenizer::quotedTokenAt() line 89
- method-call MySqlTokenizer::markerTokenAt() line 90
- method-call MySqlTokenizer::numericTokenAt() line 91
- method-call MySqlTokenizer::wordTokenAt() line 92
- method-call MySqlTokenizer::operatorTokenAt() line 93
public function quotedTokenAt(string $sql, int &$offset): string|nullReads a backtick, single-quoted or dollar-quoted run.
Parameters
$sql | string | Text being read |
$offset | int | Where to read from; moved past what was consumed |
Returns
string|null The token name, or null when no quote opens hereThrows
LexicalException When the run never closesTest cases 1
Called from 1
Calls 4
- method-call MySqlTokenizer::skipQuoted() line 111
- function-call
substrline 122 - function-call
strposline 126 - static-call LexicalException::unterminatedDollarQuotedString() line 128
public function markerTokenAt(string $sql, int &$offset, list<string> $emitted): string|nullReads a parameter marker, a user-variable sigil, or the hostname after one.
A hostname is only a hostname because an @ came before it; the same characters anywhere else are an ordinary identifier, which is why the tokens read so far decide this.
Parameters
$sql | string | Text being read |
$offset | int | Where to read from; moved past what was consumed |
$emitted | list<string> | Tokens read so far |
Returns
string|null The token name, or null when none of these start hereTest cases 2
Called from 1
public function numericTokenAt(string $sql, int &$offset): string|nullReads a numeric literal in any of the spellings MySQL accepts.
Parameters
$sql | string | Text being read |
$offset | int | Where to read from; moved past what was consumed |
Returns
string|null The token name, or null when no number starts hereThrows
LexicalException When a prefixed quoted literal never closesTest cases 1
Called from 1
Calls 8
- function-call
preg_matchline 185 - function-call
strlenline 186 - function-call
strtolowerline 188 - function-call
substrline 188 - function-call
strtoupperline 192 - method-call MySqlTokenizer::skipQuoted() line 194
- function-call
str_containsline 206 - method-call MySqlTokenizer::integerToken() line 215
public function wordTokenAt(string $sql, int &$offset): string|nullReads a keyword, function name, charset introducer or identifier.
Parameters
$sql | string | Text being read |
$offset | int | Where to read from; moved past what was consumed |
Returns
string|null The token name, or null when no word starts hereTest cases 1
Called from 1
public function operatorTokenAt(string $sql, int &$offset): string|nullReads an operator or a single punctuation character.
Parameters
$sql | string | Text being read |
$offset | int | Where to read from; moved past what was consumed |
Returns
string|null The token name, or null when nothing operator-like starts hereTest cases 1
Called from 1
Calls 2
- method-call MySqlTokenizer::operatorAt() line 255
- function-call
strlenline 260
public function skipTrivia(string $sql, int &$offset): boolConsumes whitespace or one comment.
Parameters
$sql | string | Text being read |
$offset | int | Where to read from; moved past what was consumed |
Returns
bool True when something was skippedThrows
LexicalException When a block comment never closesTest cases 2
Called from 1
public function skipQuoted(string $sql, int &$offset, string $quote): voidConsumes a quoted run, doubling being the way a quote escapes itself.
Parameters
$sql | string | Text being read |
$offset | int | Offset of the opening quote; moved past the closing one |
$quote | string | The quote character |
Throws
LexicalException When the run never closesTest cases 2
Called from 3
- method-call SqlFaker\MySql\Generation\Tokenization\MySqlTokenizer::quotedTokenAt() packages/sql-faker/src/MySql/Generation/Tokenization/MySqlTokenizer.php:111
- method-call SqlFaker\MySql\Generation\Tokenization\MySqlTokenizer::quotedTokenAt() packages/sql-faker/src/MySql/Generation/Tokenization/MySqlTokenizer.php:117
- method-call SqlFaker\MySql\Generation\Tokenization\MySqlTokenizer::numericTokenAt() packages/sql-faker/src/MySql/Generation/Tokenization/MySqlTokenizer.php:194
Calls 2
- function-call
strlenline 317 - static-call LexicalException::unterminatedQuotedToken() line 334
public function integerToken(string $integer): stringReports which integer token a run of digits widens to.
MySQL picks the narrowest of three tokens that can hold the value, so the boundaries are compared as text: the digits may be wider than an int.
Parameters
$integer | string | The digits as written |
Returns
string One of NUM, LONG_NUM or ULONGLONG_NUMTest cases 1
Called from 1
public function operatorAt(string $sql, int $offset): string|nullReports the operator written at the offset, longest first.
Parameters
$sql | string | Text being read |
$offset | int | Where to look |
Returns
string|null The operator as written, or null when there is noneTest cases 2
Called from 1
Calls 4
- class-const MySqlTokenizer::OPERATORS() line 371
- function-call
substrline 372 - function-call
strlenline 372 - method-call MySqlTokenizer::isPunctuation() line 377
public function isPunctuation(string $character): boolReports whether a single character is punctuation the grammar uses.
Parameters
$character | string | Character to inspect |
Returns
bool True for a punctuation characterTest cases 1
Called from 1
Calls 3
- function-call
strlenline 389 - function-call
str_containsline 389 - class-const MySqlTokenizer::PUNCTUATION() line 389
public function merged(list<string> $tokens): list<string>Joins the token pairs MySQL's parser sees as one.
Parameters
$tokens | list<string> | Tokens as read |
Returns
list<string> Tokens with `WITH ROLLUP` collapsedTest cases 2
Private surface 5§
Implementation details, listed for orientation only.
private const OPERATORS = ['<=>', '->>', '&&', '<=', '<>', '!=', '>=', '<<', '>>', ':=', '->', '||']private const PUNCTUATION = '!%&()*+,-./:;@^{}|~=<>'private array<string, string> $symbolTokensprivate array<string, string> $functionTokensprivate bool $dollarQuotedStringsTest cases 28§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 21
MySqlTokenizerTest::testIntegerTokenWidensByMagnitudecallsMySqlTokenizerTest::testIsPunctuationAcceptsASingleGrammarCharactercallsMySqlTokenizerTest::testMarkerTokenAtLeavesTheSameCharactersAloneWithoutASigilcallsMySqlTokenizerTest::testMarkerTokenAtReadsAHostnameOnlyAfterASigilcallsMySqlTokenizerTest::testMergedJoinsWithRollupcallsMySqlTokenizerTest::testMergedLeavesTokensThatDoNotPairUpcallsMySqlTokenizerTest::testNumericTokenAtLeavesTextThatIsNoNumbercallsMySqlTokenizerTest::testOperatorAtPrefersTheLongestOperatorcallsMySqlTokenizerTest::testOperatorAtReportsNothingForAWordcallsMySqlTokenizerTest::testOperatorTokenAtLeavesTextThatIsNoOperatorcallsMySqlTokenizerTest::testQuotedTokenAtLeavesAnythingThatOpensNoQuotecallsMySqlTokenizerTest::testSkipQuotedReportsARunThatNeverClosescallsMySqlTokenizerTest::testSkipQuotedTreatsADoubledQuoteAsAnEscapecallsMySqlTokenizerTest::testSkipTriviaReportsAnUnterminatedBlockCommentcallsMySqlTokenizerTest::testSkipTriviaReportsWhenNothingWasSkippedcallsMySqlTokenizerTest::testTokenAtReadsTheFirstTokenAndAdvancesPastItcallsMySqlTokenizerTest::testTokenizeReadsTextAsTheServerWouldcallsMySqlTokenizerTest::testTokenizeRefusesADollarQuotedStringWhenTheBuildDoesNotcallsMySqlTokenizerTest::testTokenizeReportsAnUnterminatedDollarQuotedStringcallsMySqlTokenizerTest::testTokenizeReportsTextItCannotReadcallsMySqlTokenizerTest::testWordTokenAtLeavesTextThatIsNoWordcalls
Other tests reaching this symbol 7
LexicalExceptionTestcallsGenerationContextTestcallsLexicalGrammarTestcallsSqlGeneratorFactoryTestcallsMySqlProviderTestcallsPostgreSqlProviderTestcallsSqliteProviderTestcalls