Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/main/java/graphql/ParseAndValidate.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import graphql.language.Document;
import graphql.parser.InvalidSyntaxException;
import graphql.parser.Parser;
import graphql.parser.ParserEnvironment;
import graphql.parser.ParserOptions;
import graphql.schema.GraphQLSchema;
import graphql.validation.ValidationError;
Expand Down Expand Up @@ -65,7 +66,12 @@ public static ParseAndValidateResult parse(@NotNull ExecutionInput executionInpu
// we use the query parser options by default if they are not specified
parserOptions = ofNullable(parserOptions).orElse(ParserOptions.getDefaultOperationParserOptions());
Parser parser = new Parser();
Document document = parser.parseDocument(executionInput.getQuery(), parserOptions);
Locale locale = executionInput.getLocale() == null ? Locale.getDefault() : executionInput.getLocale();
ParserEnvironment parserEnvironment = ParserEnvironment.newParserEnvironment()
.document(executionInput.getQuery()).parserOptions(parserOptions)
.locale(locale)
.build();
Document document = parser.parseDocument(parserEnvironment);
return ParseAndValidateResult.newResult().document(document).variables(executionInput.getVariables()).build();
} catch (InvalidSyntaxException e) {
return ParseAndValidateResult.newResult().syntaxException(e).variables(executionInput.getVariables()).build();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/graphql/i18n/I18n.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class I18n {
* This enum is a type safe way to control what resource bundle to load from
*/
public enum BundleType {
Parsing,
Scalars,
Validation,
Execution,
Expand All @@ -38,9 +39,9 @@ public enum BundleType {

@VisibleForTesting
protected I18n(BundleType bundleType, Locale locale) {
this.locale = locale;
assertNotNull(bundleType);
assertNotNull(locale);
this.locale = locale;
this.resourceBundle = ResourceBundle.getBundle(bundleType.baseName, locale);
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/graphql/language/PrettyAstPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import graphql.collect.ImmutableKit;
import graphql.parser.CommentParser;
import graphql.parser.NodeToRuleCapturingParser;
import graphql.parser.ParserEnvironment;

import java.util.Collections;
import java.util.List;
Expand All @@ -14,6 +15,7 @@
import java.util.stream.Collectors;

import static graphql.Assert.assertTrue;
import static graphql.parser.ParserEnvironment.newParserEnvironment;

/**
* A printer that acts as a code formatter.
Expand Down Expand Up @@ -67,7 +69,8 @@ public String print(Node node) {

public static String print(String schemaDefinition, PrettyPrinterOptions options) {
NodeToRuleCapturingParser parser = new NodeToRuleCapturingParser();
Document document = parser.parseDocument(schemaDefinition);
ParserEnvironment parserEnvironment = newParserEnvironment().document(schemaDefinition).build();
Document document = parser.parseDocument(parserEnvironment);

return new PrettyAstPrinter(parser.getParserContext(), options).print(document);
}
Expand Down
37 changes: 29 additions & 8 deletions src/main/java/graphql/parser/ExtendedBailStrategy.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package graphql.parser;

import com.google.common.collect.ImmutableList;
import graphql.Internal;
import graphql.language.SourceLocation;
import graphql.parser.exceptions.MoreTokensSyntaxException;
import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.misc.ParseCancellationException;

import java.util.List;

@Internal
public class ExtendedBailStrategy extends BailErrorStrategy {
private final MultiSourceReader multiSourceReader;
private final ParserEnvironment environment;

public ExtendedBailStrategy(MultiSourceReader multiSourceReader) {
public ExtendedBailStrategy(MultiSourceReader multiSourceReader, ParserEnvironment environment) {
this.multiSourceReader = multiSourceReader;
this.environment = environment;
}

@Override
Expand All @@ -37,23 +43,38 @@ public Token recoverInline(Parser recognizer) throws RecognitionException {
InvalidSyntaxException mkMoreTokensException(Token token) {
SourceLocation sourceLocation = AntlrHelper.createSourceLocation(multiSourceReader, token);
String sourcePreview = AntlrHelper.createPreview(multiSourceReader, token.getLine());
return new InvalidSyntaxException(sourceLocation,
"There are more tokens in the query that have not been consumed",
sourcePreview, token.getText(), null);
return new MoreTokensSyntaxException(environment.getI18N(), sourceLocation,
token.getText(), sourcePreview);
}


private InvalidSyntaxException mkException(Parser recognizer, RecognitionException cause) {
String sourcePreview = null;
String offendingToken = null;
SourceLocation sourceLocation = null;
String sourcePreview;
String offendingToken;
final SourceLocation sourceLocation;
Token currentToken = recognizer.getCurrentToken();
if (currentToken != null) {
sourceLocation = AntlrHelper.createSourceLocation(multiSourceReader, currentToken);
offendingToken = currentToken.getText();
sourcePreview = AntlrHelper.createPreview(multiSourceReader, currentToken.getLine());
} else {
sourcePreview = null;
offendingToken = null;
sourceLocation = null;
}

String msgKey;
List<Object> args;
SourceLocation location = sourceLocation == null ? SourceLocation.EMPTY : sourceLocation;
if (offendingToken == null) {
msgKey = "InvalidSyntaxBail.noToken";
args = ImmutableList.of(location.getLine(), location.getColumn());
} else {
msgKey = "InvalidSyntaxBail.full";
args = ImmutableList.of(offendingToken, sourceLocation.getLine(), sourceLocation.getColumn());
}
return new InvalidSyntaxException(sourceLocation, null, sourcePreview, offendingToken, cause);
String msg = environment.getI18N().msg(msgKey, args);
return new InvalidSyntaxException(msg, sourceLocation, offendingToken, sourcePreview, cause);
}

}
43 changes: 8 additions & 35 deletions src/main/java/graphql/parser/GraphqlAntlrToLanguage.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import graphql.Assert;
import graphql.Internal;
import graphql.collect.ImmutableKit;
import graphql.i18n.I18n;
import graphql.language.Argument;
import graphql.language.ArrayValue;
import graphql.language.BooleanValue;
Expand Down Expand Up @@ -91,44 +92,16 @@ public class GraphqlAntlrToLanguage {
private final CommonTokenStream tokens;
private final MultiSourceReader multiSourceReader;
private final ParserOptions parserOptions;

private final Map<Node<?>, ParserRuleContext> nodeToRuleMap;
private final I18n i18N;

/**
* @param tokens the token stream
* @param multiSourceReader the source of the query document
*/
public GraphqlAntlrToLanguage(CommonTokenStream tokens, MultiSourceReader multiSourceReader) {
this(tokens, multiSourceReader, null);
}

/**
* @param tokens the token stream
* @param multiSourceReader the source of the query document
* @param parserOptions the parser options
*/
public GraphqlAntlrToLanguage(CommonTokenStream tokens, MultiSourceReader multiSourceReader, ParserOptions parserOptions) {
this(tokens, multiSourceReader, parserOptions, null);
}

/**
* @param tokens the token stream
* @param multiSourceReader the source of the query document
* @param parserOptions the parser options
* @param nodeToRuleMap a map that will be used to accumulate the ParserRuleContext associated with each node.
* This information can be used after the parsing process is done to access some elements
* that are usually lost during parsing. If the map is "null", no accumulation will be performed.
*/
public GraphqlAntlrToLanguage(
CommonTokenStream tokens,
MultiSourceReader multiSourceReader,
ParserOptions parserOptions,
@Nullable Map<Node<?>, ParserRuleContext> nodeToRuleMap
) {
public GraphqlAntlrToLanguage(CommonTokenStream tokens, MultiSourceReader multiSourceReader, ParserOptions parserOptions, I18n i18N, @Nullable Map<Node<?>, ParserRuleContext> nodeToRuleMap) {
this.tokens = tokens;
this.multiSourceReader = multiSourceReader;
this.parserOptions = ofNullable(parserOptions).orElse(ParserOptions.getDefaultParserOptions());
this.i18N = i18N;
this.nodeToRuleMap = nodeToRuleMap;

}

public ParserOptions getParserOptions() {
Expand Down Expand Up @@ -244,7 +217,7 @@ protected SelectionSet createSelectionSet(GraphqlParser.SelectionSetContext ctx)
if (selectionContext.inlineFragment() != null) {
return createInlineFragment(selectionContext.inlineFragment());
}
return (Selection) Assert.assertShouldNeverHappen();
return Assert.assertShouldNeverHappen();

});
builder.selections(selections);
Expand Down Expand Up @@ -803,7 +776,7 @@ protected String quotedString(TerminalNode terminalNode) {
if (multiLine) {
return parseTripleQuotedString(strText);
} else {
return parseSingleQuotedString(strText, sourceLocation);
return parseSingleQuotedString(i18N, strText, sourceLocation);
}
}

Expand Down Expand Up @@ -880,7 +853,7 @@ protected Description newDescription(GraphqlParser.DescriptionContext descriptio
if (multiLine) {
content = parseTripleQuotedString(content);
} else {
content = parseSingleQuotedString(content, sourceLocation);
content = parseSingleQuotedString(i18N, content, sourceLocation);
}
return new Description(content, sourceLocation, multiLine);
}
Expand Down
22 changes: 4 additions & 18 deletions src/main/java/graphql/parser/InvalidSyntaxException.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import graphql.GraphQLException;
import graphql.Internal;
import graphql.InvalidSyntaxError;
import graphql.PublicApi;
import graphql.language.SourceLocation;
Expand All @@ -20,35 +21,20 @@ public class InvalidSyntaxException extends GraphQLException {
private final String offendingToken;
private final SourceLocation location;

InvalidSyntaxException(SourceLocation location, String msg, String sourcePreview, String offendingToken, Exception cause) {
@Internal
protected InvalidSyntaxException(String msg, SourceLocation location, String offendingToken, String sourcePreview, Exception cause) {
super(cause);
this.message = mkMessage(msg, offendingToken, location);
this.message = msg;
this.sourcePreview = sourcePreview;
this.offendingToken = offendingToken;
this.location = location;
}

private String mkMessage(String msg, String offendingToken, SourceLocation location) {
StringBuilder sb = new StringBuilder();
sb.append("Invalid Syntax :");
if (msg != null) {
sb.append(" ").append(msg);
}
if (offendingToken != null) {
sb.append(String.format(" offending token '%s'", offendingToken));
}
if (location != null) {
sb.append(String.format(" at line %d column %d", location.getLine(), location.getColumn()));
}
return sb.toString();
}

public InvalidSyntaxError toInvalidSyntaxError() {
List<SourceLocation> sourceLocations = location == null ? null : Collections.singletonList(location);
return new InvalidSyntaxError(sourceLocations, message, sourcePreview, offendingToken);
}


@Override
public String getMessage() {
return message;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/graphql/parser/NodeToRuleCapturingParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public NodeToRuleCapturingParser() {
}

@Override
protected GraphqlAntlrToLanguage getAntlrToLanguage(CommonTokenStream tokens, MultiSourceReader multiSourceReader, ParserOptions parserOptions) {
protected GraphqlAntlrToLanguage getAntlrToLanguage(CommonTokenStream tokens, MultiSourceReader multiSourceReader, ParserEnvironment environment) {
parserContext.tokens = tokens;
return new GraphqlAntlrToLanguage(tokens, multiSourceReader, parserOptions, parserContext.nodeToRuleMap);
return new GraphqlAntlrToLanguage(tokens, multiSourceReader, environment.getParserOptions(), environment.getI18N(), parserContext.nodeToRuleMap);
}

public ParserContext getParserContext() {
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/graphql/parser/ParseCancelledException.java

This file was deleted.

Loading