10 #include "llvm/ADT/StringSwitch.h"
18 llvm::StringRef QueryParser::lexWord() {
20 line = line.ltrim(
" \t\v\f\r");
29 if (line.front() ==
'#') {
30 word = line.substr(0, 1);
32 word = line.take_until([](
char c) {
34 return llvm::StringRef(
" \t\v\f\r").contains(c);
38 line = line.drop_front(word.size());
71 bool isCompletion =
true) {
75 else if (!caseStr.empty() && isCompletion &&
82 std::string(caseStr));
91 llvm::StringRef extra = line;
92 llvm::StringRef extraTrimmed = extra.ltrim(
" \t\v\f\r");
94 if (extraTrimmed.starts_with(
'\n') || extraTrimmed.starts_with(
"\r\n"))
95 queryRef->remainingContent = extra;
97 llvm::StringRef trailingWord = lexWord();
98 if (trailingWord.starts_with(
'#')) {
99 line = line.drop_until([](
char c) {
return c ==
'\n'; });
100 line = line.drop_while([](
char c) {
return c ==
'\n'; });
101 return endQuery(queryRef);
103 if (!trailingWord.empty()) {
104 return new InvalidQuery(
"unexpected extra input: '" + extra +
"'");
112 enum class ParsedQueryKind {
122 makeInvalidQueryFromDiagnostics(
const matcher::internal::Diagnostics &
diag) {
124 llvm::raw_string_ostream os(errStr);
126 return new InvalidQuery(errStr);
130 QueryRef QueryParser::completeMatcherExpression() {
131 std::vector<matcher::MatcherCompletion> comps =
135 for (
const auto &comp : comps) {
136 completions.emplace_back(comp.typedText, comp.matcherDecl);
143 llvm::StringRef commandStr;
144 ParsedQueryKind qKind =
145 LexOrCompleteWord<ParsedQueryKind>(
this, commandStr)
146 .Case(
"", ParsedQueryKind::NoOp)
147 .Case(
"#", ParsedQueryKind::Comment,
false)
148 .Case(
"help", ParsedQueryKind::Help)
149 .Case(
"m", ParsedQueryKind::Match,
false)
150 .Case(
"match", ParsedQueryKind::Match)
151 .Case(
"q", ParsedQueryKind::Quit,
false)
152 .Case(
"quit", ParsedQueryKind::Quit)
153 .Default(ParsedQueryKind::Invalid);
156 case ParsedQueryKind::Comment:
157 case ParsedQueryKind::NoOp:
158 line = line.drop_until([](
char c) {
return c ==
'\n'; });
159 line = line.drop_while([](
char c) {
return c ==
'\n'; });
161 return new NoOpQuery;
164 case ParsedQueryKind::Help:
165 return endQuery(
new HelpQuery);
167 case ParsedQueryKind::Quit:
168 return endQuery(
new QuitQuery);
170 case ParsedQueryKind::Match: {
172 return completeMatcherExpression();
175 matcher::internal::Diagnostics
diag;
176 auto matcherSource = line.ltrim();
177 auto origMatcherSource = matcherSource;
178 std::optional<matcher::DynMatcher> matcher =
182 return makeInvalidQueryFromDiagnostics(
diag);
184 auto actualSource = origMatcherSource.substr(0, origMatcherSource.size() -
185 matcherSource.size());
187 query->remainingContent = matcherSource;
191 case ParsedQueryKind::Invalid:
192 return new InvalidQuery(
"unknown command: " + commandStr);
195 llvm_unreachable(
"Invalid query kind");
202 std::vector<llvm::LineEditor::Completion>
206 queryParser.completionPos = line.data() + pos;
208 queryParser.doParse();
209 return queryParser.completions;
static std::string diag(const llvm::Value &value)
static QueryRef parse(llvm::StringRef line, const QuerySession &qs)
static std::vector< llvm::LineEditor::Completion > complete(llvm::StringRef line, size_t pos, const QuerySession &qs)
const matcher::Registry & getRegistryData() const
llvm::StringMap< matcher::VariantValue > namedValues
static std::vector< MatcherCompletion > completeExpression(llvm::StringRef &code, unsigned completionOffset, const Registry &matcherRegistry, const NamedValueMap *namedValues)
static std::optional< DynMatcher > parseMatcherExpression(llvm::StringRef &matcherCode, const Registry &matcherRegistry, const NamedValueMap *namedValues, Diagnostics *error)
The OpAsmOpInterface, see OpAsmInterface.td for more details.
FailureOr< Attribute > query(Operation *op, ArrayRef< DataLayoutEntryKey > keys, bool emitError=false)
Perform a DLTI-query at op, recursively querying each key of keys on query interface-implementing att...
llvm::IntrusiveRefCntPtr< Query > QueryRef
llvm::StringSwitch< T > stringSwitch
QueryParser * queryParser
LexOrCompleteWord(QueryParser *queryParser, llvm::StringRef &outWord)
LexOrCompleteWord & Case(llvm::StringLiteral caseStr, const T &value, bool isCompletion=true)