1 package net.sourceforge.jenesis4java.impl;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 import java.io.PrintWriter;
40 import java.util.ArrayList;
41 import java.util.List;
42
43 import net.sourceforge.jenesis4java.CodeWriter;
44 import net.sourceforge.jenesis4java.Codeable;
45 import net.sourceforge.jenesis4java.Comment;
46 import net.sourceforge.jenesis4java.CompilationUnit;
47
48
49
50
51 public class MCodeWriter implements CodeWriter {
52
53
54 private final PrintWriter out;
55
56 private int colNo = 0;
57
58 private int lineNo = 1;
59
60 private boolean isLineNew = true;
61
62
63
64
65 private int indentDepth = 0;
66
67
68
69
70
71
72 private int indentationQueue = 0;
73
74 private final List<Comment> queue = new ArrayList<>();
75
76 private CompilationUnit compilationUnit;
77
78 public MCodeWriter(PrintWriter out) {
79 this.out = out;
80 }
81
82
83
84
85 @Override
86 public CodeWriter dedentLine() {
87 flushQueue();
88 indentDepth--;
89 ensureNewLine();
90 indentationQueue = indentDepth;
91 return this;
92 }
93
94
95
96
97
98
99 @Override
100 public int getColumnNumber() {
101 return colNo;
102 }
103
104 @Override
105 public CompilationUnit getCompilationUnit() {
106 return compilationUnit;
107 }
108
109
110
111
112 @Override
113 public int getIndentNumber() {
114 return indentDepth;
115 }
116
117
118
119
120 @Override
121 public int getLineNumber() {
122 return lineNo;
123 }
124
125
126
127
128 @Override
129 public CodeWriter indentLine() {
130 flushQueue();
131 indentDepth++;
132 ensureNewLine();
133 indentationQueue = indentDepth;
134 return this;
135 }
136
137
138
139
140
141 @Override
142 public boolean isLineNew() {
143 return isLineNew;
144 }
145
146 @Override
147 public CodeWriter ensureNewLine() {
148 if (!isLineNew()) {
149 newLine();
150 }
151 return this;
152 }
153
154
155
156
157
158
159
160 @Override
161 public CodeWriter newLine() {
162 flushQueue();
163 out.println();
164 indentationQueue = indentDepth;
165 colNo = 0;
166 lineNo++;
167 isLineNew = true;
168 return this;
169 }
170
171
172
173
174
175
176
177
178
179 @Override
180 public MCodeWriter queue(Comment comment) {
181 if (comment != null) {
182 comment.setText(comment.getText());
183 queue.add(comment);
184 }
185 return this;
186 }
187
188
189
190
191 @Override
192 public CodeWriter resetLine() {
193 indentDepth = 0;
194 indentationQueue = 0;
195 newLine();
196 return this;
197 }
198
199 @Override
200 public void setCompilationUnit(CompilationUnit compilationUnit) {
201 this.compilationUnit = compilationUnit;
202 }
203
204
205
206
207 @Override
208 public CodeWriter space() {
209 writeIndent();
210 out.print(' ');
211 colNo++;
212 isLineNew = false;
213 return this;
214 }
215
216
217
218
219 @Override
220 public CodeWriter write(boolean b) {
221 writeIndent();
222
223 out.print(b);
224
225 colNo += b ? 4 : 5;
226 isLineNew = false;
227 return this;
228 }
229
230
231
232
233 @Override
234 public CodeWriter write(char c) {
235 writeIndent();
236
237 out.print(c);
238
239 colNo++;
240 isLineNew = false;
241 return this;
242 }
243
244
245
246
247 @Override
248 public CodeWriter write(char[] chars) {
249 if (chars != null) {
250 writeIndent();
251
252 out.print(chars);
253
254 colNo += chars.length;
255 isLineNew = false;
256 }
257 return this;
258 }
259
260
261
262
263 @Override
264 public CodeWriter write(char[] chars, int off, int len) {
265 if (chars != null) {
266 writeIndent();
267
268 out.write(chars, off, len);
269
270 colNo += chars.length;
271 isLineNew = false;
272 }
273 return this;
274 }
275
276
277
278
279
280
281 @Override
282 public CodeWriter write(Codeable ico) {
283 if (ico != null) {
284 writeIndent();
285 ico.toCode(this);
286 }
287 return this;
288 }
289
290
291
292
293 @Override
294 public CodeWriter write(Codeable[] aico) {
295 if (aico != null) {
296 writeIndent();
297 for (Codeable element : aico) {
298 write(element);
299 }
300 }
301 return this;
302 }
303
304
305
306
307 @Override
308 public CodeWriter write(double d) {
309 writeIndent();
310 out.print(d);
311 colNo += Double.toString(d).length();
312 isLineNew = false;
313 return this;
314 }
315
316
317
318
319 @Override
320 public CodeWriter write(float f) {
321 writeIndent();
322 out.print(f);
323 colNo += Float.toString(f).length();
324 isLineNew = false;
325 return this;
326 }
327
328
329
330
331 @Override
332 public CodeWriter write(int i) {
333 writeIndent();
334 out.print(i);
335 colNo += Integer.toString(i).length();
336 isLineNew = false;
337 return this;
338 }
339
340 @Override
341 public CodeWriter write(List<? extends Codeable> codeables) {
342 if (codeables != null) {
343 writeIndent();
344 for (Codeable element : codeables) {
345 write(element);
346 }
347 }
348 return this;
349 }
350
351
352
353
354 @Override
355 public CodeWriter write(Object o) {
356 if (o != null) {
357 writeIndent();
358 if (o instanceof Codeable) {
359 ((Codeable) o).toCode(this);
360 } else {
361 out.print(o);
362 colNo += o.toString().length();
363 isLineNew = false;
364 }
365 }
366 return this;
367 }
368
369
370
371
372 @Override
373 public CodeWriter write(String s) {
374 if (s != null) {
375 writeIndent();
376 out.print(s);
377 colNo += s.length();
378 isLineNew = false;
379 }
380
381 return this;
382 }
383
384 private void flushQueue() {
385 while (!queue.isEmpty()) {
386 Codeable c = queue.remove(0);
387 write(c);
388 }
389 }
390
391 private void writeIndent() {
392 if (indentationQueue != 0 && !isLineNew()) {
393 throw new IllegalStateException(
394 "Not expecting non-empty indentation queue on non-empty line.");
395 }
396
397 for (int i = 0; i < indentationQueue; ++i) {
398 out.print(" ");
399 }
400 indentationQueue = 0;
401 }
402
403 }