TestRig uses instance fields instead of static fields, allows reuse from other applications

This commit is contained in:
Sam Harwell 2013-01-12 14:10:21 -06:00
parent 7ae67de110
commit d79bc252bc
1 changed files with 21 additions and 15 deletions

View File

@ -64,22 +64,21 @@ import java.util.List;
* [input-filename(s)] * [input-filename(s)]
*/ */
public class TestRig { public class TestRig {
static String grammarName;
static String startRuleName;
static List<String> inputFiles = new ArrayList<String>();
static boolean printTree = false;
static boolean gui = false;
static String psFile = null;
static boolean showTokens = false;
static boolean trace = false;
static boolean diagnostics = false;
static String encoding = null;
static boolean SLL = false;
public static final String LEXER_START_RULE_NAME = "tokens"; public static final String LEXER_START_RULE_NAME = "tokens";
public static void main(String[] args) throws Exception { protected String grammarName;
protected String startRuleName;
protected final List<String> inputFiles = new ArrayList<String>();
protected boolean printTree = false;
protected boolean gui = false;
protected String psFile = null;
protected boolean showTokens = false;
protected boolean trace = false;
protected boolean diagnostics = false;
protected String encoding = null;
protected boolean SLL = false;
public TestRig(String[] args) throws Exception {
if ( args.length < 2 ) { if ( args.length < 2 ) {
System.err.println("java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName\n" + System.err.println("java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName\n" +
" [-tokens] [-tree] [-gui] [-ps file.ps] [-encoding encodingname]\n" + " [-tokens] [-tree] [-gui] [-ps file.ps] [-encoding encodingname]\n" +
@ -136,6 +135,14 @@ public class TestRig {
i++; i++;
} }
} }
}
public static void main(String[] args) throws Exception {
TestRig testRig = new TestRig(args);
testRig.process();
}
public void process() throws Exception {
// System.out.println("exec "+grammarName+"."+startRuleName); // System.out.println("exec "+grammarName+"."+startRuleName);
String lexerName = grammarName+"Lexer"; String lexerName = grammarName+"Lexer";
ClassLoader cl = Thread.currentThread().getContextClassLoader(); ClassLoader cl = Thread.currentThread().getContextClassLoader();
@ -203,7 +210,7 @@ public class TestRig {
} }
} }
static void process(Lexer lexer, Class<? extends Parser> parserClass, Parser parser, InputStream is, Reader r) throws IOException, IllegalAccessException, InvocationTargetException, PrintException { protected void process(Lexer lexer, Class<? extends Parser> parserClass, Parser parser, InputStream is, Reader r) throws IOException, IllegalAccessException, InvocationTargetException, PrintException {
try { try {
ANTLRInputStream input = new ANTLRInputStream(r); ANTLRInputStream input = new ANTLRInputStream(r);
lexer.setInputStream(input); lexer.setInputStream(input);
@ -219,7 +226,6 @@ public class TestRig {
if ( startRuleName.equals(LEXER_START_RULE_NAME) ) return; if ( startRuleName.equals(LEXER_START_RULE_NAME) ) return;
if ( diagnostics ) { if ( diagnostics ) {
parser.addErrorListener(new DiagnosticErrorListener()); parser.addErrorListener(new DiagnosticErrorListener());
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION); parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);