add -SLL option, allow multiple input files, reuse same parser/lexer.
This commit is contained in:
parent
75fd3264ff
commit
ee233f7dd3
|
@ -45,6 +45,8 @@ import java.io.InputStreamReader;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/** 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.
|
||||||
|
@ -54,7 +56,8 @@ import java.lang.reflect.Method;
|
||||||
* [-tokens] [-gui] [-ps file.ps]
|
* [-tokens] [-gui] [-ps file.ps]
|
||||||
* [-trace]
|
* [-trace]
|
||||||
* [-diagnostics]
|
* [-diagnostics]
|
||||||
* [input-filename]
|
* [-SLL]
|
||||||
|
* [input-filename(s)]
|
||||||
*/
|
*/
|
||||||
public class TestRig {
|
public class TestRig {
|
||||||
|
|
||||||
|
@ -63,7 +66,7 @@ public class TestRig {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
String grammarName;
|
String grammarName;
|
||||||
String startRuleName;
|
String startRuleName;
|
||||||
String inputFile = null;
|
List<String> inputFiles = new ArrayList<String>();
|
||||||
boolean printTree = false;
|
boolean printTree = false;
|
||||||
boolean gui = false;
|
boolean gui = false;
|
||||||
String psFile = null;
|
String psFile = null;
|
||||||
|
@ -71,11 +74,13 @@ public class TestRig {
|
||||||
boolean trace = false;
|
boolean trace = false;
|
||||||
boolean diagnostics = false;
|
boolean diagnostics = false;
|
||||||
String encoding = null;
|
String encoding = null;
|
||||||
|
boolean SLL = false;
|
||||||
|
|
||||||
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" +
|
||||||
" [-trace] [-diagnostics]\n"+
|
" [-trace] [-diagnostics] [-SLL]\n"+
|
||||||
" [input-filename]");
|
" [input-filename(s)]");
|
||||||
System.err.println("Use startRuleName='tokens' if GrammarName is a lexer grammar.");
|
System.err.println("Use startRuleName='tokens' if GrammarName is a lexer grammar.");
|
||||||
System.err.println("Omitting input-filename makes rig read from stdin.");
|
System.err.println("Omitting input-filename makes rig read from stdin.");
|
||||||
return;
|
return;
|
||||||
|
@ -89,7 +94,7 @@ public class TestRig {
|
||||||
String arg = args[i];
|
String arg = args[i];
|
||||||
i++;
|
i++;
|
||||||
if ( arg.charAt(0)!='-' ) { // input file name
|
if ( arg.charAt(0)!='-' ) { // input file name
|
||||||
inputFile = arg;
|
inputFiles.add(arg);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if ( arg.equals("-tree") ) {
|
if ( arg.equals("-tree") ) {
|
||||||
|
@ -104,6 +109,9 @@ public class TestRig {
|
||||||
else if ( arg.equals("-trace") ) {
|
else if ( arg.equals("-trace") ) {
|
||||||
trace = true;
|
trace = true;
|
||||||
}
|
}
|
||||||
|
else if ( arg.equals("-SLL") ) {
|
||||||
|
SLL = true;
|
||||||
|
}
|
||||||
else if ( arg.equals("-diagnostics") ) {
|
else if ( arg.equals("-diagnostics") ) {
|
||||||
diagnostics = true;
|
diagnostics = true;
|
||||||
}
|
}
|
||||||
|
@ -141,72 +149,80 @@ public class TestRig {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
InputStream is = System.in;
|
Constructor<Lexer> lexerCtor = lexerClass.getConstructor(CharStream.class);
|
||||||
if ( inputFile!=null ) {
|
Lexer lexer = lexerCtor.newInstance((CharStream)null);
|
||||||
is = new FileInputStream(inputFile);
|
String parserName = grammarName+"Parser";
|
||||||
}
|
Class parserClass = cl.loadClass(parserName);
|
||||||
Reader r;
|
if ( parserClass==null ) {
|
||||||
if ( encoding!=null ) {
|
System.err.println("Can't load "+parserName);
|
||||||
r = new InputStreamReader(is, encoding);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
r = new InputStreamReader(is);
|
|
||||||
}
|
}
|
||||||
|
Constructor<Parser> parserCtor = parserClass.getConstructor(TokenStream.class);
|
||||||
|
Parser parser = parserCtor.newInstance((TokenStream)null);
|
||||||
|
|
||||||
try {
|
for (String inputFile : inputFiles) {
|
||||||
ANTLRInputStream input = new ANTLRInputStream(r);
|
InputStream is = System.in;
|
||||||
|
if ( inputFile!=null ) {
|
||||||
Constructor<Lexer> lexerCtor = lexerClass.getConstructor(CharStream.class);
|
is = new FileInputStream(inputFile);
|
||||||
Lexer lexer = lexerCtor.newInstance(input);
|
|
||||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
|
||||||
|
|
||||||
tokens.fill();
|
|
||||||
|
|
||||||
if ( showTokens ) {
|
|
||||||
for (Object tok : tokens.getTokens()) {
|
|
||||||
System.out.println(tok);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reader r;
|
||||||
if ( startRuleName.equals(LEXER_START_RULE_NAME) ) return;
|
if ( encoding!=null ) {
|
||||||
|
r = new InputStreamReader(is, encoding);
|
||||||
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);
|
else {
|
||||||
Parser parser = parserCtor.newInstance(tokens);
|
r = new InputStreamReader(is);
|
||||||
|
|
||||||
if ( diagnostics ) parser.addErrorListener(new DiagnosticErrorListener());
|
|
||||||
|
|
||||||
if ( printTree || gui || psFile!=null ) {
|
|
||||||
parser.setBuildParseTree(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
parser.setTrace(trace);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Method startRule = parserClass.getMethod(startRuleName, (Class[])null);
|
ANTLRInputStream input = new ANTLRInputStream(r);
|
||||||
ParserRuleContext<Token> tree = (ParserRuleContext<Token>)startRule.invoke(parser, (Object[])null);
|
lexer.setInputStream(input);
|
||||||
|
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||||
|
|
||||||
if ( printTree ) {
|
tokens.fill();
|
||||||
System.out.println(tree.toStringTree(parser));
|
|
||||||
|
if ( showTokens ) {
|
||||||
|
for (Object tok : tokens.getTokens()) {
|
||||||
|
System.out.println(tok);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if ( gui ) {
|
|
||||||
tree.inspect(parser);
|
if ( startRuleName.equals(LEXER_START_RULE_NAME) ) return;
|
||||||
|
|
||||||
|
|
||||||
|
if ( diagnostics ) parser.addErrorListener(new DiagnosticErrorListener());
|
||||||
|
|
||||||
|
if ( printTree || gui || psFile!=null ) {
|
||||||
|
parser.setBuildParseTree(true);
|
||||||
}
|
}
|
||||||
if ( psFile!=null ) {
|
|
||||||
tree.save(parser, psFile); // Generate postscript
|
if ( SLL ) {
|
||||||
|
parser.getInterpreter().setSLL(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
parser.setTokenStream(tokens);
|
||||||
|
parser.setTrace(trace);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Method startRule = parserClass.getMethod(startRuleName, (Class[])null);
|
||||||
|
ParserRuleContext<Token> tree = (ParserRuleContext<Token>)startRule.invoke(parser, (Object[])null);
|
||||||
|
|
||||||
|
if ( printTree ) {
|
||||||
|
System.out.println(tree.toStringTree(parser));
|
||||||
|
}
|
||||||
|
if ( gui ) {
|
||||||
|
tree.inspect(parser);
|
||||||
|
}
|
||||||
|
if ( psFile!=null ) {
|
||||||
|
tree.save(parser, psFile); // Generate postscript
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (NoSuchMethodException nsme) {
|
||||||
|
System.err.println("No method for rule "+startRuleName+" or it has arguments");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (NoSuchMethodException nsme) {
|
finally {
|
||||||
System.err.println("No method for rule "+startRuleName+" or it has arguments");
|
if ( r!=null ) r.close();
|
||||||
|
if ( is!=null ) is.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
if ( r!=null ) r.close();
|
|
||||||
if ( is!=null ) is.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue