Bladeren bron

Add block position function to handle positions for java ast nodes.

tags/v1.0^2
Benjamin Attal 10 jaren geleden
bovenliggende
commit
6659e1fe73
4 gewijzigde bestanden met toevoegingen van 40 en 33 verwijderingen
  1. +9
    -3
      bitshift/parser/__init__.py
  2. +14
    -28
      parsers/java/src/main/java/com/bitshift/parsing/parsers/JavaParser.java
  3. +16
    -1
      parsers/java/src/main/java/com/bitshift/parsing/parsers/Parser.java
  4. +1
    -1
      parsers/java/src/main/java/com/bitshift/parsing/utils/PackableMemory.java

+ 9
- 3
bitshift/parser/__init__.py Bestand weergeven

@@ -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,
}



+ 14
- 28
parsers/java/src/main/java/com/bitshift/parsing/parsers/JavaParser.java Bestand weergeven

@@ -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<HashMap<String, Object>>();
}

public ArrayList<Integer> 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<String, Object> data = new HashMap<String, Object>();
Name nameObj = node.getName();
String name = nameObj.isQualifiedName() ?
((QualifiedName) nameObj).getFullyQualifiedName() :
((SimpleName) nameObj).getIdentifier();
List<Statement> statements;

if (node.getBody() != null)
statements = node.getBody().statements();
else
statements = new ArrayList<Statement>(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<String, Object> data = new HashMap<String, Object>();

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<String, Object> data = new HashMap<String, Object>();
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;
}


+ 16
- 1
parsers/java/src/main/java/com/bitshift/parsing/parsers/Parser.java Bestand weergeven

@@ -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) {


+ 1
- 1
parsers/java/src/main/java/com/bitshift/parsing/utils/PackableMemory.java Bestand weergeven

@@ -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;


Laden…
Annuleren
Opslaan