Przeglądaj źródła

Working version of java parser up and running.

tags/v1.0^2
Benjamin Attal 10 lat temu
rodzic
commit
2338887a52
5 zmienionych plików z 259 dodań i 25 usunięć
  1. +2
    -1
      .gitignore
  2. +13
    -16
      parsers/java/src/main/java/com/bitshift/parsing/parsers/JavaParser.java
  3. +8
    -8
      parsers/java/src/main/java/com/bitshift/parsing/symbols/JavaSymbols.java
  4. +18
    -0
      test/java_parser_test.py
  5. +218
    -0
      test/resources/Matrix.java

+ 2
- 1
.gitignore Wyświetl plik

@@ -42,4 +42,5 @@ nosetests.xml
.pydevproject

# Maven
*/target/*
parsers/java/target/*
*/tags

+ 13
- 16
parsers/java/src/main/java/com/bitshift/parsing/parsers/JavaParser.java Wyświetl plik

@@ -39,8 +39,7 @@ 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.*/
* Get rid of unecessary imports.*/
public class JavaParser extends Parser {

public JavaParser(Socket clientSocket) {
@@ -55,7 +54,6 @@ public class JavaParser extends Parser {
new InputStreamReader(this.clientSocket.getInputStream()));

int bytes = Integer.parseInt(clientReader.readLine());
System.out.println(bytes);

StringBuilder builder = new StringBuilder();
int i = 0;
@@ -105,7 +103,6 @@ public class JavaParser extends Parser {
@Override
public void run() {
JavaSymbols symbols = (JavaSymbols) this.genSymbols();
System.out.println(symbols.toString());
writeToClient(symbols.toString());
}

@@ -131,8 +128,8 @@ public class JavaParser extends Parser {
((QualifiedName) nameObj).getFullyQualifiedName() :
((SimpleName) nameObj).getIdentifier();

int sl = this.root.getLineNumber(node.getStartPosition()) - 1;
int sc = this.root.getColumnNumber(node.getStartPosition()) - 1;
int sl = this.root.getLineNumber(node.getStartPosition());
int sc = this.root.getColumnNumber(node.getStartPosition());

this.symbols.insertFieldAccess(name, sl, sc, null, null);
return true;
@@ -150,10 +147,10 @@ public class JavaParser extends Parser {
List<Statement> 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;
int sl = this.root.getLineNumber(node.getStartPosition());
int sc = this.root.getColumnNumber(node.getStartPosition());
int el = this.root.getLineNumber(last.getStartPosition());
int ec = this.root.getColumnNumber(last.getStartPosition());

this.symbols.insertMethodDeclaration(name, sl, sc, el, ec);
return true;
@@ -165,8 +162,8 @@ public class JavaParser extends Parser {
((QualifiedName) nameObj).getFullyQualifiedName() :
((SimpleName) nameObj).getIdentifier();

int sl = this.root.getLineNumber(node.getStartPosition()) - 1;
int sc = this.root.getColumnNumber(node.getStartPosition()) - 1;
int sl = this.root.getLineNumber(node.getStartPosition());
int sc = this.root.getColumnNumber(node.getStartPosition());

this.symbols.insertMethodInvocation(name, sl, sc, null, null);
return true;
@@ -188,8 +185,8 @@ public class JavaParser extends Parser {
((QualifiedName) nameObj).getFullyQualifiedName() :
((SimpleName) nameObj).getIdentifier();

int sl = this.root.getLineNumber(node.getStartPosition()) - 1;
int sc = this.root.getColumnNumber(node.getStartPosition()) - 1;
int sl = this.root.getLineNumber(node.getStartPosition());
int sc = this.root.getColumnNumber(node.getStartPosition());

if (node.isInterface()) {
this.symbols.insertInterfaceDeclaration(name, sl, sc, null, null);
@@ -205,8 +202,8 @@ public class JavaParser extends Parser {
((QualifiedName) nameObj).getFullyQualifiedName() :
((SimpleName) nameObj).getIdentifier();

int sl = this.root.getLineNumber(node.getStartPosition()) - 1;
int sc = this.root.getColumnNumber(node.getStartPosition()) - 1;
int sl = this.root.getLineNumber(node.getStartPosition());
int sc = this.root.getColumnNumber(node.getStartPosition());
this.symbols.insertVariableDeclaration(name, sl, sc, null, null);
return true;
}


+ 8
- 8
parsers/java/src/main/java/com/bitshift/parsing/symbols/JavaSymbols.java Wyświetl plik

@@ -57,7 +57,7 @@ public class JavaSymbols extends Symbols {
List<Integer> pos = new ArrayList<Integer>(4);
pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol);
List<List<Integer>> copy = (List<List<Integer>>)_classes.get(name);
List<List<Integer>> copy = (List<List<Integer>>)_interfaces.get(name);
copy = (copy == null) ? new ArrayList<List<Integer>>() : copy;

copy.add(0, pos);
@@ -68,7 +68,7 @@ public class JavaSymbols extends Symbols {
List<Integer> pos = new ArrayList<Integer>(4);
pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol);
List<List<Integer>> copy = (List<List<Integer>>)_classes.get(name);
List<List<Integer>> copy = (List<List<Integer>>)_interfaces.get(name);
copy = (copy == null) ? new ArrayList<List<Integer>>() : copy;

copy.add(pos);
@@ -80,7 +80,7 @@ public class JavaSymbols extends Symbols {
List<Integer> pos = new ArrayList<Integer>(4);
pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol);
List<List<Integer>> copy = (List<List<Integer>>)_classes.get(name);
List<List<Integer>> copy = (List<List<Integer>>)_methods.get(name);
copy = (copy == null) ? new ArrayList<List<Integer>>() : copy;

copy.add(0, pos);
@@ -91,7 +91,7 @@ public class JavaSymbols extends Symbols {
List<Integer> pos = new ArrayList<Integer>(4);
pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol);
List<List<Integer>> copy = (List<List<Integer>>)_classes.get(name);
List<List<Integer>> copy = (List<List<Integer>>)_methods.get(name);
copy = (copy == null) ? new ArrayList<List<Integer>>() : copy;

copy.add(pos);
@@ -103,7 +103,7 @@ public class JavaSymbols extends Symbols {
List<Integer> pos = new ArrayList<Integer>(4);
pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol);
List<List<Integer>> copy = (List<List<Integer>>)_classes.get(name);
List<List<Integer>> copy = (List<List<Integer>>)_fields.get(name);
copy = (copy == null) ? new ArrayList<List<Integer>>() : copy;

copy.add(0, pos);
@@ -114,7 +114,7 @@ public class JavaSymbols extends Symbols {
List<Integer> pos = new ArrayList<Integer>(4);
pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol);
List<List<Integer>> copy = (List<List<Integer>>)_classes.get(name);
List<List<Integer>> copy = (List<List<Integer>>)_fields.get(name);
copy = (copy == null) ? new ArrayList<List<Integer>>() : copy;

copy.add(pos);
@@ -126,7 +126,7 @@ public class JavaSymbols extends Symbols {
List<Integer> pos = new ArrayList<Integer>(4);
pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol);
List<List<Integer>> copy = (List<List<Integer>>)_classes.get(name);
List<List<Integer>> copy = (List<List<Integer>>)_vars.get(name);
copy = (copy == null) ? new ArrayList<List<Integer>>() : copy;

copy.add(0, pos);
@@ -137,7 +137,7 @@ public class JavaSymbols extends Symbols {
List<Integer> pos = new ArrayList<Integer>(4);
pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol);
List<List<Integer>> copy = (List<List<Integer>>)_classes.get(name);
List<List<Integer>> copy = (List<List<Integer>>)_vars.get(name);
copy = (copy == null) ? new ArrayList<List<Integer>>() : copy;

copy.add(pos);


+ 18
- 0
test/java_parser_test.py Wyświetl plik

@@ -0,0 +1,18 @@
import socket

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 5002))

with open("resources/Matrix.java", "r") as java_file:
source = java_file.read() + "\nEOS_BITSHIFT"
client_socket.send("%d\n%s" % (len(source), source));

data = ''
while True:
data = client_socket.recv(10000)

if data != '':
client_socket.close()
break;

print data;

+ 218
- 0
test/resources/Matrix.java Wyświetl plik

@@ -0,0 +1,218 @@
package battlechap;

import java.io.PrintStream;

public class Matrix {
private Object[][] _datmatrix;

public Matrix(int paramInt){
this._datmatrix = new Object[paramInt][paramInt];
}

public int size() {
return this._datmatrix.length;
}

public Object get(int paramInt1, int paramInt2) {
return this._datmatrix[paramInt1][paramInt2];
}

public boolean isEmpty(int paramInt1, int paramInt2) {
return this._datmatrix[paramInt1][paramInt2] == null;
}

public boolean equals(Object paramObject) {
boolean bool = true;
if ((paramObject instanceof Matrix)) {
Matrix localMatrix = (Matrix)paramObject;
if (localMatrix.size() == size()) {
for (int i = 0; i < size(); i++) {
for (int j = 0; j < size(); j++) {
if (!localMatrix.get(i, j).equals(get(i, j))) {
bool = false;
break;
}
}
if (!bool)
break;
}
}
else
bool = false;
}
else
{
bool = false;
}
return bool;
}

public Object set(int paramInt1, int paramInt2, Object paramObject) {
Object localObject = this._datmatrix[paramInt1][paramInt2];
this._datmatrix[paramInt1][paramInt2] = paramObject;
return localObject;
}

public void transpose() {
int i = 0;
for (int j = 0; j < size(); j++) {
for (int k = i; k < size(); k++) {
set(j, k, set(k, j, get(j, k)));
}
i++;
}
}

public static void swapRows(int paramInt1, int paramInt2, Object[][] paramArrayOfObject) {
for (int i = 0; i < paramArrayOfObject[paramInt1].length; i++) {
Object localObject = paramArrayOfObject[paramInt1][i];
paramArrayOfObject[paramInt1][i] = paramArrayOfObject[paramInt2][i];
paramArrayOfObject[paramInt2][i] = localObject;
}
}

public static void swapCols(int paramInt1, int paramInt2, Object[][] paramArrayOfObject) {
for (int i = 0; i < paramArrayOfObject.length; i++) {
Object localObject = paramArrayOfObject[i][paramInt1];
paramArrayOfObject[i][paramInt1] = paramArrayOfObject[i][paramInt2];
paramArrayOfObject[i][paramInt2] = localObject;
}
}

public Object[] getRow(int paramInt) {
Object[] arrayOfObject = new Object[this._datmatrix[paramInt].length];
for (int i = 0; i < arrayOfObject.length; i++) {
arrayOfObject[i] = this._datmatrix[paramInt][i];
}
return arrayOfObject;
}

public Object[] getCol(int paramInt) {
Object[] arrayOfObject = new Object[this._datmatrix[paramInt].length];
for (int i = 0; i < arrayOfObject.length; i++) {
arrayOfObject[i] = this._datmatrix[i][paramInt];
}
return arrayOfObject;
}

public Object[] setRow(int paramInt, Object[] paramArrayOfObject) {
Object[] arrayOfObject = getRow(paramInt);

for (int i = 0; i < size(); i++) {
set(paramInt, i, paramArrayOfObject[i]);
}

return arrayOfObject;
}

public Object[] setCol(int paramInt, Object[] paramArrayOfObject) {
Object[] arrayOfObject = getCol(paramInt);

for (int i = 0; i < size(); i++) {
set(i, paramInt, paramArrayOfObject[i]);
}

return arrayOfObject;
}

public String toString()
{
String str1 = "";
for (int i = 0; i < this._datmatrix.length; i++) {
if (i < 9)
str1 = str1 + (i + 1) + ": ";
else
str1 = str1 + (i + 1) + ":";
for (int j = 0; j < this._datmatrix[i].length; j++) {
int k = (this._datmatrix[i][j] + "").length();
String str2 = " ".substring(k);
str1 = str1 + this._datmatrix[i][j] + str2;
}
str1 = str1 + "\n";
}
return str1;
}

public static void print(Object[][] paramArrayOfObject) {
for (int i = 0; i < paramArrayOfObject.length; i++) {
for (int j = 0; j < paramArrayOfObject[i].length; j++) {
int k = (paramArrayOfObject[i][j] + "").length();
String str = " ".substring(k);
System.out.print(paramArrayOfObject[i][j] + str);
}
System.out.print("\n");
}
}

public static void printArray(Object[] paramArrayOfObject) {
for (int i = 0; i < paramArrayOfObject.length; i++) {
int j = (paramArrayOfObject[i] + "").length();
String str = " ".substring(j);
System.out.print(paramArrayOfObject[i] + str);
}
System.out.print("\n");
}

public static void main(String[] paramArrayOfString) {
Matrix localMatrix1 = new Matrix(5);
Matrix localMatrix2 = new Matrix(5);
for (int i = 0; i < localMatrix1.size(); i++) {
for (int j = 0; j < localMatrix1.size(); j++) {
Integer localInteger1 = new Integer((int)(Math.random() * 20.0D));
localMatrix1.set(i, j, localInteger1);
localMatrix2.set(i, j, localInteger1);
}
}

System.out.println("\nDemonstrating equals method (should be true)\t" + localMatrix2.equals(localMatrix1) + "\n");

System.out.println("Demonstrating get method\n" + localMatrix1.get(0, 0) + "\n");
System.out.println("Demonstrating is empty method\n" + localMatrix1.isEmpty(1, 0) + "\n");
System.out.println("Demonstrating size method \n" + localMatrix1.size() + "\n");
System.out.println("Demonstrating toString method\n" + localMatrix1 + "\n");
localMatrix1.transpose();
System.out.println("Blop has been transposed\n" + localMatrix1 + "\n");

Object[][] arrayOfObject = new Object[4][4];
for (int j = 0; j < arrayOfObject.length; j++) {
for (int k = 0; k < arrayOfObject[j].length; k++) {
Integer localInteger2 = new Integer((int)(Math.random() * 20.0D));
arrayOfObject[j][k] = localInteger2;
}
}
System.out.println("\n\n**Swapping Rows Demo**");
print(arrayOfObject);
System.out.println("\nRows 1 and 2 have been Swapped \n");
swapRows(1, 2, arrayOfObject);
print(arrayOfObject);

System.out.println("\n**Swapping Columns Demo**");
print(arrayOfObject);
System.out.println("\n\nColumns 1 and 2 have been Swapped \n");
swapCols(1, 2, arrayOfObject);
print(arrayOfObject);

System.out.println("\n**Getting rows demo (from blop)**");
System.out.println(localMatrix1);
System.out.println("\nGetting row 1\n");
printArray(localMatrix1.getRow(1));

System.out.println("\n**Getting cols demo (from blop)**");
System.out.println(localMatrix1);
System.out.println("\nGetting col 1\n");
printArray(localMatrix1.getCol(1));

System.out.println("\n**Demonstrating set row method**");
System.out.println(localMatrix1);
System.out.println("\nSwitching row 1 of blop to 1st column of blop\n");
localMatrix1.setRow(1, localMatrix1.getCol(1));
System.out.println(localMatrix1 + "\n");

System.out.println("\n**Demonstrating set col method**");
System.out.println(localMatrix1);
System.out.println("\nSwitching col 1 of blop to 2nd row of blop\n");
localMatrix1.setCol(1, localMatrix1.getRow(2));
System.out.println(localMatrix1 + "\n");
}
}


Ładowanie…
Anuluj
Zapisz