added -tokens flag
[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9756]
This commit is contained in:
parent
bf8473e0fd
commit
c1e199d77f
|
@ -39,7 +39,7 @@ import java.lang.reflect.Method;
|
|||
/** Run a lexer/parser combo, optionally printing tree string or generating
|
||||
* 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 static void main(String[] args) throws Exception {
|
||||
|
@ -49,8 +49,9 @@ public class TestRig {
|
|||
boolean printTree = false;
|
||||
boolean gui = false;
|
||||
String psFile = null;
|
||||
boolean showTokens = false;
|
||||
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;
|
||||
}
|
||||
int i=0;
|
||||
|
@ -71,6 +72,9 @@ public class TestRig {
|
|||
if ( arg.equals("-gui") ) {
|
||||
gui = true;
|
||||
}
|
||||
if ( arg.equals("-tokens") ) {
|
||||
showTokens = true;
|
||||
}
|
||||
else if ( arg.equals("-ps") ) {
|
||||
if ( i>=args.length ) {
|
||||
System.err.println("missing filename on -ps");
|
||||
|
@ -80,17 +84,6 @@ public class TestRig {
|
|||
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);
|
||||
String lexerName = grammarName+"Lexer";
|
||||
String parserName = grammarName+"Parser";
|
||||
|
@ -115,10 +108,21 @@ public class TestRig {
|
|||
Lexer lexer = lexerCtor.newInstance(input);
|
||||
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);
|
||||
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);
|
||||
ParserRuleContext<Token> tree = (ParserRuleContext<Token>)startRule.invoke(parser, (Object[])null);
|
||||
|
|
Loading…
Reference in New Issue