0

I want to use the antlr PLSQL parser to manipulate my sql code that comes from Oracle Forms.

Therefore I created all needed Java classes and now have this Main code that works fine:

CharStream charStream = CharStreams.fromString("CREATE FUNCTION totalCustomers \r\n"
                + "RETURN number IS \r\n"
                + "   total number(2) := 0; \r\n"
                + "BEGIN \r\n"
                + "   SELECT count(*) into total \r\n"
                + "   FROM customers; \r\n"
                + "   RETURN total; \r\n"
                + "END; ");

PlSqlLexer plSqlLexer = new PlSqlLexer(charStream2);
        
CommonTokenStream commonTokenStream = new CommonTokenStream(plSqlLexer);
        
PlSqlParser plSqlParser = new PlSqlParser(commonTokenStream);

PlSqlParser.Create_function_bodyContext sql_scriptContext = plSqlParser.create_function_body();

I can get the number of Syntax errors and the child nodes of the parse tree. But i wonder how to get specific positions (Character positions) of certain expressions.

I cant find anything about that in the internet, but I know that antlr is able to do this.

Does anybody know something about that?

I looked at all methods that the PLSQLParser offers, but none of them seems like they are helping me.

Alena
  • 1
  • 1
  • You have to navigate around to gather that information. In a [ParserRuleContext](https://www.antlr.org/api/Java/org/antlr/v4/runtime/ParserRuleContext.html#start) for the expression, there are `start` and `stop`. Those are the indices of the tokens into the token stream (`commonTokenStream`) for the start and stop of the expression. Then, get the [line](https://www.antlr.org/api/Java/org/antlr/v4/runtime/Token.html#getLine())/col info from the start token of the expression. You'll have to get the token after the stop token to get the ending line/col, or do the calc yourself. – kaby76 Jul 18 '23 at 10:24
  • Im not completely sure if i understood this. The ParserRuleContext is in my case the sql_scriptContext. I can get the start token but i dont find a way to get the next tokens, after the start token. There is a method: getTokens​(int ttype), but I dont understand what the ttype is. – Alena Jul 20 '23 at 11:53

0 Answers0