package org.karaffe.compiler.frontend.karaffe;

import org.antlr.v4.runtime.ANTLRErrorListener;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.atn.ATNConfigSet;
import org.antlr.v4.runtime.dfa.DFA;
import org.karaffe.compiler.util.CompilerContext;
import org.karaffe.compiler.util.Position;
import org.karaffe.compiler.util.report.Report;
import org.karaffe.compiler.util.report.ReportCode;

import java.util.BitSet;

public class DefaultErrorListener implements ANTLRErrorListener {

  private final CompilerContext context;
  private boolean hasSyntaxError = false;

  public DefaultErrorListener(CompilerContext context) {
    this.context = context;
  }

  @Override
  public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
    Position position = new Position(line, charPositionInLine, recognizer);
    context.add(Report.newReport(ReportCode.ERR_SYNTAX).with(position).build());
    this.hasSyntaxError = true;
  }

  @Override
  public void reportAmbiguity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact, BitSet ambigAlts, ATNConfigSet configs) {
//        throw new IllegalStateException("reportAmbiguity : " + dfa + ", " + startIndex + ", " + stopIndex);
  }

  @Override
  public void reportAttemptingFullContext(Parser recognizer, DFA dfa, int startIndex, int stopIndex, BitSet conflictingAlts, ATNConfigSet configs) {
//        throw new IllegalStateException("reportAttemptingFullContext : " + dfa + ", " + startIndex + ", " + stopIndex);
  }

  @Override
  public void reportContextSensitivity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, int prediction, ATNConfigSet configs) {
//        throw new IllegalStateException("reportContextSensitivity : " + dfa + ", " + startIndex + ", " + stopIndex);
  }

  public boolean hasSyntaxError() {
    return this.hasSyntaxError;
  }
}

@@ -211,24 +211,59 @@
 Recognizer;%0A
+import org.antlr.v4.runtime.Token;%0A
 import org.a
@@ -296,16 +296,16 @@
 figSet;%0A
-
 import o
@@ -702,16 +702,61 @@
 = false;
+%0A  private Position lastErrorPosition = null;
 %0A%0A  publ
@@ -1346,32 +1346,85 @@
 gSet configs) %7B%0A
+//        reportSyntaxError(recognizer, startIndex);%0A
 //        throw 
@@ -1675,32 +1675,85 @@
 gSet configs) %7B%0A
+//        reportSyntaxError(recognizer, startIndex);%0A
 //        throw 
@@ -2005,32 +2005,85 @@
 gSet configs) %7B%0A
+//        reportSyntaxError(recognizer, startIndex);%0A
 //        throw 
@@ -2190,16 +2190,507 @@
 );%0A  %7D%0A%0A
+  private void reportSyntaxError(Parser recognizer, int startIndex) %7B%0A    Token token = recognizer.getInputStream().get(startIndex);%0A    Position position = new Position(token);%0A    if (lastErrorPosition == null) %7B%0A      lastErrorPosition = position;%0A    %7D else if (lastErrorPosition.equals(position)) %7B%0A      return;%0A    %7D else %7B%0A      lastErrorPosition = position;%0A    %7D%0A    context.add(Report.newReport(ReportCode.ERR_SYNTAX).with(position).build());%0A    this.hasSyntaxError = true;%0A  %7D%0A%0A
   public

Add debug code