|
21 | 21 | import org.antlr.v4.runtime.atn.PredictionMode; |
22 | 22 | import org.antlr.v4.runtime.tree.ParseTreeListener; |
23 | 23 | import org.antlr.v4.runtime.tree.TerminalNode; |
| 24 | +import org.jetbrains.annotations.NotNull; |
24 | 25 |
|
25 | 26 | import java.io.IOException; |
26 | 27 | import java.io.Reader; |
|
29 | 30 | import java.util.Optional; |
30 | 31 | import java.util.function.BiConsumer; |
31 | 32 | import java.util.function.BiFunction; |
| 33 | +import java.util.function.Consumer; |
32 | 34 |
|
33 | 35 | /** |
34 | 36 | * This can parse graphql syntax, both Query syntax and Schema Definition Language (SDL) syntax, into an |
35 | 37 | * Abstract Syntax Tree (AST) represented by a {@link Document} |
36 | 38 | * <p> |
37 | 39 | * You should not generally need to call this class as the {@link graphql.GraphQL} code sets this up for you |
38 | 40 | * but if you are doing specific graphql utilities this class is essential. |
39 | | - * |
| 41 | + * <p> |
40 | 42 | * Graphql syntax has a series of characters, such as spaces, new lines and commas that are not considered relevant |
41 | 43 | * to the syntax. However they can be captured and associated with the AST elements they belong to. |
42 | | - * |
| 44 | + * <p> |
43 | 45 | * This costs more memory but for certain use cases (like editors) this maybe be useful. We have chosen to no capture |
44 | 46 | * ignored characters by default but you can turn this on, either per parse or statically for the whole JVM |
45 | 47 | * via {@link ParserOptions#setDefaultParserOptions(ParserOptions)} ()}} |
@@ -205,43 +207,18 @@ private Type<?> parseTypeImpl(String input) throws InvalidSyntaxException { |
205 | 207 | } |
206 | 208 |
|
207 | 209 | private Node<?> parseImpl(Reader reader, BiFunction<GraphqlParser, GraphqlAntlrToLanguage, Object[]> nodeFunction, ParserOptions parserOptions) throws InvalidSyntaxException { |
208 | | - MultiSourceReader multiSourceReader; |
209 | | - if (reader instanceof MultiSourceReader) { |
210 | | - multiSourceReader = (MultiSourceReader) reader; |
211 | | - } else { |
212 | | - multiSourceReader = MultiSourceReader.newMultiSourceReader() |
213 | | - .reader(reader, null).build(); |
214 | | - } |
215 | | - CodePointCharStream charStream; |
216 | | - try { |
217 | | - charStream = CharStreams.fromReader(multiSourceReader); |
218 | | - } catch (IOException e) { |
219 | | - throw new UncheckedIOException(e); |
220 | | - } |
| 210 | + parserOptions = Optional.ofNullable(parserOptions).orElse(ParserOptions.getDefaultParserOptions()); |
221 | 211 |
|
222 | | - GraphqlLexer lexer = new GraphqlLexer(charStream); |
223 | | - lexer.removeErrorListeners(); |
224 | | - lexer.addErrorListener(new BaseErrorListener() { |
225 | | - @Override |
226 | | - public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { |
227 | | - SourceLocation sourceLocation = AntlrHelper.createSourceLocation(multiSourceReader, line, charPositionInLine); |
228 | | - String preview = AntlrHelper.createPreview(multiSourceReader, line); |
229 | | - throw new InvalidSyntaxException(sourceLocation, msg, preview, null, null); |
230 | | - } |
231 | | - }); |
| 212 | + MultiSourceReader multiSourceReader = setupMultiSourceReader(reader, parserOptions); |
232 | 213 |
|
233 | | - // default in the parser options if they are not set |
234 | | - parserOptions = Optional.ofNullable(parserOptions).orElse(ParserOptions.getDefaultParserOptions()); |
| 214 | + SafeTokenReader safeTokenReader = setupSafeTokenReader(parserOptions, multiSourceReader); |
| 215 | + |
| 216 | + CodePointCharStream charStream = setupCharStream(safeTokenReader); |
| 217 | + |
| 218 | + GraphqlLexer lexer = setupGraphqlLexer(multiSourceReader, charStream); |
235 | 219 |
|
236 | 220 | // this lexer wrapper allows us to stop lexing when too many tokens are in place. This prevents DOS attacks. |
237 | | - int maxTokens = parserOptions.getMaxTokens(); |
238 | | - int maxWhitespaceTokens = parserOptions.getMaxWhitespaceTokens(); |
239 | | - BiConsumer<Integer, Token> onTooManyTokens = (maxTokenCount, token) -> throwIfTokenProblems( |
240 | | - token, |
241 | | - maxTokenCount, |
242 | | - multiSourceReader, |
243 | | - ParseCancelledException.class); |
244 | | - SafeTokenSource safeTokenSource = new SafeTokenSource(lexer, maxTokens, maxWhitespaceTokens, onTooManyTokens); |
| 221 | + SafeTokenSource safeTokenSource = getSafeTokenSource(parserOptions, multiSourceReader, lexer); |
245 | 222 |
|
246 | 223 | CommonTokenStream tokens = new CommonTokenStream(safeTokenSource); |
247 | 224 |
|
@@ -285,6 +262,65 @@ public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int |
285 | 262 | return node; |
286 | 263 | } |
287 | 264 |
|
| 265 | + private static MultiSourceReader setupMultiSourceReader(Reader reader, ParserOptions parserOptions) { |
| 266 | + MultiSourceReader multiSourceReader; |
| 267 | + if (reader instanceof MultiSourceReader) { |
| 268 | + multiSourceReader = (MultiSourceReader) reader; |
| 269 | + } else { |
| 270 | + multiSourceReader = MultiSourceReader.newMultiSourceReader() |
| 271 | + .reader(reader, null).build(); |
| 272 | + } |
| 273 | + return multiSourceReader; |
| 274 | + } |
| 275 | + |
| 276 | + @NotNull |
| 277 | + private static SafeTokenReader setupSafeTokenReader(ParserOptions parserOptions, MultiSourceReader multiSourceReader) { |
| 278 | + int maxCharacters = parserOptions.getMaxCharacters(); |
| 279 | + Consumer<Integer> onTooManyCharacters = it -> { |
| 280 | + String msg = String.format("More than %d characters have been presented. To prevent Denial Of Service attacks, parsing has been cancelled.", maxCharacters); |
| 281 | + throw new ParseCancelledTooManyCharsException(msg, maxCharacters); |
| 282 | + }; |
| 283 | + return new SafeTokenReader(multiSourceReader, maxCharacters, onTooManyCharacters); |
| 284 | + } |
| 285 | + |
| 286 | + @NotNull |
| 287 | + private static CodePointCharStream setupCharStream(SafeTokenReader safeTokenReader) { |
| 288 | + CodePointCharStream charStream; |
| 289 | + try { |
| 290 | + charStream = CharStreams.fromReader(safeTokenReader); |
| 291 | + } catch (IOException e) { |
| 292 | + throw new UncheckedIOException(e); |
| 293 | + } |
| 294 | + return charStream; |
| 295 | + } |
| 296 | + |
| 297 | + @NotNull |
| 298 | + private static GraphqlLexer setupGraphqlLexer(MultiSourceReader multiSourceReader, CodePointCharStream charStream) { |
| 299 | + GraphqlLexer lexer = new GraphqlLexer(charStream); |
| 300 | + lexer.removeErrorListeners(); |
| 301 | + lexer.addErrorListener(new BaseErrorListener() { |
| 302 | + @Override |
| 303 | + public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { |
| 304 | + SourceLocation sourceLocation = AntlrHelper.createSourceLocation(multiSourceReader, line, charPositionInLine); |
| 305 | + String preview = AntlrHelper.createPreview(multiSourceReader, line); |
| 306 | + throw new InvalidSyntaxException(sourceLocation, msg, preview, null, null); |
| 307 | + } |
| 308 | + }); |
| 309 | + return lexer; |
| 310 | + } |
| 311 | + |
| 312 | + @NotNull |
| 313 | + private SafeTokenSource getSafeTokenSource(ParserOptions parserOptions, MultiSourceReader multiSourceReader, GraphqlLexer lexer) { |
| 314 | + int maxTokens = parserOptions.getMaxTokens(); |
| 315 | + int maxWhitespaceTokens = parserOptions.getMaxWhitespaceTokens(); |
| 316 | + BiConsumer<Integer, Token> onTooManyTokens = (maxTokenCount, token) -> throwIfTokenProblems( |
| 317 | + token, |
| 318 | + maxTokenCount, |
| 319 | + multiSourceReader, |
| 320 | + ParseCancelledException.class); |
| 321 | + return new SafeTokenSource(lexer, maxTokens, maxWhitespaceTokens, onTooManyTokens); |
| 322 | + } |
| 323 | + |
288 | 324 | private void setupParserListener(MultiSourceReader multiSourceReader, GraphqlParser parser, GraphqlAntlrToLanguage toLanguage) { |
289 | 325 | ParserOptions parserOptions = toLanguage.getParserOptions(); |
290 | 326 | ParsingListener parsingListener = parserOptions.getParsingListener(); |
@@ -365,7 +401,8 @@ private void throwIfTokenProblems(Token token, int maxLimit, MultiSourceReader m |
365 | 401 | throw new ParseCancelledTooDeepException(msg, sourceLocation, offendingToken, maxLimit, tokenType); |
366 | 402 | } |
367 | 403 | String msg = String.format("More than %d %s tokens have been presented. To prevent Denial Of Service attacks, parsing has been cancelled.", maxLimit, tokenType); |
368 | | - throw new ParseCancelledException(msg, sourceLocation, offendingToken); } |
| 404 | + throw new ParseCancelledException(msg, sourceLocation, offendingToken); |
| 405 | + } |
369 | 406 |
|
370 | 407 | /** |
371 | 408 | * Allows you to override the ANTLR to AST code. |
|
0 commit comments