Skip to content

Commit 36ec002

Browse files
committed
refactor: keep the token nesting walk in the grammar
Move TokenNesting from its own file into the grammar as a private nested class next to the lookaheads that use it, as asked in review. The nested class compiles to its own class file, so its branches stay out of CCJSqlParser.class. Inlining the walk as a parser method took 18 more of the constant-pool entries JaCoCo can still instrument. Behaviour does not change. Signed-off-by: minleejae <mmj9808@gmail.com>
1 parent 60b80a2 commit 36ec002

2 files changed

Lines changed: 27 additions & 38 deletions

File tree

‎src/main/java/net/sf/jsqlparser/parser/TokenNesting.java‎

Lines changed: 0 additions & 38 deletions
This file was deleted.

‎src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,33 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
228228
&& TokenNesting.isTopLevelFrom(subscriptOpenings.peek().next, colon);
229229
}
230230

231+
/**
232+
* Token nesting checks shared by the lookaheads above and below. A nested class keeps its
233+
* branches out of CCJSqlParser.class, whose constant pool is close to what JaCoCo can still
234+
* instrument.
235+
*/
236+
private static final class TokenNesting {
237+
private TokenNesting() {}
238+
239+
/**
240+
* Whether the target is not nested in brackets, braces, parentheses or a CASE expression
241+
* opened from the first token on.
242+
*/
243+
static boolean isTopLevelFrom(Token first, Token target) {
244+
int depth = 0;
245+
for (Token t = first; t != null && t != target; t = t.next) {
246+
if (t.kind == K_CASE || "(".equals(t.image) || "[".equals(t.image)
247+
|| "{".equals(t.image)) {
248+
depth++;
249+
} else if (t.kind == K_END || ")".equals(t.image) || "]".equals(t.image)
250+
|| "}".equals(t.image)) {
251+
depth--;
252+
}
253+
}
254+
return depth == 0;
255+
}
256+
}
257+
231258
// first tokens of the enclosing column DEFAULT values: a COLLATE at their top level is the
232259
// column's own attribute, as in MySQL's column_attribute and PostgreSQL's ColQualList
233260
private final Deque<Token> columnDefaultStarts = new ArrayDeque<Token>();

0 commit comments

Comments
 (0)