瀏覽代碼

Add parsing for import statements in java.

tags/v1.0^2
Benjamin Attal 10 年之前
父節點
當前提交
54a3485837
共有 4 個文件被更改,包括 44 次插入0 次删除
  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 查看文件

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

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

def parse_py(codelet):
"""


+ 14
- 0
parsers/java/src/main/java/com/bitshift/parsing/parsers/JavaParser.java 查看文件

@@ -15,6 +15,7 @@ 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.ImportDeclaration;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Name;
@@ -192,5 +193,18 @@ public class JavaParser extends Parser {
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 查看文件

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

private final String assignKey = "\"assignments\"";
private final String useKey = "\"uses\"";
@@ -22,6 +23,7 @@ public class JavaSymbols extends Symbols {
_interfaces = new HashMap<String, HashMap<String, Object>>();
_methods = 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) {
@@ -116,6 +118,7 @@ public class JavaSymbols extends Symbols {
this._vars.put(name, var);
return true;
}

public boolean insertVariableAccess(String name, HashMap<String, Object> data) {
HashMap<String, Object> var = this._vars.get(name);
if (var == null) {
@@ -137,12 +140,34 @@ public class JavaSymbols extends Symbols {
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() {
StringBuilder builder = new StringBuilder();
builder.append("\"classes\":" + this._classes + ",");
builder.append("\"interfaces\":" + this._interfaces + ",");
builder.append("\"functions\":" + this._methods + ",");
builder.append("\"vars\":" + this._vars + ",");
builder.append("\"imports\":" + this._imports + ",");

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


+ 4
- 0
parsers/ruby/lib/parser.rb 查看文件

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

def on_command(*node)
# catch require statements
end

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



Loading…
取消
儲存