Skip to content

Commit dffefc4

Browse files
committed
Added parser new signature
1 parent 5919da0 commit dffefc4

3 files changed

Lines changed: 143 additions & 8 deletions

File tree

‎src/main/java/graphql/parser/Parser.java‎

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ public static Document parse(String input) throws InvalidSyntaxException {
6767
return new Parser().parseDocument(input);
6868
}
6969

70+
public static Document parse(ParserEnvironment environment) throws InvalidSyntaxException {
71+
return new Parser().parseDocument(environment);
72+
}
73+
7074
/**
7175
* Parses a string input into a graphql AST {@link Value}
7276
*
@@ -152,7 +156,10 @@ public Document parseDocument(String input, ParserOptions parserOptions) throws
152156
* @throws InvalidSyntaxException if the input is not valid graphql syntax
153157
*/
154158
public Document parseDocument(Reader reader) throws InvalidSyntaxException {
155-
return parseDocumentImpl(reader, null);
159+
ParserEnvironment parserEnvironment = ParserEnvironment.newParserEnvironment()
160+
.document(reader)
161+
.build();
162+
return parseDocumentImpl(parserEnvironment);
156163
}
157164

158165
/**
@@ -166,16 +173,34 @@ public Document parseDocument(Reader reader) throws InvalidSyntaxException {
166173
* @throws InvalidSyntaxException if the input is not valid graphql syntax
167174
*/
168175
public Document parseDocument(Reader reader, ParserOptions parserOptions) throws InvalidSyntaxException {
169-
return parseDocumentImpl(reader, parserOptions);
176+
ParserEnvironment parserEnvironment = ParserEnvironment.newParserEnvironment()
177+
.document(reader)
178+
.parserOptions(parserOptions)
179+
.build();
180+
return parseDocumentImpl(parserEnvironment);
170181
}
171182

172-
private Document parseDocumentImpl(Reader reader, ParserOptions parserOptions) throws InvalidSyntaxException {
183+
/**
184+
* Parses document text into a graphql AST {@link Document}
185+
*
186+
* @param environment the parser environment to sue
187+
*
188+
* @return an AST {@link Document}
189+
*
190+
* @throws InvalidSyntaxException if the input is not valid graphql syntax
191+
*/
192+
public Document parseDocument(ParserEnvironment environment) throws InvalidSyntaxException {
193+
return parseDocumentImpl(environment);
194+
}
195+
196+
197+
private Document parseDocumentImpl(ParserEnvironment environment) throws InvalidSyntaxException {
173198
BiFunction<GraphqlParser, GraphqlAntlrToLanguage, Object[]> nodeFunction = (parser, toLanguage) -> {
174199
GraphqlParser.DocumentContext documentContext = parser.document();
175200
Document doc = toLanguage.createDocument(documentContext);
176201
return new Object[]{documentContext, doc};
177202
};
178-
return (Document) parseImpl(reader, nodeFunction, parserOptions);
203+
return (Document) parseImpl(environment, nodeFunction);
179204
}
180205

181206
private Value<?> parseValueImpl(String input) throws InvalidSyntaxException {
@@ -188,7 +213,8 @@ private Value<?> parseValueImpl(String input) throws InvalidSyntaxException {
188213
.string(input, null)
189214
.trackData(true)
190215
.build();
191-
return (Value<?>) parseImpl(multiSourceReader, nodeFunction, null);
216+
ParserEnvironment parserEnvironment = ParserEnvironment.newParserEnvironment().document(multiSourceReader).build();
217+
return (Value<?>) parseImpl(parserEnvironment, nodeFunction);
192218
}
193219

194220
private Type<?> parseTypeImpl(String input) throws InvalidSyntaxException {
@@ -201,11 +227,14 @@ private Type<?> parseTypeImpl(String input) throws InvalidSyntaxException {
201227
.string(input, null)
202228
.trackData(true)
203229
.build();
204-
return (Type<?>) parseImpl(multiSourceReader, nodeFunction, null);
230+
231+
ParserEnvironment parserEnvironment = ParserEnvironment.newParserEnvironment().document(multiSourceReader).build();
232+
return (Type<?>) parseImpl(parserEnvironment, nodeFunction);
205233
}
206234

207-
private Node<?> parseImpl(Reader reader, BiFunction<GraphqlParser, GraphqlAntlrToLanguage, Object[]> nodeFunction, ParserOptions parserOptions) throws InvalidSyntaxException {
235+
private Node<?> parseImpl(ParserEnvironment environment, BiFunction<GraphqlParser, GraphqlAntlrToLanguage, Object[]> nodeFunction) throws InvalidSyntaxException {
208236
MultiSourceReader multiSourceReader;
237+
Reader reader = environment.getDocument();
209238
if (reader instanceof MultiSourceReader) {
210239
multiSourceReader = (MultiSourceReader) reader;
211240
} else {
@@ -231,6 +260,7 @@ public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int
231260
});
232261

233262
// default in the parser options if they are not set
263+
ParserOptions parserOptions = environment.getParserOptions();
234264
parserOptions = Optional.ofNullable(parserOptions).orElse(ParserOptions.getDefaultParserOptions());
235265

236266
// this lexer wrapper allows us to stop lexing when too many tokens are in place. This prevents DOS attacks.
@@ -320,7 +350,7 @@ public int getCharPositionInLine() {
320350
}
321351

322352
private void throwCancelParseIfTooManyTokens(Token token, int maxTokens, MultiSourceReader multiSourceReader) throws ParseCancelledException {
323-
String tokenType = "grammar";
353+
String tokenType = "grammar";
324354
SourceLocation sourceLocation = null;
325355
String offendingToken = null;
326356
if (token != null) {
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package graphql.parser;
2+
3+
import graphql.PublicApi;
4+
5+
import java.io.Reader;
6+
import java.io.StringReader;
7+
import java.util.Locale;
8+
9+
import static graphql.Assert.assertNotNull;
10+
11+
/**
12+
* This is the arguments that can be passed to a {@link Parser}
13+
*/
14+
@PublicApi
15+
public interface ParserEnvironment {
16+
17+
/**
18+
* @return the document to be parsed
19+
*/
20+
Reader getDocument();
21+
22+
/**
23+
* @return the parsing options
24+
*/
25+
ParserOptions getParserOptions();
26+
27+
/**
28+
* @return the locale to produce parsing error messages in
29+
*/
30+
Locale getLocale();
31+
32+
/**
33+
* @return a builder of new parsing options
34+
*/
35+
static Builder newParserEnvironment() {
36+
return new Builder();
37+
}
38+
39+
class Builder {
40+
Reader reader;
41+
ParserOptions parserOptions = ParserOptions.getDefaultParserOptions();
42+
43+
Locale locale = Locale.getDefault();
44+
45+
46+
public Builder() {
47+
}
48+
49+
public Builder document(Reader documentText) {
50+
this.reader = assertNotNull(documentText);
51+
return this;
52+
}
53+
54+
public Builder document(String documentText) {
55+
return document(new StringReader(documentText));
56+
}
57+
58+
public Builder parserOptions(ParserOptions parserOptions) {
59+
this.parserOptions = parserOptions;
60+
return this;
61+
}
62+
63+
public Builder locale(Locale locale) {
64+
this.locale = assertNotNull(locale);
65+
return this;
66+
}
67+
68+
public ParserEnvironment build() {
69+
return new ParserEnvironment() {
70+
@Override
71+
public Reader getDocument() {
72+
return reader;
73+
}
74+
75+
@Override
76+
public ParserOptions getParserOptions() {
77+
return parserOptions;
78+
}
79+
80+
@Override
81+
public Locale getLocale() {
82+
return locale;
83+
}
84+
};
85+
}
86+
}
87+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,24 @@ triple3 : """edge cases \\""" "" " \\"" \\" edge cases"""
858858
operationDefinition.getComments()[0].content == " Represents the 😕 emoji."
859859
}
860860

861+
862+
def "the parser can be invoked via parser environment"() {
863+
def input = '''
864+
# Represents the 😕 emoji.
865+
{
866+
foo
867+
}
868+
'''
869+
when:
870+
def parserEnvironment = ParserEnvironment.newParserEnvironment().document(input).build()
871+
872+
Document document = Parser.parse(parserEnvironment)
873+
OperationDefinition operationDefinition = (document.definitions[0] as OperationDefinition)
874+
875+
then:
876+
operationDefinition.getComments()[0].content == " Represents the 😕 emoji."
877+
}
878+
861879
def "can override antlr to ast"() {
862880

863881
def query = '''

0 commit comments

Comments
 (0)