@@ -19,21 +19,6 @@ namespace mlir::query::matcher::internal {
1919
2020// Simple structure to hold information for one token from the parser.
2121struct Parser ::TokenInfo {
22- // Different possible tokens.
23- enum TokenKind {
24- TK_Eof,
25- TK_NewLine,
26- TK_OpenParen,
27- TK_CloseParen,
28- TK_Comma,
29- TK_Period,
30- TK_Literal,
31- TK_Ident,
32- TK_InvalidChar,
33- TK_CodeCompletion,
34- TK_Error
35- };
36-
3722 TokenInfo () = default ;
3823
3924 // Method to set the kind and text of the token
@@ -43,7 +28,7 @@ struct Parser::TokenInfo {
4328 }
4429
4530 llvm::StringRef text;
46- TokenKind kind = TK_Eof ;
31+ TokenKind kind = TokenKind::Eof ;
4732 SourceRange range;
4833 VariantValue value;
4934};
@@ -76,19 +61,19 @@ class Parser::CodeTokenizer {
7661
7762 // Skip any newline tokens
7863 TokenInfo skipNewlines () {
79- while (nextToken.kind == TokenInfo::TK_NewLine )
64+ while (nextToken.kind == TokenKind::NewLine )
8065 nextToken = getNextToken ();
8166 return nextToken;
8267 }
8368
8469 // Consume and return next token, ignoring newlines
8570 TokenInfo consumeNextTokenIgnoreNewlines () {
8671 skipNewlines ();
87- return nextToken.kind == TokenInfo::TK_Eof ? nextToken : consumeNextToken ();
72+ return nextToken.kind == TokenKind::Eof ? nextToken : consumeNextToken ();
8873 }
8974
9075 // Return kind of next token
91- TokenInfo:: TokenKind nextTokenKind () const { return nextToken.kind ; }
76+ TokenKind nextTokenKind () const { return nextToken.kind ; }
9277
9378private:
9479 // Helper function to get the first character as a new StringRef and drop it
@@ -108,15 +93,15 @@ class Parser::CodeTokenizer {
10893
10994 // Code completion case
11095 if (codeCompletionLocation && codeCompletionLocation <= code.data ()) {
111- result.set (TokenInfo::TK_CodeCompletion ,
96+ result.set (TokenKind::CodeCompletion ,
11297 llvm::StringRef (codeCompletionLocation, 0 ));
11398 codeCompletionLocation = nullptr ;
11499 return result;
115100 }
116101
117102 // End of file case
118103 if (code.empty ()) {
119- result.set (TokenInfo::TK_Eof , " " );
104+ result.set (TokenKind::Eof , " " );
120105 return result;
121106 }
122107
@@ -126,21 +111,21 @@ class Parser::CodeTokenizer {
126111 code = code.drop_until ([](char c) { return c == ' \n ' ; });
127112 return getNextToken ();
128113 case ' ,' :
129- result.set (TokenInfo::TK_Comma , firstCharacterAndDrop (code));
114+ result.set (TokenKind::Comma , firstCharacterAndDrop (code));
130115 break ;
131116 case ' .' :
132- result.set (TokenInfo::TK_Period , firstCharacterAndDrop (code));
117+ result.set (TokenKind::Period , firstCharacterAndDrop (code));
133118 break ;
134119 case ' \n ' :
135120 ++line;
136121 startOfLine = code.drop_front ();
137- result.set (TokenInfo::TK_NewLine , firstCharacterAndDrop (code));
122+ result.set (TokenKind::NewLine , firstCharacterAndDrop (code));
138123 break ;
139124 case ' (' :
140- result.set (TokenInfo::TK_OpenParen , firstCharacterAndDrop (code));
125+ result.set (TokenKind::OpenParen , firstCharacterAndDrop (code));
141126 break ;
142127 case ' )' :
143- result.set (TokenInfo::TK_CloseParen , firstCharacterAndDrop (code));
128+ result.set (TokenKind::CloseParen , firstCharacterAndDrop (code));
144129 break ;
145130 case ' "' :
146131 case ' \' ' :
@@ -170,7 +155,7 @@ class Parser::CodeTokenizer {
170155 continue ;
171156 }
172157 if (code[length] == marker) {
173- result->kind = TokenInfo::TK_Literal ;
158+ result->kind = TokenKind::Literal ;
174159 result->text = code.substr (0 , length + 1 );
175160 result->value = code.substr (1 , length - 1 );
176161 code = code.drop_front (length + 1 );
@@ -184,7 +169,7 @@ class Parser::CodeTokenizer {
184169 range.end = currentLocation ();
185170 error->addError (range, Diagnostics::ErrorType::ParserStringError)
186171 << errorText;
187- result->kind = TokenInfo::TK_Error ;
172+ result->kind = TokenKind::Error ;
188173 }
189174
190175 void parseIdentifierOrInvalid (TokenInfo *result) {
@@ -198,7 +183,7 @@ class Parser::CodeTokenizer {
198183 // location to become a code completion token.
199184 if (codeCompletionLocation == code.data () + tokenLength) {
200185 codeCompletionLocation = nullptr ;
201- result->kind = TokenInfo::TK_CodeCompletion ;
186+ result->kind = TokenKind::CodeCompletion ;
202187 result->text = code.substr (0 , tokenLength);
203188 code = code.drop_front (tokenLength);
204189 return ;
@@ -207,11 +192,11 @@ class Parser::CodeTokenizer {
207192 break ;
208193 ++tokenLength;
209194 }
210- result->kind = TokenInfo::TK_Ident ;
195+ result->kind = TokenKind::Ident ;
211196 result->text = code.substr (0 , tokenLength);
212197 code = code.drop_front (tokenLength);
213198 } else {
214- result->kind = TokenInfo::TK_InvalidChar ;
199+ result->kind = TokenKind::InvalidChar ;
215200 result->text = code.substr (0 , 1 );
216201 code = code.drop_front (1 );
217202 }
@@ -270,7 +255,7 @@ struct Parser::ScopedContextEntry {
270255bool Parser::parseIdentifierPrefixImpl (VariantValue *value) {
271256 const TokenInfo nameToken = tokenizer->consumeNextToken ();
272257
273- if (tokenizer->nextTokenKind () != TokenInfo::TK_OpenParen ) {
258+ if (tokenizer->nextTokenKind () != TokenKind::OpenParen ) {
274259 // Parse as a named value.
275260 auto namedValue =
276261 namedValues ? namedValues->lookup (nameToken.text ) : VariantValue ();
@@ -281,7 +266,7 @@ bool Parser::parseIdentifierPrefixImpl(VariantValue *value) {
281266 return false ;
282267 }
283268
284- if (tokenizer->nextTokenKind () == TokenInfo::TK_NewLine ) {
269+ if (tokenizer->nextTokenKind () == TokenKind::NewLine ) {
285270 error->addError (tokenizer->peekNextToken ().range ,
286271 Diagnostics::ErrorType::ParserNoOpenParen)
287272 << " NewLine" ;
@@ -290,10 +275,10 @@ bool Parser::parseIdentifierPrefixImpl(VariantValue *value) {
290275
291276 // If the syntax is correct and the name is not a matcher either, report
292277 // an unknown named value.
293- if ((tokenizer->nextTokenKind () == TokenInfo::TK_Comma ||
294- tokenizer->nextTokenKind () == TokenInfo::TK_CloseParen ||
295- tokenizer->nextTokenKind () == TokenInfo::TK_NewLine ||
296- tokenizer->nextTokenKind () == TokenInfo::TK_Eof ) &&
278+ if ((tokenizer->nextTokenKind () == TokenKind::Comma ||
279+ tokenizer->nextTokenKind () == TokenKind::CloseParen ||
280+ tokenizer->nextTokenKind () == TokenKind::NewLine ||
281+ tokenizer->nextTokenKind () == TokenKind::Eof ) &&
297282 !sema->lookupMatcherCtor (nameToken.text )) {
298283 error->addError (nameToken.range ,
299284 Diagnostics::ErrorType::RegistryValueNotFound)
@@ -305,9 +290,9 @@ bool Parser::parseIdentifierPrefixImpl(VariantValue *value) {
305290
306291 tokenizer->skipNewlines ();
307292
308- assert (nameToken.kind == TokenInfo::TK_Ident );
293+ assert (nameToken.kind == TokenKind::Ident );
309294 TokenInfo openToken = tokenizer->consumeNextToken ();
310- if (openToken.kind != TokenInfo::TK_OpenParen ) {
295+ if (openToken.kind != TokenKind::OpenParen ) {
311296 error->addError (openToken.range , Diagnostics::ErrorType::ParserNoOpenParen)
312297 << openToken.text ;
313298 return false ;
@@ -324,8 +309,8 @@ bool Parser::parseMatcherArgs(std::vector<ParserValue> &args, MatcherCtor ctor,
324309 const TokenInfo &nameToken, TokenInfo &endToken) {
325310 ScopedContextEntry sce (this , ctor);
326311
327- while (tokenizer->nextTokenKind () != TokenInfo::TK_Eof ) {
328- if (tokenizer->nextTokenKind () == TokenInfo::TK_CloseParen ) {
312+ while (tokenizer->nextTokenKind () != TokenKind::Eof ) {
313+ if (tokenizer->nextTokenKind () == TokenKind::CloseParen ) {
329314 // end of args.
330315 endToken = tokenizer->consumeNextToken ();
331316 break ;
@@ -334,7 +319,7 @@ bool Parser::parseMatcherArgs(std::vector<ParserValue> &args, MatcherCtor ctor,
334319 if (!args.empty ()) {
335320 // We must find a , token to continue.
336321 TokenInfo commaToken = tokenizer->consumeNextToken ();
337- if (commaToken.kind != TokenInfo::TK_Comma ) {
322+ if (commaToken.kind != TokenKind::Comma ) {
338323 error->addError (commaToken.range , Diagnostics::ErrorType::ParserNoComma)
339324 << commaToken.text ;
340325 return false ;
@@ -380,7 +365,7 @@ bool Parser::parseMatcherExpressionImpl(const TokenInfo &nameToken,
380365 }
381366
382367 // Check for the missing closing parenthesis
383- if (endToken.kind != TokenInfo::TK_CloseParen ) {
368+ if (endToken.kind != TokenKind::CloseParen ) {
384369 error->addError (openToken.range , Diagnostics::ErrorType::ParserNoCloseParen)
385370 << nameToken.text ;
386371 return false ;
@@ -425,7 +410,7 @@ Parser::getNamedValueCompletions(ArrayRef<ArgKind> acceptedTypes) {
425410
426411void Parser::addExpressionCompletions () {
427412 const TokenInfo compToken = tokenizer->consumeNextTokenIgnoreNewlines ();
428- assert (compToken.kind == TokenInfo::TK_CodeCompletion );
413+ assert (compToken.kind == TokenKind::CodeCompletion );
429414
430415 // We cannot complete code if there is an invalid element on the context
431416 // stack.
@@ -447,31 +432,31 @@ void Parser::addExpressionCompletions() {
447432// Parse an <Expresssion>
448433bool Parser::parseExpressionImpl (VariantValue *value) {
449434 switch (tokenizer->nextTokenKind ()) {
450- case TokenInfo::TK_Literal :
435+ case TokenKind::Literal :
451436 *value = tokenizer->consumeNextToken ().value ;
452437 return true ;
453- case TokenInfo::TK_Ident :
438+ case TokenKind::Ident :
454439 return parseIdentifierPrefixImpl (value);
455- case TokenInfo::TK_CodeCompletion :
440+ case TokenKind::CodeCompletion :
456441 addExpressionCompletions ();
457442 return false ;
458- case TokenInfo::TK_Eof :
443+ case TokenKind::Eof :
459444 error->addError (tokenizer->consumeNextToken ().range ,
460445 Diagnostics::ErrorType::ParserNoCode);
461446 return false ;
462447
463- case TokenInfo::TK_Error :
448+ case TokenKind::Error :
464449 // This error was already reported by the tokenizer.
465450 return false ;
466- case TokenInfo::TK_NewLine :
467- case TokenInfo::TK_OpenParen :
468- case TokenInfo::TK_CloseParen :
469- case TokenInfo::TK_Comma :
470- case TokenInfo::TK_Period :
471- case TokenInfo::TK_InvalidChar :
451+ case TokenKind::NewLine :
452+ case TokenKind::OpenParen :
453+ case TokenKind::CloseParen :
454+ case TokenKind::Comma :
455+ case TokenKind::Period :
456+ case TokenKind::InvalidChar :
472457 const TokenInfo token = tokenizer->consumeNextToken ();
473458 error->addError (token.range , Diagnostics::ErrorType::ParserInvalidToken)
474- << (token.kind == TokenInfo::TK_NewLine ? " NewLine" : token.text );
459+ << (token.kind == TokenKind::NewLine ? " NewLine" : token.text );
475460 return false ;
476461 }
477462
@@ -516,8 +501,8 @@ bool Parser::parseExpression(llvm::StringRef &code, Sema *sema,
516501 if (!parser.parseExpressionImpl (value))
517502 return false ;
518503 auto nextToken = tokenizer.peekNextToken ();
519- if (nextToken.kind != TokenInfo::TK_Eof &&
520- nextToken.kind != TokenInfo::TK_NewLine ) {
504+ if (nextToken.kind != TokenKind::Eof &&
505+ nextToken.kind != TokenKind::NewLine ) {
521506 error->addError (tokenizer.peekNextToken ().range ,
522507 Diagnostics::ErrorType::ParserTrailingCode);
523508 return false ;
0 commit comments