feat: add Interface for Language Server returning ResultSet and Abstract Syntax, add Test

This commit is contained in:
Ruben 2024-12-05 18:28:45 +01:00
parent be77b7875e
commit e93058e76f
3 changed files with 82 additions and 60 deletions

View File

@ -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<ResultSet> getResultSet(String input) {
return null;
public List<ResultSet> 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<ResultSet> 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));
}
}

View File

@ -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<String> stack = new Stack<>();
String search = interestingStringList[interestingStringList.length-1];
ArrayList<Integer> 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<String> 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 "";
}
}

View File

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