Browse Source

Add parsing for import statements in java.

tags/v1.0^2
Benjamin Attal 10 years ago
parent
commit
54a3485837
4 changed files with 44 additions and 0 deletions
  1. +1
    -0
      bitshift/parser/python.py
  2. +14
    -0
      parsers/java/src/main/java/com/bitshift/parsing/parsers/JavaParser.java
  3. +25
    -0
      parsers/java/src/main/java/com/bitshift/parsing/symbols/JavaSymbols.java
  4. +4
    -0
      parsers/ruby/lib/parser.rb

+ 1
- 0
bitshift/parser/python.py View File

@@ -178,6 +178,7 @@ class _TreeWalker(ast.NodeVisitor):


def visit_Import(self, node): def visit_Import(self, node):
pos = self.block_position(node) pos = self.block_position(node)
# look through aliases


def parse_py(codelet): def parse_py(codelet):
""" """


+ 14
- 0
parsers/java/src/main/java/com/bitshift/parsing/parsers/JavaParser.java View File

@@ -15,6 +15,7 @@ import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor; import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ClassInstanceCreation; import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.MethodDeclaration; import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation; import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Name; import org.eclipse.jdt.core.dom.Name;
@@ -192,5 +193,18 @@ public class JavaParser extends Parser {
return true; return true;
} }


public boolean visit(ImportDeclaration node) {
HashMap<String, Object> data = new HashMap<String, Object>();

data.put("coord", this.blockPosition(node));
this._cache.push(data);
return true;
}

public void endVisit(ImportDeclaration node) {
HashMap<String, Object> data = this._cache.pop();
String name = (String)data.remove("name");
this.symbols.insertImportStatement("\"" + name + "\"", data);
}
} }
} }

+ 25
- 0
parsers/java/src/main/java/com/bitshift/parsing/symbols/JavaSymbols.java View File

@@ -12,6 +12,7 @@ public class JavaSymbols extends Symbols {
private HashMap<String, HashMap<String, Object>> _interfaces; private HashMap<String, HashMap<String, Object>> _interfaces;
private HashMap<String, HashMap<String, Object>> _methods; private HashMap<String, HashMap<String, Object>> _methods;
private HashMap<String, HashMap<String, Object>> _vars; private HashMap<String, HashMap<String, Object>> _vars;
private HashMap<String, HashMap<String, Object>> _imports;


private final String assignKey = "\"assignments\""; private final String assignKey = "\"assignments\"";
private final String useKey = "\"uses\""; private final String useKey = "\"uses\"";
@@ -22,6 +23,7 @@ public class JavaSymbols extends Symbols {
_interfaces = new HashMap<String, HashMap<String, Object>>(); _interfaces = new HashMap<String, HashMap<String, Object>>();
_methods = new HashMap<String, HashMap<String, Object>>(); _methods = new HashMap<String, HashMap<String, Object>>();
_vars = new HashMap<String, HashMap<String, Object>>(); _vars = new HashMap<String, HashMap<String, Object>>();
_imports = new HashMap<String, HashMap<String, Object>>();
} }


public boolean setPackage(String name) { public boolean setPackage(String name) {
@@ -116,6 +118,7 @@ public class JavaSymbols extends Symbols {
this._vars.put(name, var); this._vars.put(name, var);
return true; return true;
} }

public boolean insertVariableAccess(String name, HashMap<String, Object> data) { public boolean insertVariableAccess(String name, HashMap<String, Object> data) {
HashMap<String, Object> var = this._vars.get(name); HashMap<String, Object> var = this._vars.get(name);
if (var == null) { if (var == null) {
@@ -137,12 +140,34 @@ public class JavaSymbols extends Symbols {
return true; return true;
} }


public boolean insertImportStatement(String name, HashMap<String, Object> data) {
HashMap<String, Object> lib = this._imports.get(name);
if (lib == null) {
lib = new HashMap<String, Object>();
ArrayList<Object> assignments = new ArrayList<Object>(10);
ArrayList<Object> uses = new ArrayList<Object>(10);

uses.add(data.get("coord"));
lib.put(assignKey, assignments);
lib.put(useKey, uses);
} else {
ArrayList<Object> uses = (ArrayList<Object>)lib.get(useKey);

uses.add(data.get("coord"));
lib.put(useKey, uses);
}

this._imports.put(name, lib);
return true;
}

public String toString() { public String toString() {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.append("\"classes\":" + this._classes + ","); builder.append("\"classes\":" + this._classes + ",");
builder.append("\"interfaces\":" + this._interfaces + ","); builder.append("\"interfaces\":" + this._interfaces + ",");
builder.append("\"functions\":" + this._methods + ","); builder.append("\"functions\":" + this._methods + ",");
builder.append("\"vars\":" + this._vars + ","); builder.append("\"vars\":" + this._vars + ",");
builder.append("\"imports\":" + this._imports + ",");


String s = builder.toString().replaceAll("=", ":"); String s = builder.toString().replaceAll("=", ":");
s = s.substring(0, s.length() - 1); s = s.substring(0, s.length() - 1);


+ 4
- 0
parsers/ruby/lib/parser.rb View File

@@ -123,6 +123,10 @@ module Bitshift
return node return node
end end


def on_command(*node)
# catch require statements
end

def to_s def to_s
new_symbols = Hash.new {|hash, key| hash[key] = Hash.new} new_symbols = Hash.new {|hash, key| hash[key] = Hash.new}




Loading…
Cancel
Save