diff --git a/parsers/java/src/main/java/com/bitshift/parsing/Parse.java b/parsers/java/src/main/java/com/bitshift/parsing/Parse.java index fc1d36f..302c083 100644 --- a/parsers/java/src/main/java/com/bitshift/parsing/Parse.java +++ b/parsers/java/src/main/java/com/bitshift/parsing/Parse.java @@ -1,8 +1,5 @@ package com.bitshift.parsing; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.io.PrintWriter; import java.io.IOException; import java.net.ServerSocket; @@ -13,18 +10,12 @@ 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); + ServerSocket server = new ServerSocket(5033); while(true) { Socket clientSocket = server.accept(); - - JavaParser parser = new JavaParser(clientSocket); - Thread parserTask = new Thread(parser); - parserTask.start(); + new Thread(new JavaParser(clientSocket)).start(); } } catch (IOException ex) { } 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 index 4ba3623..989c0dd 100644 --- a/parsers/java/src/main/java/com/bitshift/parsing/parsers/JavaParser.java +++ b/parsers/java/src/main/java/com/bitshift/parsing/parsers/JavaParser.java @@ -13,7 +13,6 @@ 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.FieldDeclaration; import org.eclipse.jdt.core.dom.MethodDeclaration; import org.eclipse.jdt.core.dom.MethodInvocation; import org.eclipse.jdt.core.dom.Name; @@ -71,22 +70,6 @@ public class JavaParser extends Parser { this._cache = new Stack>(); } - public boolean visit(FieldDeclaration node) { - HashMap data = new HashMap(); - int sl = this.root.getLineNumber(node.getStartPosition()); - int sc = this.root.getColumnNumber(node.getStartPosition()); - - data.put("coord", Symbols.createCoord(sl, sc, -1, -1)); - this._cache.push(data); - return true; - } - - public void endVisit(FieldDeclaration node) { - HashMap data = this._cache.pop(); - String name = (String)data.remove("name"); - this.symbols.insertFieldDeclaration(name, data); - } - public boolean visit(MethodDeclaration node) { HashMap data = new HashMap(); Name nameObj = node.getName(); @@ -115,7 +98,7 @@ public class JavaParser extends Parser { public void endVisit(MethodDeclaration node) { HashMap data = this._cache.pop(); String name = (String)data.remove("name"); - this.symbols.insertMethodDeclaration(name, data); + this.symbols.insertMethodDeclaration("\"" + name + "\"", data); } public boolean visit(MethodInvocation node) { @@ -136,7 +119,7 @@ public class JavaParser extends Parser { public void endVisit(MethodInvocation node) { HashMap data = this._cache.pop(); String name = (String)data.remove("name"); - this.symbols.insertMethodInvocation(name, data); + this.symbols.insertMethodInvocation("\"" + name + "\"", data); } public boolean visit(PackageDeclaration node) { @@ -167,9 +150,9 @@ public class JavaParser extends Parser { String name = (String)data.remove("name"); if (node.isInterface()) { - this.symbols.insertInterfaceDeclaration(name, data); + this.symbols.insertInterfaceDeclaration("\"" + name + "\"", data); } else { - this.symbols.insertClassDeclaration(name, data); + this.symbols.insertClassDeclaration("\"" + name + "\"", data); } } @@ -186,7 +169,7 @@ public class JavaParser extends Parser { public void endVisit(VariableDeclarationFragment node) { HashMap data = this._cache.pop(); String name = (String)data.remove("name"); - this.symbols.insertVariableDeclaration(name, data); + this.symbols.insertVariableDeclaration("\"" + name + "\"", data); } public boolean visit(QualifiedName node) { 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 index 9d00954..83100f5 100644 --- a/parsers/java/src/main/java/com/bitshift/parsing/parsers/Parser.java +++ b/parsers/java/src/main/java/com/bitshift/parsing/parsers/Parser.java @@ -1,8 +1,9 @@ package com.bitshift.parsing.parsers; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.InputStreamReader; -import java.io.PrintWriter; +import java.io.OutputStreamWriter; import java.io.IOException; import java.net.Socket; @@ -46,12 +47,16 @@ public abstract class Parser implements Runnable { protected void writeToClient(String toClient) { try { - PrintWriter clientWriter = new PrintWriter( - this.clientSocket.getOutputStream(), true); + BufferedWriter clientWriter = new BufferedWriter( + new OutputStreamWriter(this.clientSocket.getOutputStream())); - PackableMemory mem = new PackableMemory(toClient.length()); + PackableMemory mem = new PackableMemory(4); + mem.pack(toClient.length(), 0); String dataSize = new String(mem.mem); - clientWriter.println(dataSize + toClient); + + clientWriter.write(dataSize + toClient); + clientWriter.flush(); + this.clientSocket.close(); } catch (IOException ex) { } } 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 index 5419d5a..6f0caf1 100644 --- a/parsers/java/src/main/java/com/bitshift/parsing/symbols/JavaSymbols.java +++ b/parsers/java/src/main/java/com/bitshift/parsing/symbols/JavaSymbols.java @@ -11,15 +11,16 @@ public class JavaSymbols extends Symbols { private HashMap> _classes; private HashMap> _interfaces; private HashMap> _methods; - private HashMap> _fields; private HashMap> _vars; + private final String assignKey = "\"assignments\""; + private final String useKey = "\"uses\""; + public JavaSymbols() { _packageName = null; _classes = new HashMap>(); _interfaces = new HashMap>(); _methods = new HashMap>(); - _fields = new HashMap>(); _vars = new HashMap>(); } @@ -34,15 +35,23 @@ public class JavaSymbols extends Symbols { HashMap klass = new HashMap(); assignments.add(data.get("coord")); - klass.put("assignments", assignments); - klass.put("uses", uses); + klass.put(assignKey, assignments); + klass.put(useKey, uses); this._classes.put(name, klass); return true; } public boolean insertInterfaceDeclaration(String name, HashMap data) { - this._interfaces.put(name, data); + ArrayList assignments = new ArrayList(10); + ArrayList uses = new ArrayList(10); + HashMap klass = new HashMap(); + + assignments.add(data.get("coord")); + klass.put(assignKey, assignments); + klass.put(useKey, uses); + + this._interfaces.put(name, klass); return true; } @@ -54,13 +63,13 @@ public class JavaSymbols extends Symbols { ArrayList uses = new ArrayList(10); assignments.add(data.get("coord")); - method.put("assignments", assignments); - method.put("uses", uses); + method.put(assignKey, assignments); + method.put(useKey, uses); } else { - ArrayList assignments = (ArrayList)method.get("assignments"); + ArrayList assignments = (ArrayList)method.get(assignKey); assignments.add(data.get("coord")); - method.put("assignments", assignments); + method.put(assignKey, assignments); } this._methods.put(name, method); @@ -74,24 +83,19 @@ public class JavaSymbols extends Symbols { ArrayList uses = new ArrayList(10); uses.add(data.get("coord")); - method.put("assignments", assignments); - method.put("uses", uses); + method.put(assignKey, assignments); + method.put(useKey, uses); } else { - ArrayList uses = (ArrayList)method.get("uses"); + ArrayList uses = (ArrayList)method.get(useKey); uses.add(data.get("coord")); - method.put("uses", uses); + method.put(useKey, uses); } this._methods.put(name, method); return true; } - public boolean insertFieldDeclaration(String name, HashMap data) { - this._fields.put(name, data); - return true; - } - public boolean insertVariableDeclaration(String name, HashMap data) { HashMap var = this._vars.get(name); if (var == null) { @@ -100,13 +104,13 @@ public class JavaSymbols extends Symbols { ArrayList uses = new ArrayList(10); assignments.add(data.get("coord")); - var.put("assignments", assignments); - var.put("uses", uses); + var.put(assignKey, assignments); + var.put(useKey, uses); } else { - ArrayList assignments = (ArrayList)var.get("assignments"); + ArrayList assignments = (ArrayList)var.get(assignKey); assignments.add(data.get("coord")); - var.put("assignments", assignments); + var.put(assignKey, assignments); } this._vars.put(name, var); @@ -120,13 +124,13 @@ public class JavaSymbols extends Symbols { ArrayList uses = new ArrayList(10); uses.add(data.get("coord")); - var.put("assignments", assignments); - var.put("uses", uses); + var.put(assignKey, assignments); + var.put(useKey, uses); } else { - ArrayList uses = (ArrayList)var.get("uses"); + ArrayList uses = (ArrayList)var.get(useKey); uses.add(data.get("coord")); - var.put("uses", uses); + var.put(useKey, uses); } this._vars.put(name, var); @@ -135,13 +139,14 @@ public class JavaSymbols extends Symbols { 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() + "}"; + builder.append("\"classes\":" + this._classes + ","); + builder.append("\"interfaces\":" + this._interfaces + ","); + builder.append("\"methods\":" + this._methods + ","); + builder.append("\"vars\":" + this._vars + ","); + + String s = builder.toString().replaceAll("=", ":"); + s = s.substring(0, s.length() - 1); + return "{" + s + "}"; } } diff --git a/parsers/java/src/main/java/com/bitshift/parsing/utils/PackableMemory.java b/parsers/java/src/main/java/com/bitshift/parsing/utils/PackableMemory.java index 24d883c..1f54d99 100644 --- a/parsers/java/src/main/java/com/bitshift/parsing/utils/PackableMemory.java +++ b/parsers/java/src/main/java/com/bitshift/parsing/utils/PackableMemory.java @@ -22,7 +22,7 @@ public class PackableMemory { // The most significant porion of the integer is stored in mem[loc]. // Bytes are masked out of the integer and stored in the array, working // from right(least significant) to left (most significant). - void pack(int val, int loc) + public void pack(int val, int loc) { final int MASK = 0xff; for (int i = 3; i >= 0; i--) diff --git a/parsers/java/src/main/java/com/bitshift/parsing/utils/Tuple.java b/parsers/java/src/main/java/com/bitshift/parsing/utils/Tuple.java new file mode 100644 index 0000000..115a3c6 --- /dev/null +++ b/parsers/java/src/main/java/com/bitshift/parsing/utils/Tuple.java @@ -0,0 +1,23 @@ +package com.bitshift.parsing.utils; + +import java.util.List; +import java.util.Arrays; + +public class Tuple { + private List _objects; + + public Tuple(T... args) { + _objects = Arrays.asList(args); + } + + public String toString() { + StringBuilder builder = new StringBuilder(); + + for(T o: this._objects) { + builder.append(o + ","); + } + + String s = builder.toString(); + return "(" + s.substring(0, s.length() - 1) + ")"; + } +} diff --git a/parsers/ruby/lib/parse_server.rb b/parsers/ruby/lib/parse_server.rb index 916f434..9a929aa 100644 --- a/parsers/ruby/lib/parse_server.rb +++ b/parsers/ruby/lib/parse_server.rb @@ -14,7 +14,7 @@ end def start_server - server = TCPServer.new 5003 + server = TCPServer.new 5065 loop do # Start a new thread for each client accepted diff --git a/parsers/ruby/lib/parser.rb b/parsers/ruby/lib/parser.rb index 6dfa175..eec293b 100644 --- a/parsers/ruby/lib/parser.rb +++ b/parsers/ruby/lib/parser.rb @@ -3,20 +3,6 @@ require 'ruby_parser' require 'sexp_processor' module Bitshift - class Tuple - attr_accessor :objects - - def initialize(arr) - @objects = arr - end - - def inspect - s = "(" - @objects.each {|o| s += "#{o},"} - s = s[0..-2] + ')' - end - end - class Parser def initialize(source) @source = source @@ -39,7 +25,8 @@ module Bitshift def initialize(offset, tree) super() - module_hash = Hash.new {|hash, key| hash[key] = { decls: [], uses: [] }} + module_hash = Hash.new {|hash, key| + hash[key] = { assignments: [], uses: [] }} class_hash = module_hash.clone function_hash = module_hash.clone var_hash = module_hash.clone @@ -64,7 +51,7 @@ module Bitshift break if cur_exp == nil end - pos = Tuple.new([start_ln, -1, end_ln, -1]) + pos = [start_ln, -1, end_ln, -1] return pos end @@ -72,7 +59,7 @@ module Bitshift pos = Hash.new end_ln = start_ln = exp.line - offset - pos = Tuple.new([start_ln, -1, end_ln, -1]) + pos = [start_ln, -1, end_ln, -1] return pos end @@ -80,7 +67,7 @@ module Bitshift pos = block_position(exp) exp.shift name = exp.shift - symbols[:modules][name][:decls] << pos + symbols[:modules][name][:assignments] << pos exp.each_sexp {|s| process(s)} return exp.clear end @@ -89,7 +76,7 @@ module Bitshift pos = block_position(exp) exp.shift name = exp.shift - symbols[:classes][name][:decls] << pos + symbols[:classes][name][:assignments] << pos exp.each_sexp {|s| process(s)} return exp.clear end @@ -98,7 +85,7 @@ module Bitshift pos = block_position(exp) exp.shift name = exp.shift - symbols[:functions][name][:decls] << pos + symbols[:functions][name][:assignments] << pos exp.each_sexp {|s| process(s)} return exp.clear end @@ -117,7 +104,7 @@ module Bitshift pos = statement_position(exp) exp.shift name = exp.shift - symbols[:vars][name][:decls] << pos + symbols[:vars][name][:assignments] << pos exp.each_sexp {|s| process(s)} return exp.clear end @@ -126,17 +113,19 @@ module Bitshift pos = statement_position(exp) exp.shift name = exp.shift - symbols[:vars][name][:decls] << pos + symbols[:vars][name][:assignments] << pos exp.each_sexp {|s| process(s)} return exp.clear end def to_s - new_symbols = Hash.new {|hash, key| hash[key] = []} + new_symbols = Hash.new {|hash, key| hash[key] = Hash.new} symbols.each do |type, sym_list| sym_list.each do |name, sym| - new_symbols[type.to_s] << Tuple.new(["'#{name}'", sym[:decls], sym[:uses]]) + new_symbols[type.to_s][name.to_s] = { + "assignments" => sym[:assignments], + "uses" => sym[:uses]} end end