forked from lykhonis/FJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClazz.java
More file actions
87 lines (66 loc) · 1.99 KB
/
Copy pathClazz.java
File metadata and controls
87 lines (66 loc) · 1.99 KB
1
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.fjava.translator;
import static com.fjava.translator.TokenType.CLASS;
import static com.fjava.translator.TokenType.COMMA;
import static com.fjava.translator.TokenType.IDENTIFIER;
import static com.fjava.translator.TokenType.LPAREN;
import static com.fjava.translator.TokenType.PRIVATE;
import static com.fjava.translator.TokenType.PROTECTED;
import static com.fjava.translator.TokenType.PUBLIC;
import static com.fjava.translator.TokenType.RPAREN;
import java.util.ArrayList;
import java.util.List;
public class Clazz extends Atom {
private static class Field {
public String visibility;
public String type;
public String name;
}
public Clazz(TranslatorFactory factory) {
super(factory);
}
@Override
public String translate() {
final StringBuilder result = new StringBuilder(format("class $2 {"));
final String constructor = format("public $2(");
skipRequired();
final List<Field> fields = new ArrayList<Field>();
TokenType type = require();
while (type != RPAREN) {
final Field field = new Field();
if (type == PUBLIC || type == PRIVATE || type == PROTECTED) {
field.visibility = get();
type = require();
} else
field.visibility = "public";
field.type = get();
require();
field.name = get();
fields.add(field);
type = require();
if (type == COMMA)
type = require();
}
for (Field field : fields) {
result.append(field.visibility).append(' ').append("final ").append(field.type).append(' ')
.append(field.name).append(';');
}
result.append(constructor);
boolean first = true;
for (Field field : fields) {
if (first)
first = false;
else
result.append(',');
result.append(field.type).append(' ').append(field.name);
}
result.append(") {");
for (Field field : fields)
result.append("this.").append(field.name).append('=').append(field.name).append(';');
result.append("}}");
return result.toString();
}
@Override
public boolean test() {
return test(CLASS, IDENTIFIER, LPAREN);
}
}