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

PostgreSQL lexical generation using source-based candidate and boundary definitions. Tokenization is exposed separately for diagnostics and does not decide generation success.

Methods§

public function __construct(
    private Generator $faker,
    private string $profileVersion,
    KeywordIndex|null $index = null,
)

Parameters

$fakerGeneratorSource of the choices realization makes
$profileVersionstringExact server version to generate for, e.g. "pg-17.2"
$indexKeywordIndex|nullInverts the profile's terminal-to-spelling map

Throws

RuntimeException When the exact release has no declaration
Test cases 490
Calls 7
public function version(): string

Names the server version this grammar generates for.

Returns

string Profile version, e.g. "pg-17.2"
Test cases 1
public function isNonOutput(string $terminal): bool

Reports parser selector tokens that intentionally write no characters.

Parameters

$terminalstring

Returns

bool
Test cases 435
Calls 1
public function normalizeLookahead(list<string> $terminals): list<string>

Settles each terminal on the spelling its neighbour calls for.

Parameters

$terminalslist<string>Terminals a derivation produced

Returns

list<string> The terminals with each lookahead substitution settled
Test cases 1
Calls 1
public function realize(list<string> $terminals, GenerationPlan<bool>|null $plan = null): string

Realizes a terminal sequence using the same candidate and spacing pipeline as grammar generation.

Parameters

$terminalslist<string>
$planGenerationPlan<bool>|null

Returns

string

Throws

LexicalException When a requested realization is unavailable
Test cases 3
Calls 2
public function realizeSequence(
    TerminalSequence $sequence,
    GenerationPlan<bool>|null $plan = null,
): string
public function resolveSequence(
    TerminalSequence $sequence,
    GenerationPlan<bool>|null $plan,
    Closure(int): int $choose,
    Closure(positive-int): ?int|null $valueChoice = null,
): ResolvedOutput

Exposes the resolved choices to a plan compiler without interpreting its input.

Parameters

$sequenceTerminalSequence
$planGenerationPlan<bool>|null
$chooseClosure(int): int
$valueChoiceClosure(positive-int): ?int|nullConstructive values selected only while compiling a plan

Throws

LexicalException When no compatible candidate exists
Test cases 439
Called from 1
Calls 1
public function tokenize(string $sql): list<string>

Reads SQL text into the tokens PostgreSQL'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 411
Calls 1
public function generateQuotedIdentifier(
    int $minLength = 1,
    int $maxLength = 63,
): non-empty-string
public function generateStringLiteral(
    int $minLength = 1,
    int $maxLength = 255,
): non-empty-string
public function generateIntegerLiteral(int $min = 1, int $max = 2147483647): non-empty-string
public function generateFloatLiteral(
    int $precision = 10,
    int $scale = 2,
    int $minExponent = -307,
    int $maxExponent = 308,
): non-empty-string

Writes a floating-point literal in exponent form.

Parameters

$precisionintTotal digits of the mantissa
$scaleintDigits after the point in the mantissa
$minExponentintSmallest exponent to write
$maxExponentintLargest exponent to write

Returns

non-empty-string A float literal
Test cases 6
Called from 1
Calls 2
public function generateHexLiteral(int $minLength = 1, int $maxLength = 16): non-empty-string
public function generateBinaryLiteral(int $minLength = 1, int $maxLength = 64): non-empty-string
public function generateDollarQuotedString(
    int $minLength = 1,
    int $maxLength = 255,
): non-empty-string
public function generateParameterMarker(int $min = 1, int $max = 99): non-empty-string
public function generate(GenerationPlan<bool> $plan): non-empty-string

Writes the one lexeme a lexical generation plan asks for.

Parameters

$planGenerationPlan<bool>Plan naming the lexeme kind and its bounds

Returns

non-empty-string The lexeme

Throws

InvalidArgumentException When the plan names a lexeme kind this dialect has none of
Test cases 27
Calls 12

Private surface 7§

Implementation details, listed for orientation only.

private LiteralGenerator $strings
private ReverseLexemeGenerator $pipeline
private list<string> $nonOutput
private PgLookahead $lookahead
private PgTokenizer $tokenizer
private Generator $faker
private string $profileVersion

Test cases 509§

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

Dedicated tests 32
Other tests reaching this symbol 477

Relations§

Instantiated in 1