How to use plsql-parser (ANTLR)

I would like to check the syntax of the PL / SQL query in automated tests, and it seems that https://github.com/porcelli/plsql-parser can be useful for this. It's not easy for me to find out how I will install and use it, though.

Please note that this is for a Ruby project, but I am pretty competent in Java. I hope that I can run the check through the console, pass to SQL and get error information, including row / column.

Thank.

+3
source share
1 answer
  • Download ANTLR ver 3.5.1 tool
  • Download the source code from here (since this version has been ported to 3.5.1)
  • poarsers/no-ast.
  • 1- PLSQLLexer.g
  • 2- PLSQLParser.g no-ast subdir
  • :
    import org.antlr.runtime.ANTLRNoCaseFileStream;
    import org.antlr.runtime.CommonTokenStream;
    import org.antlr.runtime.RecognitionException;

    import br.com.porcelli.parser.plsql.PLSQLLexer;
    import br.com.porcelli.parser.plsql.PLSQLParser;

public static void parse(String file) {
    try {
        PLSQLLexer lex = new PLSQLLexer(new ANTLRNoCaseFileStream(file));
        CommonTokenStream tokens = new CommonTokenStream(lex);
        PLSQLParser parser = new PLSQLParser(tokens);

        /*start_rule_return AST =*/ parser.data_manipulation_language_statements();

        System.err.println(file +": " + parser.getNumberOfSyntaxErrors());

        if(parser.getNumberOfSyntaxErrors() != 0)
        {
            //System.exit(1);
        }

    } catch (RecognitionException e) {
        System.err.println(e.toString());
    } catch (IOException e) {
        System.err.println(e.toString());
    } catch (java.lang.OutOfMemoryError e) {
        System.err.println(file + ":");
        System.err.println(e.toString());
    } catch (java.lang.ArrayIndexOutOfBoundsException e) {
        System.err.println(file + ":");
        System.err.println(e.toString());
    }       
}
+2

All Articles