package com.kodcu.listener;

import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * Created by Hakan on 6/26/2015.
 */
public class QuerySyntaxErrorListener extends BaseErrorListener {

    private final Logger logger = LoggerFactory.getLogger(QuerySyntaxErrorListener.class);

    @Override
    public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
        List<String> stack = ((Parser)recognizer).getRuleInvocationStack();
        Collections.reverse(stack);
        logger.error(String.join("","line", String.valueOf(line) ,":", String.valueOf(charPositionInLine), "at", String.valueOf(offendingSymbol) ,":",msg));
        System.exit(1);
    }
}

@@ -713,16 +713,17 @@
 (Parser)
+ 
 recogniz
@@ -788,16 +788,62 @@
 stack);%0A
+        logger.error(%22Rule stack: %22 + stack);%0A
         
@@ -870,16 +870,17 @@
 join(%22%22,
+ 
 %22line%22, 
@@ -899,18 +899,18 @@
 Of(line)
- 
 ,
+ 
 %22:%22, Str
@@ -983,14 +983,15 @@
 bol)
- 
 ,
+ 
 %22:%22,
+ 
 msg)

Update error listener