package de.dhbwstuttgart.languageServerInterface.model;



import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.atn.ATNConfigSet;
import org.antlr.v4.runtime.dfa.DFA;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;

public class CustomParserErrorHandler implements ANTLRErrorListener {
    private final List<ParserError> errorMessages = new ArrayList<>();

    @Override
    public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
        int endCharPosition = charPositionInLine;
        if (offendingSymbol instanceof Token) {
            Token offendingToken = (Token) offendingSymbol;
            endCharPosition = charPositionInLine + offendingToken.getText().length();
        }

        ParserError parserError = new ParserError(line, charPositionInLine, endCharPosition, msg);
        errorMessages.add(parserError);

    }

    @Override
    public void reportAmbiguity(Parser parser, DFA dfa, int i, int i1, boolean b, BitSet bitSet, ATNConfigSet atnConfigSet) {

    }

    @Override
    public void reportAttemptingFullContext(Parser parser, DFA dfa, int i, int i1, BitSet bitSet, ATNConfigSet atnConfigSet) {

    }

    @Override
    public void reportContextSensitivity(Parser parser, DFA dfa, int i, int i1, int i2, ATNConfigSet atnConfigSet) {

    }

    public List<ParserError> getErrorMessages() {
        return errorMessages;
    }
}
