summaryrefslogtreecommitdiffstats
path: root/source/compiler-core/slang-pretty-writer.cpp
diff options
context:
space:
mode:
authortareksander <57038324+tareksander@users.noreply.github.com>2024-11-11 20:32:00 +0100
committerGitHub <noreply@github.com>2024-11-11 11:32:00 -0800
commit7e51180ed80ce1c5718b65c6625dc12fe64c8bfa (patch)
tree6a6451b388d52ba01e8b4190dc498b435c5ea978 /source/compiler-core/slang-pretty-writer.cpp
parent98dab05e80a90ddf64c9d31420a3b49ec49e31d1 (diff)
Reflection compiler option (#5507)
* Moved the pretty writer code from slang-reflection-test into core * Moved reflection test code into the slang codebase and added the compiler option -reflection-json to store the reflection data in a separate file. * Documented -reflection-json command line option * moved PrettyWriter from core to compiler-core * Fixed variable shadowing warning * Use File::writeAllText instead of OSFilesystem and write to stdout if - is used as the path * format code * Fixed linker error * Fix COM Ptr life time issues. * Move enum to the end. * Fix formatting. --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'source/compiler-core/slang-pretty-writer.cpp')
-rw-r--r--source/compiler-core/slang-pretty-writer.cpp85
1 files changed, 85 insertions, 0 deletions
diff --git a/source/compiler-core/slang-pretty-writer.cpp b/source/compiler-core/slang-pretty-writer.cpp
new file mode 100644
index 000000000..682b02c1e
--- /dev/null
+++ b/source/compiler-core/slang-pretty-writer.cpp
@@ -0,0 +1,85 @@
+#include "slang-pretty-writer.h"
+
+#include "../core/slang-string-escape-util.h"
+
+namespace Slang
+{
+
+void PrettyWriter::writeRaw(char const* begin, char const* end)
+{
+ SLANG_ASSERT(end >= begin);
+ writeRaw(UnownedStringSlice(begin, end));
+}
+
+void PrettyWriter::adjust()
+{
+ // Only indent if at start of a line
+ if (m_startOfLine)
+ {
+ // Output current indentation
+ m_builder.appendRepeatedChar(' ', m_indent * 4);
+ m_startOfLine = false;
+ }
+}
+
+void PrettyWriter::dedent()
+{
+ SLANG_ASSERT(m_indent > 0);
+ m_indent--;
+}
+
+void PrettyWriter::write(const UnownedStringSlice& slice)
+{
+ const auto end = slice.end();
+ auto start = slice.begin();
+
+ while (start < end)
+ {
+ const char* cur = start;
+
+ // Search for \n if there is one
+ while (cur < end && *cur != '\n')
+ cur++;
+
+ // If there were some chars, adjust and write
+ if (cur > start)
+ {
+ adjust();
+ writeRaw(UnownedStringSlice(start, cur));
+ }
+
+ if (cur < end && *cur == '\n')
+ {
+ writeRawChar('\n');
+ // Skip the CR
+ cur++;
+ // Mark we are at the start of a line
+ m_startOfLine = true;
+ }
+
+ start = cur;
+ }
+}
+
+void PrettyWriter::writeEscapedString(const UnownedStringSlice& slice)
+{
+ adjust();
+ auto handler = StringEscapeUtil::getHandler(StringEscapeUtil::Style::Cpp);
+ StringEscapeUtil::appendQuoted(handler, slice, m_builder);
+}
+
+void PrettyWriter::maybeComma()
+{
+ if (auto state = m_commaState)
+ {
+ if (!state->needComma)
+ {
+ state->needComma = true;
+ return;
+ }
+ }
+
+ write(toSlice(",\n"));
+}
+
+} // namespace Slang