From 6659e1fe73ba4de8d2de4d4c843feea1cac41d8a Mon Sep 17 00:00:00 2001 From: Benjamin Attal Date: Tue, 10 Jun 2014 11:00:30 -0400 Subject: [PATCH] Add block position function to handle positions for java ast nodes. --- bitshift/parser/__init__.py | 12 +++++-- .../com/bitshift/parsing/parsers/JavaParser.java | 42 ++++++++-------------- .../java/com/bitshift/parsing/parsers/Parser.java | 17 ++++++++- .../com/bitshift/parsing/utils/PackableMemory.java | 2 +- 4 files changed, 40 insertions(+), 33 deletions(-) diff --git a/bitshift/parser/__init__.py b/bitshift/parser/__init__.py index 0da3ccd..6620c7c 100644 --- a/bitshift/parser/__init__.py +++ b/bitshift/parser/__init__.py @@ -1,3 +1,4 @@ +import functools import json import sys import socket @@ -98,18 +99,23 @@ def start_parse_servers(): return procs -def parse_via_server(codelet): +def parse_via_server(codelet, buffered=True): port = 5001 + codelet.language server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.connect(("localhost", port)) server_socket.send("%d\n%s" % (len(codelet.code), codelet.code)) - symbols = json.loads(_recv_data(server_socket)) + if buffered: + data = _recv_data(server_socket) + else: + data = server_socket.recv(5000) + + symbols = json.loads(data) return symbols PARSERS = { "Python": parse_py, - "Java": parse_via_server, + "Java": functools.partial(parse_via_server, buffered=False), "Ruby": parse_via_server, } 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 b3b2fdb..dc83154 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 @@ -10,6 +10,7 @@ import java.net.Socket; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.ASTVisitor; import org.eclipse.jdt.core.dom.CompilationUnit; @@ -71,31 +72,23 @@ public class JavaParser extends Parser { this._cache = new Stack>(); } + public ArrayList blockPosition(ASTNode node) { + int sl = this.root.getLineNumber(node.getStartPosition()); + int sc = this.root.getColumnNumber(node.getStartPosition()) + 1; + int el = this.root.getLineNumber(node.getStartPosition() + node.getLength()); + int ec = this.root.getColumnNumber(node.getStartPosition() + node.getLength()) + 1; + + return Symbols.createCoord(sl, sc, el, ec); + } + public boolean visit(MethodDeclaration node) { HashMap data = new HashMap(); Name nameObj = node.getName(); String name = nameObj.isQualifiedName() ? ((QualifiedName) nameObj).getFullyQualifiedName() : ((SimpleName) nameObj).getIdentifier(); - List statements; - - if (node.getBody() != null) - statements = node.getBody().statements(); - else - statements = new ArrayList(0); - - int sl = this.root.getLineNumber(node.getStartPosition()); - int sc = this.root.getColumnNumber(node.getStartPosition()); - Integer el = sl; - Integer ec = -1; - - if (statements.size() > 0) { - Statement last = statements.get(statements.size() - 1); - el = this.root.getLineNumber(last.getStartPosition()); - ec = this.root.getColumnNumber(last.getStartPosition()); - } - data.put("coord", Symbols.createCoord(sl, sc, el, ec)); + data.put("coord", this.blockPosition(node)); data.put("name", name); this._cache.push(data); return true; @@ -113,10 +106,8 @@ public class JavaParser extends Parser { String name = nameObj.isQualifiedName() ? ((QualifiedName) nameObj).getFullyQualifiedName() : ((SimpleName) nameObj).getIdentifier(); - int sl = this.root.getLineNumber(node.getStartPosition()); - int sc = this.root.getColumnNumber(node.getStartPosition()); - data.put("coord", Symbols.createCoord(sl, sc, sl, -1)); + data.put("coord", this.blockPosition(node)); data.put("name", name); this._cache.push(data); return true; @@ -143,10 +134,7 @@ public class JavaParser extends Parser { public boolean visit(TypeDeclaration 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, sl, -1)); + data.put("coord", this.blockPosition(node)); this._cache.push(data); return true; } @@ -164,10 +152,8 @@ public class JavaParser extends Parser { public boolean visit(VariableDeclarationFragment 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, sl, -1)); + data.put("coord", this.blockPosition(node)); this._cache.push(data); 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 index 83100f5..b8e4e60 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,5 +1,7 @@ package com.bitshift.parsing.parsers; +import java.util.Formatter; + import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; @@ -45,6 +47,19 @@ public abstract class Parser implements Runnable { return fromClient; } + public String escapeUnicode(String input) { + StringBuilder b = new StringBuilder(input.length()); + Formatter f = new Formatter(b); + for (char c : input.toCharArray()) { + if (c < 128) { + b.append(c); + } else { + f.format("\\u%04x", (int) c); + } + } + return b.toString(); + } + protected void writeToClient(String toClient) { try { BufferedWriter clientWriter = new BufferedWriter( @@ -54,7 +69,7 @@ public abstract class Parser implements Runnable { mem.pack(toClient.length(), 0); String dataSize = new String(mem.mem); - clientWriter.write(dataSize + toClient); + clientWriter.write(toClient); clientWriter.flush(); this.clientSocket.close(); } catch (IOException ex) { 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 1f54d99..83babfb 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 @@ -37,7 +37,7 @@ public class PackableMemory { // The most significant porion of the integer is stored in mem[loc]. // Bytes are 'OR'ed into the integer, working from left (most significant) // to right (least significant) - int unpack(int loc) + public int unpack(int loc) { final int MASK = 0xff; int v = (int)mem[loc] & MASK;