From 2338887a52c419c98c7f87ce01d80189e003d12f Mon Sep 17 00:00:00 2001 From: Benjamin Attal Date: Sun, 20 Apr 2014 02:16:06 -0400 Subject: [PATCH] Working version of java parser up and running. --- .gitignore | 3 +- .../com/bitshift/parsing/parsers/JavaParser.java | 29 ++- .../com/bitshift/parsing/symbols/JavaSymbols.java | 16 +- test/java_parser_test.py | 18 ++ test/resources/Matrix.java | 218 +++++++++++++++++++++ 5 files changed, 259 insertions(+), 25 deletions(-) create mode 100644 test/java_parser_test.py create mode 100644 test/resources/Matrix.java diff --git a/.gitignore b/.gitignore index 8b72a6d..26d8b64 100644 --- a/.gitignore +++ b/.gitignore @@ -42,4 +42,5 @@ nosetests.xml .pydevproject # Maven -*/target/* +parsers/java/target/* +*/tags 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 c6d9b2a..be5fc37 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 @@ -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 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; } 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 09d32b4..ef8efb5 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 @@ -57,7 +57,7 @@ public class JavaSymbols extends Symbols { List pos = new ArrayList(4); pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); - List> copy = (List>)_classes.get(name); + List> copy = (List>)_interfaces.get(name); copy = (copy == null) ? new ArrayList>() : copy; copy.add(0, pos); @@ -68,7 +68,7 @@ public class JavaSymbols extends Symbols { List pos = new ArrayList(4); pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); - List> copy = (List>)_classes.get(name); + List> copy = (List>)_interfaces.get(name); copy = (copy == null) ? new ArrayList>() : copy; copy.add(pos); @@ -80,7 +80,7 @@ public class JavaSymbols extends Symbols { List pos = new ArrayList(4); pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); - List> copy = (List>)_classes.get(name); + List> copy = (List>)_methods.get(name); copy = (copy == null) ? new ArrayList>() : copy; copy.add(0, pos); @@ -91,7 +91,7 @@ public class JavaSymbols extends Symbols { List pos = new ArrayList(4); pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); - List> copy = (List>)_classes.get(name); + List> copy = (List>)_methods.get(name); copy = (copy == null) ? new ArrayList>() : copy; copy.add(pos); @@ -103,7 +103,7 @@ public class JavaSymbols extends Symbols { List pos = new ArrayList(4); pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); - List> copy = (List>)_classes.get(name); + List> copy = (List>)_fields.get(name); copy = (copy == null) ? new ArrayList>() : copy; copy.add(0, pos); @@ -114,7 +114,7 @@ public class JavaSymbols extends Symbols { List pos = new ArrayList(4); pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); - List> copy = (List>)_classes.get(name); + List> copy = (List>)_fields.get(name); copy = (copy == null) ? new ArrayList>() : copy; copy.add(pos); @@ -126,7 +126,7 @@ public class JavaSymbols extends Symbols { List pos = new ArrayList(4); pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); - List> copy = (List>)_classes.get(name); + List> copy = (List>)_vars.get(name); copy = (copy == null) ? new ArrayList>() : copy; copy.add(0, pos); @@ -137,7 +137,7 @@ public class JavaSymbols extends Symbols { List pos = new ArrayList(4); pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); - List> copy = (List>)_classes.get(name); + List> copy = (List>)_vars.get(name); copy = (copy == null) ? new ArrayList>() : copy; copy.add(pos); diff --git a/test/java_parser_test.py b/test/java_parser_test.py new file mode 100644 index 0000000..7f87f2c --- /dev/null +++ b/test/java_parser_test.py @@ -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; diff --git a/test/resources/Matrix.java b/test/resources/Matrix.java new file mode 100644 index 0000000..5d641e0 --- /dev/null +++ b/test/resources/Matrix.java @@ -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"); + } +} +