12 #include "clang/ASTMatchers/Dynamic/Parser.h"
13 #include "clang/Basic/CharInfo.h"
14 #include "clang/Tooling/NodeIntrospection.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/StringSwitch.h"
20 using namespace clang::ast_matchers::dynamic;
29 StringRef QueryParser::lexWord() {
32 return StringRef(
" \t\v\f\r").contains(c);
42 if (
Line.front() ==
'#')
45 Word =
Line.take_until(isWhitespace);
66 :
Word(P->lexWord()), Switch(
Word), P(P),
67 WordCompletionPos(StringRef::npos) {
69 if (P->CompletionPos && P->CompletionPos <=
Word.data() +
Word.size()) {
70 if (P->CompletionPos <
Word.data())
71 WordCompletionPos = 0;
73 WordCompletionPos = P->CompletionPos -
Word.data();
78 bool IsCompletion =
true) {
80 if (WordCompletionPos == StringRef::npos)
81 Switch.Case(CaseStr, Value);
82 else if (CaseStr.size() != 0 && IsCompletion && WordCompletionPos <= CaseStr.size() &&
83 CaseStr.substr(0, WordCompletionPos) ==
84 Word.substr(0, WordCompletionPos))
85 P->Completions.push_back(LineEditor::Completion(
86 (CaseStr.substr(WordCompletionPos) +
" ").str(),
87 std::string(CaseStr)));
91 T Default(T Value) {
return Switch.Default(Value); }
96 unsigned Value = LexOrCompleteWord<unsigned>(
this, ValStr)
101 return new InvalidQuery(
"expected 'true' or 'false', got '" + ValStr +
"'");
103 return new SetQuery<bool>(Var, Value);
106 template <
typename QueryType>
QueryRef QueryParser::parseSetOutputKind() {
108 bool HasIntrospection = tooling::NodeIntrospection::hasIntrospectionSupport();
110 LexOrCompleteWord<unsigned>(
this, ValStr)
114 .Case(
"srcloc",
OK_SrcLoc, HasIntrospection)
117 if (OutKind == ~0u) {
118 return new InvalidQuery(
"expected 'diag', 'print', 'detailed-ast'" +
119 StringRef(HasIntrospection ?
", 'srcloc'" :
"") +
120 " or 'dump', got '" + ValStr +
"'");
125 return new QueryType(&QuerySession::DetailedASTOutput);
127 return new QueryType(&QuerySession::DiagOutput);
129 return new QueryType(&QuerySession::PrintOutput);
131 if (HasIntrospection)
132 return new QueryType(&QuerySession::SrcLocOutput);
133 return new InvalidQuery(
"'srcloc' output support is not available.");
136 llvm_unreachable(
"Invalid output kind");
139 QueryRef QueryParser::parseSetTraversalKind(TraversalKind QuerySession::*Var) {
142 LexOrCompleteWord<unsigned>(
this, ValStr)
143 .Case(
"AsIs", TK_AsIs)
144 .Case(
"IgnoreUnlessSpelledInSource", TK_IgnoreUnlessSpelledInSource)
147 return new InvalidQuery(
"expected traversal kind, got '" + ValStr +
"'");
149 return new SetQuery<TraversalKind>(Var,
static_cast<TraversalKind
>(Value));
153 StringRef Extra =
Line;
154 StringRef ExtraTrimmed = Extra.drop_while(
155 [](
char c) {
return StringRef(
" \t\v\f\r").contains(c); });
157 if ((!ExtraTrimmed.empty() && ExtraTrimmed[0] ==
'\n') ||
158 (ExtraTrimmed.size() >= 2 && ExtraTrimmed[0] ==
'\r' &&
159 ExtraTrimmed[1] ==
'\n'))
160 Q->RemainingContent = Extra;
162 StringRef TrailingWord = lexWord();
163 if (!TrailingWord.empty() && TrailingWord.front() ==
'#') {
164 Line =
Line.drop_until([](
char c) {
return c ==
'\n'; });
165 Line =
Line.drop_while([](
char c) {
return c ==
'\n'; });
168 if (!TrailingWord.empty()) {
169 return new InvalidQuery(
"unexpected extra input: '" + Extra +
"'");
177 enum ParsedQueryKind {
191 enum ParsedQueryVariable {
201 llvm::raw_string_ostream
OS(ErrStr);
202 Diag.printToStreamFull(
OS);
203 return new InvalidQuery(
OS.str());
208 QueryRef QueryParser::completeMatcherExpression() {
209 std::vector<MatcherCompletion> Comps = Parser::completeExpression(
210 Line, CompletionPos -
Line.begin(),
nullptr, &QS.NamedValues);
211 for (
auto I = Comps.begin(),
E = Comps.end(); I !=
E; ++I) {
212 Completions.push_back(LineEditor::Completion(I->TypedText, I->MatcherDecl));
218 StringRef CommandStr;
219 ParsedQueryKind QKind = LexOrCompleteWord<ParsedQueryKind>(
this, CommandStr)
221 .Case(
"#", PQK_Comment,
false)
222 .Case(
"help", PQK_Help)
223 .Case(
"l", PQK_Let,
false)
224 .Case(
"let", PQK_Let)
225 .Case(
"m", PQK_Match,
false)
226 .Case(
"match", PQK_Match)
227 .Case(
"q", PQK_Quit,
false)
228 .Case(
"quit", PQK_Quit)
229 .Case(
"set", PQK_Set)
230 .Case(
"enable", PQK_Enable)
231 .Case(
"disable", PQK_Disable)
232 .Case(
"unlet", PQK_Unlet)
233 .Default(PQK_Invalid);
238 Line =
Line.drop_until([](
char c) {
return c ==
'\n'; });
239 Line =
Line.drop_while([](
char c) {
return c ==
'\n'; });
241 return new NoOpQuery;
245 return endQuery(
new HelpQuery);
248 return endQuery(
new QuitQuery);
251 StringRef
Name = lexWord();
254 return new InvalidQuery(
"expected variable name");
257 return completeMatcherExpression();
260 ast_matchers::dynamic::VariantValue Value;
261 if (!Parser::parseExpression(
Line,
nullptr, &QS.NamedValues, &Value,
263 return makeInvalidQueryFromDiagnostics(Diag);
266 auto *Q =
new LetQuery(
Name, Value);
267 Q->RemainingContent =
Line;
273 return completeMatcherExpression();
276 auto MatcherSource =
Line.ltrim();
277 auto OrigMatcherSource = MatcherSource;
278 Optional<DynTypedMatcher> Matcher = Parser::parseMatcherExpression(
279 MatcherSource,
nullptr, &QS.NamedValues, &Diag);
281 return makeInvalidQueryFromDiagnostics(Diag);
283 auto ActualSource = OrigMatcherSource.slice(0, OrigMatcherSource.size() -
284 MatcherSource.size());
285 auto *Q =
new MatchQuery(ActualSource, *Matcher);
286 Q->RemainingContent = MatcherSource;
292 ParsedQueryVariable Var =
293 LexOrCompleteWord<ParsedQueryVariable>(
this, VarStr)
294 .Case(
"output", PQV_Output)
295 .Case(
"bind-root", PQV_BindRoot)
296 .Case(
"print-matcher", PQV_PrintMatcher)
297 .Case(
"traversal", PQV_Traversal)
298 .Default(PQV_Invalid);
300 return new InvalidQuery(
"expected variable name");
301 if (Var == PQV_Invalid)
302 return new InvalidQuery(
"unknown variable: '" + VarStr +
"'");
307 Q = parseSetOutputKind<SetExclusiveOutputQuery>();
310 Q = parseSetBool(&QuerySession::BindRoot);
312 case PQV_PrintMatcher:
313 Q = parseSetBool(&QuerySession::PrintMatcher);
316 Q = parseSetTraversalKind(&QuerySession::TK);
319 llvm_unreachable(
"Invalid query kind");
327 ParsedQueryVariable Var =
328 LexOrCompleteWord<ParsedQueryVariable>(
this, VarStr)
329 .Case(
"output", PQV_Output)
330 .Default(PQV_Invalid);
332 return new InvalidQuery(
"expected variable name");
333 if (Var == PQV_Invalid)
334 return new InvalidQuery(
"unknown variable: '" + VarStr +
"'");
338 if (QKind == PQK_Enable)
339 Q = parseSetOutputKind<EnableOutputQuery>();
340 else if (QKind == PQK_Disable)
341 Q = parseSetOutputKind<DisableOutputQuery>();
343 llvm_unreachable(
"Invalid query kind");
348 StringRef
Name = lexWord();
351 return new InvalidQuery(
"expected variable name");
353 return endQuery(
new LetQuery(
Name, VariantValue()));
357 return new InvalidQuery(
"unknown command: " + CommandStr);
360 llvm_unreachable(
"Invalid query kind");
367 std::vector<LineEditor::Completion>
370 P.CompletionPos =
Line.data() +
Pos;
373 return P.Completions;