Skip to content

Commit 632876d

Browse files
authored
SourceLocations can be turned off (graphql-java#2439)
* Made a ParserOptions class so we can have more options later * SourceLocations can be turned off
1 parent 97efeb3 commit 632876d

4 files changed

Lines changed: 75 additions & 10 deletions

File tree

src/main/java/graphql/language/SourceLocation.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
@PublicApi
1010
public class SourceLocation implements Serializable {
1111

12+
public static final SourceLocation EMPTY = new SourceLocation(-1, -1);
13+
1214
private final int line;
1315
private final int column;
1416
private final String sourceName;
@@ -37,13 +39,21 @@ public String getSourceName() {
3739

3840
@Override
3941
public boolean equals(Object o) {
40-
if (this == o) return true;
41-
if (o == null || getClass() != o.getClass()) return false;
42+
if (this == o) {
43+
return true;
44+
}
45+
if (o == null || getClass() != o.getClass()) {
46+
return false;
47+
}
4248

4349
SourceLocation that = (SourceLocation) o;
4450

45-
if (line != that.line) return false;
46-
if (column != that.column) return false;
51+
if (line != that.line) {
52+
return false;
53+
}
54+
if (column != that.column) {
55+
return false;
56+
}
4757
return Objects.equals(sourceName, that.sourceName);
4858
}
4959

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -849,14 +849,18 @@ protected Description newDescription(GraphqlParser.DescriptionContext descriptio
849849
return new Description(content, sourceLocation, multiLine);
850850
}
851851

852-
protected SourceLocation getSourceLocation(Token token) {
853-
return AntlrHelper.createSourceLocation(multiSourceReader, token);
854-
}
855-
856852
protected SourceLocation getSourceLocation(ParserRuleContext parserRuleContext) {
857853
return getSourceLocation(parserRuleContext.getStart());
858854
}
859855

856+
protected SourceLocation getSourceLocation(Token token) {
857+
if (parserOptions.isCaptureSourceLocation()) {
858+
return AntlrHelper.createSourceLocation(multiSourceReader, token);
859+
} else {
860+
return SourceLocation.EMPTY;
861+
}
862+
}
863+
860864
protected List<Comment> getComments(ParserRuleContext ctx) {
861865
Token start = ctx.getStart();
862866
if (start != null) {
@@ -885,7 +889,12 @@ protected List<Comment> getCommentOnChannel(List<Token> refChannel) {
885889
int column = refTok.getCharPositionInLine();
886890
// graphql spec says line numbers start at 1
887891
int line = sourceAndLine.getLine() + 1;
888-
comments.add(new Comment(text, new SourceLocation(line, column, sourceAndLine.getSourceName())));
892+
893+
SourceLocation sourceLocation = SourceLocation.EMPTY;
894+
if (parserOptions.isCaptureSourceLocation()) {
895+
sourceLocation = new SourceLocation(line, column, sourceAndLine.getSourceName());
896+
}
897+
comments.add(new Comment(text, sourceLocation));
889898
}
890899
return comments.build();
891900
}

src/main/java/graphql/parser/ParserOptions.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
@PublicApi
1010
public class ParserOptions {
1111

12-
private static ParserOptions defaultJvmParserOptions = newParserOptions().captureIgnoredChars(false).build();
12+
private static ParserOptions defaultJvmParserOptions = newParserOptions()
13+
.captureIgnoredChars(false)
14+
.captureSourceLocation(true)
15+
.build();
1316

1417
/**
1518
* By default the Parser will not capture ignored characters. A static holds this default
@@ -21,6 +24,7 @@ public class ParserOptions {
2124
* @return the static default value on whether to capture ignored chars
2225
*
2326
* @see graphql.language.IgnoredChar
27+
* @see graphql.language.SourceLocation
2428
*/
2529
public static ParserOptions getDefaultParserOptions() {
2630
return defaultJvmParserOptions;
@@ -38,15 +42,18 @@ public static ParserOptions getDefaultParserOptions() {
3842
* @param options - the new default JVM parser options
3943
*
4044
* @see graphql.language.IgnoredChar
45+
* @see graphql.language.SourceLocation
4146
*/
4247
public static void setDefaultParserOptions(ParserOptions options) {
4348
defaultJvmParserOptions = Assert.assertNotNull(options);
4449
}
4550

4651
private final boolean captureIgnoredChars;
52+
private final boolean captureSourceLocation;
4753

4854
private ParserOptions(Builder builder) {
4955
this.captureIgnoredChars = builder.captureIgnoredChars;
56+
this.captureSourceLocation = builder.captureSourceLocation;
5057
}
5158

5259
/**
@@ -59,19 +66,38 @@ public boolean isCaptureIgnoredChars() {
5966
return captureIgnoredChars;
6067
}
6168

69+
70+
/**
71+
* Memory savings can be made if we do NOT set {@link graphql.language.SourceLocation}s
72+
* on AST nodes, especially in SDL parsing.
73+
*
74+
* @return true if {@link graphql.language.SourceLocation}s are captured in AST nodes
75+
*
76+
* @see graphql.language.SourceLocation
77+
*/
78+
public boolean isCaptureSourceLocation() {
79+
return captureSourceLocation;
80+
}
81+
6282
public static Builder newParserOptions() {
6383
return new Builder();
6484
}
6585

6686
public static class Builder {
6787

6888
private boolean captureIgnoredChars = false;
89+
private boolean captureSourceLocation = true;
6990

7091
public Builder captureIgnoredChars(boolean captureIgnoredChars) {
7192
this.captureIgnoredChars = captureIgnoredChars;
7293
return this;
7394
}
7495

96+
public Builder captureSourceLocation(boolean captureSourceLocation) {
97+
this.captureSourceLocation = captureSourceLocation;
98+
return this;
99+
}
100+
75101
public ParserOptions build() {
76102
return new ParserOptions(this);
77103
}

src/test/groovy/graphql/parser/ParserTest.groovy

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,4 +1051,24 @@ triple3 : """edge cases \\""" "" " \\"" \\" edge cases"""
10511051
InvalidSyntaxException e = thrown(InvalidSyntaxException)
10521052
e.message == "Invalid Syntax : Invalid unicode - trailing surrogate must be preceded with a leading surrogate - offending token '\\uDC00' at line 3 column 24"
10531053
}
1054+
1055+
def "source locations are on by default but can be turned off"() {
1056+
when:
1057+
def options = ParserOptions.getDefaultParserOptions()
1058+
1059+
def document = new Parser().parseDocument("{ f }")
1060+
then:
1061+
options.isCaptureSourceLocation()
1062+
document.getSourceLocation() == new SourceLocation(1, 1)
1063+
document.getDefinitions()[0].getSourceLocation() == new SourceLocation(1, 1)
1064+
1065+
when:
1066+
options = ParserOptions.newParserOptions().captureSourceLocation(false).build()
1067+
document = new Parser().parseDocument("{ f }", options)
1068+
1069+
then:
1070+
!options.isCaptureSourceLocation()
1071+
document.getSourceLocation() == SourceLocation.EMPTY
1072+
document.getDefinitions()[0].getSourceLocation() == SourceLocation.EMPTY
1073+
}
10541074
}

0 commit comments

Comments
 (0)