From 63b09caa6cb3d1f9bc3dbddf49b9292ec619f2b6 Mon Sep 17 00:00:00 2001 From: Benjamin Attal Date: Fri, 18 Apr 2014 19:40:31 -0400 Subject: [PATCH] Changed directory structure of java parser. Decided on multiple parsers in different languages, refactored bitshift/parser to fit with that paradigm. --- bitshift/languages.py | 2 +- bitshift/parser/__init__.py | 55 ++++++++----- parsers/java/src/org/bitshift/parsing/Parse.java | 5 ++ .../src/org/bitshift/parsing/parsers/CParser.java | 7 ++ .../org/bitshift/parsing/parsers/JavaParser.java | 90 ++++++++++++++++++++++ .../src/org/bitshift/parsing/parsers/Parser.java | 17 ++++ .../src/org/bitshift/parsing/symbols/CSymbols.java | 1 + .../org/bitshift/parsing/symbols/JavaSymbols.java | 1 + .../src/org/bitshift/parsing/symbols/Symbols.java | 9 +++ parsers/src/org/bitshift/parsers/Parse.java | 0 .../src/org/bitshift/parsers/langs/CParser.java | 0 .../src/org/bitshift/parsers/langs/JavaParser.java | 0 .../org/bitshift/parsers/langs/PythonParser.java | 0 .../src/org/bitshift/parsers/langs/RubyParser.java | 0 14 files changed, 168 insertions(+), 19 deletions(-) create mode 100644 parsers/java/src/org/bitshift/parsing/Parse.java create mode 100644 parsers/java/src/org/bitshift/parsing/parsers/CParser.java create mode 100644 parsers/java/src/org/bitshift/parsing/parsers/JavaParser.java create mode 100644 parsers/java/src/org/bitshift/parsing/parsers/Parser.java create mode 100644 parsers/java/src/org/bitshift/parsing/symbols/CSymbols.java create mode 100644 parsers/java/src/org/bitshift/parsing/symbols/JavaSymbols.java create mode 100644 parsers/java/src/org/bitshift/parsing/symbols/Symbols.java delete mode 100644 parsers/src/org/bitshift/parsers/Parse.java delete mode 100644 parsers/src/org/bitshift/parsers/langs/CParser.java delete mode 100644 parsers/src/org/bitshift/parsers/langs/JavaParser.java delete mode 100644 parsers/src/org/bitshift/parsers/langs/PythonParser.java delete mode 100644 parsers/src/org/bitshift/parsers/langs/RubyParser.java diff --git a/bitshift/languages.py b/bitshift/languages.py index 6775711..b04c094 100644 --- a/bitshift/languages.py +++ b/bitshift/languages.py @@ -1,2 +1,2 @@ -LANGS = ["Python", "C"] +LANGS = ["Python", "C", "Java", "Ruby"] diff --git a/bitshift/parser/__init__.py b/bitshift/parser/__init__.py index 0fe4602..6f2f898 100644 --- a/bitshift/parser/__init__.py +++ b/bitshift/parser/__init__.py @@ -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]) diff --git a/parsers/java/src/org/bitshift/parsing/Parse.java b/parsers/java/src/org/bitshift/parsing/Parse.java new file mode 100644 index 0000000..cad9434 --- /dev/null +++ b/parsers/java/src/org/bitshift/parsing/Parse.java @@ -0,0 +1,5 @@ + + +public class Parse { + +} diff --git a/parsers/java/src/org/bitshift/parsing/parsers/CParser.java b/parsers/java/src/org/bitshift/parsing/parsers/CParser.java new file mode 100644 index 0000000..247f9ee --- /dev/null +++ b/parsers/java/src/org/bitshift/parsing/parsers/CParser.java @@ -0,0 +1,7 @@ +package org.bitshift.parsing.parsers; + +import org.bitshift.parsing.parsers.Parser; + +public class CParser extends Parser { + +} diff --git a/parsers/java/src/org/bitshift/parsing/parsers/JavaParser.java b/parsers/java/src/org/bitshift/parsing/parsers/JavaParser.java new file mode 100644 index 0000000..e33c8e4 --- /dev/null +++ b/parsers/java/src/org/bitshift/parsing/parsers/JavaParser.java @@ -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) { + + } + + } +} diff --git a/parsers/java/src/org/bitshift/parsing/parsers/Parser.java b/parsers/java/src/org/bitshift/parsing/parsers/Parser.java new file mode 100644 index 0000000..ae74725 --- /dev/null +++ b/parsers/java/src/org/bitshift/parsing/parsers/Parser.java @@ -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() { + + } +} + diff --git a/parsers/java/src/org/bitshift/parsing/symbols/CSymbols.java b/parsers/java/src/org/bitshift/parsing/symbols/CSymbols.java new file mode 100644 index 0000000..f71667e --- /dev/null +++ b/parsers/java/src/org/bitshift/parsing/symbols/CSymbols.java @@ -0,0 +1 @@ +package org.bitshift.parsing.symbols; diff --git a/parsers/java/src/org/bitshift/parsing/symbols/JavaSymbols.java b/parsers/java/src/org/bitshift/parsing/symbols/JavaSymbols.java new file mode 100644 index 0000000..f71667e --- /dev/null +++ b/parsers/java/src/org/bitshift/parsing/symbols/JavaSymbols.java @@ -0,0 +1 @@ +package org.bitshift.parsing.symbols; diff --git a/parsers/java/src/org/bitshift/parsing/symbols/Symbols.java b/parsers/java/src/org/bitshift/parsing/symbols/Symbols.java new file mode 100644 index 0000000..70762b1 --- /dev/null +++ b/parsers/java/src/org/bitshift/parsing/symbols/Symbols.java @@ -0,0 +1,9 @@ +package org.bitshift.parsing.symbols; + +public class Symbols { + + public Symbols() { + + } + +} diff --git a/parsers/src/org/bitshift/parsers/Parse.java b/parsers/src/org/bitshift/parsers/Parse.java deleted file mode 100644 index e69de29..0000000 diff --git a/parsers/src/org/bitshift/parsers/langs/CParser.java b/parsers/src/org/bitshift/parsers/langs/CParser.java deleted file mode 100644 index e69de29..0000000 diff --git a/parsers/src/org/bitshift/parsers/langs/JavaParser.java b/parsers/src/org/bitshift/parsers/langs/JavaParser.java deleted file mode 100644 index e69de29..0000000 diff --git a/parsers/src/org/bitshift/parsers/langs/PythonParser.java b/parsers/src/org/bitshift/parsers/langs/PythonParser.java deleted file mode 100644 index e69de29..0000000 diff --git a/parsers/src/org/bitshift/parsers/langs/RubyParser.java b/parsers/src/org/bitshift/parsers/langs/RubyParser.java deleted file mode 100644 index e69de29..0000000