Browse Source

Fix errors in java parser, mostly casting issues. In Parse.java, set up a tcp server for communication with python processes. Builds with maven

tags/v1.0^2
Benjamin Attal 10 years ago
parent
commit
77e2b6f524
10 changed files with 258 additions and 58 deletions
  1. +3
    -0
      .gitignore
  2. +99
    -0
      parsers/java/pom.xml
  3. +40
    -0
      parsers/java/src/main/java/org/bitshift/parsing/Parse.java
  4. +0
    -4
      parsers/java/src/main/java/org/bitshift/parsing/parsers/CParser.java
  5. +59
    -22
      parsers/java/src/main/java/org/bitshift/parsing/parsers/JavaParser.java
  6. +3
    -4
      parsers/java/src/main/java/org/bitshift/parsing/parsers/Parser.java
  7. +0
    -0
      parsers/java/src/main/java/org/bitshift/parsing/symbols/CSymbols.java
  8. +54
    -22
      parsers/java/src/main/java/org/bitshift/parsing/symbols/JavaSymbols.java
  9. +0
    -0
      parsers/java/src/main/java/org/bitshift/parsing/symbols/Symbols.java
  10. +0
    -6
      parsers/java/src/org/bitshift/parsing/Parse.java

+ 3
- 0
.gitignore View File

@@ -40,3 +40,6 @@ nosetests.xml
.mr.developer.cfg
.project
.pydevproject

# Maven
*/target/*

+ 99
- 0
parsers/java/pom.xml View File

@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<prerequisites>
<maven>3.0.0</maven>
</prerequisites>

<groupId>org.bitshift.parsing</groupId>
<artifactId>parsing</artifactId>
<version>0.1</version>
<name>Java Parser for Bitshift</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>core</artifactId>
<version>3.3.0-v_771</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.6</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.bishift.parsing.Parse</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

+ 40
- 0
parsers/java/src/main/java/org/bitshift/parsing/Parse.java View File

@@ -0,0 +1,40 @@
import java.io.*;
import java.net.*;

public class Parse {

public static void main(String[][] args) {
String fromClient;
String toClient;

try {
ServerSocket server = new ServerSocket(5002);

while(true) {
Socket connected = server.accept();
System.out.println("The client is connected.");

BufferedReader clientReader = new BufferedReader(
new InputStreamReader(connected.getInputStream()));

PrintWriter clientWriter = new PrintWriter(
connected.getOutputStream(), true);

while(true) {
StringBuilder builder = new StringBuilder();
while((fromClient = clientReader.readLine()) != null) {
builder.append(fromClient);
}

fromClient = builder.toString();

//Handle the data from the client here
}
}
} catch (IOException ex) {
}
}

}

parsers/java/src/org/bitshift/parsing/parsers/CParser.java → parsers/java/src/main/java/org/bitshift/parsing/parsers/CParser.java View File

@@ -1,7 +1,3 @@
package org.bitshift.parsing.parsers;

import org.bitshift.parsing.parsers.Parser;

public class CParser extends Parser {

}

parsers/java/src/org/bitshift/parsing/parsers/JavaParser.java → parsers/java/src/main/java/org/bitshift/parsing/parsers/JavaParser.java View File

@@ -1,6 +1,9 @@
package org.bitshift.parsing.parsers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.AST;
@@ -8,23 +11,39 @@ 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.FieldAccess
import org.eclipse.jdt.core.dom.FieldAccess;
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;
import org.eclipse.jdt.core.dom.PackageDeclaration;
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.QualifiedType;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;

import org.bitshift.parsing.parsers.Parser;
import org.bitshift.parsing.symbols.Symbols;
import org.bitshift.parsing.symbols.JavaSymbols;

/*TODO: Work on parsing partial java code.
* Make sure all names of nodes are strings.*/
* Change visits to endVisit and implement a cache*/
public class JavaParser extends Parser {

protected JavaSymbols symbols;
protected CompilationUnit compUnit;
private Stack<Map<String, Object>> _cache;

public JavaParser(String source) {
super(source);
this.symbols = new JavaSymbols();
this._cache = new Stack<Map<String, Object>>();
}

@Override
public Symbols genSymbols() {
char[] source = this.source.toCharArray();
@@ -36,49 +55,54 @@ public class JavaParser extends Parser {
parser.setCompilerOptions(options);

//Work on parsing partial java code later
CompilationUnit result = (CompilationUnit) parser.createAST(null);
this.compUnit = (CompilationUnit) parser.createAST(null);

ASTVisitor visitor = new NodeVisitor(result);
result.accept(visitor);
ASTVisitor visitor = new NodeVisitor();
this.compUnit.accept(visitor);

return visitor.symbols;
return this.symbols;
}

class NodeVisitor extends ASTVisitor {

protected Symbols symbols;
protected CompilationUnit compUnit;

public NodeVisitor(CompilationUnit compUnit) {
symbols = new JavaSymbols();
}

public boolean visit(ClassInstanceCreation node) {
String name = node.getType().getName();
Type typeObj = node.getType();
Name nameObj = typeObj.isQualifiedType() ? ((QualifiedType) typeObj).getName() : ((SimpleType) typeObj).getName();
String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier();

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

symbols.insertClassInstance(name, sl, sc, null, null);
return true;
}

public boolean visit(FieldAccess node) {
String name = node.getName();
Name nameObj = node.getName();
String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier();

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

symbols.insertFieldAccess(name, sl, sc, null, null);
return true;
}

public boolean visit(FieldDeclaration node) {
String name = node.getType().getName();
Type typeObj = node.getType();
Name nameObj = typeObj.isQualifiedType() ? ((QualifiedType) typeObj).getName() : ((SimpleType) typeObj).getName();
String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier();

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

symbols.insertFieldDeclaration(name, sl, sc, null, null);
return true;
}

public boolean visit(MethodDeclaration node) {
String name = node.getName();
Name nameObj = node.getName();
String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier();
List<Statement> statements = node.getBody().statements();
Statement last = statements.get(statements.size() - 1);

@@ -86,27 +110,37 @@ public class JavaParser extends Parser {
int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1;
int el = compUnit.getLineNumber(last.getStartPosition()) - 1;
int ec = compUnit.getColumnNumber(last.getStartPosition()) - 1;

symbols.insertMethodDeclaration(name, sl, sc, el, ec);
return true;
}

public boolean visit(MethodInvocation node) {
String name = node.getName();
Name nameObj = node.getName();
String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier();

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

symbols.insertMethodInvocation(name, sl, sc, null, null);
return true;
}

public boolean visit(PackageDeclaration node) {
symbols.setPackage(node.getName());
Name nameObj = node.getName();
String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier();

symbols.setPackage(name);
return true;
}

public boolean visit(TypeDeclaration node) {
String name = node.getName();
Name nameObj = node.getName();
String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier();

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

if (node.isInterface()) {
symbols.insertInterfaceDeclaration(name, sl, sc, null, null);
} else {
@@ -116,7 +150,10 @@ public class JavaParser extends Parser {
}

public boolean visit(VariableDeclarationStatement node) {
String name = node.getType().getName();
Type typeObj = node.getType();
Name nameObj = typeObj.isQualifiedType() ? ((QualifiedType) typeObj).getName() : ((SimpleType) typeObj).getName();
String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier();

int sl = compUnit.getLineNumber(node.getStartPosition()) - 1;
int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1;
symbols.insertVariableDeclaration(name, sl, sc, null, null);

parsers/java/src/org/bitshift/parsing/parsers/Parser.java → parsers/java/src/main/java/org/bitshift/parsing/parsers/Parser.java View File

@@ -2,16 +2,15 @@ package org.bitshift.parsing.parsers;

import org.bitshift.parsing.symbols.Symbols;

public class Parser {
public abstract class Parser {

protected String source;

public Parser(String source) {
this.source = source;
}

public Symbols genSymbols() {
abstract Symbols genSymbols();
}
}


parsers/java/src/org/bitshift/parsing/symbols/CSymbols.java → parsers/java/src/main/java/org/bitshift/parsing/symbols/CSymbols.java View File


parsers/java/src/org/bitshift/parsing/symbols/JavaSymbols.java → parsers/java/src/main/java/org/bitshift/parsing/symbols/JavaSymbols.java View File

@@ -6,33 +6,38 @@ import java.util.HashMap;
import java.util.ArrayList;
import org.bitshift.parsing.symbols.Symbols;

/*TODO: Overwrite toString*/
/*TODO: Overwrite toString
* Change instance vars to HashMaps of HashMaps*/
public class JavaSymbols extends Symbols {

private String _packageName;
private Map<String, List<List<Integer>>> _classes;
private Map<String, List<List<Integer>>> _interfaces;
private Map<String, List<List<Integer>>> _methods;
private Map<String, List<List<Integer>>> _fields;
private Map<String, List<List<Integer>>> _vars;
private Map<String, Object> _classes;
private Map<String, Object> _interfaces;
private Map<String, Object> _methods;
private Map<String, Object> _fields;
private Map<String, Object> _vars;

public JavaSymbols() {
_packageName = null;
_classes = new HashMap<String, ArrayList<ArrayList<Integer>>>();
_interfaces = new HashMap<String, ArrayList<ArrayList<Integer>>>();
_methods = new HashMap<String, ArrayList<ArrayList<Integer>>>();
_fields = new HashMap<String, ArrayList<ArrayList<Integer>>>();
_vars = new HashMap<String, ArrayList<ArrayList<Integer>>>();
_classes = new HashMap<String, Object>();
_interfaces = new HashMap<String, Object>();
_methods = new HashMap<String, Object>();
_fields = new HashMap<String, Object>();
_vars = new HashMap<String, Object>();
}

public boolean setPackage(String name) {
_packageName = name;
return true;
}

public boolean insertClassDeclaration(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) {
List<Integer> pos = new ArrayList<Integer>(4);
pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol);
List<List<Integer>> copy = _classes.get(name);
List<List<Integer>> copy = (List<List<Integer>>)_classes.get(name);
copy = (copy == null) ? new ArrayList<List<Integer>>() : copy;

copy.add(0, pos);
_classes.put(name, copy);
return true;
@@ -40,7 +45,10 @@ public class JavaSymbols extends Symbols {
public boolean insertClassInstance(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) {
List<Integer> pos = new ArrayList<Integer>(4);
pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol);
List<List<Integer>> copy = _classes.get(name);
List<List<Integer>> copy = (List<List<Integer>>)_classes.get(name);
copy = (copy == null) ? new ArrayList<List<Integer>>() : copy;

copy.add(pos);
_classes.put(name, copy);
return true;
@@ -49,7 +57,10 @@ public class JavaSymbols extends Symbols {
public boolean insertInterfaceDeclaration(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) {
List<Integer> pos = new ArrayList<Integer>(4);
pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol);
List<List<Integer>> copy = _classes.get(name);
List<List<Integer>> copy = (List<List<Integer>>)_classes.get(name);
copy = (copy == null) ? new ArrayList<List<Integer>>() : copy;

copy.add(0, pos);
_classes.put(name, copy);
return true;
@@ -57,7 +68,10 @@ public class JavaSymbols extends Symbols {
public boolean insertInterfaceInstance(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) {
List<Integer> pos = new ArrayList<Integer>(4);
pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol);
List<List<Integer>> copy = _classes.get(name);
List<List<Integer>> copy = (List<List<Integer>>)_classes.get(name);
copy = (copy == null) ? new ArrayList<List<Integer>>() : copy;

copy.add(pos);
_classes.put(name, copy);
return true;
@@ -66,7 +80,10 @@ public class JavaSymbols extends Symbols {
public boolean insertMethodDeclaration(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) {
List<Integer> pos = new ArrayList<Integer>(4);
pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol);
List<List<Integer>> copy = _classes.get(name);
List<List<Integer>> copy = (List<List<Integer>>)_classes.get(name);
copy = (copy == null) ? new ArrayList<List<Integer>>() : copy;

copy.add(0, pos);
_classes.put(name, copy);
return true;
@@ -74,7 +91,10 @@ public class JavaSymbols extends Symbols {
public boolean insertMethodInvocation(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) {
List<Integer> pos = new ArrayList<Integer>(4);
pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol);
List<List<Integer>> copy = _classes.get(name);
List<List<Integer>> copy = (List<List<Integer>>)_classes.get(name);
copy = (copy == null) ? new ArrayList<List<Integer>>() : copy;

copy.add(pos);
_classes.put(name, copy);
return true;
@@ -83,7 +103,10 @@ public class JavaSymbols extends Symbols {
public boolean insertFieldDeclaration(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) {
List<Integer> pos = new ArrayList<Integer>(4);
pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol);
List<List<Integer>> copy = _classes.get(name);
List<List<Integer>> copy = (List<List<Integer>>)_classes.get(name);
copy = (copy == null) ? new ArrayList<List<Integer>>() : copy;

copy.add(0, pos);
_classes.put(name, copy);
return true;
@@ -91,7 +114,10 @@ public class JavaSymbols extends Symbols {
public boolean insertFieldAccess(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) {
List<Integer> pos = new ArrayList<Integer>(4);
pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol);
List<List<Integer>> copy = _classes.get(name);
List<List<Integer>> copy = (List<List<Integer>>)_classes.get(name);
copy = (copy == null) ? new ArrayList<List<Integer>>() : copy;

copy.add(pos);
_classes.put(name, copy);
return true;
@@ -100,7 +126,10 @@ public class JavaSymbols extends Symbols {
public boolean insertVariableDeclaration(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) {
List<Integer> pos = new ArrayList<Integer>(4);
pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol);
List<List<Integer>> copy = _classes.get(name);
List<List<Integer>> copy = (List<List<Integer>>)_classes.get(name);
copy = (copy == null) ? new ArrayList<List<Integer>>() : copy;

copy.add(0, pos);
_classes.put(name, copy);
return true;
@@ -108,14 +137,17 @@ public class JavaSymbols extends Symbols {
public boolean insertVariableAccess(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) {
List<Integer> pos = new ArrayList<Integer>(4);
pos.set(0, startLine); pos.set(1, startCol); pos.set(2, endLine); pos.set(3, endCol);
List<List<Integer>> copy = _classes.get(name);
List<List<Integer>> copy = (List<List<Integer>>)_classes.get(name);
copy = (copy == null) ? new ArrayList<List<Integer>>() : copy;

copy.add(pos);
_classes.put(name, copy);
return true;
}

public String toString() {
return "";
}
}


parsers/java/src/org/bitshift/parsing/symbols/Symbols.java → parsers/java/src/main/java/org/bitshift/parsing/symbols/Symbols.java View File


+ 0
- 6
parsers/java/src/org/bitshift/parsing/Parse.java View File

@@ -1,6 +0,0 @@

/*TODO: Create main method which will loop and check for updates to a file.
* If that file is updated, parse the input and print it out.*/
public class Parse {

}

Loading…
Cancel
Save