root / trunk / compiler / cppAst / ast / TextEmitter.hpp @ 7
1 | 7 | krennw | /**
|
---|---|---|---|
2 | *
|
||
3 | * OOAS Compiler - C++ AST
|
||
4 | *
|
||
5 | * Copyright 2015, AIT Austrian Institute of Technology.
|
||
6 | * All rights reserved.
|
||
7 | *
|
||
8 | * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
|
||
9 | *
|
||
10 | * If you modify the file please update the list of contributors below to in-
|
||
11 | * clude your name. Please also stick to the coding convention of using TABs
|
||
12 | * to do the basic (block-level) indentation and spaces for anything after
|
||
13 | * that. (Enable the display of special chars and it should be pretty obvious
|
||
14 | * what this means.) Also, remove all trailing whitespace.
|
||
15 | *
|
||
16 | * Contributors:
|
||
17 | * Willibald Krenn (AIT)
|
||
18 | * Stephan Zimmerer (AIT)
|
||
19 | * Christoph Czurda (AIT)
|
||
20 | *
|
||
21 | */
|
||
22 | 2 | krennw | |
23 | |||
24 | |||
25 | |||
26 | #pragma once
|
||
27 | |||
28 | #include <string> |
||
29 | #include <cstdint> |
||
30 | #include <boost/format.hpp> |
||
31 | |||
32 | |||
33 | namespace Ast {
|
||
34 | |||
35 | /**
|
||
36 | * Shameless copy of the Java "CodeEmitter"...
|
||
37 | */
|
||
38 | |||
39 | class TextEmitter |
||
40 | { |
||
41 | protected:
|
||
42 | std::string m_output;
|
||
43 | unsigned int m_indentLevel; |
||
44 | const unsigned int m_indentTabWidth; |
||
45 | |||
46 | public:
|
||
47 | TextEmitter(): |
||
48 | m_output (""),
|
||
49 | m_indentLevel (0),
|
||
50 | m_indentTabWidth(4)
|
||
51 | {} |
||
52 | |||
53 | void indent()
|
||
54 | { |
||
55 | for (unsigned int i = 0; i < m_indentLevel * m_indentTabWidth; i++) |
||
56 | m_output += " ";
|
||
57 | } |
||
58 | |||
59 | void append(const boost::format& msg) { |
||
60 | append(msg.str()); |
||
61 | } |
||
62 | |||
63 | void append(std::int64_t num)
|
||
64 | { |
||
65 | m_output += std::to_string(num); |
||
66 | } |
||
67 | |||
68 | void append(const std::string& text) |
||
69 | { |
||
70 | m_output += text; |
||
71 | } |
||
72 | |||
73 | void append(const char* text) |
||
74 | { |
||
75 | m_output += text; |
||
76 | } |
||
77 | |||
78 | void appendLineIncIndent(std::string& text) |
||
79 | { |
||
80 | m_indentLevel++; |
||
81 | appendLine(text); |
||
82 | } |
||
83 | |||
84 | void appendLineDecIndent(std::string& text) |
||
85 | { |
||
86 | if (m_indentLevel > 0) |
||
87 | { |
||
88 | m_indentLevel--; |
||
89 | string::size_type startIndex = m_output.length() - m_indentTabWidth;
|
||
90 | m_output = m_output.erase(startIndex, std::string::npos);
|
||
91 | } |
||
92 | appendLine(text); |
||
93 | } |
||
94 | |||
95 | void appendLine(const boost::format& msg) { |
||
96 | appendLine(msg.str()); |
||
97 | } |
||
98 | |||
99 | void appendLine(const char* text) |
||
100 | { |
||
101 | m_output += text; |
||
102 | appendLine(); |
||
103 | } |
||
104 | |||
105 | void appendLine(const std::string& text) |
||
106 | { |
||
107 | appendLine(text.c_str()); |
||
108 | } |
||
109 | |||
110 | void appendLine()
|
||
111 | { |
||
112 | m_output += "\n";
|
||
113 | indent(); |
||
114 | } |
||
115 | |||
116 | |||
117 | |||
118 | void incIndent()
|
||
119 | { |
||
120 | m_indentLevel++; |
||
121 | } |
||
122 | |||
123 | void decIndent()
|
||
124 | { |
||
125 | m_indentLevel--; |
||
126 | } |
||
127 | |||
128 | void clear()
|
||
129 | { |
||
130 | m_output = "";
|
||
131 | } |
||
132 | |||
133 | |||
134 | std::string& toString()
|
||
135 | { |
||
136 | return m_output;
|
||
137 | } |
||
138 | }; |
||
139 | |||
140 | } /* namespace Ast */
|