mirror of
https://github.com/revng/revng
synced 2026-06-21 14:07:57 +00:00
f05c08547d
By using this class we can create ptml::Tags without XML tags. It is useful in the revng-c part when we want to generate Plain C. All PTML Tags, from now on, should be created via this class only. In addition, port `Yield` library to be using this.
52 lines
1.2 KiB
C++
52 lines
1.2 KiB
C++
//
|
|
// This file is distributed under the MIT License. See LICENSE.md for details.
|
|
//
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "revng/PTML/Constants.h"
|
|
#include "revng/PTML/IndentedOstream.h"
|
|
#include "revng/PTML/Tag.h"
|
|
|
|
namespace ptml {
|
|
|
|
uint64_t PTMLIndentedOstream::current_pos() const {
|
|
return OS.tell();
|
|
}
|
|
|
|
void PTMLIndentedOstream::write_impl(const char *Ptr, size_t Size) {
|
|
llvm::StringRef Str(Ptr, Size);
|
|
llvm::SmallVector<llvm::StringRef> Lines;
|
|
Str.split(Lines, '\n');
|
|
|
|
bool EndsInNewLine = Str.endswith("\n");
|
|
if (EndsInNewLine)
|
|
Lines.pop_back();
|
|
|
|
if (TrailingNewline)
|
|
writeIndent();
|
|
|
|
for (auto &Line : llvm::make_range(Lines.begin(), std::prev(Lines.end()))) {
|
|
OS << Line << '\n';
|
|
writeIndent();
|
|
}
|
|
|
|
OS << Lines.pop_back_val();
|
|
if (EndsInNewLine)
|
|
OS << '\n';
|
|
|
|
TrailingNewline = EndsInNewLine;
|
|
}
|
|
|
|
void PTMLIndentedOstream::writeIndent() {
|
|
if (IndentDepth > 0) {
|
|
OS << ThePTMLBuilder
|
|
.getTag(tags::Span, std::string(IndentSize * IndentDepth, ' '))
|
|
.addAttribute(attributes::Token, ptml::tokens::Indentation)
|
|
.serialize();
|
|
}
|
|
TrailingNewline = false;
|
|
}
|
|
|
|
} // namespace ptml
|