@@ -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 ) {
0 commit comments