llvm-project/mlir/lib/Query/QueryParser.h
Devajith 02d9f4d1f1 [mlir][mlir-query] Introduce mlir-query tool with autocomplete support
This commit adds the initial version of the mlir-query tool, which leverages the pre-existing matchers defined in mlir/include/mlir/IR/Matchers.h

The tool provides the following set of basic queries:

hasOpAttrName(string)
hasOpName(string)
isConstantOp()
isNegInfFloat()
isNegZeroFloat()
isNonZero()
isOne()
isOneFloat()
isPosInfFloat()
isPosZeroFloat()
isZero()
isZeroFloat()

Reviewed By: jpienaar

Differential Revision: https://reviews.llvm.org/D155127
2023-10-13 14:03:27 -07:00

60 lines
1.6 KiB
C++

//===--- QueryParser.h - ----------------------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
#ifndef MLIR_TOOLS_MLIRQUERY_QUERYPARSER_H
#define MLIR_TOOLS_MLIRQUERY_QUERYPARSER_H
#include "Matcher/Parser.h"
#include "mlir/Query/Query.h"
#include "mlir/Query/QuerySession.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/LineEditor/LineEditor.h"
namespace mlir::query {
class QuerySession;
class QueryParser {
public:
// Parse line as a query and return a QueryRef representing the query, which
// may be an InvalidQuery.
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);
private:
QueryParser(llvm::StringRef line, const QuerySession &qs)
: line(line), completionPos(nullptr), qs(qs) {}
llvm::StringRef lexWord();
template <typename T>
struct LexOrCompleteWord;
QueryRef completeMatcherExpression();
QueryRef endQuery(QueryRef queryRef);
// Parse [begin, end) and returns a reference to the parsed query object,
// which may be an InvalidQuery if a parse error occurs.
QueryRef doParse();
llvm::StringRef line;
const char *completionPos;
std::vector<llvm::LineEditor::Completion> completions;
const QuerySession &qs;
};
} // namespace mlir::query
#endif // MLIR_TOOLS_MLIRQUERY_QUERYPARSER_H