diff --git a/LanguageServer/src/main/java/de/dhbw/compiler/languageServerInterface/LanguageServerInterface.java b/LanguageServer/src/main/java/de/dhbw/compiler/languageServerInterface/LanguageServerInterface.java index 1678c00..2abaacb 100644 --- a/LanguageServer/src/main/java/de/dhbw/compiler/languageServerInterface/LanguageServerInterface.java +++ b/LanguageServer/src/main/java/de/dhbw/compiler/languageServerInterface/LanguageServerInterface.java @@ -1,7 +1,14 @@ package de.dhbw.compiler.languageServerInterface; +import de.dhbw.compiler.syntaxtree.SourceFile; +import de.dhbw.compiler.syntaxtree.visual.ASTTypePrinter; import de.dhbw.compiler.typeinference.result.ResultSet; +import de.dhbw.compiler.core.JavaTXCompiler; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.util.List; /** @@ -12,14 +19,45 @@ public class LanguageServerInterface { /** * get final Result Set * */ - public List getResultSet(String input) { - return null; + public List getResultSet(String input) throws IOException, ClassNotFoundException { + File tempSourcefile = File.createTempFile("pattern", ".java"); + tempSourcefile.deleteOnExit(); + + BufferedWriter out = new BufferedWriter(new FileWriter(tempSourcefile)); + out.write(input); + out.close(); + + JavaTXCompiler tx = new JavaTXCompiler(tempSourcefile); + return tx.typeInference(tempSourcefile); } /** * get abstract Syntax - * */ - public List getAbstractSyntax(String input) { - return null; + */ + public SourceFile getAbstractSyntax(String input) throws IOException, ClassNotFoundException { + File tempSourcefile = File.createTempFile("pattern", ".java"); + tempSourcefile.deleteOnExit(); + + BufferedWriter out = new BufferedWriter(new FileWriter(tempSourcefile)); + out.write(input); + out.close(); + + JavaTXCompiler tx = new JavaTXCompiler(tempSourcefile); + return tx.sourceFiles.get(tempSourcefile); + } + + /** + * get abstract Syntax as String + */ + public String getAbstractSyntaxAsString(String input) throws IOException, ClassNotFoundException { + File tempSourcefile = File.createTempFile("pattern", ".java"); + tempSourcefile.deleteOnExit(); + + BufferedWriter out = new BufferedWriter(new FileWriter(tempSourcefile)); + out.write(input); + out.close(); + + JavaTXCompiler tx = new JavaTXCompiler(tempSourcefile); + return ASTTypePrinter.print(tx.sourceFiles.get(tempSourcefile)); } } diff --git a/LanguageServer/src/main/java/de/dhbw/helper/TypeFinder.java b/LanguageServer/src/main/java/de/dhbw/helper/TypeFinder.java index bd65fd1..2a3864f 100644 --- a/LanguageServer/src/main/java/de/dhbw/helper/TypeFinder.java +++ b/LanguageServer/src/main/java/de/dhbw/helper/TypeFinder.java @@ -1,6 +1,9 @@ package de.dhbw.helper; +import de.dhbw.compiler.languageServerInterface.LanguageServerInterface; + import java.awt.desktop.SystemSleepEvent; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Stack; @@ -13,6 +16,11 @@ import java.util.concurrent.atomic.AtomicReference; * */ public class TypeFinder { + LanguageServerInterface languageServer; + + public TypeFinder(){ + languageServer = new LanguageServerInterface(); + } /** * find the Type of a specific Word, given by its line and character. The character must be any Part of the Word. * @@ -22,61 +30,9 @@ public class TypeFinder { * @param character the character of the character or the char-Number of the String * @param currentTextDocument the String of the Content of the current Document. * */ - public String find(int line, int character, String currentTextDocument) { - - String[] validTypes = { - "int", "long", "short", "byte", "float", "double", "char", "boolean", - "String", "List", "Map", "Set", "Object", "void" - }; - - var stringlist = currentTextDocument.replace(";", " ").replace("{", " { ").replace("}", " } ").split("\n"); - StringBuilder interestingString = new StringBuilder(); - - - int counter = 0; - for(String stringLine : stringlist) { - if(counter < line) { - interestingString.append(stringLine); - } - else if(counter == line) { - interestingString.append(stringLine, 0, character); - } - else{ - break; - } - counter++; - } - - var interestingStringList = interestingString.toString().split(" "); - - Stack stack = new Stack<>(); - String search = interestingStringList[interestingStringList.length-1]; - ArrayList foundPositions = new ArrayList<>(); - - for(int i = interestingStringList.length-1; i >= 0; i--) { - - System.out.println(interestingStringList[i]); - if(stack.isEmpty() && search.equals(interestingStringList[i])) { - foundPositions.add(i); - }else if ("}".equals(interestingStringList[i])) { - stack.push("}"); - }else if ("{".equals(interestingStringList[i]) && !stack.isEmpty()) { - stack.pop(); - } - - } - AtomicReference type = new AtomicReference<>(""); - foundPositions.forEach(el -> { - System.out.println(el); - String potentialType = interestingStringList[el-1]; - - if(Arrays.asList(validTypes).contains(potentialType)){ - type.getAndSet(potentialType); - } - - }); - - return type.get(); + public String findAvailableTypes(int line, int character, String currentTextDocument) throws IOException, ClassNotFoundException { + var resultSet = languageServer.getResultSet(currentTextDocument); + return ""; } } \ No newline at end of file diff --git a/LanguageServer/src/test/java/CompilerInterfaceTest.java b/LanguageServer/src/test/java/CompilerInterfaceTest.java new file mode 100644 index 0000000..384114a --- /dev/null +++ b/LanguageServer/src/test/java/CompilerInterfaceTest.java @@ -0,0 +1,28 @@ +import de.dhbw.compiler.languageServerInterface.LanguageServerInterface; +import de.dhbw.helper.TypeFinder; +import org.junit.Test; + +import java.io.IOException; + +public class CompilerInterfaceTest { + + @Test + public void testAbstractSyntaxAsString() throws IOException, ClassNotFoundException { + LanguageServerInterface languageServer = new LanguageServerInterface(); + var abstractSyntaxString = languageServer.getAbstractSyntaxAsString("public class test{\n" + + " \n" + + " public main(int test){\n" + + "return test;\n" + + " }\n" + + "}"); + var resultSet = languageServer.getResultSet("public class test{\n" + + " \n" + + " public main(int test){\n" + + "return test;\n" + + " }\n" + + "}"); + + System.out.println(resultSet.toString()); + System.out.println(abstractSyntaxString); + } +}