[Mlir-commits] [mlir] [mlir] Add struct parsing and printing utilities (PR #133939)
Mehdi Amini
llvmlistbot at llvm.org
Thu Apr 3 09:42:14 PDT 2025
================
@@ -570,6 +570,51 @@ class AsmParserImpl : public BaseT {
parser.getState().cyclicParsingStack.pop_back();
}
+ //===----------------------------------------------------------------------===//
+ // Struct Parsing
+ //===----------------------------------------------------------------------===//
+
+ /// Parse a comma-separated list of key-value pairs with a specified
+ /// delimiter.
+ ParseResult
+ parseStruct(Delimiter delimiter, ArrayRef<StringRef> keywords,
+ ArrayRef<llvm::function_ref<ParseResult()>> parseFuncs) override {
+ assert(keywords.size() == parseFuncs.size());
+ auto keyError = [&]() -> ParseResult {
+ InFlightDiagnostic parseError =
+ emitError(getCurrentLocation(), "expected one of: ");
+ llvm::interleaveComma(keywords, parseError, [&](StringRef kw) {
+ parseError << '`' << kw << '`';
+ });
+ return parseError;
+ };
+ SmallVector<bool> seen(keywords.size(), false);
+ DenseMap<StringRef, size_t> keywordToIndex;
+ for (auto &&[idx, keyword] : llvm::enumerate(keywords))
+ keywordToIndex[keyword] = idx;
+ return parseCommaSeparatedList(
+ delimiter,
+ [&]() -> ParseResult {
+ StringRef keyword;
+ if (failed(parseOptionalKeyword(&keyword)))
+ return keyError();
+ if (!keywordToIndex.contains(keyword))
+ return keyError();
----------------
joker-eph wrote:
I think the location will not be exact here: it will point to the token after the keyword right?
Also it would be worth for the error to mention what keyword was parsed
https://github.com/llvm/llvm-project/pull/133939
More information about the Mlir-commits
mailing list