- Notifications
You must be signed in to change notification settings - Fork 13.4k
/
Copy pathQueryParser.cpp
376 lines (324 loc) · 11.4 KB
/
QueryParser.cpp
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
365
366
367
368
369
370
371
372
373
374
375
376
//===---- QueryParser.cpp - clang-query command parser --------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include"QueryParser.h"
#include"Query.h"
#include"QuerySession.h"
#include"clang/ASTMatchers/Dynamic/Parser.h"
#include"clang/Basic/CharInfo.h"
#include"llvm/ADT/StringRef.h"
#include"llvm/ADT/StringSwitch.h"
#include<optional>
#include<set>
usingnamespacellvm;
usingnamespaceclang::ast_matchers::dynamic;
namespaceclang {
namespacequery {
// Lex any amount of whitespace followed by a "word" (any sequence of
// non-whitespace characters) from the start of region [Begin,End). If no word
// is found before End, return StringRef(). Begin is adjusted to exclude the
// lexed region.
StringRef QueryParser::lexWord() {
// Don't trim newlines.
Line = Line.ltrim("\t\v\f\r");
if (Line.empty())
// Even though the Line is empty, it contains a pointer and
// a (zero) length. The pointer is used in the LexOrCompleteWord
// code completion.
return Line;
StringRef Word;
if (Line.front() == '#')
Word = Line.substr(0, 1);
else
Word = Line.take_until(isWhitespace);
Line = Line.drop_front(Word.size());
return Word;
}
// This is the StringSwitch-alike used by lexOrCompleteWord below. See that
// function for details.
template <typename T> structQueryParser::LexOrCompleteWord {
StringRef Word;
StringSwitch<T> Switch;
QueryParser *P;
// Set to the completion point offset in Word, or StringRef::npos if
// completion point not in Word.
size_t WordCompletionPos;
// Lexes a word and stores it in Word. Returns a LexOrCompleteWord<T> object
// that can be used like a llvm::StringSwitch<T>, but adds cases as possible
// completions if the lexed word contains the completion point.
LexOrCompleteWord(QueryParser *P, StringRef &OutWord)
: Word(P->lexWord()), Switch(Word), P(P),
WordCompletionPos(StringRef::npos) {
OutWord = Word;
if (P->CompletionPos && P->CompletionPos <= Word.data() + Word.size()) {
if (P->CompletionPos < Word.data())
WordCompletionPos = 0;
else
WordCompletionPos = P->CompletionPos - Word.data();
}
}
LexOrCompleteWord &Case(llvm::StringLiteral CaseStr, const T &Value,
bool IsCompletion = true) {
if (WordCompletionPos == StringRef::npos)
Switch.Case(CaseStr, Value);
elseif (CaseStr.size() != 0 && IsCompletion && WordCompletionPos <= CaseStr.size() &&
CaseStr.substr(0, WordCompletionPos) ==
Word.substr(0, WordCompletionPos))
P->Completions.push_back(LineEditor::Completion(
(CaseStr.substr(WordCompletionPos) + "").str(),
std::string(CaseStr)));
return *this;
}
T Default(T Value) { return Switch.Default(Value); }
};
QueryRef QueryParser::parseSetBool(bool QuerySession::*Var) {
StringRef ValStr;
unsigned Value = LexOrCompleteWord<unsigned>(this, ValStr)
.Case("false", 0)
.Case("true", 1)
.Default(~0u);
if (Value == ~0u) {
returnnewInvalidQuery("expected 'true' or 'false', got '" + ValStr + "'");
}
returnnew SetQuery<bool>(Var, Value);
}
template <typename QueryType> QueryRef QueryParser::parseSetOutputKind() {
StringRef ValStr;
unsigned OutKind = LexOrCompleteWord<unsigned>(this, ValStr)
.Case("diag", OK_Diag)
.Case("print", OK_Print)
.Case("detailed-ast", OK_DetailedAST)
.Case("dump", OK_DetailedAST)
.Default(~0u);
if (OutKind == ~0u) {
returnnewInvalidQuery("expected 'diag', 'print', 'detailed-ast' or "
"'dump', got '" +
ValStr + "'");
}
switch (OutKind) {
case OK_DetailedAST:
returnnewQueryType(&QuerySession::DetailedASTOutput);
case OK_Diag:
returnnewQueryType(&QuerySession::DiagOutput);
case OK_Print:
returnnewQueryType(&QuerySession::PrintOutput);
}
llvm_unreachable("Invalid output kind");
}
QueryRef QueryParser::parseSetTraversalKind(TraversalKind QuerySession::*Var) {
StringRef ValStr;
unsigned Value =
LexOrCompleteWord<unsigned>(this, ValStr)
.Case("AsIs", TK_AsIs)
.Case("IgnoreUnlessSpelledInSource", TK_IgnoreUnlessSpelledInSource)
.Default(~0u);
if (Value == ~0u) {
returnnewInvalidQuery("expected traversal kind, got '" + ValStr + "'");
}
returnnew SetQuery<TraversalKind>(Var, static_cast<TraversalKind>(Value));
}
QueryRef QueryParser::endQuery(QueryRef Q) {
StringRef Extra = Line;
StringRef ExtraTrimmed = Extra.ltrim("\t\v\f\r");
if (ExtraTrimmed.starts_with('\n') || ExtraTrimmed.starts_with("\r\n"))
Q->RemainingContent = Extra;
else {
StringRef TrailingWord = lexWord();
if (TrailingWord.starts_with('#')) {
Line = Line.drop_until([](char c) { return c == '\n'; });
Line = Line.drop_while([](char c) { return c == '\n'; });
returnendQuery(Q);
}
if (!TrailingWord.empty()) {
returnnewInvalidQuery("unexpected extra input: '" + Extra + "'");
}
}
return Q;
}
namespace {
enum ParsedQueryKind {
PQK_Invalid,
PQK_Comment,
PQK_NoOp,
PQK_Help,
PQK_Let,
PQK_Match,
PQK_Set,
PQK_Unlet,
PQK_Quit,
PQK_Enable,
PQK_Disable,
PQK_File
};
enum ParsedQueryVariable {
PQV_Invalid,
PQV_Output,
PQV_BindRoot,
PQV_PrintMatcher,
PQV_EnableProfile,
PQV_Traversal
};
QueryRef makeInvalidQueryFromDiagnostics(const Diagnostics &Diag) {
std::string ErrStr;
llvm::raw_string_ostream OS(ErrStr);
Diag.printToStreamFull(OS);
returnnewInvalidQuery(OS.str());
}
} // namespace
QueryRef QueryParser::completeMatcherExpression() {
std::vector<MatcherCompletion> Comps = Parser::completeExpression(
Line, CompletionPos - Line.begin(), nullptr, &QS.NamedValues);
for (auto I = Comps.begin(), E = Comps.end(); I != E; ++I) {
Completions.push_back(LineEditor::Completion(I->TypedText, I->MatcherDecl));
}
returnQueryRef();
}
QueryRef QueryParser::doParse() {
StringRef CommandStr;
ParsedQueryKind QKind = LexOrCompleteWord<ParsedQueryKind>(this, CommandStr)
.Case("", PQK_NoOp)
.Case("#", PQK_Comment, /*IsCompletion=*/false)
.Case("help", PQK_Help)
.Case("l", PQK_Let, /*IsCompletion=*/false)
.Case("let", PQK_Let)
.Case("m", PQK_Match, /*IsCompletion=*/false)
.Case("match", PQK_Match)
.Case("q", PQK_Quit, /*IsCompletion=*/false)
.Case("quit", PQK_Quit)
.Case("set", PQK_Set)
.Case("enable", PQK_Enable)
.Case("disable", PQK_Disable)
.Case("unlet", PQK_Unlet)
.Case("f", PQK_File, /*IsCompletion=*/false)
.Case("file", PQK_File)
.Default(PQK_Invalid);
switch (QKind) {
case PQK_Comment:
case PQK_NoOp:
Line = Line.drop_until([](char c) { return c == '\n'; });
Line = Line.drop_while([](char c) { return c == '\n'; });
if (Line.empty())
returnnew NoOpQuery;
returndoParse();
case PQK_Help:
returnendQuery(new HelpQuery);
case PQK_Quit:
returnendQuery(new QuitQuery);
case PQK_Let: {
StringRef Name = lexWord();
if (Name.empty())
returnnewInvalidQuery("expected variable name");
if (CompletionPos)
returncompleteMatcherExpression();
Diagnostics Diag;
ast_matchers::dynamic::VariantValue Value;
if (!Parser::parseExpression(Line, nullptr, &QS.NamedValues, &Value,
&Diag)) {
returnmakeInvalidQueryFromDiagnostics(Diag);
}
auto *Q = newLetQuery(Name, Value);
Q->RemainingContent = Line;
return Q;
}
case PQK_Match: {
if (CompletionPos)
returncompleteMatcherExpression();
Diagnostics Diag;
auto MatcherSource = Line.ltrim();
auto OrigMatcherSource = MatcherSource;
std::optional<DynTypedMatcher> Matcher = Parser::parseMatcherExpression(
MatcherSource, nullptr, &QS.NamedValues, &Diag);
if (!Matcher) {
returnmakeInvalidQueryFromDiagnostics(Diag);
}
auto ActualSource = OrigMatcherSource.slice(0, OrigMatcherSource.size() -
MatcherSource.size());
auto *Q = newMatchQuery(ActualSource, *Matcher);
Q->RemainingContent = MatcherSource;
return Q;
}
case PQK_Set: {
StringRef VarStr;
ParsedQueryVariable Var =
LexOrCompleteWord<ParsedQueryVariable>(this, VarStr)
.Case("output", PQV_Output)
.Case("bind-root", PQV_BindRoot)
.Case("print-matcher", PQV_PrintMatcher)
.Case("enable-profile", PQV_EnableProfile)
.Case("traversal", PQV_Traversal)
.Default(PQV_Invalid);
if (VarStr.empty())
returnnewInvalidQuery("expected variable name");
if (Var == PQV_Invalid)
returnnewInvalidQuery("unknown variable: '" + VarStr + "'");
QueryRef Q;
switch (Var) {
case PQV_Output:
Q = parseSetOutputKind<SetExclusiveOutputQuery>();
break;
case PQV_BindRoot:
Q = parseSetBool(&QuerySession::BindRoot);
break;
case PQV_PrintMatcher:
Q = parseSetBool(&QuerySession::PrintMatcher);
break;
case PQV_EnableProfile:
Q = parseSetBool(&QuerySession::EnableProfile);
break;
case PQV_Traversal:
Q = parseSetTraversalKind(&QuerySession::TK);
break;
case PQV_Invalid:
llvm_unreachable("Invalid query kind");
}
returnendQuery(Q);
}
case PQK_Enable:
case PQK_Disable: {
StringRef VarStr;
ParsedQueryVariable Var =
LexOrCompleteWord<ParsedQueryVariable>(this, VarStr)
.Case("output", PQV_Output)
.Default(PQV_Invalid);
if (VarStr.empty())
returnnewInvalidQuery("expected variable name");
if (Var == PQV_Invalid)
returnnewInvalidQuery("unknown variable: '" + VarStr + "'");
QueryRef Q;
if (QKind == PQK_Enable)
Q = parseSetOutputKind<EnableOutputQuery>();
elseif (QKind == PQK_Disable)
Q = parseSetOutputKind<DisableOutputQuery>();
else
llvm_unreachable("Invalid query kind");
returnendQuery(Q);
}
case PQK_Unlet: {
StringRef Name = lexWord();
if (Name.empty())
returnnewInvalidQuery("expected variable name");
returnendQuery(newLetQuery(Name, VariantValue()));
}
case PQK_File:
returnnewFileQuery(Line);
case PQK_Invalid:
returnnewInvalidQuery("unknown command: " + CommandStr);
}
llvm_unreachable("Invalid query kind");
}
QueryRef QueryParser::parse(StringRef Line, const QuerySession &QS) {
returnQueryParser(Line, QS).doParse();
}
std::vector<LineEditor::Completion>
QueryParser::complete(StringRef Line, size_t Pos, const QuerySession &QS) {
QueryParser P(Line, QS);
P.CompletionPos = Line.data() + Pos;
P.doParse();
return P.Completions;
}
} // namespace query
} // namespace clang