blob: 9930c530c081407a5524a93a4d02c0088e99677a [file] [log] [blame]
Manuel Klimekf7f295f2013-05-14 09:13:00 +00001//===--- Parser.cpp - Matcher expression parser -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief Recursive parser implementation for the matcher expression grammar.
12///
13//===----------------------------------------------------------------------===//
14
Manuel Klimekf7f295f2013-05-14 09:13:00 +000015#include "clang/ASTMatchers/Dynamic/Parser.h"
16#include "clang/ASTMatchers/Dynamic/Registry.h"
17#include "clang/Basic/CharInfo.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070018#include "llvm/ADT/Optional.h"
Manuel Klimekf7f295f2013-05-14 09:13:00 +000019#include "llvm/ADT/Twine.h"
Stephen Hines176edba2014-12-01 14:53:08 -080020#include "llvm/Support/ManagedStatic.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070021#include <string>
22#include <vector>
Manuel Klimekf7f295f2013-05-14 09:13:00 +000023
24namespace clang {
25namespace ast_matchers {
26namespace dynamic {
27
28/// \brief Simple structure to hold information for one token from the parser.
29struct Parser::TokenInfo {
30 /// \brief Different possible tokens.
31 enum TokenKind {
Stephen Hines651f13c2014-04-23 16:59:28 -070032 TK_Eof,
33 TK_OpenParen,
34 TK_CloseParen,
35 TK_Comma,
36 TK_Period,
37 TK_Literal,
38 TK_Ident,
39 TK_InvalidChar,
40 TK_Error,
41 TK_CodeCompletion
Manuel Klimekf7f295f2013-05-14 09:13:00 +000042 };
43
Samuel Benzaquen4f37d922013-06-03 19:31:08 +000044 /// \brief Some known identifiers.
45 static const char* const ID_Bind;
46
Manuel Klimekf7f295f2013-05-14 09:13:00 +000047 TokenInfo() : Text(), Kind(TK_Eof), Range(), Value() {}
48
49 StringRef Text;
50 TokenKind Kind;
51 SourceRange Range;
52 VariantValue Value;
53};
54
Samuel Benzaquen4f37d922013-06-03 19:31:08 +000055const char* const Parser::TokenInfo::ID_Bind = "bind";
56
Manuel Klimekf7f295f2013-05-14 09:13:00 +000057/// \brief Simple tokenizer for the parser.
58class Parser::CodeTokenizer {
59public:
60 explicit CodeTokenizer(StringRef MatcherCode, Diagnostics *Error)
Stephen Hines651f13c2014-04-23 16:59:28 -070061 : Code(MatcherCode), StartOfLine(MatcherCode), Line(1), Error(Error),
Stephen Hines6bcf27b2014-05-29 04:14:42 -070062 CodeCompletionLocation(nullptr) {
Stephen Hines651f13c2014-04-23 16:59:28 -070063 NextToken = getNextToken();
64 }
65
66 CodeTokenizer(StringRef MatcherCode, Diagnostics *Error,
67 unsigned CodeCompletionOffset)
68 : Code(MatcherCode), StartOfLine(MatcherCode), Line(1), Error(Error),
69 CodeCompletionLocation(MatcherCode.data() + CodeCompletionOffset) {
Manuel Klimekf7f295f2013-05-14 09:13:00 +000070 NextToken = getNextToken();
71 }
72
73 /// \brief Returns but doesn't consume the next token.
74 const TokenInfo &peekNextToken() const { return NextToken; }
75
76 /// \brief Consumes and returns the next token.
77 TokenInfo consumeNextToken() {
78 TokenInfo ThisToken = NextToken;
79 NextToken = getNextToken();
80 return ThisToken;
81 }
82
83 TokenInfo::TokenKind nextTokenKind() const { return NextToken.Kind; }
84
85private:
86 TokenInfo getNextToken() {
87 consumeWhitespace();
88 TokenInfo Result;
89 Result.Range.Start = currentLocation();
90
Stephen Hines651f13c2014-04-23 16:59:28 -070091 if (CodeCompletionLocation && CodeCompletionLocation <= Code.data()) {
92 Result.Kind = TokenInfo::TK_CodeCompletion;
93 Result.Text = StringRef(CodeCompletionLocation, 0);
Stephen Hines6bcf27b2014-05-29 04:14:42 -070094 CodeCompletionLocation = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -070095 return Result;
96 }
97
Manuel Klimekf7f295f2013-05-14 09:13:00 +000098 if (Code.empty()) {
99 Result.Kind = TokenInfo::TK_Eof;
100 Result.Text = "";
101 return Result;
102 }
103
104 switch (Code[0]) {
105 case ',':
106 Result.Kind = TokenInfo::TK_Comma;
107 Result.Text = Code.substr(0, 1);
108 Code = Code.drop_front();
109 break;
Samuel Benzaquen4f37d922013-06-03 19:31:08 +0000110 case '.':
111 Result.Kind = TokenInfo::TK_Period;
112 Result.Text = Code.substr(0, 1);
113 Code = Code.drop_front();
114 break;
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000115 case '(':
116 Result.Kind = TokenInfo::TK_OpenParen;
117 Result.Text = Code.substr(0, 1);
118 Code = Code.drop_front();
119 break;
120 case ')':
121 Result.Kind = TokenInfo::TK_CloseParen;
122 Result.Text = Code.substr(0, 1);
123 Code = Code.drop_front();
124 break;
125
126 case '"':
127 case '\'':
128 // Parse a string literal.
129 consumeStringLiteral(&Result);
130 break;
131
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000132 case '0': case '1': case '2': case '3': case '4':
133 case '5': case '6': case '7': case '8': case '9':
134 // Parse an unsigned literal.
135 consumeUnsignedLiteral(&Result);
136 break;
137
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000138 default:
139 if (isAlphanumeric(Code[0])) {
140 // Parse an identifier
141 size_t TokenLength = 1;
Stephen Hines651f13c2014-04-23 16:59:28 -0700142 while (1) {
143 // A code completion location in/immediately after an identifier will
144 // cause the portion of the identifier before the code completion
145 // location to become a code completion token.
146 if (CodeCompletionLocation == Code.data() + TokenLength) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700147 CodeCompletionLocation = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -0700148 Result.Kind = TokenInfo::TK_CodeCompletion;
149 Result.Text = Code.substr(0, TokenLength);
150 Code = Code.drop_front(TokenLength);
151 return Result;
152 }
153 if (TokenLength == Code.size() || !isAlphanumeric(Code[TokenLength]))
154 break;
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000155 ++TokenLength;
Stephen Hines651f13c2014-04-23 16:59:28 -0700156 }
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000157 Result.Kind = TokenInfo::TK_Ident;
158 Result.Text = Code.substr(0, TokenLength);
159 Code = Code.drop_front(TokenLength);
160 } else {
161 Result.Kind = TokenInfo::TK_InvalidChar;
162 Result.Text = Code.substr(0, 1);
163 Code = Code.drop_front(1);
164 }
165 break;
166 }
167
168 Result.Range.End = currentLocation();
169 return Result;
170 }
171
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000172 /// \brief Consume an unsigned literal.
173 void consumeUnsignedLiteral(TokenInfo *Result) {
174 unsigned Length = 1;
175 if (Code.size() > 1) {
176 // Consume the 'x' or 'b' radix modifier, if present.
177 switch (toLowercase(Code[1])) {
178 case 'x': case 'b': Length = 2;
179 }
180 }
181 while (Length < Code.size() && isHexDigit(Code[Length]))
182 ++Length;
183
184 Result->Text = Code.substr(0, Length);
185 Code = Code.drop_front(Length);
186
187 unsigned Value;
188 if (!Result->Text.getAsInteger(0, Value)) {
189 Result->Kind = TokenInfo::TK_Literal;
190 Result->Value = Value;
191 } else {
192 SourceRange Range;
193 Range.Start = Result->Range.Start;
194 Range.End = currentLocation();
Samuel Benzaquen8a77c202013-07-18 19:47:59 +0000195 Error->addError(Range, Error->ET_ParserUnsignedError) << Result->Text;
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000196 Result->Kind = TokenInfo::TK_Error;
197 }
198 }
199
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000200 /// \brief Consume a string literal.
201 ///
202 /// \c Code must be positioned at the start of the literal (the opening
203 /// quote). Consumed until it finds the same closing quote character.
204 void consumeStringLiteral(TokenInfo *Result) {
205 bool InEscape = false;
206 const char Marker = Code[0];
207 for (size_t Length = 1, Size = Code.size(); Length != Size; ++Length) {
208 if (InEscape) {
209 InEscape = false;
210 continue;
211 }
212 if (Code[Length] == '\\') {
213 InEscape = true;
214 continue;
215 }
216 if (Code[Length] == Marker) {
217 Result->Kind = TokenInfo::TK_Literal;
218 Result->Text = Code.substr(0, Length + 1);
219 Result->Value = Code.substr(1, Length - 1).str();
220 Code = Code.drop_front(Length + 1);
221 return;
222 }
223 }
224
225 StringRef ErrorText = Code;
226 Code = Code.drop_front(Code.size());
227 SourceRange Range;
228 Range.Start = Result->Range.Start;
229 Range.End = currentLocation();
Samuel Benzaquen8a77c202013-07-18 19:47:59 +0000230 Error->addError(Range, Error->ET_ParserStringError) << ErrorText;
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000231 Result->Kind = TokenInfo::TK_Error;
232 }
233
234 /// \brief Consume all leading whitespace from \c Code.
235 void consumeWhitespace() {
236 while (!Code.empty() && isWhitespace(Code[0])) {
237 if (Code[0] == '\n') {
238 ++Line;
239 StartOfLine = Code.drop_front();
240 }
241 Code = Code.drop_front();
242 }
243 }
244
245 SourceLocation currentLocation() {
246 SourceLocation Location;
247 Location.Line = Line;
248 Location.Column = Code.data() - StartOfLine.data() + 1;
249 return Location;
250 }
251
252 StringRef Code;
253 StringRef StartOfLine;
254 unsigned Line;
255 Diagnostics *Error;
256 TokenInfo NextToken;
Stephen Hines651f13c2014-04-23 16:59:28 -0700257 const char *CodeCompletionLocation;
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000258};
259
260Parser::Sema::~Sema() {}
261
Stephen Hines176edba2014-12-01 14:53:08 -0800262std::vector<ArgKind> Parser::Sema::getAcceptedCompletionTypes(
263 llvm::ArrayRef<std::pair<MatcherCtor, unsigned>> Context) {
264 return std::vector<ArgKind>();
265}
266
267std::vector<MatcherCompletion>
268Parser::Sema::getMatcherCompletions(llvm::ArrayRef<ArgKind> AcceptedTypes) {
269 return std::vector<MatcherCompletion>();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700270}
271
Stephen Hines651f13c2014-04-23 16:59:28 -0700272struct Parser::ScopedContextEntry {
273 Parser *P;
274
275 ScopedContextEntry(Parser *P, MatcherCtor C) : P(P) {
276 P->ContextStack.push_back(std::make_pair(C, 0u));
277 }
278
279 ~ScopedContextEntry() {
280 P->ContextStack.pop_back();
281 }
282
283 void nextArg() {
284 ++P->ContextStack.back().second;
285 }
286};
287
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700288/// \brief Parse expressions that start with an identifier.
289///
290/// This function can parse named values and matchers.
291/// In case of failure it will try to determine the user's intent to give
292/// an appropriate error message.
293bool Parser::parseIdentifierPrefixImpl(VariantValue *Value) {
294 const TokenInfo NameToken = Tokenizer->consumeNextToken();
295
296 if (Tokenizer->nextTokenKind() != TokenInfo::TK_OpenParen) {
297 // Parse as a named value.
Stephen Hines176edba2014-12-01 14:53:08 -0800298 if (const VariantValue NamedValue =
299 NamedValues ? NamedValues->lookup(NameToken.Text)
300 : VariantValue()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700301 *Value = NamedValue;
302 return true;
303 }
304 // If the syntax is correct and the name is not a matcher either, report
305 // unknown named value.
306 if ((Tokenizer->nextTokenKind() == TokenInfo::TK_Comma ||
307 Tokenizer->nextTokenKind() == TokenInfo::TK_CloseParen ||
308 Tokenizer->nextTokenKind() == TokenInfo::TK_Eof) &&
309 !S->lookupMatcherCtor(NameToken.Text)) {
310 Error->addError(NameToken.Range, Error->ET_RegistryValueNotFound)
311 << NameToken.Text;
312 return false;
313 }
314 // Otherwise, fallback to the matcher parser.
315 }
316
317 // Parse as a matcher expression.
318 return parseMatcherExpressionImpl(NameToken, Value);
319}
320
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000321/// \brief Parse and validate a matcher expression.
322/// \return \c true on success, in which case \c Value has the matcher parsed.
323/// If the input is malformed, or some argument has an error, it
324/// returns \c false.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700325bool Parser::parseMatcherExpressionImpl(const TokenInfo &NameToken,
326 VariantValue *Value) {
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000327 assert(NameToken.Kind == TokenInfo::TK_Ident);
328 const TokenInfo OpenToken = Tokenizer->consumeNextToken();
329 if (OpenToken.Kind != TokenInfo::TK_OpenParen) {
Samuel Benzaquen8a77c202013-07-18 19:47:59 +0000330 Error->addError(OpenToken.Range, Error->ET_ParserNoOpenParen)
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000331 << OpenToken.Text;
332 return false;
333 }
334
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700335 llvm::Optional<MatcherCtor> Ctor = S->lookupMatcherCtor(NameToken.Text);
336
337 if (!Ctor) {
338 Error->addError(NameToken.Range, Error->ET_RegistryMatcherNotFound)
339 << NameToken.Text;
340 // Do not return here. We need to continue to give completion suggestions.
341 }
342
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000343 std::vector<ParserValue> Args;
344 TokenInfo EndToken;
Stephen Hines651f13c2014-04-23 16:59:28 -0700345
346 {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700347 ScopedContextEntry SCE(this, Ctor ? *Ctor : nullptr);
Stephen Hines651f13c2014-04-23 16:59:28 -0700348
349 while (Tokenizer->nextTokenKind() != TokenInfo::TK_Eof) {
350 if (Tokenizer->nextTokenKind() == TokenInfo::TK_CloseParen) {
351 // End of args.
352 EndToken = Tokenizer->consumeNextToken();
353 break;
354 }
355 if (Args.size() > 0) {
356 // We must find a , token to continue.
357 const TokenInfo CommaToken = Tokenizer->consumeNextToken();
358 if (CommaToken.Kind != TokenInfo::TK_Comma) {
359 Error->addError(CommaToken.Range, Error->ET_ParserNoComma)
360 << CommaToken.Text;
361 return false;
362 }
363 }
364
365 Diagnostics::Context Ctx(Diagnostics::Context::MatcherArg, Error,
366 NameToken.Text, NameToken.Range,
367 Args.size() + 1);
368 ParserValue ArgValue;
369 ArgValue.Text = Tokenizer->peekNextToken().Text;
370 ArgValue.Range = Tokenizer->peekNextToken().Range;
371 if (!parseExpressionImpl(&ArgValue.Value)) {
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000372 return false;
373 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700374
375 Args.push_back(ArgValue);
376 SCE.nextArg();
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000377 }
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000378 }
379
380 if (EndToken.Kind == TokenInfo::TK_Eof) {
Samuel Benzaquen8a77c202013-07-18 19:47:59 +0000381 Error->addError(OpenToken.Range, Error->ET_ParserNoCloseParen);
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000382 return false;
383 }
384
Samuel Benzaquen4f37d922013-06-03 19:31:08 +0000385 std::string BindID;
386 if (Tokenizer->peekNextToken().Kind == TokenInfo::TK_Period) {
387 // Parse .bind("foo")
388 Tokenizer->consumeNextToken(); // consume the period.
389 const TokenInfo BindToken = Tokenizer->consumeNextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -0700390 if (BindToken.Kind == TokenInfo::TK_CodeCompletion) {
Stephen Hines176edba2014-12-01 14:53:08 -0800391 addCompletion(BindToken, MatcherCompletion("bind(\"", "bind", 1));
Stephen Hines651f13c2014-04-23 16:59:28 -0700392 return false;
393 }
394
Samuel Benzaquen4f37d922013-06-03 19:31:08 +0000395 const TokenInfo OpenToken = Tokenizer->consumeNextToken();
396 const TokenInfo IDToken = Tokenizer->consumeNextToken();
397 const TokenInfo CloseToken = Tokenizer->consumeNextToken();
398
399 // TODO: We could use different error codes for each/some to be more
400 // explicit about the syntax error.
401 if (BindToken.Kind != TokenInfo::TK_Ident ||
402 BindToken.Text != TokenInfo::ID_Bind) {
Samuel Benzaquen8a77c202013-07-18 19:47:59 +0000403 Error->addError(BindToken.Range, Error->ET_ParserMalformedBindExpr);
Samuel Benzaquen4f37d922013-06-03 19:31:08 +0000404 return false;
405 }
406 if (OpenToken.Kind != TokenInfo::TK_OpenParen) {
Samuel Benzaquen8a77c202013-07-18 19:47:59 +0000407 Error->addError(OpenToken.Range, Error->ET_ParserMalformedBindExpr);
Samuel Benzaquen4f37d922013-06-03 19:31:08 +0000408 return false;
409 }
410 if (IDToken.Kind != TokenInfo::TK_Literal || !IDToken.Value.isString()) {
Samuel Benzaquen8a77c202013-07-18 19:47:59 +0000411 Error->addError(IDToken.Range, Error->ET_ParserMalformedBindExpr);
Samuel Benzaquen4f37d922013-06-03 19:31:08 +0000412 return false;
413 }
414 if (CloseToken.Kind != TokenInfo::TK_CloseParen) {
Samuel Benzaquen8a77c202013-07-18 19:47:59 +0000415 Error->addError(CloseToken.Range, Error->ET_ParserMalformedBindExpr);
Samuel Benzaquen4f37d922013-06-03 19:31:08 +0000416 return false;
417 }
418 BindID = IDToken.Value.getString();
419 }
420
Stephen Hines651f13c2014-04-23 16:59:28 -0700421 if (!Ctor)
422 return false;
423
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000424 // Merge the start and end infos.
Samuel Benzaquen8a77c202013-07-18 19:47:59 +0000425 Diagnostics::Context Ctx(Diagnostics::Context::ConstructMatcher, Error,
426 NameToken.Text, NameToken.Range);
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000427 SourceRange MatcherRange = NameToken.Range;
428 MatcherRange.End = EndToken.Range.End;
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000429 VariantMatcher Result = S->actOnMatcherExpression(
Stephen Hines651f13c2014-04-23 16:59:28 -0700430 *Ctor, MatcherRange, BindID, Args, Error);
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000431 if (Result.isNull()) return false;
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000432
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000433 *Value = Result;
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000434 return true;
435}
436
Stephen Hines651f13c2014-04-23 16:59:28 -0700437// If the prefix of this completion matches the completion token, add it to
438// Completions minus the prefix.
Stephen Hines176edba2014-12-01 14:53:08 -0800439void Parser::addCompletion(const TokenInfo &CompToken,
440 const MatcherCompletion& Completion) {
441 if (StringRef(Completion.TypedText).startswith(CompToken.Text) &&
442 Completion.Specificity > 0) {
443 Completions.emplace_back(Completion.TypedText.substr(CompToken.Text.size()),
444 Completion.MatcherDecl, Completion.Specificity);
Stephen Hines651f13c2014-04-23 16:59:28 -0700445 }
446}
447
Stephen Hines176edba2014-12-01 14:53:08 -0800448std::vector<MatcherCompletion> Parser::getNamedValueCompletions(
449 ArrayRef<ArgKind> AcceptedTypes) {
450 if (!NamedValues) return std::vector<MatcherCompletion>();
451 std::vector<MatcherCompletion> Result;
452 for (const auto &Entry : *NamedValues) {
453 unsigned Specificity;
454 if (Entry.getValue().isConvertibleTo(AcceptedTypes, &Specificity)) {
455 std::string Decl =
456 (Entry.getValue().getTypeAsString() + " " + Entry.getKey()).str();
457 Result.emplace_back(Entry.getKey(), Decl, Specificity);
458 }
459 }
460 return Result;
461}
462
Stephen Hines651f13c2014-04-23 16:59:28 -0700463void Parser::addExpressionCompletions() {
464 const TokenInfo CompToken = Tokenizer->consumeNextToken();
465 assert(CompToken.Kind == TokenInfo::TK_CodeCompletion);
466
467 // We cannot complete code if there is an invalid element on the context
468 // stack.
469 for (ContextStackTy::iterator I = ContextStack.begin(),
470 E = ContextStack.end();
471 I != E; ++I) {
472 if (!I->first)
473 return;
474 }
475
Stephen Hines176edba2014-12-01 14:53:08 -0800476 auto AcceptedTypes = S->getAcceptedCompletionTypes(ContextStack);
477 for (const auto &Completion : S->getMatcherCompletions(AcceptedTypes)) {
478 addCompletion(CompToken, Completion);
479 }
480
481 for (const auto &Completion : getNamedValueCompletions(AcceptedTypes)) {
482 addCompletion(CompToken, Completion);
Stephen Hines651f13c2014-04-23 16:59:28 -0700483 }
484}
485
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000486/// \brief Parse an <Expresssion>
487bool Parser::parseExpressionImpl(VariantValue *Value) {
488 switch (Tokenizer->nextTokenKind()) {
489 case TokenInfo::TK_Literal:
490 *Value = Tokenizer->consumeNextToken().Value;
491 return true;
492
493 case TokenInfo::TK_Ident:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700494 return parseIdentifierPrefixImpl(Value);
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000495
Stephen Hines651f13c2014-04-23 16:59:28 -0700496 case TokenInfo::TK_CodeCompletion:
497 addExpressionCompletions();
498 return false;
499
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000500 case TokenInfo::TK_Eof:
Samuel Benzaquen8a77c202013-07-18 19:47:59 +0000501 Error->addError(Tokenizer->consumeNextToken().Range,
502 Error->ET_ParserNoCode);
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000503 return false;
504
505 case TokenInfo::TK_Error:
506 // This error was already reported by the tokenizer.
507 return false;
508
509 case TokenInfo::TK_OpenParen:
510 case TokenInfo::TK_CloseParen:
511 case TokenInfo::TK_Comma:
Samuel Benzaquen4f37d922013-06-03 19:31:08 +0000512 case TokenInfo::TK_Period:
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000513 case TokenInfo::TK_InvalidChar:
514 const TokenInfo Token = Tokenizer->consumeNextToken();
Samuel Benzaquen8a77c202013-07-18 19:47:59 +0000515 Error->addError(Token.Range, Error->ET_ParserInvalidToken) << Token.Text;
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000516 return false;
517 }
518
519 llvm_unreachable("Unknown token kind.");
520}
521
Stephen Hines176edba2014-12-01 14:53:08 -0800522static llvm::ManagedStatic<Parser::RegistrySema> DefaultRegistrySema;
523
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000524Parser::Parser(CodeTokenizer *Tokenizer, Sema *S,
Stephen Hines176edba2014-12-01 14:53:08 -0800525 const NamedValueMap *NamedValues, Diagnostics *Error)
526 : Tokenizer(Tokenizer), S(S ? S : &*DefaultRegistrySema),
527 NamedValues(NamedValues), Error(Error) {}
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000528
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700529Parser::RegistrySema::~RegistrySema() {}
530
531llvm::Optional<MatcherCtor>
532Parser::RegistrySema::lookupMatcherCtor(StringRef MatcherName) {
533 return Registry::lookupMatcherCtor(MatcherName);
534}
535
536VariantMatcher Parser::RegistrySema::actOnMatcherExpression(
537 MatcherCtor Ctor, const SourceRange &NameRange, StringRef BindID,
538 ArrayRef<ParserValue> Args, Diagnostics *Error) {
539 if (BindID.empty()) {
540 return Registry::constructMatcher(Ctor, NameRange, Args, Error);
541 } else {
542 return Registry::constructBoundMatcher(Ctor, NameRange, BindID, Args,
543 Error);
Stephen Hines651f13c2014-04-23 16:59:28 -0700544 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700545}
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000546
Stephen Hines176edba2014-12-01 14:53:08 -0800547std::vector<ArgKind> Parser::RegistrySema::getAcceptedCompletionTypes(
548 ArrayRef<std::pair<MatcherCtor, unsigned>> Context) {
549 return Registry::getAcceptedCompletionTypes(Context);
550}
551
552std::vector<MatcherCompletion> Parser::RegistrySema::getMatcherCompletions(
553 ArrayRef<ArgKind> AcceptedTypes) {
554 return Registry::getMatcherCompletions(AcceptedTypes);
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000555}
556
557bool Parser::parseExpression(StringRef Code, Sema *S,
Stephen Hines176edba2014-12-01 14:53:08 -0800558 const NamedValueMap *NamedValues,
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000559 VariantValue *Value, Diagnostics *Error) {
560 CodeTokenizer Tokenizer(Code, Error);
Stephen Hines176edba2014-12-01 14:53:08 -0800561 if (!Parser(&Tokenizer, S, NamedValues, Error).parseExpressionImpl(Value))
562 return false;
Samuel Benzaquen4f37d922013-06-03 19:31:08 +0000563 if (Tokenizer.peekNextToken().Kind != TokenInfo::TK_Eof) {
Samuel Benzaquen8a77c202013-07-18 19:47:59 +0000564 Error->addError(Tokenizer.peekNextToken().Range,
565 Error->ET_ParserTrailingCode);
Samuel Benzaquen4f37d922013-06-03 19:31:08 +0000566 return false;
567 }
568 return true;
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000569}
570
Stephen Hines651f13c2014-04-23 16:59:28 -0700571std::vector<MatcherCompletion>
Stephen Hines176edba2014-12-01 14:53:08 -0800572Parser::completeExpression(StringRef Code, unsigned CompletionOffset, Sema *S,
573 const NamedValueMap *NamedValues) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700574 Diagnostics Error;
575 CodeTokenizer Tokenizer(Code, &Error, CompletionOffset);
Stephen Hines176edba2014-12-01 14:53:08 -0800576 Parser P(&Tokenizer, S, NamedValues, &Error);
Stephen Hines651f13c2014-04-23 16:59:28 -0700577 VariantValue Dummy;
578 P.parseExpressionImpl(&Dummy);
579
Stephen Hines176edba2014-12-01 14:53:08 -0800580 // Sort by specificity, then by name.
581 std::sort(P.Completions.begin(), P.Completions.end(),
582 [](const MatcherCompletion &A, const MatcherCompletion &B) {
583 if (A.Specificity != B.Specificity)
584 return A.Specificity > B.Specificity;
585 return A.TypedText < B.TypedText;
586 });
587
Stephen Hines651f13c2014-04-23 16:59:28 -0700588 return P.Completions;
589}
590
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000591llvm::Optional<DynTypedMatcher>
Stephen Hines176edba2014-12-01 14:53:08 -0800592Parser::parseMatcherExpression(StringRef Code, Sema *S,
593 const NamedValueMap *NamedValues,
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000594 Diagnostics *Error) {
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000595 VariantValue Value;
Stephen Hines176edba2014-12-01 14:53:08 -0800596 if (!parseExpression(Code, S, NamedValues, &Value, Error))
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000597 return llvm::Optional<DynTypedMatcher>();
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000598 if (!Value.isMatcher()) {
Samuel Benzaquen8a77c202013-07-18 19:47:59 +0000599 Error->addError(SourceRange(), Error->ET_ParserNotAMatcher);
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000600 return llvm::Optional<DynTypedMatcher>();
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000601 }
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000602 llvm::Optional<DynTypedMatcher> Result =
603 Value.getMatcher().getSingleMatcher();
604 if (!Result.hasValue()) {
Samuel Benzaquen8a77c202013-07-18 19:47:59 +0000605 Error->addError(SourceRange(), Error->ET_ParserOverloadedType)
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000606 << Value.getTypeAsString();
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000607 }
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000608 return Result;
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000609}
610
611} // namespace dynamic
612} // namespace ast_matchers
613} // namespace clang