package de.dhbwstuttgart.languageServerInterface;



import de.dhbwstuttgart.languageServerInterface.model.CustomParserErrorHandler;
import de.dhbwstuttgart.languageServerInterface.model.ParserError;
import de.dhbwstuttgart.parser.antlr.Java17Lexer;
import de.dhbwstuttgart.parser.antlr.Java17Parser;
import de.dhbwstuttgart.parser.antlr.Java17ParserBaseListener;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

import java.util.List;

public class ParserInterface {

    public List<ParserError> getParseErrors(String input){

        CustomParserErrorHandler errorListener = new CustomParserErrorHandler();
        CharStream charStream = CharStreams.fromString(input);

        Java17Lexer lexer = new Java17Lexer(charStream);
        CommonTokenStream tokens = new CommonTokenStream(lexer);

        Java17Parser parser = new Java17Parser(tokens);
        parser.removeErrorListeners();
        parser.addErrorListener(errorListener);


        ParseTree tree = parser.sourceFile();
        ParseTreeWalker walker = new ParseTreeWalker();
        Java17ParserBaseListener listener = new Java17ParserBaseListener();
        walker.walk(listener, tree);

        return errorListener.getErrorMessages();
    }
}
