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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
|
//===- Parser.cpp - Matcher expression 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
//
//===----------------------------------------------------------------------===//
//
// Recursive parser implementation for the matcher expression grammar.
//
//===----------------------------------------------------------------------===//
#include "Parser.h"
#include <vector>
namespace mlir::query::matcher::internal {
// Simple structure to hold information for one token from the parser.
struct Parser::TokenInfo {
TokenInfo() = default;
// Method to set the kind and text of the token
void set(TokenKind newKind, llvm::StringRef newText) {
kind = newKind;
text = newText;
}
// Known identifiers.
static const char *const ID_Extract;
llvm::StringRef text;
TokenKind kind = TokenKind::Eof;
SourceRange range;
VariantValue value;
};
const char *const Parser::TokenInfo::ID_Extract = "extract";
class Parser::CodeTokenizer {
public:
// Constructor with matcherCode and error
explicit CodeTokenizer(llvm::StringRef matcherCode, Diagnostics *error)
: code(matcherCode), startOfLine(matcherCode), error(error) {
nextToken = getNextToken();
}
// Constructor with matcherCode, error, and codeCompletionOffset
CodeTokenizer(llvm::StringRef matcherCode, Diagnostics *error,
unsigned codeCompletionOffset)
: code(matcherCode), startOfLine(matcherCode), error(error),
codeCompletionLocation(matcherCode.data() + codeCompletionOffset) {
nextToken = getNextToken();
}
// Peek at next token without consuming it
const TokenInfo &peekNextToken() const { return nextToken; }
// Consume and return the next token
TokenInfo consumeNextToken() {
TokenInfo thisToken = nextToken;
nextToken = getNextToken();
return thisToken;
}
// Skip any newline tokens
TokenInfo skipNewlines() {
while (nextToken.kind == TokenKind::NewLine)
nextToken = getNextToken();
return nextToken;
}
// Consume and return next token, ignoring newlines
TokenInfo consumeNextTokenIgnoreNewlines() {
skipNewlines();
return nextToken.kind == TokenKind::Eof ? nextToken : consumeNextToken();
}
// Return kind of next token
TokenKind nextTokenKind() const { return nextToken.kind; }
private:
// Helper function to get the first character as a new StringRef and drop it
// from the original string
llvm::StringRef firstCharacterAndDrop(llvm::StringRef &str) {
assert(!str.empty());
llvm::StringRef firstChar = str.substr(0, 1);
str = str.drop_front();
return firstChar;
}
// Get next token, consuming whitespaces and handling different token types
TokenInfo getNextToken() {
consumeWhitespace();
TokenInfo result;
result.range.start = currentLocation();
// Code completion case
if (codeCompletionLocation && codeCompletionLocation <= code.data()) {
result.set(TokenKind::CodeCompletion,
llvm::StringRef(codeCompletionLocation, 0));
codeCompletionLocation = nullptr;
return result;
}
// End of file case
if (code.empty()) {
result.set(TokenKind::Eof, "");
return result;
}
// Switch to handle specific characters
switch (code[0]) {
case '#':
code = code.drop_until([](char c) { return c == '\n'; });
return getNextToken();
case ',':
result.set(TokenKind::Comma, firstCharacterAndDrop(code));
break;
case '.':
result.set(TokenKind::Period, firstCharacterAndDrop(code));
break;
case '\n':
++line;
startOfLine = code.drop_front();
result.set(TokenKind::NewLine, firstCharacterAndDrop(code));
break;
case '(':
result.set(TokenKind::OpenParen, firstCharacterAndDrop(code));
break;
case ')':
result.set(TokenKind::CloseParen, firstCharacterAndDrop(code));
break;
case '"':
case '\'':
consumeStringLiteral(&result);
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
consumeNumberLiteral(&result);
break;
default:
parseIdentifierOrInvalid(&result);
break;
}
result.range.end = currentLocation();
return result;
}
void consumeNumberLiteral(TokenInfo *result) {
StringRef original = code;
unsigned value = 0;
if (!code.consumeInteger(0, value)) {
size_t numConsumed = original.size() - code.size();
result->text = original.take_front(numConsumed);
result->kind = TokenKind::Literal;
result->value = static_cast<int64_t>(value);
return;
}
}
// Consume a string literal, handle escape sequences and missing closing
// quote.
void consumeStringLiteral(TokenInfo *result) {
bool inEscape = false;
const char marker = code[0];
for (size_t length = 1; length < code.size(); ++length) {
if (inEscape) {
inEscape = false;
continue;
}
if (code[length] == '\\') {
inEscape = true;
continue;
}
if (code[length] == marker) {
result->kind = TokenKind::Literal;
result->text = code.substr(0, length + 1);
result->value = code.substr(1, length - 1);
code = code.drop_front(length + 1);
return;
}
}
llvm::StringRef errorText = code;
code = code.drop_front(code.size());
SourceRange range;
range.start = result->range.start;
range.end = currentLocation();
error->addError(range, ErrorType::ParserStringError) << errorText;
result->kind = TokenKind::Error;
}
void parseIdentifierOrInvalid(TokenInfo *result) {
if (isalnum(code[0])) {
// Parse an identifier
size_t tokenLength = 1;
while (true) {
// A code completion location in/immediately after an identifier will
// cause the portion of the identifier before the code completion
// location to become a code completion token.
if (codeCompletionLocation == code.data() + tokenLength) {
codeCompletionLocation = nullptr;
result->kind = TokenKind::CodeCompletion;
result->text = code.substr(0, tokenLength);
code = code.drop_front(tokenLength);
return;
}
if (tokenLength == code.size() || !(isalnum(code[tokenLength])))
break;
++tokenLength;
}
llvm::StringRef token = code.substr(0, tokenLength);
code = code.drop_front(tokenLength);
// Check if the identifier is a boolean literal
if (token == "true") {
result->text = "false";
result->kind = TokenKind::Literal;
result->value = true;
} else if (token == "false") {
result->text = "false";
result->kind = TokenKind::Literal;
result->value = false;
} else {
// Otherwise it is treated as a normal identifier
result->kind = TokenKind::Ident;
result->text = token;
}
} else {
result->kind = TokenKind::InvalidChar;
result->text = code.substr(0, 1);
code = code.drop_front(1);
}
}
// Consume all leading whitespace from code, except newlines
void consumeWhitespace() { code = code.ltrim(" \t\v\f\r"); }
// Returns the current location in the source code
SourceLocation currentLocation() {
SourceLocation location;
location.line = line;
location.column = code.data() - startOfLine.data() + 1;
return location;
}
llvm::StringRef code;
llvm::StringRef startOfLine;
unsigned line = 1;
Diagnostics *error;
TokenInfo nextToken;
const char *codeCompletionLocation = nullptr;
};
Parser::Sema::~Sema() = default;
std::vector<ArgKind> Parser::Sema::getAcceptedCompletionTypes(
llvm::ArrayRef<std::pair<MatcherCtor, unsigned>> context) {
return {};
}
std::vector<MatcherCompletion>
Parser::Sema::getMatcherCompletions(llvm::ArrayRef<ArgKind> acceptedTypes) {
return {};
}
// Entry for the scope of a parser
struct Parser::ScopedContextEntry {
Parser *parser;
ScopedContextEntry(Parser *parser, MatcherCtor c) : parser(parser) {
parser->contextStack.emplace_back(c, 0u);
}
~ScopedContextEntry() { parser->contextStack.pop_back(); }
void nextArg() { ++parser->contextStack.back().second; }
};
// Parse and validate expressions starting with an identifier.
// This function can parse named values and matchers. In case of failure, it
// will try to determine the user's intent to give an appropriate error message.
bool Parser::parseIdentifierPrefixImpl(VariantValue *value) {
const TokenInfo nameToken = tokenizer->consumeNextToken();
if (tokenizer->nextTokenKind() != TokenKind::OpenParen) {
// Parse as a named value.
if (auto namedValue = namedValues ? namedValues->lookup(nameToken.text)
: VariantValue()) {
if (tokenizer->nextTokenKind() != TokenKind::Period) {
*value = namedValue;
return true;
}
if (!namedValue.isMatcher()) {
error->addError(tokenizer->peekNextToken().range,
ErrorType::ParserNotAMatcher);
return false;
}
}
if (tokenizer->nextTokenKind() == TokenKind::NewLine) {
error->addError(tokenizer->peekNextToken().range,
ErrorType::ParserNoOpenParen)
<< "NewLine";
return false;
}
// If the syntax is correct and the name is not a matcher either, report
// an unknown named value.
if ((tokenizer->nextTokenKind() == TokenKind::Comma ||
tokenizer->nextTokenKind() == TokenKind::CloseParen ||
tokenizer->nextTokenKind() == TokenKind::NewLine ||
tokenizer->nextTokenKind() == TokenKind::Eof) &&
!sema->lookupMatcherCtor(nameToken.text)) {
error->addError(nameToken.range, ErrorType::RegistryValueNotFound)
<< nameToken.text;
return false;
}
// Otherwise, fallback to the matcher parser.
}
tokenizer->skipNewlines();
assert(nameToken.kind == TokenKind::Ident);
TokenInfo openToken = tokenizer->consumeNextToken();
if (openToken.kind != TokenKind::OpenParen) {
error->addError(openToken.range, ErrorType::ParserNoOpenParen)
<< openToken.text;
return false;
}
std::optional<MatcherCtor> ctor = sema->lookupMatcherCtor(nameToken.text);
// Parse as a matcher expression.
return parseMatcherExpressionImpl(nameToken, openToken, ctor, value);
}
bool Parser::parseChainedExpression(std::string &argument) {
// Parse the parenthesized argument to .extract("foo")
// Note: EOF is handled inside the consume functions and would fail below when
// checking token kind.
const TokenInfo openToken = tokenizer->consumeNextToken();
const TokenInfo argumentToken = tokenizer->consumeNextTokenIgnoreNewlines();
const TokenInfo closeToken = tokenizer->consumeNextTokenIgnoreNewlines();
if (openToken.kind != TokenKind::OpenParen) {
error->addError(openToken.range, ErrorType::ParserChainedExprNoOpenParen);
return false;
}
if (argumentToken.kind != TokenKind::Literal ||
!argumentToken.value.isString()) {
error->addError(argumentToken.range,
ErrorType::ParserChainedExprInvalidArg);
return false;
}
if (closeToken.kind != TokenKind::CloseParen) {
error->addError(closeToken.range, ErrorType::ParserChainedExprNoCloseParen);
return false;
}
// If all checks passed, extract the argument and return true.
argument = argumentToken.value.getString();
return true;
}
// Parse the arguments of a matcher
bool Parser::parseMatcherArgs(std::vector<ParserValue> &args, MatcherCtor ctor,
const TokenInfo &nameToken, TokenInfo &endToken) {
ScopedContextEntry sce(this, ctor);
while (tokenizer->nextTokenKind() != TokenKind::Eof) {
if (tokenizer->nextTokenKind() == TokenKind::CloseParen) {
// end of args.
endToken = tokenizer->consumeNextToken();
break;
}
if (!args.empty()) {
// We must find a , token to continue.
TokenInfo commaToken = tokenizer->consumeNextToken();
if (commaToken.kind != TokenKind::Comma) {
error->addError(commaToken.range, ErrorType::ParserNoComma)
<< commaToken.text;
return false;
}
}
ParserValue argValue;
tokenizer->skipNewlines();
argValue.text = tokenizer->peekNextToken().text;
argValue.range = tokenizer->peekNextToken().range;
if (!parseExpressionImpl(&argValue.value)) {
return false;
}
tokenizer->skipNewlines();
args.push_back(argValue);
sce.nextArg();
}
return true;
}
// Parse and validate a matcher expression.
bool Parser::parseMatcherExpressionImpl(const TokenInfo &nameToken,
const TokenInfo &openToken,
std::optional<MatcherCtor> ctor,
VariantValue *value) {
if (!ctor) {
error->addError(nameToken.range, ErrorType::RegistryMatcherNotFound)
<< nameToken.text;
// Do not return here. We need to continue to give completion suggestions.
}
std::vector<ParserValue> args;
TokenInfo endToken;
tokenizer->skipNewlines();
if (!parseMatcherArgs(args, ctor.value_or(nullptr), nameToken, endToken)) {
return false;
}
// Check for the missing closing parenthesis
if (endToken.kind != TokenKind::CloseParen) {
error->addError(openToken.range, ErrorType::ParserNoCloseParen)
<< nameToken.text;
return false;
}
std::string functionName;
if (tokenizer->peekNextToken().kind == TokenKind::Period) {
tokenizer->consumeNextToken();
TokenInfo chainCallToken = tokenizer->consumeNextToken();
if (chainCallToken.kind == TokenKind::CodeCompletion) {
addCompletion(chainCallToken, MatcherCompletion("extract(\"", "extract"));
return false;
}
if (chainCallToken.kind != TokenKind::Ident ||
chainCallToken.text != TokenInfo::ID_Extract) {
error->addError(chainCallToken.range,
ErrorType::ParserMalformedChainedExpr);
return false;
}
if (chainCallToken.text == TokenInfo::ID_Extract &&
!parseChainedExpression(functionName))
return false;
}
if (!ctor)
return false;
// Merge the start and end infos.
SourceRange matcherRange = nameToken.range;
matcherRange.end = endToken.range.end;
VariantMatcher result = sema->actOnMatcherExpression(
*ctor, matcherRange, functionName, args, error);
if (result.isNull())
return false;
*value = result;
return true;
}
// If the prefix of this completion matches the completion token, add it to
// completions minus the prefix.
void Parser::addCompletion(const TokenInfo &compToken,
const MatcherCompletion &completion) {
if (llvm::StringRef(completion.typedText).starts_with(compToken.text)) {
completions.emplace_back(completion.typedText.substr(compToken.text.size()),
completion.matcherDecl);
}
}
std::vector<MatcherCompletion>
Parser::getNamedValueCompletions(llvm::ArrayRef<ArgKind> acceptedTypes) {
if (!namedValues)
return {};
std::vector<MatcherCompletion> result;
for (const auto &entry : *namedValues) {
std::string decl =
(entry.getValue().getTypeAsString() + " " + entry.getKey()).str();
result.emplace_back(entry.getKey(), decl);
}
return result;
}
void Parser::addExpressionCompletions() {
const TokenInfo compToken = tokenizer->consumeNextTokenIgnoreNewlines();
assert(compToken.kind == TokenKind::CodeCompletion);
// We cannot complete code if there is an invalid element on the context
// stack.
for (const auto &entry : contextStack) {
if (!entry.first)
return;
}
auto acceptedTypes = sema->getAcceptedCompletionTypes(contextStack);
for (const auto &completion : sema->getMatcherCompletions(acceptedTypes)) {
addCompletion(compToken, completion);
}
for (const auto &completion : getNamedValueCompletions(acceptedTypes)) {
addCompletion(compToken, completion);
}
}
// Parse an <Expresssion>
bool Parser::parseExpressionImpl(VariantValue *value) {
switch (tokenizer->nextTokenKind()) {
case TokenKind::Literal:
*value = tokenizer->consumeNextToken().value;
return true;
case TokenKind::Ident:
return parseIdentifierPrefixImpl(value);
case TokenKind::CodeCompletion:
addExpressionCompletions();
return false;
case TokenKind::Eof:
error->addError(tokenizer->consumeNextToken().range,
ErrorType::ParserNoCode);
return false;
case TokenKind::Error:
// This error was already reported by the tokenizer.
return false;
case TokenKind::NewLine:
case TokenKind::OpenParen:
case TokenKind::CloseParen:
case TokenKind::Comma:
case TokenKind::Period:
case TokenKind::InvalidChar:
const TokenInfo token = tokenizer->consumeNextToken();
error->addError(token.range, ErrorType::ParserInvalidToken)
<< (token.kind == TokenKind::NewLine ? "NewLine" : token.text);
return false;
}
llvm_unreachable("Unknown token kind.");
}
Parser::Parser(CodeTokenizer *tokenizer, const Registry &matcherRegistry,
const NamedValueMap *namedValues, Diagnostics *error)
: tokenizer(tokenizer),
sema(std::make_unique<RegistrySema>(matcherRegistry)),
namedValues(namedValues), error(error) {}
Parser::RegistrySema::~RegistrySema() = default;
std::optional<MatcherCtor>
Parser::RegistrySema::lookupMatcherCtor(llvm::StringRef matcherName) {
return RegistryManager::lookupMatcherCtor(matcherName, matcherRegistry);
}
VariantMatcher Parser::RegistrySema::actOnMatcherExpression(
MatcherCtor ctor, SourceRange nameRange, llvm::StringRef functionName,
llvm::ArrayRef<ParserValue> args, Diagnostics *error) {
return RegistryManager::constructMatcher(ctor, nameRange, functionName, args,
error);
}
std::vector<ArgKind> Parser::RegistrySema::getAcceptedCompletionTypes(
llvm::ArrayRef<std::pair<MatcherCtor, unsigned>> context) {
return RegistryManager::getAcceptedCompletionTypes(context);
}
std::vector<MatcherCompletion> Parser::RegistrySema::getMatcherCompletions(
llvm::ArrayRef<ArgKind> acceptedTypes) {
return RegistryManager::getMatcherCompletions(acceptedTypes, matcherRegistry);
}
bool Parser::parseExpression(llvm::StringRef &code,
const Registry &matcherRegistry,
const NamedValueMap *namedValues,
VariantValue *value, Diagnostics *error) {
CodeTokenizer tokenizer(code, error);
Parser parser(&tokenizer, matcherRegistry, namedValues, error);
if (!parser.parseExpressionImpl(value))
return false;
auto nextToken = tokenizer.peekNextToken();
if (nextToken.kind != TokenKind::Eof &&
nextToken.kind != TokenKind::NewLine) {
error->addError(tokenizer.peekNextToken().range,
ErrorType::ParserTrailingCode);
return false;
}
return true;
}
std::vector<MatcherCompletion>
Parser::completeExpression(llvm::StringRef &code, unsigned completionOffset,
const Registry &matcherRegistry,
const NamedValueMap *namedValues) {
Diagnostics error;
CodeTokenizer tokenizer(code, &error, completionOffset);
Parser parser(&tokenizer, matcherRegistry, namedValues, &error);
VariantValue dummy;
parser.parseExpressionImpl(&dummy);
return parser.completions;
}
std::optional<DynMatcher> Parser::parseMatcherExpression(
llvm::StringRef &code, const Registry &matcherRegistry,
const NamedValueMap *namedValues, Diagnostics *error) {
VariantValue value;
if (!parseExpression(code, matcherRegistry, namedValues, &value, error))
return std::nullopt;
if (!value.isMatcher()) {
error->addError(SourceRange(), ErrorType::ParserNotAMatcher);
return std::nullopt;
}
std::optional<DynMatcher> result = value.getMatcher().getDynMatcher();
if (!result) {
error->addError(SourceRange(), ErrorType::ParserOverloadedType)
<< value.getTypeAsString();
}
return result;
}
} // namespace mlir::query::matcher::internal
|