Java tutorial
/******************************************************************************* * 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.mcrlOuter; import java.io.File; import java.io.IOException; import mbtarranger.MbtArrangerException; import mbtarranger.explorers.ExtractMcrl; import mbtarranger.languages.IBaseListenerWithFileContext; import mbtarranger.languages.mcrlOuter.gen.McrlOuterLexer; import mbtarranger.languages.mcrlOuter.gen.McrlOuterParser; 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 org.antlr.v4.runtime.tree.ParseTreeListener & IBaseListenerWithFileContext> T_Pass extractSinglePass( File osPath, Class<T_Pass> cls) throws IOException, RecognitionException, MbtArrangerException { McrlOuterLexer lexer; lexer = new McrlOuterLexer(new ANTLRFileStream(osPath.getAbsolutePath())); CommonTokenStream tokens = new CommonTokenStream(lexer); McrlOuterParser p = new McrlOuterParser(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 ExtractMcrl extractMcrl(File osPath) throws IOException, RecognitionException, MbtArrangerException { return extractSinglePass(osPath, ExtractMcrl.class); } public static ExtractMcrlProcesses_Pass1 extractMcrlProcesses(File osPath) throws IOException, RecognitionException, MbtArrangerException { return extractSinglePass(osPath, ExtractMcrlProcesses_Pass1.class); } public static ExtractMcrlInit_Pass1 extractMcrlInit(File osPath) throws IOException, RecognitionException, MbtArrangerException { return extractSinglePass(osPath, ExtractMcrlInit_Pass1.class); } public static ExtractMcrlSorts_Pass1 extractMcrlSorts(File osPath) throws IOException, RecognitionException, MbtArrangerException { return extractSinglePass(osPath, ExtractMcrlSorts_Pass1.class); } public static ExtractMcrlActs_Pass1 extractMcrlActs(File osPath) throws IOException, RecognitionException, MbtArrangerException { return extractSinglePass(osPath, ExtractMcrlActs_Pass1.class); } public static ExtractMcrlMaps_Pass1 extractMcrlMaps(File osPath) throws IOException, RecognitionException, MbtArrangerException { return extractSinglePass(osPath, ExtractMcrlMaps_Pass1.class); } public static ExtractMcrlVars_Pass1 extractMcrlVars(File osPath) throws IOException, RecognitionException, MbtArrangerException { return extractSinglePass(osPath, ExtractMcrlVars_Pass1.class); } //--------- Referencings extractions ------- }