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;

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.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;
        }
    }

}

@@ -329,16 +329,64 @@
 ception;
+%0Aimport org.antlr.v4.runtime.atn.PredictionMode;
 %0A%0Apublic
@@ -655,16 +655,87 @@
 okens);%0A
+        parser.getInterpreter().setPredictionMode(PredictionMode.SLL);%0A
         

set parser prediction mode to SLL