Search in sources :

Example 6 with BaseErrorListener

use of org.antlr.v4.runtime.BaseErrorListener 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

BaseErrorListener (org.antlr.v4.runtime.BaseErrorListener)6 RecognitionException (org.antlr.v4.runtime.RecognitionException)6 ArrayList (java.util.ArrayList)5 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)5 BoaLexer (boa.parser.BoaLexer)3 BoaParser (boa.parser.BoaParser)3 IOException (java.io.IOException)3 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)3 Start (boa.compiler.ast.Start)2 BoaErrorListener (boa.compiler.listeners.BoaErrorListener)2 LexerErrorListener (boa.compiler.listeners.LexerErrorListener)2 ParserErrorListener (boa.compiler.listeners.ParserErrorListener)2 TaskClassifyingVisitor (boa.compiler.visitors.TaskClassifyingVisitor)2 TypeCheckingVisitor (boa.compiler.visitors.TypeCheckingVisitor)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 URL (java.net.URL)2 JavaFileObject (javax.tools.JavaFileObject)2 ANTLRFileStream (org.antlr.v4.runtime.ANTLRFileStream)2 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)2