Skip to content

Commit 1033135

Browse files
committed
Refractor parser to reduce loc by moving to header
1 parent c95127f commit 1033135

File tree

2 files changed

+32
-68
lines changed

2 files changed

+32
-68
lines changed

mlir/lib/Tools/mlir-query/Matcher/Parser.cpp

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -247,18 +247,6 @@ class Parser::CodeTokenizer {
247247
const char *codeCompletionLocation = nullptr;
248248
};
249249

250-
Parser::Sema::~Sema() = default;
251-
252-
std::vector<ArgKind> Parser::Sema::getAcceptedCompletionTypes(
253-
llvm::ArrayRef<std::pair<MatcherCtor, unsigned>> context) {
254-
return {};
255-
}
256-
257-
std::vector<MatcherCompletion>
258-
Parser::Sema::getMatcherCompletions(llvm::ArrayRef<ArgKind> acceptedTypes) {
259-
return {};
260-
}
261-
262250
// Entry for the scope of a parser
263251
struct Parser::ScopedContextEntry {
264252
Parser *parser;
@@ -568,40 +556,6 @@ Parser::Parser(CodeTokenizer *tokenizer, Sema *sema,
568556
: tokenizer(tokenizer), sema(sema ? sema : &*defaultRegistrySema),
569557
namedValues(namedValues), error(error) {}
570558

571-
Parser::RegistrySema::~RegistrySema() = default;
572-
573-
std::optional<MatcherCtor>
574-
Parser::RegistrySema::lookupMatcherCtor(llvm::StringRef matcherName) {
575-
return Registry::lookupMatcherCtor(matcherName);
576-
}
577-
578-
VariantMatcher Parser::RegistrySema::actOnMatcherExpression(
579-
MatcherCtor ctor, SourceRange nameRange, ArrayRef<ParserValue> args,
580-
Diagnostics *error) {
581-
return Registry::constructMatcher(ctor, nameRange, args, error);
582-
}
583-
584-
std::vector<ArgKind> Parser::RegistrySema::getAcceptedCompletionTypes(
585-
ArrayRef<std::pair<MatcherCtor, unsigned>> context) {
586-
return Registry::getAcceptedCompletionTypes(context);
587-
}
588-
589-
std::vector<MatcherCompletion>
590-
Parser::RegistrySema::getMatcherCompletions(ArrayRef<ArgKind> acceptedTypes) {
591-
return Registry::getMatcherCompletions(acceptedTypes);
592-
}
593-
594-
bool Parser::RegistrySema::isBuilderMatcher(MatcherCtor ctor) const {
595-
return Registry::isBuilderMatcher(ctor);
596-
}
597-
598-
internal::MatcherDescriptorPtr
599-
Parser::RegistrySema::buildMatcherCtor(MatcherCtor ctor, SourceRange nameRange,
600-
ArrayRef<ParserValue> args,
601-
Diagnostics *error) const {
602-
return Registry::buildMatcherCtor(ctor, nameRange, args, error);
603-
}
604-
605559
bool Parser::parseExpression(llvm::StringRef &code, Sema *sema,
606560
const NamedValueMap *namedValues,
607561
VariantValue *value, Diagnostics *error) {

mlir/lib/Tools/mlir-query/Matcher/Parser.h

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Parser {
4545
// matcher tokens.
4646
class Sema {
4747
public:
48-
virtual ~Sema();
48+
virtual ~Sema() = default;
4949

5050
// Process a matcher expression. The caller takes ownership of the Matcher
5151
// object returned.
@@ -58,47 +58,51 @@ class Parser {
5858
virtual std::optional<MatcherCtor>
5959
lookupMatcherCtor(llvm::StringRef matcherName) = 0;
6060

61-
virtual bool isBuilderMatcher(MatcherCtor) const = 0;
61+
virtual bool isBuilderMatcher(MatcherCtor ctor) const = 0;
6262

6363
virtual internal::MatcherDescriptorPtr
64-
buildMatcherCtor(MatcherCtor, SourceRange nameRange,
64+
buildMatcherCtor(MatcherCtor ctor, SourceRange nameRange,
6565
ArrayRef<ParserValue> args, Diagnostics *error) const = 0;
6666

67-
// Compute the list of completion types for Context.
6867
virtual std::vector<ArgKind> getAcceptedCompletionTypes(
69-
llvm::ArrayRef<std::pair<MatcherCtor, unsigned>> Context);
68+
llvm::ArrayRef<std::pair<MatcherCtor, unsigned>> context) {
69+
return {};
70+
}
7071

71-
// Compute the list of completions that match any of acceptedTypes.
7272
virtual std::vector<MatcherCompletion>
73-
getMatcherCompletions(llvm::ArrayRef<ArgKind> acceptedTypes);
73+
getMatcherCompletions(llvm::ArrayRef<ArgKind> acceptedTypes) {
74+
return {};
75+
}
7476
};
7577

7678
// RegistrySema class - an implementation of the Sema interface that uses the
7779
// matcher registry to process tokens.
7880
class RegistrySema : public Parser::Sema {
7981
public:
80-
~RegistrySema() override;
82+
~RegistrySema() override = default;
8183

8284
std::optional<MatcherCtor>
83-
lookupMatcherCtor(llvm::StringRef matcherName) override;
85+
lookupMatcherCtor(llvm::StringRef matcherName) override {
86+
return Registry::lookupMatcherCtor(matcherName);
87+
}
8488

8589
VariantMatcher actOnMatcherExpression(MatcherCtor ctor,
8690
SourceRange nameRange,
8791
ArrayRef<ParserValue> args,
88-
Diagnostics *error) override;
92+
Diagnostics *error) override {
93+
return Registry::constructMatcher(ctor, nameRange, args, error);
94+
}
8995

90-
bool isBuilderMatcher(MatcherCtor ctor) const override;
91-
92-
std::vector<ArgKind> getAcceptedCompletionTypes(
93-
llvm::ArrayRef<std::pair<MatcherCtor, unsigned>> context) override;
96+
bool isBuilderMatcher(MatcherCtor ctor) const override {
97+
return Registry::isBuilderMatcher(ctor);
98+
}
9499

95100
internal::MatcherDescriptorPtr
96-
buildMatcherCtor(MatcherCtor, SourceRange nameRange,
101+
buildMatcherCtor(MatcherCtor ctor, SourceRange nameRange,
97102
ArrayRef<ParserValue> args,
98-
Diagnostics *error) const override;
99-
100-
std::vector<MatcherCompletion>
101-
getMatcherCompletions(llvm::ArrayRef<ArgKind> acceptedTypes) override;
103+
Diagnostics *error) const override {
104+
return Registry::buildMatcherCtor(ctor, nameRange, args, error);
105+
}
102106
};
103107

104108
using NamedValueMap = llvm::StringMap<VariantValue>;
@@ -108,17 +112,18 @@ class Parser {
108112
static std::optional<DynMatcher>
109113
parseMatcherExpression(llvm::StringRef &matcherCode, Sema *sema,
110114
const NamedValueMap *namedValues, Diagnostics *error);
115+
111116
static std::optional<DynMatcher>
112117
parseMatcherExpression(llvm::StringRef &matcherCode, Sema *sema,
113118
Diagnostics *error) {
114119
return parseMatcherExpression(matcherCode, sema, nullptr, error);
115120
}
121+
116122
static std::optional<DynMatcher>
117123
parseMatcherExpression(llvm::StringRef &matcherCode, Diagnostics *error) {
118124
return parseMatcherExpression(matcherCode, nullptr, error);
119125
}
120126

121-
// Methods to parse any expression supported by this parser.
122127
static bool parseExpression(llvm::StringRef &code, Sema *sema,
123128
const NamedValueMap *namedValues,
124129
VariantValue *value, Diagnostics *error);
@@ -127,20 +132,22 @@ class Parser {
127132
VariantValue *value, Diagnostics *error) {
128133
return parseExpression(code, sema, nullptr, value, error);
129134
}
135+
130136
static bool parseExpression(llvm::StringRef &code, VariantValue *value,
131137
Diagnostics *error) {
132138
return parseExpression(code, nullptr, value, error);
133139
}
134140

135-
// Methods to complete an expression at a given offset.
136141
static std::vector<MatcherCompletion>
137142
completeExpression(llvm::StringRef &code, unsigned completionOffset,
138143
Sema *sema, const NamedValueMap *namedValues);
144+
139145
static std::vector<MatcherCompletion>
140146
completeExpression(llvm::StringRef &code, unsigned completionOffset,
141147
Sema *sema) {
142148
return completeExpression(code, completionOffset, sema, nullptr);
143149
}
150+
144151
static std::vector<MatcherCompletion>
145152
completeExpression(llvm::StringRef &code, unsigned completionOffset) {
146153
return completeExpression(code, completionOffset, nullptr);
@@ -160,11 +167,14 @@ class Parser {
160167
const TokenInfo &nameToken,
161168
const TokenInfo &openToken,
162169
const TokenInfo &endToken, VariantValue *value);
170+
163171
bool parseMatcherArgs(bool isBuilder, std::vector<ParserValue> &args,
164172
MatcherCtor ctor, const TokenInfo &nameToken,
165173
TokenInfo &endToken);
174+
166175
bool parseMatcherBuilder(MatcherCtor ctor, const TokenInfo &nameToken,
167176
const TokenInfo &openToken, VariantValue *value);
177+
168178
bool parseMatcherExpressionImpl(const TokenInfo &nameToken,
169179
const TokenInfo &openToken,
170180
std::optional<MatcherCtor> ctor,
@@ -174,6 +184,7 @@ class Parser {
174184

175185
void addCompletion(const TokenInfo &compToken,
176186
const MatcherCompletion &completion);
187+
177188
void addExpressionCompletions();
178189

179190
std::vector<MatcherCompletion>
@@ -185,7 +196,6 @@ class Parser {
185196
Diagnostics *const error;
186197

187198
using ContextStackTy = std::vector<std::pair<MatcherCtor, unsigned>>;
188-
189199
ContextStackTy contextStack;
190200
std::vector<MatcherCompletion> completions;
191201
};

0 commit comments

Comments
 (0)