mbtarranger.languages.promelaOuter.Glue.java Source code

Java tutorial

Introduction

Here is the source code for mbtarranger.languages.promelaOuter.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.promelaOuter;

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

import mbtarranger.MbtArrangerException;
import mbtarranger.languages.promelaOuter.gen.PromelaOuterLexer;
import mbtarranger.languages.promelaOuter.gen.PromelaOuterParser;

import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.RecognitionException;

public class Glue {
    private static <T_Pass extends PromelaOuterBaseListenerWithFileContext> T_Pass extractSinglePass(File osPath,
            Class<T_Pass> cls) throws IOException, RecognitionException, MbtArrangerException {
        PromelaOuterLexer lexer;
        lexer = new PromelaOuterLexer(new ANTLRFileStream(osPath.getAbsolutePath()));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        PromelaOuterParser p = new PromelaOuterParser(tokens);
        p.setBuildParseTree(true);
        p.setErrorHandler(new ErrorReporter(osPath));
        T_Pass pass1;
        try {
            pass1 = cls.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new MbtArrangerException("misdesigned generics using reflection to type instantiation", e);
        }
        pass1.SetFileContext(osPath);
        p.setBuildParseTree(true);
        p.addParseListener(pass1);
        try {
            p.root();
        } 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 ExtractMachines_Pass1 extractMachines(File osPath)
            throws IOException, RecognitionException, MbtArrangerException {
        return extractSinglePass(osPath, ExtractMachines_Pass1.class);
    }

    public static ExtractMtypes_Pass1 extractMtypes_Pass1(File osPath)
            throws IOException, RecognitionException, MbtArrangerException {
        return extractSinglePass(osPath, ExtractMtypes_Pass1.class);
    }

    public static ExtractChannelDefs_Pass1 extractChannelDefs_Pass1(File osPath)
            throws IOException, RecognitionException, MbtArrangerException {
        return extractSinglePass(osPath, ExtractChannelDefs_Pass1.class);
    }

    public static ExtractChannelDecls_Pass1 extractChannelDecls_Pass1(File osPath)
            throws IOException, RecognitionException, MbtArrangerException {
        return extractSinglePass(osPath, ExtractChannelDecls_Pass1.class);
    }

    public static ExtractInlines_Pass1 extractInlines(File osPath)
            throws IOException, RecognitionException, MbtArrangerException {
        return extractSinglePass(osPath, ExtractInlines_Pass1.class);
    }

    public static ExtractDefines_Pass1 extractDefines(File osPath)
            throws IOException, RecognitionException, MbtArrangerException {
        return extractSinglePass(osPath, ExtractDefines_Pass1.class);
    }

    public static ExtractTypedefs_Pass1 extractTypedefs_Pass1(File osPath)
            throws IOException, RecognitionException, MbtArrangerException {
        return extractSinglePass(osPath, ExtractTypedefs_Pass1.class);
    }
    //--------- Referencings extractions -------
}