Parse.java: Added comments JavaParser.java: Updated the genSymbols method and a private class 'NodeVisitor' which implements ASTVisitor. genSymbols returns an instance of the Symbols class containing all relevant data about the Java code. JavaSymbols.java: Add fields which map class, interface, method, field, and variable names to positions.tags/v1.0^2
@@ -1 +0,0 @@ | |||
Subproject commit 323dd4e266b47579aef2347e68214a6fbe083add |
@@ -1,5 +1,6 @@ | |||
/*TODO: Create main method which will loop and check for updates to a file. | |||
* If that file is updated, parse the input and print it out.*/ | |||
public class Parse { | |||
} |
@@ -7,13 +7,13 @@ import org.eclipse.jdt.core.dom.AST; | |||
import org.eclipse.jdt.core.dom.ASTParser; | |||
import org.eclipse.jdt.core.dom.ASTVisitor; | |||
import org.eclipse.jdt.core.dom.CompilationUnit; | |||
import org.eclipse.jdt.core.dom.Assignment; | |||
import org.eclipse.jdt.core.dom.ClassInstanceCreation; | |||
import org.eclipse.jdt.core.dom.FieldAccess | |||
import org.eclipse.jdt.core.dom.FieldDeclaration; | |||
import org.eclipse.jdt.core.dom.MethodDeclaration; | |||
import org.eclipse.jdt.core.dom.MethodInvocation; | |||
import org.eclipse.jdt.core.dom.PackageDeclaration; | |||
import org.eclipse.jdt.core.dom.Statement; | |||
import org.eclipse.jdt.core.dom.TypeDeclaration; | |||
import org.eclipse.jdt.core.dom.VariableDeclarationStatement | |||
@@ -21,6 +21,8 @@ import org.bitshift.parsing.parsers.Parser; | |||
import org.bitshift.parsing.symbols.Symbols; | |||
import org.bitshift.parsing.symbols.JavaSymbols; | |||
/*TODO: Work on parsing partial java code. | |||
* Make sure all names of nodes are strings.*/ | |||
public class JavaParser extends Parser { | |||
@Override | |||
@@ -36,7 +38,7 @@ public class JavaParser extends Parser { | |||
//Work on parsing partial java code later | |||
CompilationUnit result = (CompilationUnit) parser.createAST(null); | |||
ASTVisitor visitor = new NodeVisitor(); | |||
ASTVisitor visitor = new NodeVisitor(result); | |||
result.accept(visitor); | |||
return visitor.symbols; | |||
@@ -45,45 +47,80 @@ public class JavaParser extends Parser { | |||
class NodeVisitor extends ASTVisitor { | |||
protected Symbols symbols; | |||
protected CompilationUnit compUnit; | |||
public NodeVisitor() { | |||
public NodeVisitor(CompilationUnit compUnit) { | |||
symbols = new JavaSymbols(); | |||
} | |||
public boolean visit(Assignment node) { | |||
} | |||
public boolean visit(ClassInstanceCreation node) { | |||
String name = node.getType().getName(); | |||
int sl = compUnit.getLineNumber(node.getStartPosition()) - 1; | |||
int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1; | |||
symbols.insertClassInstance(name, sl, sc, null, null); | |||
return true; | |||
} | |||
public boolean visit(FieldAccess node) { | |||
String name = node.getName(); | |||
int sl = compUnit.getLineNumber(node.getStartPosition()) - 1; | |||
int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1; | |||
symbols.insertFieldAccess(name, sl, sc, null, null); | |||
return true; | |||
} | |||
public boolean visit(FieldDeclaration node) { | |||
String name = node.getType().getName(); | |||
int sl = compUnit.getLineNumber(node.getStartPosition()) - 1; | |||
int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1; | |||
symbols.insertFieldDeclaration(name, sl, sc, null, null); | |||
return true; | |||
} | |||
public boolean visit(MethodDeclaration node) { | |||
String name = node.getName(); | |||
List<Statement> statements = node.getBody().statements(); | |||
Statement last = statements.get(statements.size() - 1); | |||
int sl = compUnit.getLineNumber(node.getStartPosition()) - 1; | |||
int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1; | |||
int el = compUnit.getLineNumber(last.getStartPosition()) - 1; | |||
int ec = compUnit.getColumnNumber(last.getStartPosition()) - 1; | |||
symbols.insertMethodDeclaration(name, sl, sc, el, ec); | |||
return true; | |||
} | |||
public boolean visit(MethodInvocation node) { | |||
String name = node.getName(); | |||
int sl = compUnit.getLineNumber(node.getStartPosition()) - 1; | |||
int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1; | |||
symbols.insertMethodInvocation(name, sl, sc, null, null); | |||
return true; | |||
} | |||
public boolean visit(PackageDeclaration node) { | |||
symbols.setPackage(node.getName()); | |||
return true; | |||
} | |||
public boolean visit(TypeDeclaration node) { | |||
String name = node.getName(); | |||
int sl = compUnit.getLineNumber(node.getStartPosition()) - 1; | |||
int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1; | |||
if (node.isInterface()) { | |||
symbols.insertInterfaceDeclaration(name, sl, sc, null, null); | |||
} else { | |||
symbols.insertClassDeclaration(name, sl, sc, null, null); | |||
} | |||
return true; | |||
} | |||
public boolean visit(VariableDeclarationStatement node) { | |||
String name = node.getType().getName(); | |||
int sl = compUnit.getLineNumber(node.getStartPosition()) - 1; | |||
int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1; | |||
symbols.insertVariableDeclaration(name, sl, sc, null, null); | |||
return true; | |||
} | |||
} | |||
@@ -1 +1,121 @@ | |||
package org.bitshift.parsing.symbols; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.HashMap; | |||
import java.util.ArrayList; | |||
import org.bitshift.parsing.symbols.Symbols; | |||
/*TODO: Overwrite toString*/ | |||
public class JavaSymbols extends Symbols { | |||
private String _packageName; | |||
private Map<String, List<List<Integer>>> _classes; | |||
private Map<String, List<List<Integer>>> _interfaces; | |||
private Map<String, List<List<Integer>>> _methods; | |||
private Map<String, List<List<Integer>>> _fields; | |||
private Map<String, List<List<Integer>>> _vars; | |||
public JavaSymbols() { | |||
_packageName = null; | |||
_classes = new HashMap<String, ArrayList<ArrayList<Integer>>>(); | |||
_interfaces = new HashMap<String, ArrayList<ArrayList<Integer>>>(); | |||
_methods = new HashMap<String, ArrayList<ArrayList<Integer>>>(); | |||
_fields = new HashMap<String, ArrayList<ArrayList<Integer>>>(); | |||
_vars = new HashMap<String, ArrayList<ArrayList<Integer>>>(); | |||
} | |||
public boolean setPackage(String name) { | |||
_packageName = name; | |||
} | |||
public boolean insertClassDeclaration(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { | |||
List<Integer> pos = new ArrayList<Integer>(4); | |||
pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol); | |||
List<List<Integer>> copy = _classes.get(name); | |||
copy.add(0, pos); | |||
_classes.put(name, copy); | |||
return true; | |||
} | |||
public boolean insertClassInstance(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { | |||
List<Integer> pos = new ArrayList<Integer>(4); | |||
pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol); | |||
List<List<Integer>> copy = _classes.get(name); | |||
copy.add(pos); | |||
_classes.put(name, copy); | |||
return true; | |||
} | |||
public boolean insertInterfaceDeclaration(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { | |||
List<Integer> pos = new ArrayList<Integer>(4); | |||
pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol); | |||
List<List<Integer>> copy = _classes.get(name); | |||
copy.add(0, pos); | |||
_classes.put(name, copy); | |||
return true; | |||
} | |||
public boolean insertInterfaceInstance(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { | |||
List<Integer> pos = new ArrayList<Integer>(4); | |||
pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol); | |||
List<List<Integer>> copy = _classes.get(name); | |||
copy.add(pos); | |||
_classes.put(name, copy); | |||
return true; | |||
} | |||
public boolean insertMethodDeclaration(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { | |||
List<Integer> pos = new ArrayList<Integer>(4); | |||
pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol); | |||
List<List<Integer>> copy = _classes.get(name); | |||
copy.add(0, pos); | |||
_classes.put(name, copy); | |||
return true; | |||
} | |||
public boolean insertMethodInvocation(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { | |||
List<Integer> pos = new ArrayList<Integer>(4); | |||
pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol); | |||
List<List<Integer>> copy = _classes.get(name); | |||
copy.add(pos); | |||
_classes.put(name, copy); | |||
return true; | |||
} | |||
public boolean insertFieldDeclaration(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { | |||
List<Integer> pos = new ArrayList<Integer>(4); | |||
pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol); | |||
List<List<Integer>> copy = _classes.get(name); | |||
copy.add(0, pos); | |||
_classes.put(name, copy); | |||
return true; | |||
} | |||
public boolean insertFieldAccess(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { | |||
List<Integer> pos = new ArrayList<Integer>(4); | |||
pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol); | |||
List<List<Integer>> copy = _classes.get(name); | |||
copy.add(pos); | |||
_classes.put(name, copy); | |||
return true; | |||
} | |||
public boolean insertVariableDeclaration(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { | |||
List<Integer> pos = new ArrayList<Integer>(4); | |||
pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol); | |||
List<List<Integer>> copy = _classes.get(name); | |||
copy.add(0, pos); | |||
_classes.put(name, copy); | |||
return true; | |||
} | |||
public boolean insertVariableAccess(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { | |||
List<Integer> pos = new ArrayList<Integer>(4); | |||
pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol); | |||
List<List<Integer>> copy = _classes.get(name); | |||
copy.add(pos); | |||
_classes.put(name, copy); | |||
return true; | |||
} | |||
public String toString() { | |||
} | |||
} | |||