Skip to content

Commit 432e734

Browse files
authored
Adding Locale to Parser (#2921)
* Adding Locale to Parser * Adding Locale to Parser - merged in master * Merged master in parser branch
1 parent 8c94777 commit 432e734

23 files changed

+312
-221
lines changed

src/main/java/graphql/ParseAndValidate.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import graphql.language.Document;
44
import graphql.parser.InvalidSyntaxException;
55
import graphql.parser.Parser;
6+
import graphql.parser.ParserEnvironment;
67
import graphql.parser.ParserOptions;
78
import graphql.schema.GraphQLSchema;
89
import graphql.validation.ValidationError;
@@ -65,7 +66,12 @@ public static ParseAndValidateResult parse(@NotNull ExecutionInput executionInpu
6566
// we use the query parser options by default if they are not specified
6667
parserOptions = ofNullable(parserOptions).orElse(ParserOptions.getDefaultOperationParserOptions());
6768
Parser parser = new Parser();
68-
Document document = parser.parseDocument(executionInput.getQuery(), parserOptions);
69+
Locale locale = executionInput.getLocale() == null ? Locale.getDefault() : executionInput.getLocale();
70+
ParserEnvironment parserEnvironment = ParserEnvironment.newParserEnvironment()
71+
.document(executionInput.getQuery()).parserOptions(parserOptions)
72+
.locale(locale)
73+
.build();
74+
Document document = parser.parseDocument(parserEnvironment);
6975
return ParseAndValidateResult.newResult().document(document).variables(executionInput.getVariables()).build();
7076
} catch (InvalidSyntaxException e) {
7177
return ParseAndValidateResult.newResult().syntaxException(e).variables(executionInput.getVariables()).build();

src/main/java/graphql/i18n/I18n.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class I18n {
2121
* This enum is a type safe way to control what resource bundle to load from
2222
*/
2323
public enum BundleType {
24+
Parsing,
2425
Scalars,
2526
Validation,
2627
Execution,
@@ -38,9 +39,9 @@ public enum BundleType {
3839

3940
@VisibleForTesting
4041
protected I18n(BundleType bundleType, Locale locale) {
41-
this.locale = locale;
4242
assertNotNull(bundleType);
4343
assertNotNull(locale);
44+
this.locale = locale;
4445
this.resourceBundle = ResourceBundle.getBundle(bundleType.baseName, locale);
4546
}
4647

src/main/java/graphql/language/PrettyAstPrinter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import graphql.collect.ImmutableKit;
66
import graphql.parser.CommentParser;
77
import graphql.parser.NodeToRuleCapturingParser;
8+
import graphql.parser.ParserEnvironment;
89

910
import java.util.Collections;
1011
import java.util.List;
@@ -14,6 +15,7 @@
1415
import java.util.stream.Collectors;
1516

1617
import static graphql.Assert.assertTrue;
18+
import static graphql.parser.ParserEnvironment.newParserEnvironment;
1719

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

6870
public static String print(String schemaDefinition, PrettyPrinterOptions options) {
6971
NodeToRuleCapturingParser parser = new NodeToRuleCapturingParser();
70-
Document document = parser.parseDocument(schemaDefinition);
72+
ParserEnvironment parserEnvironment = newParserEnvironment().document(schemaDefinition).build();
73+
Document document = parser.parseDocument(parserEnvironment);
7174

7275
return new PrettyAstPrinter(parser.getParserContext(), options).print(document);
7376
}

src/main/java/graphql/parser/ExtendedBailStrategy.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
package graphql.parser;
22

3+
import com.google.common.collect.ImmutableList;
34
import graphql.Internal;
45
import graphql.language.SourceLocation;
6+
import graphql.parser.exceptions.MoreTokensSyntaxException;
57
import org.antlr.v4.runtime.BailErrorStrategy;
68
import org.antlr.v4.runtime.Parser;
79
import org.antlr.v4.runtime.RecognitionException;
810
import org.antlr.v4.runtime.Token;
911
import org.antlr.v4.runtime.misc.ParseCancellationException;
1012

13+
import java.util.List;
14+
1115
@Internal
1216
public class ExtendedBailStrategy extends BailErrorStrategy {
1317
private final MultiSourceReader multiSourceReader;
18+
private final ParserEnvironment environment;
1419

15-
public ExtendedBailStrategy(MultiSourceReader multiSourceReader) {
20+
public ExtendedBailStrategy(MultiSourceReader multiSourceReader, ParserEnvironment environment) {
1621
this.multiSourceReader = multiSourceReader;
22+
this.environment = environment;
1723
}
1824

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

4550

4651
private InvalidSyntaxException mkException(Parser recognizer, RecognitionException cause) {
47-
String sourcePreview = null;
48-
String offendingToken = null;
49-
SourceLocation sourceLocation = null;
52+
String sourcePreview;
53+
String offendingToken;
54+
final SourceLocation sourceLocation;
5055
Token currentToken = recognizer.getCurrentToken();
5156
if (currentToken != null) {
5257
sourceLocation = AntlrHelper.createSourceLocation(multiSourceReader, currentToken);
5358
offendingToken = currentToken.getText();
5459
sourcePreview = AntlrHelper.createPreview(multiSourceReader, currentToken.getLine());
60+
} else {
61+
sourcePreview = null;
62+
offendingToken = null;
63+
sourceLocation = null;
64+
}
65+
66+
String msgKey;
67+
List<Object> args;
68+
SourceLocation location = sourceLocation == null ? SourceLocation.EMPTY : sourceLocation;
69+
if (offendingToken == null) {
70+
msgKey = "InvalidSyntaxBail.noToken";
71+
args = ImmutableList.of(location.getLine(), location.getColumn());
72+
} else {
73+
msgKey = "InvalidSyntaxBail.full";
74+
args = ImmutableList.of(offendingToken, sourceLocation.getLine(), sourceLocation.getColumn());
5575
}
56-
return new InvalidSyntaxException(sourceLocation, null, sourcePreview, offendingToken, cause);
76+
String msg = environment.getI18N().msg(msgKey, args);
77+
return new InvalidSyntaxException(msg, sourceLocation, offendingToken, sourcePreview, cause);
5778
}
5879

5980
}

src/main/java/graphql/parser/GraphqlAntlrToLanguage.java

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import graphql.Assert;
66
import graphql.Internal;
77
import graphql.collect.ImmutableKit;
8+
import graphql.i18n.I18n;
89
import graphql.language.Argument;
910
import graphql.language.ArrayValue;
1011
import graphql.language.BooleanValue;
@@ -91,44 +92,16 @@ public class GraphqlAntlrToLanguage {
9192
private final CommonTokenStream tokens;
9293
private final MultiSourceReader multiSourceReader;
9394
private final ParserOptions parserOptions;
94-
9595
private final Map<Node<?>, ParserRuleContext> nodeToRuleMap;
96+
private final I18n i18N;
9697

97-
/**
98-
* @param tokens the token stream
99-
* @param multiSourceReader the source of the query document
100-
*/
101-
public GraphqlAntlrToLanguage(CommonTokenStream tokens, MultiSourceReader multiSourceReader) {
102-
this(tokens, multiSourceReader, null);
103-
}
104-
105-
/**
106-
* @param tokens the token stream
107-
* @param multiSourceReader the source of the query document
108-
* @param parserOptions the parser options
109-
*/
110-
public GraphqlAntlrToLanguage(CommonTokenStream tokens, MultiSourceReader multiSourceReader, ParserOptions parserOptions) {
111-
this(tokens, multiSourceReader, parserOptions, null);
112-
}
113-
114-
/**
115-
* @param tokens the token stream
116-
* @param multiSourceReader the source of the query document
117-
* @param parserOptions the parser options
118-
* @param nodeToRuleMap a map that will be used to accumulate the ParserRuleContext associated with each node.
119-
* This information can be used after the parsing process is done to access some elements
120-
* that are usually lost during parsing. If the map is "null", no accumulation will be performed.
121-
*/
122-
public GraphqlAntlrToLanguage(
123-
CommonTokenStream tokens,
124-
MultiSourceReader multiSourceReader,
125-
ParserOptions parserOptions,
126-
@Nullable Map<Node<?>, ParserRuleContext> nodeToRuleMap
127-
) {
98+
public GraphqlAntlrToLanguage(CommonTokenStream tokens, MultiSourceReader multiSourceReader, ParserOptions parserOptions, I18n i18N, @Nullable Map<Node<?>, ParserRuleContext> nodeToRuleMap) {
12899
this.tokens = tokens;
129100
this.multiSourceReader = multiSourceReader;
130101
this.parserOptions = ofNullable(parserOptions).orElse(ParserOptions.getDefaultParserOptions());
102+
this.i18N = i18N;
131103
this.nodeToRuleMap = nodeToRuleMap;
104+
132105
}
133106

134107
public ParserOptions getParserOptions() {
@@ -244,7 +217,7 @@ protected SelectionSet createSelectionSet(GraphqlParser.SelectionSetContext ctx)
244217
if (selectionContext.inlineFragment() != null) {
245218
return createInlineFragment(selectionContext.inlineFragment());
246219
}
247-
return (Selection) Assert.assertShouldNeverHappen();
220+
return Assert.assertShouldNeverHappen();
248221

249222
});
250223
builder.selections(selections);
@@ -803,7 +776,7 @@ protected String quotedString(TerminalNode terminalNode) {
803776
if (multiLine) {
804777
return parseTripleQuotedString(strText);
805778
} else {
806-
return parseSingleQuotedString(strText, sourceLocation);
779+
return parseSingleQuotedString(i18N, strText, sourceLocation);
807780
}
808781
}
809782

@@ -880,7 +853,7 @@ protected Description newDescription(GraphqlParser.DescriptionContext descriptio
880853
if (multiLine) {
881854
content = parseTripleQuotedString(content);
882855
} else {
883-
content = parseSingleQuotedString(content, sourceLocation);
856+
content = parseSingleQuotedString(i18N, content, sourceLocation);
884857
}
885858
return new Description(content, sourceLocation, multiLine);
886859
}

src/main/java/graphql/parser/InvalidSyntaxException.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import graphql.GraphQLException;
5+
import graphql.Internal;
56
import graphql.InvalidSyntaxError;
67
import graphql.PublicApi;
78
import graphql.language.SourceLocation;
@@ -20,35 +21,20 @@ public class InvalidSyntaxException extends GraphQLException {
2021
private final String offendingToken;
2122
private final SourceLocation location;
2223

23-
InvalidSyntaxException(SourceLocation location, String msg, String sourcePreview, String offendingToken, Exception cause) {
24+
@Internal
25+
protected InvalidSyntaxException(String msg, SourceLocation location, String offendingToken, String sourcePreview, Exception cause) {
2426
super(cause);
25-
this.message = mkMessage(msg, offendingToken, location);
27+
this.message = msg;
2628
this.sourcePreview = sourcePreview;
2729
this.offendingToken = offendingToken;
2830
this.location = location;
2931
}
3032

31-
private String mkMessage(String msg, String offendingToken, SourceLocation location) {
32-
StringBuilder sb = new StringBuilder();
33-
sb.append("Invalid Syntax :");
34-
if (msg != null) {
35-
sb.append(" ").append(msg);
36-
}
37-
if (offendingToken != null) {
38-
sb.append(String.format(" offending token '%s'", offendingToken));
39-
}
40-
if (location != null) {
41-
sb.append(String.format(" at line %d column %d", location.getLine(), location.getColumn()));
42-
}
43-
return sb.toString();
44-
}
45-
4633
public InvalidSyntaxError toInvalidSyntaxError() {
4734
List<SourceLocation> sourceLocations = location == null ? null : Collections.singletonList(location);
4835
return new InvalidSyntaxError(sourceLocations, message, sourcePreview, offendingToken);
4936
}
5037

51-
5238
@Override
5339
public String getMessage() {
5440
return message;

src/main/java/graphql/parser/NodeToRuleCapturingParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public NodeToRuleCapturingParser() {
2121
}
2222

2323
@Override
24-
protected GraphqlAntlrToLanguage getAntlrToLanguage(CommonTokenStream tokens, MultiSourceReader multiSourceReader, ParserOptions parserOptions) {
24+
protected GraphqlAntlrToLanguage getAntlrToLanguage(CommonTokenStream tokens, MultiSourceReader multiSourceReader, ParserEnvironment environment) {
2525
parserContext.tokens = tokens;
26-
return new GraphqlAntlrToLanguage(tokens, multiSourceReader, parserOptions, parserContext.nodeToRuleMap);
26+
return new GraphqlAntlrToLanguage(tokens, multiSourceReader, environment.getParserOptions(), environment.getI18N(), parserContext.nodeToRuleMap);
2727
}
2828

2929
public ParserContext getParserContext() {

src/main/java/graphql/parser/ParseCancelledException.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)