Search in sources :

Example 31 with RecognitionException

use of org.antlr.v4.runtime.RecognitionException in project compiler by boalang.

the class BoaCompiler method parseOnly.

public static void parseOnly(final String[] args) throws IOException {
    final CommandLine cl = processParseCommandLineOptions(args);
    if (cl == null)
        return;
    final ArrayList<File> inputFiles = BoaCompiler.inputFiles;
    // find custom libs to load
    final List<URL> libs = new ArrayList<URL>();
    if (cl.hasOption('l'))
        for (final String lib : cl.getOptionValues('l')) libs.add(new File(lib).toURI().toURL());
    SymbolTable.initialize(libs);
    final int maxVisitors;
    if (cl.hasOption('v'))
        maxVisitors = Integer.parseInt(cl.getOptionValue('v'));
    else
        maxVisitors = Integer.MAX_VALUE;
    for (int i = 0; i < inputFiles.size(); i++) {
        final File f = inputFiles.get(i);
        try {
            final BoaLexer lexer = new BoaLexer(new ANTLRFileStream(f.getAbsolutePath()));
            lexer.removeErrorListeners();
            lexer.addErrorListener(new LexerErrorListener());
            final CommonTokenStream tokens = new CommonTokenStream(lexer);
            final BoaParser parser = new BoaParser(tokens);
            parser.removeErrorListeners();
            parser.addErrorListener(new BaseErrorListener() {

                @Override
                public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) throws ParseCancellationException {
                    throw new ParseCancellationException(e);
                }
            });
            final BoaErrorListener parserErrorListener = new ParserErrorListener();
            final Start p = parse(tokens, parser, parserErrorListener);
            try {
                if (!parserErrorListener.hasError) {
                    new TypeCheckingVisitor().start(p, new SymbolTable());
                    final TaskClassifyingVisitor simpleVisitor = new TaskClassifyingVisitor();
                    simpleVisitor.start(p);
                    LOG.info(f.getName() + ": task complexity: " + (!simpleVisitor.isComplex() ? "simple" : "complex"));
                }
            } catch (final TypeCheckException e) {
                parserErrorListener.error("typecheck", lexer, null, e.n.beginLine, e.n.beginColumn, e.n2.endColumn - e.n.beginColumn + 1, e.getMessage(), e);
            }
        } catch (final Exception e) {
            System.err.print(f.getName() + ": parsing failed: ");
            e.printStackTrace();
        }
    }
}
Also used : BoaParser(boa.parser.BoaParser) Start(boa.compiler.ast.Start) ArrayList(java.util.ArrayList) URL(java.net.URL) BoaErrorListener(boa.compiler.listeners.BoaErrorListener) TypeCheckingVisitor(boa.compiler.visitors.TypeCheckingVisitor) BoaLexer(boa.parser.BoaLexer) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) LexerErrorListener(boa.compiler.listeners.LexerErrorListener) BaseErrorListener(org.antlr.v4.runtime.BaseErrorListener) TaskClassifyingVisitor(boa.compiler.visitors.TaskClassifyingVisitor) FileNotFoundException(java.io.FileNotFoundException) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) IOException(java.io.IOException) RecognitionException(org.antlr.v4.runtime.RecognitionException) ParserErrorListener(boa.compiler.listeners.ParserErrorListener) CommandLine(org.apache.commons.cli.CommandLine) ANTLRFileStream(org.antlr.v4.runtime.ANTLRFileStream) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) File(java.io.File) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Aggregations

RecognitionException (org.antlr.v4.runtime.RecognitionException)13 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)10 ArrayList (java.util.ArrayList)8 BaseErrorListener (org.antlr.v4.runtime.BaseErrorListener)6 Token (org.antlr.v4.runtime.Token)6 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)6 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)5 RecognitionException (org.antlr.runtime.RecognitionException)4 ParseTree (org.antlr.v4.runtime.tree.ParseTree)4 BoaLexer (boa.parser.BoaLexer)3 BoaParser (boa.parser.BoaParser)3 Expression (com.sri.ai.expresso.api.Expression)3 IOException (java.io.IOException)3 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)3 NoViableAltException (org.antlr.v4.runtime.NoViableAltException)3 ASPCore2Lexer (at.ac.tuwien.kr.alpha.antlr.ASPCore2Lexer)2 ASPCore2Parser (at.ac.tuwien.kr.alpha.antlr.ASPCore2Parser)2 ParsedProgram (at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram)2 ParsedTreeVisitor (at.ac.tuwien.kr.alpha.grounder.parser.ParsedTreeVisitor)2 Start (boa.compiler.ast.Start)2