How to use plsql-parser (ANTLR)

我的未来我决定 提交于 2019-12-18 13:38:45

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/21637991/how-to-use-plsql-parser-antlr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!