This commit is contained in:
Terence Parr 2012-08-26 16:03:59 -07:00
parent d8b4d6403a
commit ad737ebdf6
5 changed files with 118 additions and 2 deletions

View File

@ -216,7 +216,7 @@ HAVE_DOT = YES
CLASS_GRAPH = YES CLASS_GRAPH = YES
COLLABORATION_GRAPH = YES COLLABORATION_GRAPH = YES
GROUP_GRAPHS = YES GROUP_GRAPHS = YES
UML_LOOK = NO UML_LOOK = YES
TEMPLATE_RELATIONS = NO TEMPLATE_RELATIONS = NO
INCLUDE_GRAPH = YES INCLUDE_GRAPH = YES
INCLUDED_BY_GRAPH = YES INCLUDED_BY_GRAPH = YES

View File

@ -35,7 +35,7 @@ import org.antlr.v4.runtime.misc.Interval;
public interface TokenStream extends IntStream { public interface TokenStream extends IntStream {
/** Get Token at current input pointer + i ahead where i=1 is next Token. /** Get Token at current input pointer + i ahead where i=1 is next Token.
* i<0 indicates tokens in the past. So -1 is previous token and -2 is * i<0 indicates tokens in the past. So -1 is previous token and -2 is
* two tokens ago. LT(0) is undefined. For i>=n, return Token.EOFToken. * two tokens ago. LT(0) is undefined. For i>=n, return eof token.
* Return null for LT(0) and any index that results in an absolute address * Return null for LT(0) and any index that results in an absolute address
* that is negative. * that is negative.
* TODO (Sam): Throw exception for invalid k? * TODO (Sam): Throw exception for invalid k?

View File

@ -0,0 +1,54 @@
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.TerminalNode;
import java.util.List;
public class ExtractInheritance extends JavaLRBaseListener {
JavaLRParser parser;
public ExtractInheritance(JavaLRParser parser) { this.parser = parser; }
/*
normalClassDeclaration
: 'class' Identifier typeParameters?
('extends' type)?
('implements' typeList)?
classBody
;
*/
@Override
public void enterNormalClassDeclaration(JavaLRParser.NormalClassDeclarationContext ctx) {
TerminalNode<Token> id = ctx.Identifier();
String sup = null;
if ( ctx.type()!=null ) {
sup = ctx.type().getText();
System.out.println("\""+id+"\" -> \""+sup+"\"");
}
if ( ctx.typeList()!=null ) {
List<? extends JavaLRParser.TypeContext> type = ctx.typeList().type();
for (JavaLRParser.TypeContext t : type) {
System.out.println("\""+id+"\" -> \""+t.getText()+"\"");
}
}
}
/*
normalInterfaceDeclaration
: 'interface' Identifier typeParameters? ('extends' typeList)? interfaceBody
;
*/
@Override
public void enterNormalInterfaceDeclaration(JavaLRParser.NormalInterfaceDeclarationContext ctx) {
TerminalNode<Token> id = ctx.Identifier();
System.out.println("###### interface "+id);
String args = null;
if ( ctx.typeList()!=null ) {
List<? extends JavaLRParser.TypeContext> type = ctx.typeList().type();
for (JavaLRParser.TypeContext t : type) {
System.out.println("\""+id+"\" -> \""+t.getText()+"\"");
}
}
}
}

View File

@ -0,0 +1,61 @@
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class GenHierarchy {
public static void main(String[] args) throws Exception {
// START: input
String inputFile = null;
if ( args.length>0 ) inputFile = args[0];
List<String> files = getFilenames(new File(inputFile));
for (String file : files) {
InputStream is = new FileInputStream(file);
ANTLRInputStream input = new ANTLRInputStream(is);
// END: input
// System.out.println(file);
// START: launch
JavaLRLexer lexer = new JavaLRLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
JavaLRParser parser = new JavaLRParser(tokens);
ParserRuleContext<Token> tree = parser.compilationUnit(); // parse
ParseTreeWalker walker = new ParseTreeWalker(); // create standard walker
ExtractInheritance extractor = new ExtractInheritance(parser);
walker.walk(extractor, tree); // initiate walk of tree with listener
}
// END: launch
}
public static List<String> getFilenames(File f) throws Exception {
List<String> files = new ArrayList<String>();
getFilenames_(f, files);
return files;
}
public static void getFilenames_(File f, List<String> files) throws Exception {
// If this is a directory, walk each file/dir in that directory
if (f.isDirectory()) {
String flist[] = f.list();
for(int i=0; i < flist.length; i++) {
getFilenames_(new File(f, flist[i]), files);
}
}
// otherwise, if this is a java file, parse it!
else if ( ((f.getName().length()>5) &&
f.getName().substring(f.getName().length()-5).equals(".java")) )
{
files.add(f.getAbsolutePath());
}
}
}

View File

@ -36,6 +36,7 @@ import org.antlr.v4.runtime.atn.ATNSimulator;
import org.antlr.v4.runtime.atn.ATNState; import org.antlr.v4.runtime.atn.ATNState;
import org.antlr.v4.runtime.atn.ActionTransition; import org.antlr.v4.runtime.atn.ActionTransition;
import org.antlr.v4.runtime.atn.AtomTransition; import org.antlr.v4.runtime.atn.AtomTransition;
import org.antlr.v4.runtime.atn.BlockStartState;
import org.antlr.v4.runtime.atn.DecisionState; import org.antlr.v4.runtime.atn.DecisionState;
import org.antlr.v4.runtime.atn.LoopEndState; import org.antlr.v4.runtime.atn.LoopEndState;
import org.antlr.v4.runtime.atn.PredicateTransition; import org.antlr.v4.runtime.atn.PredicateTransition;