package graphql.parser;

import graphql.language.Document;
import graphql.parser.antlr.GraphqlLexer;
import graphql.parser.antlr.GraphqlParser;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DefaultErrorStrategy;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.atn.PredictionMode;

public class Parser {


    public Document parseDocument(String input) {

        GraphqlLexer lexer = new GraphqlLexer(new ANTLRInputStream(input));

        CommonTokenStream tokens = new CommonTokenStream(lexer);

        GraphqlParser parser = new GraphqlParser(tokens);
        parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
        parser.setErrorHandler(new ErrorStrategy());
        GraphqlParser.DocumentContext document = parser.document();


        GraphqlAntlrToLanguage antlrToLanguage = new GraphqlAntlrToLanguage();
        antlrToLanguage.visitDocument(document);
        return antlrToLanguage.result;
    }

    private class ErrorStrategy extends DefaultErrorStrategy {

        @Override
        public void recover(org.antlr.v4.runtime.Parser recognizer, RecognitionException e) {
            throw e;
        }
    }

}

@@ -165,32 +165,17 @@
 runtime.
-ANTLRInputStream
+*
 ;%0Aimport
@@ -200,197 +200,185 @@
 ime.
-CommonTokenStream;%0Aimport org.antlr.v4.runtime.DefaultErrorStrategy;%0Aimport org.antlr.v4.runtime.RecognitionException;%0Aimport org.antlr.v4.runtime.atn.PredictionMode;%0A%0Apublic class Parser %7B
+atn.PredictionMode;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Apublic class Parser %7B%0A%0A    private static final Logger log = LoggerFactory.getLogger(Parser.class);
 %0A%0A%0A 
@@ -628,16 +628,109 @@
 okens);%0A
+        parser.removeErrorListeners();%0A        parser.addErrorListener(new ErrorListener());%0A
         
@@ -1303,10 +1303,362 @@
 %0A    %7D%0A%0A
+    private class ErrorListener extends BaseErrorListener %7B%0A        @Override%0A        public void syntaxError(Recognizer%3C?, ?%3E recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) %7B%0A            log.error(%22graphql syntax error: line %22 + line + %22:%22 + charPositionInLine + %22 %22 + msg);%0A        %7D%0A    %7D%0A%0A
 %7D%0A

replace antlr default error listener (which prints to console) with error listener which uses our logger