graphql.parser.Parser Maven / Gradle / Ivy
package graphql.parser;
import com.google.common.collect.ImmutableList;
import graphql.DeprecatedAt;
import graphql.Internal;
import graphql.PublicApi;
import graphql.language.Document;
import graphql.language.Node;
import graphql.language.SourceLocation;
import graphql.language.Type;
import graphql.language.Value;
import graphql.parser.antlr.GraphqlBaseListener;
import graphql.parser.antlr.GraphqlLexer;
import graphql.parser.antlr.GraphqlParser;
import graphql.parser.exceptions.ParseCancelledException;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CodePointCharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.atn.PredictionMode;
import org.antlr.v4.runtime.tree.ParseTreeListener;
import org.antlr.v4.runtime.tree.TerminalNode;
import java.io.IOException;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
/**
* This can parse graphql syntax, both Query syntax and Schema Definition Language (SDL) syntax, into an
* Abstract Syntax Tree (AST) represented by a {@link Document}
*
* You should not generally need to call this class as the {@link graphql.GraphQL} code sets this up for you
* but if you are doing specific graphql utilities this class is essential.
*
* Graphql syntax has a series of characters, such as spaces, new lines and commas that are not considered relevant
* to the syntax. However they can be captured and associated with the AST elements they belong to.
*
* This costs more memory but for certain use cases (like editors) this maybe be useful. We have chosen to no capture
* ignored characters by default but you can turn this on, either per parse or statically for the whole JVM
* via {@link ParserOptions#setDefaultParserOptions(ParserOptions)} ()}}
*
* @see graphql.language.IgnoredChar
*/
@PublicApi
public class Parser {
@Internal
public static final int CHANNEL_COMMENTS = 2;
@Internal
public static final int CHANNEL_WHITESPACE = 3;
/**
* Parses a string input into a graphql AST {@link Document}
*
* @param environment the parser environment to use
*
* @return an AST {@link Document}
*
* @throws InvalidSyntaxException if the document is not valid graphql syntax
*/
public static Document parse(ParserEnvironment environment) throws InvalidSyntaxException {
return new Parser().parseDocument(environment);
}
/**
* Parses a string input into a graphql AST {@link Document}
*
* @param input the input to parse
*
* @return an AST {@link Document}
*
* @throws InvalidSyntaxException if the input is not valid graphql syntax
*/
public static Document parse(String input) throws InvalidSyntaxException {
return new Parser().parseDocument(input);
}
/**
* Parses a string input into a graphql AST {@link Value}
*
* @param input the input to parse
*
* @return an AST {@link Value}
*
* @throws InvalidSyntaxException if the input is not valid graphql syntax
*/
public static Value> parseValue(String input) throws InvalidSyntaxException {
return new Parser().parseValueImpl(input);
}
/**
* Parses a string input into a graphql AST Type
*
* @param input the input to parse
*
* @return an AST {@link Type}
*
* @throws InvalidSyntaxException if the input is not valid graphql syntax
*/
public static Type> parseType(String input) throws InvalidSyntaxException {
return new Parser().parseTypeImpl(input);
}
/**
* Parses document text into a graphql AST {@link Document}
*
* @param environment the parser environment to sue
*
* @return an AST {@link Document}
*
* @throws InvalidSyntaxException if the input is not valid graphql syntax
*/
public Document parseDocument(ParserEnvironment environment) throws InvalidSyntaxException {
return parseDocumentImpl(environment);
}
/**
* Parses a string input into a graphql AST {@link Document}
*
* @param input the input to parse
*
* @return an AST {@link Document}
*
* @throws InvalidSyntaxException if the input is not valid graphql syntax
*/
public Document parseDocument(String input) throws InvalidSyntaxException {
return parseDocument(input, (ParserOptions) null);
}
/**
* Parses reader input into a graphql AST {@link Document}
*
* @param reader the reader input to parse
*
* @return an AST {@link Document}
*
* @throws InvalidSyntaxException if the input is not valid graphql syntax
*/
public Document parseDocument(Reader reader) throws InvalidSyntaxException {
ParserEnvironment parserEnvironment = ParserEnvironment.newParserEnvironment()
.document(reader)
.build();
return parseDocumentImpl(parserEnvironment);
}
/**
* Parses a string input into a graphql AST {@link Document}
*
* @param input the input to parse
* @param sourceName - the name to attribute to the input text in {@link SourceLocation#getSourceName()}
*
* @return an AST {@link Document}
*
* @throws InvalidSyntaxException if the input is not valid graphql syntax
* @deprecated use {#{@link #parse(ParserEnvironment)}} instead
*/
@DeprecatedAt("2022-08-31")
@Deprecated
public Document parseDocument(String input, String sourceName) throws InvalidSyntaxException {
MultiSourceReader multiSourceReader = MultiSourceReader.newMultiSourceReader()
.string(input, sourceName)
.trackData(true)
.build();
return parseDocument(multiSourceReader);
}
/**
* Parses a string input into a graphql AST {@link Document}
*
* @param input the input to parse
* @param parserOptions the parser options
*
* @return an AST {@link Document}
*
* @throws InvalidSyntaxException if the input is not valid graphql syntax
* @deprecated use {#{@link #parse(ParserEnvironment)}} instead
*/
@DeprecatedAt("2022-08-31")
@Deprecated
public Document parseDocument(String input, ParserOptions parserOptions) throws InvalidSyntaxException {
MultiSourceReader multiSourceReader = MultiSourceReader.newMultiSourceReader()
.string(input, null)
.trackData(true)
.build();
return parseDocument(multiSourceReader, parserOptions);
}
/**
* Parses reader input into a graphql AST {@link Document}
*
* @param reader the reader input to parse
* @param parserOptions the parser options
*
* @return an AST {@link Document}
*
* @throws InvalidSyntaxException if the input is not valid graphql syntax
* @deprecated use {#{@link #parse(ParserEnvironment)}} instead
*/
@DeprecatedAt("2022-08-31")
@Deprecated
public Document parseDocument(Reader reader, ParserOptions parserOptions) throws InvalidSyntaxException {
ParserEnvironment parserEnvironment = ParserEnvironment.newParserEnvironment()
.document(reader)
.parserOptions(parserOptions)
.build();
return parseDocumentImpl(parserEnvironment);
}
private Document parseDocumentImpl(ParserEnvironment environment) throws InvalidSyntaxException {
BiFunction nodeFunction = (parser, toLanguage) -> {
GraphqlParser.DocumentContext documentContext = parser.document();
Document doc = toLanguage.createDocument(documentContext);
return new Object[]{documentContext, doc};
};
return (Document) parseImpl(environment, nodeFunction);
}
private Value> parseValueImpl(String input) throws InvalidSyntaxException {
BiFunction nodeFunction = (parser, toLanguage) -> {
GraphqlParser.ValueContext documentContext = parser.value();
Value> value = toLanguage.createValue(documentContext);
return new Object[]{documentContext, value};
};
MultiSourceReader multiSourceReader = MultiSourceReader.newMultiSourceReader()
.string(input, null)
.trackData(true)
.build();
ParserEnvironment parserEnvironment = ParserEnvironment.newParserEnvironment().document(multiSourceReader).build();
return (Value>) parseImpl(parserEnvironment, nodeFunction);
}
private Type> parseTypeImpl(String input) throws InvalidSyntaxException {
BiFunction nodeFunction = (parser, toLanguage) -> {
final GraphqlParser.TypeContext documentContext = parser.type();
Type> value = toLanguage.createType(documentContext);
return new Object[]{documentContext, value};
};
MultiSourceReader multiSourceReader = MultiSourceReader.newMultiSourceReader()
.string(input, null)
.trackData(true)
.build();
ParserEnvironment parserEnvironment = ParserEnvironment.newParserEnvironment().document(multiSourceReader).build();
return (Type>) parseImpl(parserEnvironment, nodeFunction);
}
private Node> parseImpl(ParserEnvironment environment, BiFunction nodeFunction) throws InvalidSyntaxException {
MultiSourceReader multiSourceReader;
Reader reader = environment.getDocument();
if (reader instanceof MultiSourceReader) {
multiSourceReader = (MultiSourceReader) reader;
} else {
multiSourceReader = MultiSourceReader.newMultiSourceReader()
.reader(reader, null).build();
}
CodePointCharStream charStream;
try {
charStream = CharStreams.fromReader(multiSourceReader);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
GraphqlLexer lexer = new GraphqlLexer(charStream);
lexer.removeErrorListeners();
lexer.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(Recognizer, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String antlerMsg, RecognitionException e) {
SourceLocation sourceLocation = AntlrHelper.createSourceLocation(multiSourceReader, line, charPositionInLine);
String preview = AntlrHelper.createPreview(multiSourceReader, line);
String msgKey;
List