public final class SqlScopeDetector extends Object
Pp2TokenStream, its SQL query-scope
level: how many enclosing subqueries / CTE bodies contain it.
A token's SQL scope level is the number of enclosing query parens.
A query paren is a ( whose next solid (non-comment) token is
SELECT or WITH — i.e. a parenthesised subquery or CTE body.
Top-level query tokens are level 0; tokens inside one subquery are level 1;
doubly-nested are level 2; and so on.
This makes the model robust and well-defined lexically:
SELECT * FROM (SELECT * FROM (SELECT 1)) → levels 0, 1, 2.SELECT 1 UNION SELECT 2 → all level 0 (set-operation siblings
share a scope; no extra query paren is introduced).WITH x AS (SELECT 1) SELECT * FROM x → the CTE body is level 1,
the outer query level 0.SELECT (1 + 2) FROM t → all level 0 (a grouping paren is not a
query paren — its next solid token is not SELECT/WITH).Traversal uses an explicit ArrayDeque of paren frames — never
recursion — so a 2000-deep nested-subquery input cannot
StackOverflowError (CLAUDE.md iterative-traversal mandate; plan
§10.1, §13/R3). The detector does not mutate any Pp2Token, its roles,
or the wrapped TSourceToken; it returns a SqlScopeDetector.SqlScopeResult side
structure (consistent with S18/S19).
Plan reference: §7.3/S20, §7.4/S20, §10.1, §13/R3.
| Modifier and Type | Class and Description |
|---|---|
static class |
SqlScopeDetector.SqlScope
An immutable subquery/CTE scope (a query paren and its contents).
|
static class |
SqlScopeDetector.SqlScopeResult
Per-token SQL scope level plus the list of detected subquery/CTE scopes.
|
| Constructor and Description |
|---|
SqlScopeDetector() |
| Modifier and Type | Method and Description |
|---|---|
SqlScopeDetector.SqlScopeResult |
detect(Pp2TokenStream stream)
Annotate the SQL scope level of every token in
stream. |
public SqlScopeDetector()
public SqlScopeDetector.SqlScopeResult detect(Pp2TokenStream stream)
stream.stream - the token stream; must not be nullSqlScopeDetector.SqlScopeResult indexed by token position; never nullNullPointerException - if stream is null