-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Parser.java
364 lines (327 loc) · 15 KB
/
Parser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package graphql.parser;
import graphql.Internal;
import graphql.PublicApi;
import graphql.language.Document;
import graphql.language.Node;
import graphql.language.SourceLocation;
import graphql.language.Type;
import graphql.language.Value;
import graphql.parser.antlr.GraphqlBaseListener;
import graphql.parser.antlr.GraphqlLexer;
import graphql.parser.antlr.GraphqlParser;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CodePointCharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.atn.PredictionMode;
import org.antlr.v4.runtime.tree.ParseTreeListener;
import org.antlr.v4.runtime.tree.TerminalNode;
import java.io.IOException;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
/**
* This can parse graphql syntax, both Query syntax and Schema Definition Language (SDL) syntax, into an
* Abstract Syntax Tree (AST) represented by a {@link Document}
* <p>
* You should not generally need to call this class as the {@link graphql.GraphQL} code sets this up for you
* but if you are doing specific graphql utilities this class is essential.
*
* Graphql syntax has a series of characters, such as spaces, new lines and commas that are not considered relevant
* to the syntax. However they can be captured and associated with the AST elements they belong to.
*
* This costs more memory but for certain use cases (like editors) this maybe be useful. We have chosen to no capture
* ignored characters by default but you can turn this on, either per parse or statically for the whole JVM
* via {@link ParserOptions#setDefaultParserOptions(ParserOptions)} ()}}
*
* @see graphql.language.IgnoredChar
*/
@PublicApi
public class Parser {
@Internal
public static final int CHANNEL_COMMENTS = 2;
@Internal
public static final int CHANNEL_IGNORED_CHARS = 3;
/**
* Parses a string input into a graphql AST {@link Document}
*
* @param input the input to parse
*
* @return an AST {@link Document}
*
* @throws InvalidSyntaxException if the input is not valid graphql syntax
*/
public static Document parse(String input) throws InvalidSyntaxException {
return new Parser().parseDocument(input);
}
/**
* Parses a string input into a graphql AST {@link Value}
*
* @param input the input to parse
*
* @return an AST {@link Value}
*
* @throws InvalidSyntaxException if the input is not valid graphql syntax
*/
public static Value<?> parseValue(String input) throws InvalidSyntaxException {
return new Parser().parseValueImpl(input);
}
/**
* Parses a string input into a graphql AST Type
*
* @param input the input to parse
*
* @return an AST {@link Type}
*
* @throws InvalidSyntaxException if the input is not valid graphql syntax
*/
public static Type<?> parseType(String input) throws InvalidSyntaxException {
return new Parser().parseTypeImpl(input);
}
/**
* Parses a string input into a graphql AST {@link Document}
*
* @param input the input to parse
*
* @return an AST {@link Document}
*
* @throws InvalidSyntaxException if the input is not valid graphql syntax
*/
public Document parseDocument(String input) throws InvalidSyntaxException {
return parseDocument(input, (ParserOptions) null);
}
/**
* Parses a string input into a graphql AST {@link Document}
*
* @param input the input to parse
* @param sourceName - the name to attribute to the input text in {@link SourceLocation#getSourceName()}
*
* @return an AST {@link Document}
*
* @throws InvalidSyntaxException if the input is not valid graphql syntax
*/
public Document parseDocument(String input, String sourceName) throws InvalidSyntaxException {
MultiSourceReader multiSourceReader = MultiSourceReader.newMultiSourceReader()
.string(input, sourceName)
.trackData(true)
.build();
return parseDocument(multiSourceReader);
}
/**
* Parses a string input into a graphql AST {@link Document}
*
* @param input the input to parse
* @param parserOptions the parser options
*
* @return an AST {@link Document}
*
* @throws InvalidSyntaxException if the input is not valid graphql syntax
*/
public Document parseDocument(String input, ParserOptions parserOptions) throws InvalidSyntaxException {
MultiSourceReader multiSourceReader = MultiSourceReader.newMultiSourceReader()
.string(input, null)
.trackData(true)
.build();
return parseDocument(multiSourceReader, parserOptions);
}
/**
* Parses reader input into a graphql AST {@link Document}
*
* @param reader the reader input to parse
*
* @return an AST {@link Document}
*
* @throws InvalidSyntaxException if the input is not valid graphql syntax
*/
public Document parseDocument(Reader reader) throws InvalidSyntaxException {
return parseDocumentImpl(reader, null);
}
/**
* Parses reader input into a graphql AST {@link Document}
*
* @param reader the reader input to parse
* @param parserOptions the parser options
*
* @return an AST {@link Document}
*
* @throws InvalidSyntaxException if the input is not valid graphql syntax
*/
public Document parseDocument(Reader reader, ParserOptions parserOptions) throws InvalidSyntaxException {
return parseDocumentImpl(reader, parserOptions);
}
private Document parseDocumentImpl(Reader reader, ParserOptions parserOptions) throws InvalidSyntaxException {
BiFunction<GraphqlParser, GraphqlAntlrToLanguage, Object[]> nodeFunction = (parser, toLanguage) -> {
GraphqlParser.DocumentContext documentContext = parser.document();
Document doc = toLanguage.createDocument(documentContext);
return new Object[]{documentContext, doc};
};
return (Document) parseImpl(reader, nodeFunction, parserOptions);
}
private Value<?> parseValueImpl(String input) throws InvalidSyntaxException {
BiFunction<GraphqlParser, GraphqlAntlrToLanguage, Object[]> nodeFunction = (parser, toLanguage) -> {
GraphqlParser.ValueContext documentContext = parser.value();
Value<?> value = toLanguage.createValue(documentContext);
return new Object[]{documentContext, value};
};
MultiSourceReader multiSourceReader = MultiSourceReader.newMultiSourceReader()
.string(input, null)
.trackData(true)
.build();
return (Value<?>) parseImpl(multiSourceReader, nodeFunction, null);
}
private Type<?> parseTypeImpl(String input) throws InvalidSyntaxException {
BiFunction<GraphqlParser, GraphqlAntlrToLanguage, Object[]> nodeFunction = (parser, toLanguage) -> {
final GraphqlParser.TypeContext documentContext = parser.type();
Type<?> value = toLanguage.createType(documentContext);
return new Object[]{documentContext, value};
};
MultiSourceReader multiSourceReader = MultiSourceReader.newMultiSourceReader()
.string(input, null)
.trackData(true)
.build();
return (Type<?>) parseImpl(multiSourceReader, nodeFunction, null);
}
private Node<?> parseImpl(Reader reader, BiFunction<GraphqlParser, GraphqlAntlrToLanguage, Object[]> nodeFunction, ParserOptions parserOptions) throws InvalidSyntaxException {
MultiSourceReader multiSourceReader;
if (reader instanceof MultiSourceReader) {
multiSourceReader = (MultiSourceReader) reader;
} else {
multiSourceReader = MultiSourceReader.newMultiSourceReader()
.reader(reader, null).build();
}
CodePointCharStream charStream;
try {
charStream = CharStreams.fromReader(multiSourceReader);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
GraphqlLexer lexer = new GraphqlLexer(charStream);
lexer.removeErrorListeners();
lexer.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
SourceLocation sourceLocation = AntlrHelper.createSourceLocation(multiSourceReader, line, charPositionInLine);
String preview = AntlrHelper.createPreview(multiSourceReader, line);
throw new InvalidSyntaxException(sourceLocation, msg, preview, null, null);
}
});
// default in the parser options if they are not set
parserOptions = Optional.ofNullable(parserOptions).orElse(ParserOptions.getDefaultParserOptions());
// this lexer wrapper allows us to stop lexing when too many tokens are in place. This prevents DOS attacks.
int maxTokens = parserOptions.getMaxTokens();
int maxWhitespaceTokens = parserOptions.getMaxWhitespaceTokens();
BiConsumer<Integer, Token> onTooManyTokens = (maxTokenCount, token) -> throwCancelParseIfTooManyTokens(token, maxTokenCount, multiSourceReader);
SafeTokenSource safeTokenSource = new SafeTokenSource(lexer, maxTokens, maxWhitespaceTokens, onTooManyTokens);
CommonTokenStream tokens = new CommonTokenStream(safeTokenSource);
GraphqlParser parser = new GraphqlParser(tokens);
parser.removeErrorListeners();
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
ExtendedBailStrategy bailStrategy = new ExtendedBailStrategy(multiSourceReader);
parser.setErrorHandler(bailStrategy);
// preserve old protected call semantics - remove at some point
GraphqlAntlrToLanguage toLanguage = getAntlrToLanguage(tokens, multiSourceReader);
if (toLanguage == null) {
toLanguage = getAntlrToLanguage(tokens, multiSourceReader, parserOptions);
}
setupParserListener(multiSourceReader, parser, toLanguage);
//
// parsing starts ...... now!
//
Object[] contextAndNode = nodeFunction.apply(parser, toLanguage);
ParserRuleContext parserRuleContext = (ParserRuleContext) contextAndNode[0];
Node<?> node = (Node<?>) contextAndNode[1];
Token stop = parserRuleContext.getStop();
List<Token> allTokens = tokens.getTokens();
if (stop != null && allTokens != null && !allTokens.isEmpty()) {
Token last = allTokens.get(allTokens.size() - 1);
//
// do we have more tokens in the stream than we consumed in the parse?
// if yes then it's invalid. We make sure it's the same channel
boolean notEOF = last.getType() != Token.EOF;
boolean lastGreaterThanDocument = last.getTokenIndex() > stop.getTokenIndex();
boolean sameChannel = last.getChannel() == stop.getChannel();
if (notEOF && lastGreaterThanDocument && sameChannel) {
throw bailStrategy.mkMoreTokensException(last);
}
}
return node;
}
private void setupParserListener(MultiSourceReader multiSourceReader, GraphqlParser parser, GraphqlAntlrToLanguage toLanguage) {
ParserOptions parserOptions = toLanguage.getParserOptions();
ParsingListener parsingListener = parserOptions.getParsingListener();
int maxTokens = parserOptions.getMaxTokens();
// prevent a billion laugh attacks by restricting how many tokens we allow
ParseTreeListener listener = new GraphqlBaseListener() {
int count = 0;
@Override
public void visitTerminal(TerminalNode node) {
final Token token = node.getSymbol();
parsingListener.onToken(new ParsingListener.Token() {
@Override
public String getText() {
return token == null ? null : token.getText();
}
@Override
public int getLine() {
return token == null ? -1 : token.getLine();
}
@Override
public int getCharPositionInLine() {
return token == null ? -1 : token.getCharPositionInLine();
}
});
count++;
if (count > maxTokens) {
throwCancelParseIfTooManyTokens(token, maxTokens, multiSourceReader);
}
}
};
parser.addParseListener(listener);
}
private void throwCancelParseIfTooManyTokens(Token token, int maxTokens, MultiSourceReader multiSourceReader) throws ParseCancelledException {
String tokenType = "grammar";
SourceLocation sourceLocation = null;
String offendingToken = null;
if (token != null) {
int channel = token.getChannel();
tokenType = channel == CHANNEL_IGNORED_CHARS ? "whitespace" : (channel == CHANNEL_COMMENTS ? "comments" : "grammar");
offendingToken = token.getText();
sourceLocation = AntlrHelper.createSourceLocation(multiSourceReader, token.getLine(), token.getCharPositionInLine());
}
String msg = String.format("More than %d %s tokens have been presented. To prevent Denial Of Service attacks, parsing has been cancelled.", maxTokens, tokenType);
throw new ParseCancelledException(msg, sourceLocation, offendingToken);
}
/**
* Allows you to override the ANTLR to AST code.
*
* @param tokens the token stream
* @param multiSourceReader the source of the query document
*
* @return a new GraphqlAntlrToLanguage instance
*
* @deprecated - really should use {@link #getAntlrToLanguage(CommonTokenStream, MultiSourceReader, ParserOptions)}
*/
@Deprecated
protected GraphqlAntlrToLanguage getAntlrToLanguage(CommonTokenStream tokens, MultiSourceReader multiSourceReader) {
return null;
}
/**
* Allows you to override the ANTLR to AST code.
*
* @param tokens the token stream
* @param multiSourceReader the source of the query document
* @param parserOptions - the parser options
*
* @return a new GraphqlAntlrToLanguage instance
*/
protected GraphqlAntlrToLanguage getAntlrToLanguage(CommonTokenStream tokens, MultiSourceReader multiSourceReader, ParserOptions parserOptions) {
return new GraphqlAntlrToLanguage(tokens, multiSourceReader, parserOptions);
}
}