Переглянути джерело

Mod:

Parse.java:
        Added comments

    JavaParser.java:
        Updated the genSymbols method and a private class 'NodeVisitor' which
        implements ASTVisitor.  genSymbols returns an instance of the
        Symbols class containing all relevant data about the Java code.

    JavaSymbols.java:
        Add fields which map class, interface, method, field, and
        variable names to positions.
tags/v1.0^2
Benjamin Attal 10 роки тому
джерело
коміт
669c30cac7
4 змінених файлів з 174 додано та 17 видалено
  1. +0
    -1
      bitshift/parser/pylj
  2. +2
    -1
      parsers/java/src/org/bitshift/parsing/Parse.java
  3. +52
    -15
      parsers/java/src/org/bitshift/parsing/parsers/JavaParser.java
  4. +120
    -0
      parsers/java/src/org/bitshift/parsing/symbols/JavaSymbols.java

+ 0
- 1
bitshift/parser/pylj

@@ -1 +0,0 @@
Subproject commit 323dd4e266b47579aef2347e68214a6fbe083add

+ 2
- 1
parsers/java/src/org/bitshift/parsing/Parse.java Переглянути файл

@@ -1,5 +1,6 @@


/*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 {

}

+ 52
- 15
parsers/java/src/org/bitshift/parsing/parsers/JavaParser.java Переглянути файл

@@ -7,13 +7,13 @@ import org.eclipse.jdt.core.dom.AST;
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.Assignment;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
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.PackageDeclaration;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement

@@ -21,6 +21,8 @@ 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.*/
public class JavaParser extends Parser {

@Override
@@ -36,7 +38,7 @@ public class JavaParser extends Parser {
//Work on parsing partial java code later
CompilationUnit result = (CompilationUnit) parser.createAST(null);

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

return visitor.symbols;
@@ -45,45 +47,80 @@ public class JavaParser extends Parser {
class NodeVisitor extends ASTVisitor {

protected Symbols symbols;
protected CompilationUnit compUnit;

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

public boolean visit(Assignment node) {
}

public boolean visit(ClassInstanceCreation node) {
String name = node.getType().getName();
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();
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();
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();
List<Statement> statements = node.getBody().statements();
Statement last = statements.get(statements.size() - 1);

int sl = compUnit.getLineNumber(node.getStartPosition()) - 1;
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();
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());
return true;
}

public boolean visit(TypeDeclaration node) {
String name = node.getName();
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 {
symbols.insertClassDeclaration(name, sl, sc, null, null);
}
return true;
}

public boolean visit(VariableDeclarationStatement node) {
String name = node.getType().getName();
int sl = compUnit.getLineNumber(node.getStartPosition()) - 1;
int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1;
symbols.insertVariableDeclaration(name, sl, sc, null, null);
return true;
}

}


+ 120
- 0
parsers/java/src/org/bitshift/parsing/symbols/JavaSymbols.java Переглянути файл

@@ -1 +1,121 @@
package org.bitshift.parsing.symbols;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import org.bitshift.parsing.symbols.Symbols;

/*TODO: Overwrite toString*/
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;

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>>>();
}

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

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);
copy.add(0, pos);
_classes.put(name, copy);
return true;
}
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);
copy.add(pos);
_classes.put(name, copy);
return true;
}

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);
copy.add(0, pos);
_classes.put(name, copy);
return true;
}
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);
copy.add(pos);
_classes.put(name, copy);
return true;
}

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);
copy.add(0, pos);
_classes.put(name, copy);
return true;
}
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);
copy.add(pos);
_classes.put(name, copy);
return true;
}

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);
copy.add(0, pos);
_classes.put(name, copy);
return true;
}
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);
copy.add(pos);
_classes.put(name, copy);
return true;
}

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);
copy.add(0, pos);
_classes.put(name, copy);
return true;
}
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);
copy.add(pos);
_classes.put(name, copy);
return true;
}

public String toString() {

}
}


Завантаження…
Відмінити
Зберегти