| @@ -1,2 +1,2 @@ | |||||
| LANGS = ["Python", "C"] | |||||
| LANGS = ["Python", "C", "Java", "Ruby"] | |||||
| @@ -1,8 +1,26 @@ | |||||
| import os, ast | import os, ast | ||||
| import pygments.lexers as pgl | |||||
| from ..languages import LANGS | |||||
| from .python import parse_py | |||||
| _all__ = ["parse"] | _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): | def parse(codelet, pid): | ||||
| """ | """ | ||||
| @@ -16,28 +34,29 @@ def parse(codelet, pid): | |||||
| :param pid: str. | :param pid: str. | ||||
| .. todo:: | .. 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]) | |||||
| @@ -0,0 +1,5 @@ | |||||
| public class Parse { | |||||
| } | |||||
| @@ -0,0 +1,7 @@ | |||||
| package org.bitshift.parsing.parsers; | |||||
| import org.bitshift.parsing.parsers.Parser; | |||||
| public class CParser extends Parser { | |||||
| } | |||||
| @@ -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) { | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -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() { | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1 @@ | |||||
| package org.bitshift.parsing.symbols; | |||||
| @@ -0,0 +1 @@ | |||||
| package org.bitshift.parsing.symbols; | |||||
| @@ -0,0 +1,9 @@ | |||||
| package org.bitshift.parsing.symbols; | |||||
| public class Symbols { | |||||
| public Symbols() { | |||||
| } | |||||
| } | |||||