added -tokens flag

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9756]
This commit is contained in:
parrt 2011-12-26 18:10:29 -08:00
parent bf8473e0fd
commit c1e199d77f
1 changed files with 18 additions and 14 deletions

View File

@ -39,7 +39,7 @@ import java.lang.reflect.Method;
/** Run a lexer/parser combo, optionally printing tree string or generating /** Run a lexer/parser combo, optionally printing tree string or generating
* postscript file. Optionally taking input file. * postscript file. Optionally taking input file.
* *
* $ java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName [-print] [-gui] [-ps file.ps] [input-filename] * $ java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName [-print] [-tokens] [-gui] [-ps file.ps] [input-filename]
*/ */
public class TestRig { public class TestRig {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
@ -49,8 +49,9 @@ public class TestRig {
boolean printTree = false; boolean printTree = false;
boolean gui = false; boolean gui = false;
String psFile = null; String psFile = null;
boolean showTokens = false;
if ( args.length < 2 ) { if ( args.length < 2 ) {
System.err.println("java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName [-print] [-gui] [-ps file.ps] [input-filename]"); System.err.println("java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName [-print] [-tokens] [-gui] [-ps file.ps] [input-filename]");
return; return;
} }
int i=0; int i=0;
@ -71,6 +72,9 @@ public class TestRig {
if ( arg.equals("-gui") ) { if ( arg.equals("-gui") ) {
gui = true; gui = true;
} }
if ( arg.equals("-tokens") ) {
showTokens = true;
}
else if ( arg.equals("-ps") ) { else if ( arg.equals("-ps") ) {
if ( i>=args.length ) { if ( i>=args.length ) {
System.err.println("missing filename on -ps"); System.err.println("missing filename on -ps");
@ -80,17 +84,6 @@ public class TestRig {
i++; i++;
} }
} }
exec(grammarName, startRuleName, inputFile, printTree, gui, psFile);
}
public static void exec(String grammarName,
String startRuleName,
String inputFile,
boolean printTree,
boolean gui,
String psFile)
throws Exception
{
// System.out.println("exec "+grammarName+"."+startRuleName); // System.out.println("exec "+grammarName+"."+startRuleName);
String lexerName = grammarName+"Lexer"; String lexerName = grammarName+"Lexer";
String parserName = grammarName+"Parser"; String parserName = grammarName+"Parser";
@ -115,10 +108,21 @@ public class TestRig {
Lexer lexer = lexerCtor.newInstance(input); Lexer lexer = lexerCtor.newInstance(input);
CommonTokenStream tokens = new CommonTokenStream(lexer); CommonTokenStream tokens = new CommonTokenStream(lexer);
if ( showTokens ) {
tokens.fill();
for (Object tok : tokens.getTokens()) {
System.out.println(tok);
}
}
Constructor<Parser> parserCtor = parserClass.getConstructor(TokenStream.class); Constructor<Parser> parserCtor = parserClass.getConstructor(TokenStream.class);
Parser parser = parserCtor.newInstance(tokens); Parser parser = parserCtor.newInstance(tokens);
parser.setBuildParseTree(true);
parser.setErrorHandler(new DiagnosticErrorStrategy<Token>());
if ( printTree || gui || psFile!=null ) {
parser.setBuildParseTree(true);
}
Method startRule = parserClass.getMethod(startRuleName, (Class[])null); Method startRule = parserClass.getMethod(startRuleName, (Class[])null);
ParserRuleContext<Token> tree = (ParserRuleContext<Token>)startRule.invoke(parser, (Object[])null); ParserRuleContext<Token> tree = (ParserRuleContext<Token>)startRule.invoke(parser, (Object[])null);