1/* 2 [The "BSD licence"] 3 Copyright (c) 2007-2008 Leon Jen-Yuan Su 4 All rights reserved. 5 6 Redistribution and use in source and binary forms, with or without 7 modification, are permitted provided that the following conditions 8 are met: 9 1. Redistributions of source code must retain the above copyright 10 notice, this list of conditions and the following disclaimer. 11 2. Redistributions in binary form must reproduce the above copyright 12 notice, this list of conditions and the following disclaimer in the 13 documentation and/or other materials provided with the distribution. 14 3. The name of the author may not be used to endorse or promote products 15 derived from this software without specific prior written permission. 16 17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27*/ 28package org.antlr.gunit; 29import java.io.File; 30import java.io.IOException; 31 32import org.antlr.runtime.*; 33 34/** The main gUnit interpreter entry point. 35 * Read a gUnit script, run unit tests or generate a junit file. 36 */ 37public class Interp { 38 static String testPackage; 39 static boolean genJUnit; 40 static String gunitFile; 41 42 public static void main(String[] args) throws IOException, ClassNotFoundException, RecognitionException { 43 /** Pull char from where? */ 44 CharStream input = null; 45 /** If the input source is a testsuite file, where is it? */ 46 String testsuiteDir = System.getProperty("user.dir"); 47 48 processArgs(args); 49 50 if ( genJUnit ) { 51 if ( gunitFile!=null ) { 52 input = new ANTLRFileStream(gunitFile); 53 File f = new File(gunitFile); 54 testsuiteDir = getTestsuiteDir(f.getCanonicalPath(), f.getName()); 55 } 56 else { 57 input = new ANTLRInputStream(System.in); 58 } 59 GrammarInfo grammarInfo = parse(input); 60 grammarInfo.setTestPackage(testPackage); 61 JUnitCodeGen generater = new JUnitCodeGen(grammarInfo, testsuiteDir); 62 generater.compile(); 63 return; 64 } 65 66 if ( gunitFile!=null ) { 67 input = new ANTLRFileStream(gunitFile); 68 File f = new File(gunitFile); 69 testsuiteDir = getTestsuiteDir(f.getCanonicalPath(), f.getName()); 70 } 71 else 72 input = new ANTLRInputStream(System.in); 73 74 gUnitExecutor executer = new gUnitExecutor(parse(input), testsuiteDir); 75 76 System.out.print(executer.execTest()); // unit test result 77 78 //return an error code of the number of failures 79 System.exit(executer.failures.size() + executer.invalids.size()); 80 } 81 82 public static void processArgs(String[] args) { 83 if (args == null || args.length == 0) return; 84 for (int i = 0; i < args.length; i++) { 85 if (args[i].equals("-p")) { 86 if (i + 1 >= args.length) { 87 System.err.println("missing library directory with -lib option; ignoring"); 88 } 89 else { 90 i++; 91 testPackage = args[i]; 92 } 93 } 94 else if (args[i].equals("-o")) genJUnit = true; 95 else gunitFile = args[i]; // Must be the gunit file 96 } 97 } 98 99 public static GrammarInfo parse(CharStream input) throws RecognitionException { 100 gUnitLexer lexer = new gUnitLexer(input); 101 CommonTokenStream tokens = new CommonTokenStream(lexer); 102 103 GrammarInfo grammarInfo = new GrammarInfo(); 104 gUnitParser parser = new gUnitParser(tokens, grammarInfo); 105 parser.gUnitDef(); // parse gunit script and save elements to grammarInfo 106 return grammarInfo; 107 } 108 109 public static String getTestsuiteDir(String fullPath, String fileName) { 110 return fullPath.substring(0, fullPath.length()-fileName.length()); 111 } 112 113} 114