Browse Source

Changed directory structure of java parser. Decided on multiple parsers in different languages, refactored bitshift/parser to fit with that paradigm.

tags/v1.0^2
Benjamin Attal 10 years ago
parent
commit
63b09caa6c
14 changed files with 168 additions and 19 deletions
  1. +1
    -1
      bitshift/languages.py
  2. +37
    -18
      bitshift/parser/__init__.py
  3. +5
    -0
      parsers/java/src/org/bitshift/parsing/Parse.java
  4. +7
    -0
      parsers/java/src/org/bitshift/parsing/parsers/CParser.java
  5. +90
    -0
      parsers/java/src/org/bitshift/parsing/parsers/JavaParser.java
  6. +17
    -0
      parsers/java/src/org/bitshift/parsing/parsers/Parser.java
  7. +1
    -0
      parsers/java/src/org/bitshift/parsing/symbols/CSymbols.java
  8. +1
    -0
      parsers/java/src/org/bitshift/parsing/symbols/JavaSymbols.java
  9. +9
    -0
      parsers/java/src/org/bitshift/parsing/symbols/Symbols.java
  10. +0
    -0
     
  11. +0
    -0
     
  12. +0
    -0
     
  13. +0
    -0
     
  14. +0
    -0
     

+ 1
- 1
bitshift/languages.py View File

@@ -1,2 +1,2 @@

LANGS = ["Python", "C"]
LANGS = ["Python", "C", "Java", "Ruby"]

+ 37
- 18
bitshift/parser/__init__.py View File

@@ -1,8 +1,26 @@
import os, ast
import pygments.lexers as pgl
from ..languages import LANGS
from .python import parse_py

_all__ = ["parse"]

WRITE_F = "../../tmp/parser.proc"
def _lang(codelet):
"""
Private function to identify the language of a codelet.

:param codelet: The codelet object to identified.

:type code: Codelet

.. todo::
Modify function to incorporate tags from stackoverflow.
"""

if codelet.filename is not None:
return pgl.guess_lexer_for_filename(codelet.filename).name

return LANGS.index(pgl.guess_lexer(codelet.code))

def parse(codelet, pid):
"""
@@ -16,28 +34,29 @@ def parse(codelet, pid):
:param pid: str.

.. todo::
Create a named pipe for python process to communicate with Java
process.
Identify languages using pygments and change the write file based on
that.
"""

Send the process id and codelet code through the named pipe.
codelet.language = _lang(codelet)

Read the result from the named pipe and turn it into a dict.
"""
if codelet.language == LANGS.index("Python"):
parse_py(codelet)

with open(WRITE_F, 'a') as wf:
wf.write('pid:' + str(pid) + '\n')
wf.write('body:\n' + codelet.code)
else:
write_f = "../../tmp/%d_parser.proc" % codelet.language

read_f = '../../tmp/%s_py.data' % str(pid)
data = ''
with open(write_f, 'a') as wf:
wf.write('pid:' + str(pid) + '\n')
wf.write('body:\n' + codelet.code)

while data == '':
with open(read_f) as rf:
data = rf.read()
read_f = '../../tmp/%s_py.data' % str(pid)
data = ''

os.remove(read_f)
while data == '':
with open(read_f) as rf:
data = rf.read()

results = data.split('\n')
codelet.language = results[0].split(',')[1]
codelet.symbols = ast.literal_eval(results[1].split(',')[1])
os.remove(read_f)
codelet.symbols = ast.literal_eval(data.split(',')[1])


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

@@ -0,0 +1,5 @@


public class Parse {

}

+ 7
- 0
parsers/java/src/org/bitshift/parsing/parsers/CParser.java View File

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

import org.bitshift.parsing.parsers.Parser;

public class CParser extends Parser {

}

+ 90
- 0
parsers/java/src/org/bitshift/parsing/parsers/JavaParser.java View File

@@ -0,0 +1,90 @@
package org.bitshift.parsing.parsers;

import java.util.Map;

import org.eclipse.jdt.core.JavaCore;
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.TypeDeclaration;
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;

public class JavaParser extends Parser {

@Override
public Symbols genSymbols() {
char[] source = this.source.toCharArray();

ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(source);

Map options = JavaCore.getOptions();
parser.setCompilerOptions(options);

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

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

return visitor.symbols;
}

class NodeVisitor extends ASTVisitor {

protected Symbols symbols;

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

public boolean visit(Assignment node) {
}

public boolean visit(ClassInstanceCreation node) {
}

public boolean visit(FieldAccess node) {
}

public boolean visit(FieldDeclaration node) {
}

public boolean visit(MethodDeclaration node) {
}

public boolean visit(MethodInvocation node) {
}

public boolean visit(PackageDeclaration node) {
}

public boolean visit(TypeDeclaration node) {
}

public boolean visit(VariableDeclarationStatement node) {
}

}
}

+ 17
- 0
parsers/java/src/org/bitshift/parsing/parsers/Parser.java View File

@@ -0,0 +1,17 @@
package org.bitshift.parsing.parsers;

import org.bitshift.parsing.symbols.Symbols;

public class Parser {

protected String source;

public Parser(String source) {
}

public Symbols genSymbols() {
}
}


+ 1
- 0
parsers/java/src/org/bitshift/parsing/symbols/CSymbols.java View File

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

+ 1
- 0
parsers/java/src/org/bitshift/parsing/symbols/JavaSymbols.java View File

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

+ 9
- 0
parsers/java/src/org/bitshift/parsing/symbols/Symbols.java View File

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

public class Symbols {

public Symbols() {
}

}

+ 0
- 0
View File


+ 0
- 0
View File


+ 0
- 0
View File


+ 0
- 0
View File


+ 0
- 0
View File


Loading…
Cancel
Save