diff --git a/parsers/java/pom.xml b/parsers/java/pom.xml old mode 100755 new mode 100644 index 81ed975..340feb0 --- a/parsers/java/pom.xml +++ b/parsers/java/pom.xml @@ -1,22 +1,23 @@ - - + 4.0.0 - - 3.0.0 - - org.bitshift.parsing + com.bitshift.parsing parsing - 0.1 - Java Parser for Bitshift - - - UTF-8 - UTF-8 - + jar + 1.0-SNAPSHOT + parsing + http://maven.apache.org + junit + junit + 3.8.1 + test + + + org.eclipse.jdt core 3.3.0-v_771 @@ -26,74 +27,16 @@ - org.apache.maven.plugins - maven-compiler-plugin - 3.1 + org.codehaus.mojo + exec-maven-plugin + 1.2.1 - 1.7 - 1.7 - UTF-8 + com.bitshift.parsing.Parse + + - - 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/com/bitshift/parsing/Parse.java b/parsers/java/src/main/java/com/bitshift/parsing/Parse.java new file mode 100644 index 0000000..fc1d36f --- /dev/null +++ b/parsers/java/src/main/java/com/bitshift/parsing/Parse.java @@ -0,0 +1,33 @@ +package com.bitshift.parsing; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.io.IOException; + +import java.net.ServerSocket; +import java.net.Socket; + +import com.bitshift.parsing.parsers.JavaParser; + +public class Parse { + + public static void main(String[] args) { + String fromClient; + String toClient; + + try { + ServerSocket server = new ServerSocket(5002); + + while(true) { + Socket clientSocket = server.accept(); + + JavaParser parser = new JavaParser(clientSocket); + Thread parserTask = new Thread(parser); + parserTask.start(); + } + } catch (IOException ex) { + } + } + +} diff --git a/parsers/java/src/main/java/com/bitshift/parsing/parsers/CParser.java b/parsers/java/src/main/java/com/bitshift/parsing/parsers/CParser.java new file mode 100644 index 0000000..dbe93fb --- /dev/null +++ b/parsers/java/src/main/java/com/bitshift/parsing/parsers/CParser.java @@ -0,0 +1,3 @@ +package com.bitshift.parsing.parsers; + +import com.bitshift.parsing.parsers.Parser; diff --git a/parsers/java/src/main/java/com/bitshift/parsing/parsers/JavaParser.java b/parsers/java/src/main/java/com/bitshift/parsing/parsers/JavaParser.java new file mode 100644 index 0000000..c6d9b2a --- /dev/null +++ b/parsers/java/src/main/java/com/bitshift/parsing/parsers/JavaParser.java @@ -0,0 +1,215 @@ +package com.bitshift.parsing.parsers; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Stack; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.io.IOException; + +import java.net.Socket; + +import org.eclipse.jdt.core.JavaCore; +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.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.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.VariableDeclarationFragment; + +import com.bitshift.parsing.parsers.Parser; +import com.bitshift.parsing.symbols.Symbols; +import com.bitshift.parsing.symbols.JavaSymbols; + +/*TODO: Work on parsing partial java code. + * Change visits to endVisit and implement a cache for more concise code structure. + * Get rid of unecessary imports. + * Fix column and line numbers.*/ +public class JavaParser extends Parser { + + public JavaParser(Socket clientSocket) { + super(clientSocket); + } + + private String readFromClient() { + String fromClient = ""; + + try { + BufferedReader clientReader = new BufferedReader( + new InputStreamReader(this.clientSocket.getInputStream())); + + int bytes = Integer.parseInt(clientReader.readLine()); + System.out.println(bytes); + + StringBuilder builder = new StringBuilder(); + int i = 0; + + while(i < bytes) { + char aux = (char)clientReader.read(); + builder.append(aux); + i++; + } + + fromClient = builder.toString(); + + } catch (IOException ex) { + } + + return fromClient; + } + + private void writeToClient(String toClient) { + try { + PrintWriter clientWriter = new PrintWriter( + this.clientSocket.getOutputStream(), true); + + clientWriter.println(toClient); + } catch (IOException ex) { + } + } + + @Override + public Symbols genSymbols() { + char[] source = this.readFromClient().toCharArray(); + + ASTParser parser = ASTParser.newParser(AST.JLS3); + parser.setSource(source); + + Map options = JavaCore.getOptions(); + parser.setCompilerOptions(options); + + CompilationUnit root = (CompilationUnit) parser.createAST(null); + + NodeVisitor visitor = new NodeVisitor(root); + root.accept(visitor); + + return visitor.symbols; + } + + @Override + public void run() { + JavaSymbols symbols = (JavaSymbols) this.genSymbols(); + System.out.println(symbols.toString()); + writeToClient(symbols.toString()); + } + + class NodeVisitor extends ASTVisitor { + + protected CompilationUnit root; + protected JavaSymbols symbols; + private Stack> _cache; + + public NodeVisitor(CompilationUnit root) { + this.root = root; + this.symbols = new JavaSymbols(); + this._cache = new Stack>(); + } + + public boolean visit(ClassInstanceCreation node) { + return true; + } + + public boolean visit(FieldAccess node) { + Name nameObj = node.getName(); + String name = nameObj.isQualifiedName() ? + ((QualifiedName) nameObj).getFullyQualifiedName() : + ((SimpleName) nameObj).getIdentifier(); + + int sl = this.root.getLineNumber(node.getStartPosition()) - 1; + int sc = this.root.getColumnNumber(node.getStartPosition()) - 1; + + this.symbols.insertFieldAccess(name, sl, sc, null, null); + return true; + } + + public boolean visit(FieldDeclaration node) { + return true; + } + + public boolean visit(MethodDeclaration node) { + 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); + + int sl = this.root.getLineNumber(node.getStartPosition()) - 1; + int sc = this.root.getColumnNumber(node.getStartPosition()) - 1; + int el = this.root.getLineNumber(last.getStartPosition()) - 1; + int ec = this.root.getColumnNumber(last.getStartPosition()) - 1; + + this.symbols.insertMethodDeclaration(name, sl, sc, el, ec); + return true; + } + + public boolean visit(MethodInvocation node) { + Name nameObj = node.getName(); + String name = nameObj.isQualifiedName() ? + ((QualifiedName) nameObj).getFullyQualifiedName() : + ((SimpleName) nameObj).getIdentifier(); + + int sl = this.root.getLineNumber(node.getStartPosition()) - 1; + int sc = this.root.getColumnNumber(node.getStartPosition()) - 1; + + this.symbols.insertMethodInvocation(name, sl, sc, null, null); + return true; + } + + public boolean visit(PackageDeclaration node) { + Name nameObj = node.getName(); + String name = nameObj.isQualifiedName() ? + ((QualifiedName) nameObj).getFullyQualifiedName() : + ((SimpleName) nameObj).getIdentifier(); + + this.symbols.setPackage(name); + return true; + } + + public boolean visit(TypeDeclaration node) { + Name nameObj = node.getName(); + String name = nameObj.isQualifiedName() ? + ((QualifiedName) nameObj).getFullyQualifiedName() : + ((SimpleName) nameObj).getIdentifier(); + + int sl = this.root.getLineNumber(node.getStartPosition()) - 1; + int sc = this.root.getColumnNumber(node.getStartPosition()) - 1; + + if (node.isInterface()) { + this.symbols.insertInterfaceDeclaration(name, sl, sc, null, null); + } else { + this.symbols.insertClassDeclaration(name, sl, sc, null, null); + } + return true; + } + + public boolean visit(VariableDeclarationFragment node) { + Name nameObj = node.getName(); + String name = nameObj.isQualifiedName() ? + ((QualifiedName) nameObj).getFullyQualifiedName() : + ((SimpleName) nameObj).getIdentifier(); + + int sl = this.root.getLineNumber(node.getStartPosition()) - 1; + int sc = this.root.getColumnNumber(node.getStartPosition()) - 1; + this.symbols.insertVariableDeclaration(name, sl, sc, null, null); + return true; + } + + } +} diff --git a/parsers/java/src/main/java/com/bitshift/parsing/parsers/Parser.java b/parsers/java/src/main/java/com/bitshift/parsing/parsers/Parser.java new file mode 100644 index 0000000..7ce9b7c --- /dev/null +++ b/parsers/java/src/main/java/com/bitshift/parsing/parsers/Parser.java @@ -0,0 +1,19 @@ +package com.bitshift.parsing.parsers; + +import java.net.Socket; +import com.bitshift.parsing.symbols.Symbols; + +public abstract class Parser implements Runnable { + + protected Socket clientSocket; + + public Parser(Socket clientSocket) { + this.clientSocket = clientSocket; + } + + abstract Symbols genSymbols(); + + public abstract void run(); + +} + diff --git a/parsers/java/src/main/java/com/bitshift/parsing/symbols/CSymbols.java b/parsers/java/src/main/java/com/bitshift/parsing/symbols/CSymbols.java new file mode 100644 index 0000000..9abd60d --- /dev/null +++ b/parsers/java/src/main/java/com/bitshift/parsing/symbols/CSymbols.java @@ -0,0 +1 @@ +package com.bitshift.parsing.symbols; diff --git a/parsers/java/src/main/java/com/bitshift/parsing/symbols/JavaSymbols.java b/parsers/java/src/main/java/com/bitshift/parsing/symbols/JavaSymbols.java new file mode 100644 index 0000000..09d32b4 --- /dev/null +++ b/parsers/java/src/main/java/com/bitshift/parsing/symbols/JavaSymbols.java @@ -0,0 +1,159 @@ +package com.bitshift.parsing.symbols; + +import java.util.List; +import java.util.Map; +import java.util.HashMap; +import java.util.ArrayList; +import com.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; + return true; + } + + public boolean insertClassDeclaration(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { + List pos = new ArrayList(4); + pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); + + List> copy = (List>)_classes.get(name); + copy = (copy == null) ? new ArrayList>() : copy; + + copy.add(0, pos); + this._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.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); + + List> copy = (List>)_classes.get(name); + copy = (copy == null) ? new ArrayList>() : copy; + + copy.add(pos); + this._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.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); + + List> copy = (List>)_classes.get(name); + copy = (copy == null) ? new ArrayList>() : copy; + + copy.add(0, pos); + this._interfaces.put(name, copy); + return true; + } + public boolean insertInterfaceInstance(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { + List pos = new ArrayList(4); + pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); + + List> copy = (List>)_classes.get(name); + copy = (copy == null) ? new ArrayList>() : copy; + + copy.add(pos); + this._interfaces.put(name, copy); + return true; + } + + public boolean insertMethodDeclaration(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { + List pos = new ArrayList(4); + pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); + + List> copy = (List>)_classes.get(name); + copy = (copy == null) ? new ArrayList>() : copy; + + copy.add(0, pos); + this._methods.put(name, copy); + return true; + } + public boolean insertMethodInvocation(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { + List pos = new ArrayList(4); + pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); + + List> copy = (List>)_classes.get(name); + copy = (copy == null) ? new ArrayList>() : copy; + + copy.add(pos); + this._methods.put(name, copy); + return true; + } + + public boolean insertFieldDeclaration(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { + List pos = new ArrayList(4); + pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); + + List> copy = (List>)_classes.get(name); + copy = (copy == null) ? new ArrayList>() : copy; + + copy.add(0, pos); + this._fields.put(name, copy); + return true; + } + public boolean insertFieldAccess(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { + List pos = new ArrayList(4); + pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); + + List> copy = (List>)_classes.get(name); + copy = (copy == null) ? new ArrayList>() : copy; + + copy.add(pos); + this._fields.put(name, copy); + return true; + } + + public boolean insertVariableDeclaration(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { + List pos = new ArrayList(4); + pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); + + List> copy = (List>)_classes.get(name); + copy = (copy == null) ? new ArrayList>() : copy; + + copy.add(0, pos); + this._vars.put(name, copy); + return true; + } + public boolean insertVariableAccess(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { + List pos = new ArrayList(4); + pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); + + List> copy = (List>)_classes.get(name); + copy = (copy == null) ? new ArrayList>() : copy; + + copy.add(pos); + this._vars.put(name, copy); + return true; + } + + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("classes:" + this._classes + ","); + builder.append("interfaces:" + this._interfaces + ","); + builder.append("methods:" + this._methods + ","); + builder.append("fields:" + this._fields + ","); + builder.append("vars:" + this._vars + ","); + + return "{" + builder.toString() + "}"; + } +} + diff --git a/parsers/java/src/main/java/com/bitshift/parsing/symbols/Symbols.java b/parsers/java/src/main/java/com/bitshift/parsing/symbols/Symbols.java new file mode 100644 index 0000000..116dbd7 --- /dev/null +++ b/parsers/java/src/main/java/com/bitshift/parsing/symbols/Symbols.java @@ -0,0 +1,9 @@ +package com.bitshift.parsing.symbols; + +public abstract class Symbols { + + public Symbols() { + + } + +}