root/trunk/compiler/cppAst/ast/TextEmitter.hpp @ 2
2 | krennw | /**
|
|
*
|
|||
* OOAS Compiler - C++ AST
|
|||
*
|
|||
* Copyright 2015, AIT Austrian Institute of Technology.
|
|||
* All rights reserved.
|
|||
*
|
|||
* SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
|
|||
*
|
|||
* If you modify the file please update the list of contributors below to in-
|
|||
* clude your name. Please also stick to the coding convention of using TABs
|
|||
* to do the basic (block-level) indentation and spaces for anything after
|
|||
* that. (Enable the display of special chars and it should be pretty obvious
|
|||
* what this means.) Also, remove all trailing whitespace.
|
|||
*
|
|||
* Contributors:
|
|||
* Willibald Krenn (AIT)
|
|||
* Stephan Zimmerer (AIT)
|
|||
* Christoph Czurda (AIT)
|
|||
*
|
|||
*/
|
|||
#pragma once
|
|||
#include <string>
|
|||
#include <cstdint>
|
|||
#include <boost/format.hpp>
|
|||
namespace Ast {
|
|||
/**
|
|||
* Shameless copy of the Java "CodeEmitter"...
|
|||
*/
|
|||
class TextEmitter
|
|||
{
|
|||
protected:
|
|||
std::string m_output;
|
|||
unsigned int m_indentLevel;
|
|||
const unsigned int m_indentTabWidth;
|
|||
public:
|
|||
TextEmitter():
|
|||
m_output (""),
|
|||
m_indentLevel (0),
|
|||
m_indentTabWidth(4)
|
|||
{}
|
|||
void indent()
|
|||
{
|
|||
for (unsigned int i = 0; i < m_indentLevel * m_indentTabWidth; i++)
|
|||
m_output += " ";
|
|||
}
|
|||
void append(const boost::format& msg) {
|
|||
append(msg.str());
|
|||
}
|
|||
void append(std::int64_t num)
|
|||
{
|
|||
m_output += std::to_string(num);
|
|||
}
|
|||
void append(const std::string& text)
|
|||
{
|
|||
m_output += text;
|
|||
}
|
|||
void append(const char* text)
|
|||
{
|
|||
m_output += text;
|
|||
}
|
|||
void appendLineIncIndent(std::string& text)
|
|||
{
|
|||
m_indentLevel++;
|
|||
appendLine(text);
|
|||
}
|
|||
void appendLineDecIndent(std::string& text)
|
|||
{
|
|||
if (m_indentLevel > 0)
|
|||
{
|
|||
m_indentLevel--;
|
|||
string::size_type startIndex = m_output.length() - m_indentTabWidth;
|
|||
m_output = m_output.erase(startIndex, std::string::npos);
|
|||
}
|
|||
appendLine(text);
|
|||
}
|
|||
void appendLine(const boost::format& msg) {
|
|||
appendLine(msg.str());
|
|||
}
|
|||
void appendLine(const char* text)
|
|||
{
|
|||
m_output += text;
|
|||
appendLine();
|
|||
}
|
|||
void appendLine(const std::string& text)
|
|||
{
|
|||
appendLine(text.c_str());
|
|||
}
|
|||
void appendLine()
|
|||
{
|
|||
m_output += "\n";
|
|||
indent();
|
|||
}
|
|||
void incIndent()
|
|||
{
|
|||
m_indentLevel++;
|
|||
}
|
|||
void decIndent()
|
|||
{
|
|||
m_indentLevel--;
|
|||
}
|
|||
void clear()
|
|||
{
|
|||
m_output = "";
|
|||
}
|
|||
std::string& toString()
|
|||
{
|
|||
return m_output;
|
|||
}
|
|||
};
|
|||
} /* namespace Ast */
|