mbtarranger.languages.dot.Glue.java Source code

Java tutorial

Introduction

Here is the source code for mbtarranger.languages.dot.Glue.java

Source

/*******************************************************************************
 * Copyright (c) 2013, 2014 ETAS GmbH.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Dennis Eder (ETAS GmbH) - initial API and implementation and/or initial documentation
 *******************************************************************************/
package mbtarranger.languages.dot;

import java.io.File;
import java.io.IOException;

import mbtarranger.MbtArrangerException;
import mbtarranger.languages.dot.ast1.AST;
import mbtarranger.languages.dot.gen.DotLexer;
import mbtarranger.languages.dot.gen.DotParser;

import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeVisitor;

public class Glue {

    protected static <T_Pass extends ParseTreeVisitor<AST>> T_Pass extractSinglePassVisiting(File osPath,
            Class<T_Pass> cls) throws IOException, RecognitionException, MbtArrangerException {
        DotLexer lexer;
        lexer = new DotLexer(new ANTLRFileStream(osPath.getAbsolutePath(), "UTF-8"/*"ANSI"*/));
        lexer.removeErrorListeners();
        ErrorListener errListener = new ErrorListener(osPath);
        lexer.addErrorListener(errListener);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        DotParser parser = new DotParser(tokens);
        parser.setBuildParseTree(true);
        parser.removeErrorListeners();
        parser.addErrorListener(errListener);
        T_Pass pass1;
        try {
            pass1 = cls.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new MbtArrangerException("misdesigned generics using reflection to type instantiation", e);
        }
        parser.setBuildParseTree(true);
        try {
            ParseTree tree = parser.root();
            pass1.visit(tree);
        } catch (NullPointerException nulex) {
            throw new MbtArrangerException(
                    /* NullPointerExceptions caught here mean, that the parser needs additional logic to handle grammar robustly */
                    "ARRANGE parser failed", nulex);
        }
        return pass1;
    }

    // --------- AST extractions -------

    public static ExtractDigraphs_Pass1 extractDigraphs(File osPath)
            throws IOException, RecognitionException, MbtArrangerException {
        return extractSinglePassVisiting(osPath, ExtractDigraphs_Pass1.class);
    }
    // --------- Referencings extractions -------

}