Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions llvm/lib/Support/Mustache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include <cctype>
#include <optional>
#include <sstream>

#define DEBUG_TYPE "mustache"
Expand Down Expand Up @@ -364,35 +365,33 @@ static Tag findNextTag(StringRef Template, size_t StartPos, StringRef Open,
return Result;
}

static void processTag(const Tag &T, SmallVectorImpl<Token> &Tokens,
SmallString<8> &Open, SmallString<8> &Close) {
static std::optional<std::pair<StringRef, StringRef>>
processTag(const Tag &T, SmallVectorImpl<Token> &Tokens) {
LLVM_DEBUG(dbgs() << " Found tag: \"" << T.FullMatch << "\", Content: \""
<< T.Content << "\"\n");
if (T.TagKind == Tag::Kind::Triple) {
Tokens.emplace_back(T.FullMatch.str(), "&" + T.Content.str(), '&');
LLVM_DEBUG(dbgs() << " Created UnescapeVariable token.\n");
return;
return std::nullopt;
}
StringRef Interpolated = T.Content;
std::string RawBody = T.FullMatch.str();
if (!Interpolated.trim().starts_with("=")) {
char Front = Interpolated.empty() ? ' ' : Interpolated.trim().front();
Tokens.emplace_back(RawBody, Interpolated.str(), Front);
LLVM_DEBUG(dbgs() << " Created tag token of type '" << Front << "'\n");
return;
return std::nullopt;
}
Tokens.emplace_back(RawBody, Interpolated.str(), '=');
StringRef DelimSpec = Interpolated.trim();
DelimSpec = DelimSpec.drop_front(1);
DelimSpec = DelimSpec.take_until([](char C) { return C == '='; });
DelimSpec = DelimSpec.trim();

auto [NewOpen, NewClose] = DelimSpec.split(' ');
Open = NewOpen;
Close = NewClose;

LLVM_DEBUG(dbgs() << " Found Set Delimiter tag. NewOpen='" << Open
<< "', NewClose='" << Close << "'\n");
std::pair<StringRef, StringRef> Ret = DelimSpec.split(' ');
LLVM_DEBUG(dbgs() << " Found Set Delimiter tag. NewOpen='" << Ret.first
<< "', NewClose='" << Ret.second << "'\n");
return Ret;
}

// Simple tokenizer that splits the template into tokens.
Expand Down Expand Up @@ -426,7 +425,9 @@ static SmallVector<Token> tokenize(StringRef Template) {
LLVM_DEBUG(dbgs() << " Created Text token: \"" << Text << "\"\n");
}

processTag(T, Tokens, Open, Close);
if (auto NewDelims = processTag(T, Tokens)) {
std::tie(Open, Close) = *NewDelims;
}

// Move past the tag.
Start = T.StartPosition + T.FullMatch.size();
Expand Down