root / trunk / compiler / ooasCompiler / src / org / momut / ooas / codegen / OoasCodeEmitter.java @ 7
1 |
/**
|
---|---|
2 |
*
|
3 |
* OOAS Compiler
|
4 |
*
|
5 |
* Copyright 2015, AIT Austrian Institute of Technology.
|
6 |
* This code is based on the C# Version of the OOAS Compiler, which is
|
7 |
* copyright 2015 by the Institute of Software Technology, Graz University
|
8 |
* of Technology with portions copyright by the AIT Austrian Institute of
|
9 |
* Technology. All rights reserved.
|
10 |
*
|
11 |
* SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
|
12 |
*
|
13 |
* If you modify the file please update the list of contributors below to in-
|
14 |
* clude your name. Please also stick to the coding convention of using TABs
|
15 |
* to do the basic (block-level) indentation and spaces for anything after
|
16 |
* that. (Enable the display of special chars and it should be pretty obvious
|
17 |
* what this means.) Also, remove all trailing whitespace.
|
18 |
*
|
19 |
* Contributors:
|
20 |
* Willibald Krenn (AIT)
|
21 |
* Stephan Zimmerer (AIT)
|
22 |
* Markus Demetz (AIT)
|
23 |
* Christoph Czurda (AIT)
|
24 |
*
|
25 |
*/
|
26 |
|
27 |
|
28 |
package org.momut.ooas.codegen; |
29 |
|
30 |
/// <summary>
|
31 |
/// Class that is used as text buffer during code emission
|
32 |
/// </summary>
|
33 |
public class OoasCodeEmitter |
34 |
{ |
35 |
private StringBuilder output; |
36 |
private int indentLevel; |
37 |
private final int indentTabWidth; |
38 |
|
39 |
|
40 |
public OoasCodeEmitter()
|
41 |
{ |
42 |
indentLevel = 0;
|
43 |
indentTabWidth = 4;
|
44 |
output = new StringBuilder(); |
45 |
} |
46 |
|
47 |
public void Indent() |
48 |
{ |
49 |
for (int i = 0; i < indentLevel * indentTabWidth; i++) |
50 |
output.append(" ");
|
51 |
} |
52 |
|
53 |
public void Append(int num) |
54 |
{ |
55 |
output.append(num); |
56 |
} |
57 |
|
58 |
public void Append(String text) |
59 |
{ |
60 |
output.append(text); |
61 |
} |
62 |
|
63 |
public void AppendLineIncIndent(String text) |
64 |
{ |
65 |
indentLevel++; |
66 |
this.AppendLine(text);
|
67 |
} |
68 |
|
69 |
public void AppendLineDecIndent(String text) |
70 |
{ |
71 |
if (indentLevel > 0) |
72 |
{ |
73 |
indentLevel--; |
74 |
int startIndex = output.length() - indentTabWidth;
|
75 |
output.delete(startIndex, output.length()); |
76 |
} |
77 |
this.AppendLine(text);
|
78 |
} |
79 |
|
80 |
|
81 |
public void AppendLine(String text) |
82 |
{ |
83 |
output.append(text); |
84 |
output.append("\n");
|
85 |
Indent(); |
86 |
} |
87 |
|
88 |
public void AppendLine() |
89 |
{ |
90 |
output.append("\n");
|
91 |
Indent(); |
92 |
} |
93 |
|
94 |
|
95 |
|
96 |
public void IncIndent() |
97 |
{ |
98 |
indentLevel++; |
99 |
} |
100 |
|
101 |
public void DecIndent() |
102 |
{ |
103 |
indentLevel--; |
104 |
} |
105 |
|
106 |
public void Clear() |
107 |
{ |
108 |
output = new StringBuilder(); |
109 |
} |
110 |
|
111 |
|
112 |
@Override
|
113 |
public String toString() |
114 |
{ |
115 |
return output.toString();
|
116 |
} |
117 |
|
118 |
} |