From 77e2b6f524bf5d6006fd8a42166abdde4095d672 Mon Sep 17 00:00:00 2001 From: Benjamin Attal Date: Sat, 19 Apr 2014 15:26:38 -0400 Subject: [PATCH] Fix errors in java parser, mostly casting issues. In Parse.java, set up a tcp server for communication with python processes. Builds with maven --- .gitignore | 3 + parsers/java/pom.xml | 99 ++++++++++++++++++++++ .../src/main/java/org/bitshift/parsing/Parse.java | 40 +++++++++ .../org/bitshift/parsing/parsers/CParser.java | 4 - .../org/bitshift/parsing/parsers/JavaParser.java | 81 +++++++++++++----- .../java}/org/bitshift/parsing/parsers/Parser.java | 7 +- .../org/bitshift/parsing/symbols/CSymbols.java | 0 .../org/bitshift/parsing/symbols/JavaSymbols.java | 76 ++++++++++++----- .../org/bitshift/parsing/symbols/Symbols.java | 0 parsers/java/src/org/bitshift/parsing/Parse.java | 6 -- 10 files changed, 258 insertions(+), 58 deletions(-) create mode 100755 parsers/java/pom.xml create mode 100644 parsers/java/src/main/java/org/bitshift/parsing/Parse.java rename parsers/java/src/{ => main/java}/org/bitshift/parsing/parsers/CParser.java (66%) rename parsers/java/src/{ => main/java}/org/bitshift/parsing/parsers/JavaParser.java (57%) rename parsers/java/src/{ => main/java}/org/bitshift/parsing/parsers/Parser.java (63%) rename parsers/java/src/{ => main/java}/org/bitshift/parsing/symbols/CSymbols.java (100%) rename parsers/java/src/{ => main/java}/org/bitshift/parsing/symbols/JavaSymbols.java (65%) rename parsers/java/src/{ => main/java}/org/bitshift/parsing/symbols/Symbols.java (100%) delete mode 100644 parsers/java/src/org/bitshift/parsing/Parse.java diff --git a/.gitignore b/.gitignore index 4ce656b..8b72a6d 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,6 @@ nosetests.xml .mr.developer.cfg .project .pydevproject + +# Maven +*/target/* diff --git a/parsers/java/pom.xml b/parsers/java/pom.xml new file mode 100755 index 0000000..81ed975 --- /dev/null +++ b/parsers/java/pom.xml @@ -0,0 +1,99 @@ + + + 4.0.0 + + 3.0.0 + + + org.bitshift.parsing + parsing + 0.1 + Java Parser for Bitshift + + + UTF-8 + UTF-8 + + + + + org.eclipse.jdt + core + 3.3.0-v_771 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + UTF-8 + + + + org.apache.maven.plugins + maven-source-plugin + 2.2.1 + + + attach-sources + + jar + + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.4 + + + + true + + + + + + org.apache.maven.plugins + maven-shade-plugin + 1.6 + + true + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + package + + shade + + + + + + org.bishift.parsing.Parse + + + + + + + + + diff --git a/parsers/java/src/main/java/org/bitshift/parsing/Parse.java b/parsers/java/src/main/java/org/bitshift/parsing/Parse.java new file mode 100644 index 0000000..593645b --- /dev/null +++ b/parsers/java/src/main/java/org/bitshift/parsing/Parse.java @@ -0,0 +1,40 @@ +import java.io.*; +import java.net.*; + +public class Parse { + + public static void main(String[][] args) { + String fromClient; + String toClient; + + try { + ServerSocket server = new ServerSocket(5002); + + while(true) { + Socket connected = server.accept(); + System.out.println("The client is connected."); + + BufferedReader clientReader = new BufferedReader( + new InputStreamReader(connected.getInputStream())); + + PrintWriter clientWriter = new PrintWriter( + connected.getOutputStream(), true); + + while(true) { + StringBuilder builder = new StringBuilder(); + + while((fromClient = clientReader.readLine()) != null) { + builder.append(fromClient); + } + + fromClient = builder.toString(); + + //Handle the data from the client here + } + } + } catch (IOException ex) { + + } + } + +} diff --git a/parsers/java/src/org/bitshift/parsing/parsers/CParser.java b/parsers/java/src/main/java/org/bitshift/parsing/parsers/CParser.java similarity index 66% rename from parsers/java/src/org/bitshift/parsing/parsers/CParser.java rename to parsers/java/src/main/java/org/bitshift/parsing/parsers/CParser.java index 247f9ee..9cd4308 100644 --- a/parsers/java/src/org/bitshift/parsing/parsers/CParser.java +++ b/parsers/java/src/main/java/org/bitshift/parsing/parsers/CParser.java @@ -1,7 +1,3 @@ package org.bitshift.parsing.parsers; import org.bitshift.parsing.parsers.Parser; - -public class CParser extends Parser { - -} diff --git a/parsers/java/src/org/bitshift/parsing/parsers/JavaParser.java b/parsers/java/src/main/java/org/bitshift/parsing/parsers/JavaParser.java similarity index 57% rename from parsers/java/src/org/bitshift/parsing/parsers/JavaParser.java rename to parsers/java/src/main/java/org/bitshift/parsing/parsers/JavaParser.java index 39d4b47..bb7b7e4 100644 --- a/parsers/java/src/org/bitshift/parsing/parsers/JavaParser.java +++ b/parsers/java/src/main/java/org/bitshift/parsing/parsers/JavaParser.java @@ -1,6 +1,9 @@ package org.bitshift.parsing.parsers; +import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.Stack; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.dom.AST; @@ -8,23 +11,39 @@ 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.ClassInstanceCreation; -import org.eclipse.jdt.core.dom.FieldAccess +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.Name; import org.eclipse.jdt.core.dom.PackageDeclaration; +import org.eclipse.jdt.core.dom.QualifiedName; +import org.eclipse.jdt.core.dom.QualifiedType; +import org.eclipse.jdt.core.dom.SimpleName; +import org.eclipse.jdt.core.dom.SimpleType; import org.eclipse.jdt.core.dom.Statement; +import org.eclipse.jdt.core.dom.Type; import org.eclipse.jdt.core.dom.TypeDeclaration; -import org.eclipse.jdt.core.dom.VariableDeclarationStatement +import org.eclipse.jdt.core.dom.VariableDeclarationStatement; 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.*/ + * Change visits to endVisit and implement a cache*/ public class JavaParser extends Parser { + protected JavaSymbols symbols; + protected CompilationUnit compUnit; + private Stack> _cache; + + public JavaParser(String source) { + super(source); + this.symbols = new JavaSymbols(); + this._cache = new Stack>(); + } + @Override public Symbols genSymbols() { char[] source = this.source.toCharArray(); @@ -36,49 +55,54 @@ public class JavaParser extends Parser { parser.setCompilerOptions(options); //Work on parsing partial java code later - CompilationUnit result = (CompilationUnit) parser.createAST(null); + this.compUnit = (CompilationUnit) parser.createAST(null); - ASTVisitor visitor = new NodeVisitor(result); - result.accept(visitor); + ASTVisitor visitor = new NodeVisitor(); + this.compUnit.accept(visitor); - return visitor.symbols; + return this.symbols; } class NodeVisitor extends ASTVisitor { - protected Symbols symbols; - protected CompilationUnit compUnit; - - public NodeVisitor(CompilationUnit compUnit) { - symbols = new JavaSymbols(); - } - public boolean visit(ClassInstanceCreation node) { - String name = node.getType().getName(); + Type typeObj = node.getType(); + Name nameObj = typeObj.isQualifiedType() ? ((QualifiedType) typeObj).getName() : ((SimpleType) typeObj).getName(); + String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier(); + 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(); + Name nameObj = node.getName(); + String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier(); + 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(); + Type typeObj = node.getType(); + Name nameObj = typeObj.isQualifiedType() ? ((QualifiedType) typeObj).getName() : ((SimpleType) typeObj).getName(); + String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier(); + 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(); + Name nameObj = node.getName(); + String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier(); List statements = node.getBody().statements(); Statement last = statements.get(statements.size() - 1); @@ -86,27 +110,37 @@ public class JavaParser extends Parser { 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(); + Name nameObj = node.getName(); + String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier(); + 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()); + Name nameObj = node.getName(); + String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier(); + + symbols.setPackage(name); return true; } public boolean visit(TypeDeclaration node) { - String name = node.getName(); + Name nameObj = node.getName(); + String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier(); + 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 { @@ -116,7 +150,10 @@ public class JavaParser extends Parser { } public boolean visit(VariableDeclarationStatement node) { - String name = node.getType().getName(); + Type typeObj = node.getType(); + Name nameObj = typeObj.isQualifiedType() ? ((QualifiedType) typeObj).getName() : ((SimpleType) typeObj).getName(); + String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier(); + int sl = compUnit.getLineNumber(node.getStartPosition()) - 1; int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1; symbols.insertVariableDeclaration(name, sl, sc, null, null); diff --git a/parsers/java/src/org/bitshift/parsing/parsers/Parser.java b/parsers/java/src/main/java/org/bitshift/parsing/parsers/Parser.java similarity index 63% rename from parsers/java/src/org/bitshift/parsing/parsers/Parser.java rename to parsers/java/src/main/java/org/bitshift/parsing/parsers/Parser.java index ae74725..9b96a8d 100644 --- a/parsers/java/src/org/bitshift/parsing/parsers/Parser.java +++ b/parsers/java/src/main/java/org/bitshift/parsing/parsers/Parser.java @@ -2,16 +2,15 @@ package org.bitshift.parsing.parsers; import org.bitshift.parsing.symbols.Symbols; -public class Parser { +public abstract class Parser { protected String source; public Parser(String source) { - + this.source = source; } - public Symbols genSymbols() { + abstract Symbols genSymbols(); - } } diff --git a/parsers/java/src/org/bitshift/parsing/symbols/CSymbols.java b/parsers/java/src/main/java/org/bitshift/parsing/symbols/CSymbols.java similarity index 100% rename from parsers/java/src/org/bitshift/parsing/symbols/CSymbols.java rename to parsers/java/src/main/java/org/bitshift/parsing/symbols/CSymbols.java diff --git a/parsers/java/src/org/bitshift/parsing/symbols/JavaSymbols.java b/parsers/java/src/main/java/org/bitshift/parsing/symbols/JavaSymbols.java similarity index 65% rename from parsers/java/src/org/bitshift/parsing/symbols/JavaSymbols.java rename to parsers/java/src/main/java/org/bitshift/parsing/symbols/JavaSymbols.java index 4f39c95..aa39cfd 100644 --- a/parsers/java/src/org/bitshift/parsing/symbols/JavaSymbols.java +++ b/parsers/java/src/main/java/org/bitshift/parsing/symbols/JavaSymbols.java @@ -6,33 +6,38 @@ import java.util.HashMap; import java.util.ArrayList; import org.bitshift.parsing.symbols.Symbols; -/*TODO: Overwrite toString*/ +/*TODO: Overwrite toString + * Change instance vars to HashMaps of HashMaps*/ public class JavaSymbols extends Symbols { private String _packageName; - private Map>> _classes; - private Map>> _interfaces; - private Map>> _methods; - private Map>> _fields; - private Map>> _vars; + 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>>(); + _classes = new HashMap(); + _interfaces = new HashMap(); + _methods = new HashMap(); + _fields = new HashMap(); + _vars = new HashMap(); } public boolean setPackage(String name) { _packageName = name; + return true; } 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); + + List> copy = (List>)_classes.get(name); + copy = (copy == null) ? new ArrayList>() : copy; + copy.add(0, pos); _classes.put(name, copy); return true; @@ -40,7 +45,10 @@ public class JavaSymbols extends Symbols { 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); + + List> copy = (List>)_classes.get(name); + copy = (copy == null) ? new ArrayList>() : copy; + copy.add(pos); _classes.put(name, copy); return true; @@ -49,7 +57,10 @@ public class JavaSymbols extends Symbols { 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); + + List> copy = (List>)_classes.get(name); + copy = (copy == null) ? new ArrayList>() : copy; + copy.add(0, pos); _classes.put(name, copy); return true; @@ -57,7 +68,10 @@ public class JavaSymbols extends Symbols { 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); + + List> copy = (List>)_classes.get(name); + copy = (copy == null) ? new ArrayList>() : copy; + copy.add(pos); _classes.put(name, copy); return true; @@ -66,7 +80,10 @@ public class JavaSymbols extends Symbols { 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); + + List> copy = (List>)_classes.get(name); + copy = (copy == null) ? new ArrayList>() : copy; + copy.add(0, pos); _classes.put(name, copy); return true; @@ -74,7 +91,10 @@ public class JavaSymbols extends Symbols { 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); + + List> copy = (List>)_classes.get(name); + copy = (copy == null) ? new ArrayList>() : copy; + copy.add(pos); _classes.put(name, copy); return true; @@ -83,7 +103,10 @@ public class JavaSymbols extends Symbols { 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); + + List> copy = (List>)_classes.get(name); + copy = (copy == null) ? new ArrayList>() : copy; + copy.add(0, pos); _classes.put(name, copy); return true; @@ -91,7 +114,10 @@ public class JavaSymbols extends Symbols { 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); + + List> copy = (List>)_classes.get(name); + copy = (copy == null) ? new ArrayList>() : copy; + copy.add(pos); _classes.put(name, copy); return true; @@ -100,7 +126,10 @@ public class JavaSymbols extends Symbols { 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); + + List> copy = (List>)_classes.get(name); + copy = (copy == null) ? new ArrayList>() : copy; + copy.add(0, pos); _classes.put(name, copy); return true; @@ -108,14 +137,17 @@ public class JavaSymbols extends Symbols { 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); + + List> copy = (List>)_classes.get(name); + copy = (copy == null) ? new ArrayList>() : copy; + copy.add(pos); _classes.put(name, copy); return true; } public String toString() { - + return ""; } } diff --git a/parsers/java/src/org/bitshift/parsing/symbols/Symbols.java b/parsers/java/src/main/java/org/bitshift/parsing/symbols/Symbols.java similarity index 100% rename from parsers/java/src/org/bitshift/parsing/symbols/Symbols.java rename to parsers/java/src/main/java/org/bitshift/parsing/symbols/Symbols.java diff --git a/parsers/java/src/org/bitshift/parsing/Parse.java b/parsers/java/src/org/bitshift/parsing/Parse.java deleted file mode 100644 index 9d8ade7..0000000 --- a/parsers/java/src/org/bitshift/parsing/Parse.java +++ /dev/null @@ -1,6 +0,0 @@ - -/*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 { - -}