diff --git a/bitshift/parser/pylj b/bitshift/parser/pylj deleted file mode 160000 index 323dd4e..0000000 --- a/bitshift/parser/pylj +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 323dd4e266b47579aef2347e68214a6fbe083add diff --git a/parsers/java/src/org/bitshift/parsing/Parse.java b/parsers/java/src/org/bitshift/parsing/Parse.java index cad9434..9d8ade7 100644 --- a/parsers/java/src/org/bitshift/parsing/Parse.java +++ b/parsers/java/src/org/bitshift/parsing/Parse.java @@ -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 { } diff --git a/parsers/java/src/org/bitshift/parsing/parsers/JavaParser.java b/parsers/java/src/org/bitshift/parsing/parsers/JavaParser.java index e33c8e4..39d4b47 100644 --- a/parsers/java/src/org/bitshift/parsing/parsers/JavaParser.java +++ b/parsers/java/src/org/bitshift/parsing/parsers/JavaParser.java @@ -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 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; } } diff --git a/parsers/java/src/org/bitshift/parsing/symbols/JavaSymbols.java b/parsers/java/src/org/bitshift/parsing/symbols/JavaSymbols.java index f71667e..4f39c95 100644 --- a/parsers/java/src/org/bitshift/parsing/symbols/JavaSymbols.java +++ b/parsers/java/src/org/bitshift/parsing/symbols/JavaSymbols.java @@ -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>> _classes; + private Map>> _interfaces; + private Map>> _methods; + private Map>> _fields; + private Map>> _vars; + + public JavaSymbols() { + _packageName = null; + _classes = new HashMap>>(); + _interfaces = new HashMap>>(); + _methods = new HashMap>>(); + _fields = new HashMap>>(); + _vars = new HashMap>>(); + } + + public boolean setPackage(String name) { + _packageName = name; + } + + public boolean insertClassDeclaration(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { + List pos = new ArrayList(4); + pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol); + List> 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 pos = new ArrayList(4); + pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol); + List> 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 pos = new ArrayList(4); + pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol); + List> 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 pos = new ArrayList(4); + pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol); + List> 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 pos = new ArrayList(4); + pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol); + List> 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 pos = new ArrayList(4); + pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol); + List> 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 pos = new ArrayList(4); + pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol); + List> 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 pos = new ArrayList(4); + pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol); + List> 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 pos = new ArrayList(4); + pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol); + List> 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 pos = new ArrayList(4); + pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol); + List> copy = _classes.get(name); + copy.add(pos); + _classes.put(name, copy); + return true; + } + + public String toString() { + + } +} +