allow special "tokens" start rule name so we can test pure lexer grammars.

This commit is contained in:
Terence Parr 2012-03-31 15:54:00 -07:00
parent 9fbe9b6e21
commit cc20a52cdd
1 changed files with 16 additions and 9 deletions

View File

@ -57,6 +57,9 @@ import java.lang.reflect.Method;
* [input-filename] * [input-filename]
*/ */
public class TestRig { public class TestRig {
public static final String LEXER_START_RULE_NAME = "tokens";
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
String grammarName; String grammarName;
String startRuleName; String startRuleName;
@ -69,10 +72,12 @@ public class TestRig {
boolean diagnostics = false; boolean diagnostics = false;
String encoding = null; String encoding = null;
if ( args.length < 2 ) { if ( args.length < 2 ) {
System.err.println("java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName" + System.err.println("java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName\n" +
" [-tokens] [-print] [-gui] [-ps file.ps] [-encoding encodingname]" + " [-tokens] [-print] [-gui] [-ps file.ps] [-encoding encodingname]\n" +
" [-trace] [-diagnostics]"+ " [-trace] [-diagnostics]\n"+
" [input-filename]"); " [input-filename]");
System.err.println("Use startRuleName='tokens' if GrammarName is a lexer grammar.");
System.err.println("Omitting input-filename makes rig read from stdin.");
return; return;
} }
int i=0; int i=0;
@ -121,16 +126,11 @@ public class TestRig {
} }
// System.out.println("exec "+grammarName+"."+startRuleName); // System.out.println("exec "+grammarName+"."+startRuleName);
String lexerName = grammarName+"Lexer"; String lexerName = grammarName+"Lexer";
String parserName = grammarName+"Parser";
ClassLoader cl = Thread.currentThread().getContextClassLoader(); ClassLoader cl = Thread.currentThread().getContextClassLoader();
Class lexerClass = cl.loadClass(lexerName); Class lexerClass = cl.loadClass(lexerName);
if ( lexerClass==null ) { if ( lexerClass==null ) {
System.err.println("Can't load "+lexerName); System.err.println("Can't load "+lexerName);
} }
Class parserClass = cl.loadClass(parserName);
if ( parserClass==null ) {
System.err.println("Can't load "+parserName);
}
InputStream is = System.in; InputStream is = System.in;
if ( inputFile!=null ) { if ( inputFile!=null ) {
@ -158,6 +158,13 @@ public class TestRig {
} }
} }
if ( startRuleName.equals(LEXER_START_RULE_NAME) ) return;
String parserName = grammarName+"Parser";
Class parserClass = cl.loadClass(parserName);
if ( parserClass==null ) {
System.err.println("Can't load "+parserName);
}
Constructor<Parser> parserCtor = parserClass.getConstructor(TokenStream.class); Constructor<Parser> parserCtor = parserClass.getConstructor(TokenStream.class);
Parser parser = parserCtor.newInstance(tokens); Parser parser = parserCtor.newInstance(tokens);