Antlr - Walk / Visitor tree

Card Puncher Data Processing

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>();
SQLiteLexer lexer = new SQLiteLexer(new ANTLRInputStream(sql));
SQLiteParser parser = new SQLiteParser(new CommonTokenStream(lexer));
  *  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);







Share this page:
Follow us:
Task Runner