How to use plsql-parser (ANTLR)

antlr, plsql

Solution

- Download the ANTLR tool ver 3.5.1

- Download the source codes from here (as this version was ported to 3.5.1)

- Use the tool to compile the sources from poarsers/no-ast subdir

- 1st compile PLSQLLexer.g

- 2nd compile PLSQLParser.g from no-ast subdir

- the use this sample code as an example:

    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());
    }       
}

Problem

I'd like to check PL/SQL query syntax in automated tests, and it looks like https://github.com/porcelli/plsql-parser might be useful for that. I am not easily finding out how I would install and use it though. Note that this is for a Ruby project, but I'm reasonably competent in Java. I'm hoping there's some way I can run the checking via console, pass in the SQL, and get back any error info, including line/column. Thanks.

Original source