2011-11-30 07:46:51 +08:00
|
|
|
/*
|
|
|
|
[The "BSD license"]
|
|
|
|
Copyright (c) 2011 Terence Parr
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
3. The name of the author may not be used to endorse or promote products
|
|
|
|
derived from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2011-11-27 03:02:45 +08:00
|
|
|
import org.antlr.runtime.debug.BlankDebugEventListener;
|
|
|
|
import org.antlr.v4.runtime.ANTLRFileStream;
|
|
|
|
import org.antlr.v4.runtime.CommonTokenStream;
|
2011-11-27 04:48:57 +08:00
|
|
|
import org.antlr.v4.runtime.ParserRuleContext;
|
|
|
|
import org.antlr.v4.runtime.Token;
|
2011-11-27 03:02:45 +08:00
|
|
|
import org.antlr.v4.runtime.atn.LexerATNSimulator;
|
2012-02-08 05:18:51 +08:00
|
|
|
import org.antlr.v4.runtime.atn.ParserATNSimulator;
|
2011-11-27 03:02:45 +08:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
class TestJavaLR {
|
|
|
|
public static long lexerTime = 0;
|
|
|
|
public static boolean profile = false;
|
2011-11-30 07:46:51 +08:00
|
|
|
public static JavaLRLexer lexer;
|
|
|
|
public static JavaLRParser parser = null;
|
2011-12-01 09:44:49 +08:00
|
|
|
public static boolean showTree = false;
|
2011-11-27 03:02:45 +08:00
|
|
|
|
|
|
|
public static void main(String[] args) {
|
2011-12-12 05:02:58 +08:00
|
|
|
doAll(args);
|
|
|
|
// doAll(args);
|
2011-12-10 08:35:21 +08:00
|
|
|
}
|
|
|
|
|
2011-12-12 05:02:58 +08:00
|
|
|
public static void doAll(String[] args) {
|
2011-11-27 03:02:45 +08:00
|
|
|
try {
|
2011-12-12 05:02:58 +08:00
|
|
|
lexerTime = 0;
|
2011-11-27 03:02:45 +08:00
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
if (args.length > 0 ) {
|
|
|
|
// for each directory/file specified on the command line
|
|
|
|
for(int i=0; i< args.length;i++) {
|
2011-12-01 09:44:49 +08:00
|
|
|
if ( args[i].equals("-tree") ) showTree = true;
|
2011-11-27 03:02:45 +08:00
|
|
|
doFile(new File(args[i])); // parse it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
System.err.println("Usage: java Main <directory or file name>");
|
|
|
|
}
|
2011-11-27 06:23:57 +08:00
|
|
|
|
2011-11-27 03:02:45 +08:00
|
|
|
long stop = System.currentTimeMillis();
|
|
|
|
System.out.println("Lexer total time " + lexerTime + "ms.");
|
|
|
|
System.out.println("Total time " + (stop - start) + "ms.");
|
|
|
|
|
|
|
|
System.out.println("finished parsing OK");
|
|
|
|
System.out.println(LexerATNSimulator.ATN_failover+" lexer failovers");
|
|
|
|
System.out.println(LexerATNSimulator.match_calls+" lexer match calls");
|
2012-02-08 05:18:51 +08:00
|
|
|
System.out.println(ParserATNSimulator.ATN_failover+" parser failovers");
|
|
|
|
System.out.println(ParserATNSimulator.predict_calls +" parser predict calls");
|
|
|
|
System.out.println(ParserATNSimulator.retry_with_context +" retry_with_context after SLL conflict");
|
|
|
|
System.out.println(ParserATNSimulator.retry_with_context_indicates_no_conflict +" retry sees no conflict");
|
2011-11-27 03:02:45 +08:00
|
|
|
if ( profile ) {
|
|
|
|
System.out.println("num decisions "+profiler.numDecisions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(Exception e) {
|
|
|
|
System.err.println("exception: "+e);
|
|
|
|
e.printStackTrace(System.err); // so we can get stack trace
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// This method decides what action to take based on the type of
|
|
|
|
// file we are looking at
|
2011-12-12 05:02:58 +08:00
|
|
|
public static void doFile(File f) throws Exception {
|
2011-11-27 03:02:45 +08:00
|
|
|
// If this is a directory, walk each file/dir in that directory
|
|
|
|
if (f.isDirectory()) {
|
|
|
|
String files[] = f.list();
|
|
|
|
for(int i=0; i < files.length; i++)
|
|
|
|
doFile(new File(f, files[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, if this is a java file, parse it!
|
|
|
|
else if ( ((f.getName().length()>5) &&
|
|
|
|
f.getName().substring(f.getName().length()-5).equals(".java"))
|
|
|
|
|| f.getName().equals("input") )
|
|
|
|
{
|
2011-12-27 09:14:29 +08:00
|
|
|
System.err.println(f.getAbsolutePath());
|
2011-11-27 03:02:45 +08:00
|
|
|
parseFile(f.getAbsolutePath());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static class CountDecisions extends BlankDebugEventListener {
|
|
|
|
public int numDecisions = 0;
|
|
|
|
public void enterDecision(int decisionNumber) {
|
|
|
|
numDecisions++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static CountDecisions profiler = new CountDecisions();
|
|
|
|
|
|
|
|
// Here's where we do the real work...
|
|
|
|
public static void parseFile(String f)
|
|
|
|
throws Exception {
|
|
|
|
try {
|
|
|
|
// Create a scanner that reads from the input stream passed to us
|
|
|
|
if ( lexer==null ) {
|
2011-11-30 07:46:51 +08:00
|
|
|
lexer = new JavaLRLexer(null);
|
2011-11-27 03:02:45 +08:00
|
|
|
}
|
|
|
|
lexer.setInputStream(new ANTLRFileStream(f));
|
|
|
|
|
|
|
|
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
|
|
|
long start = System.currentTimeMillis();
|
2011-12-01 10:57:45 +08:00
|
|
|
tokens.fill(); // load all and check time
|
2011-11-27 03:02:45 +08:00
|
|
|
// System.out.println(tokens.getTokens());
|
|
|
|
long stop = System.currentTimeMillis();
|
|
|
|
lexerTime += stop-start;
|
|
|
|
|
|
|
|
if ( true ) {
|
|
|
|
// Create a parser that reads from the scanner
|
|
|
|
if ( parser==null ) {
|
2011-12-01 09:44:49 +08:00
|
|
|
parser = new JavaLRParser(null);
|
2011-12-01 10:57:45 +08:00
|
|
|
if ( showTree ) parser.setBuildParseTree(true);
|
2011-11-27 03:02:45 +08:00
|
|
|
// parser.setErrorHandler(new BailErrorStrategy<Token>());
|
|
|
|
// parser.getInterpreter().setContextSensitive(true);
|
|
|
|
}
|
2011-12-01 09:44:49 +08:00
|
|
|
|
2011-11-27 03:02:45 +08:00
|
|
|
parser.setTokenStream(tokens);
|
|
|
|
// start parsing at the compilationUnit rule
|
2011-11-27 04:48:57 +08:00
|
|
|
ParserRuleContext<Token> tree = parser.compilationUnit();
|
2011-12-01 09:44:49 +08:00
|
|
|
if ( showTree ) tree.inspect(parser);
|
2011-11-27 03:02:45 +08:00
|
|
|
//System.err.println("finished "+f);
|
|
|
|
// System.out.println("cache size = "+DefaultErrorStrategy.cache.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
System.err.println("parser exception: "+e);
|
|
|
|
e.printStackTrace(); // so we can get stack trace
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|