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.smv; import java.io.File; import java.io.IOException; import mbtarranger.MbtArrangerException; import mbtarranger.languages.IBaseListenerWithFileContext; import mbtarranger.languages.smv.gen.SmvLexer; import mbtarranger.languages.smv.gen.SmvParser; 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 { SmvLexer lexer; lexer = new SmvLexer(new ANTLRFileStream(osPath.getAbsolutePath())); CommonTokenStream tokens = new CommonTokenStream(lexer); SmvParser p = new SmvParser(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.nuSmvModel(); } 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 ExtractSmv extractSmv(File osPath) throws RecognitionException, IOException, MbtArrangerException { return extractSinglePass(osPath, ExtractSmv.class); } // --------- Referencings extractions ------- }