Repository navigation
Expand file tree
/
Copy pathParser.java
More file actions
257 lines (236 loc) · 10.4 KB
/
Copy pathParser.java
File metadata and controls
257 lines (236 loc) · 10.4 KB
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
package graphql.parser;
import graphql.PublicApi;
import graphql.language.Document;
import graphql.language.Node;
import graphql.language.SourceLocation;
import graphql.language.Value;
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 java.io.IOException;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.util.List;
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 {
/**
* 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 {@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 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, "Invalid syntax: " + msg, preview, null, null);
}
});
CommonTokenStream tokens = new CommonTokenStream(lexer);
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);
}
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 its invalid. We make sure its 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;
}
/**
* 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);
}
}