How to gather token
Attach a listener to the parse tree that listens when the parse tree enters an SQL expression and gathers what you want
From sqlite-parser
- A variable to hold the function names.
final List<String> functionNames = new ArrayList<String>();
- Create a lexer
SQLiteLexer lexer = new SQLiteLexer(new ANTLRInputStream(sql));
SQLiteParser parser = new SQLiteParser(new CommonTokenStream(lexer));
- Create the parse tree from the top production (select_stmt is the rule (production) that represents a sql statement)
* Walk the tree and [[listener|listen (SQLiteBaseListener)]] when it enters a rule (here the ''expr'' [[rule|production (rule)]])
<code java>
ParseTreeWalker.DEFAULT.walk(new SQLiteBaseListener() {
@Override
public void enterExpr(@NotNull SQLiteParser.ExprContext ctx) {
// Check if the expression is a function call.
if (ctx.function_name() != null) {
// Yes, it was a function call: add the name of the function
// to out list.
functionNames.add(ctx.function_name().getText());
}
}
}, tree);