forked from PLRI/arden2bytecode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArdenCodeBuilder.java
225 lines (189 loc) · 7.05 KB
/
ArdenCodeBuilder.java
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package arden.tests.specification.testcompiler;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* Builder for generating Arden code from a template.
*/
public class ArdenCodeBuilder {
private static final String DEFAULT_TEMPLATE = readTemplate("Template.mlm");
private static final String V1_TEMPLATE = readTemplate("Template_V1.mlm");
private static final String RESOURCES_TEMPLATE = readTemplate("Template_Resources.mlm");
private StringBuilder resultBuilder;
private static String readTemplate(String filename) {
InputStream stream = ArdenCodeBuilder.class.getResourceAsStream(filename);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
StringBuilder stringBuilder = new StringBuilder();
try {
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append('\n');
}
} catch (IOException e) {
throw new ExceptionInInitializerError("Could not open template file (" + filename + ")");
} finally {
try {
bufferedReader.close();
} catch (IOException e) {
throw new ExceptionInInitializerError("Could not close template file reader");
}
}
return stringBuilder.toString();
}
/**
* Initialize the code builder with a template for the given version and set
* the "arden:" slot accordingly. <br>
* For version 1 the "filename:" slot is used instead of the "mlmname:" slot
* and the "arden:" slot is removed. <br>
* For versions greater or equal to version 2.9 the resources category is
* used in the builder's template, as it is no longer optional.
*
* @param version
* The Arden Syntax version for which the template must be
* compatible.
*/
ArdenCodeBuilder(ArdenVersion version) {
if (version == ArdenVersion.V1) {
resultBuilder = new StringBuilder(V1_TEMPLATE);
} else if (version.ordinal() >= ArdenVersion.V2_6.ordinal()) {
resultBuilder = new StringBuilder(RESOURCES_TEMPLATE);
setArdenVersion(version);
} else {
resultBuilder = new StringBuilder(DEFAULT_TEMPLATE);
setArdenVersion(version);
}
}
/**
* Initialize the code builder with a template or the output from another
* code builder.
*
* @param template
* complete code for an MLM
*/
public ArdenCodeBuilder(String template) {
resultBuilder = new StringBuilder(template);
}
private void checkSlotIndex(int index, String slotname) {
if (index == -1) {
throw new IllegalArgumentException("The \"" + slotname + "\" slot could not be found in the template");
}
}
public ArdenCodeBuilder removeSlot(String slotname) {
int slotIndex = resultBuilder.indexOf(slotname.toLowerCase());
checkSlotIndex(slotIndex, slotname);
int endIndex = resultBuilder.indexOf(";;", slotIndex);
checkSlotIndex(endIndex, slotname);
endIndex += ";;".length();
resultBuilder.delete(slotIndex, endIndex);
return this;
}
public ArdenCodeBuilder renameSlot(String slotname, String newname) {
int slotIndex = resultBuilder.indexOf(slotname.toLowerCase());
checkSlotIndex(slotIndex, slotname);
int endIndex = slotIndex + slotname.length();
resultBuilder.replace(slotIndex, endIndex, newname);
return this;
}
/**
* @param slotname
* e.g. <code>"title:"</code>
* @param slotcontent
* e.g. <code>"My Test MLM"</code>
* @param append
* append to slot or replace content
* @param ignoreCase
* ignore slot name case
*/
private ArdenCodeBuilder insertSlotContent(String slotname, String content, boolean append, boolean ignoreCase) {
slotname = ignoreCase ? slotname.toLowerCase() : slotname;
int slotIndex = resultBuilder.indexOf(slotname);
checkSlotIndex(slotIndex, slotname);
int startOfContent = slotIndex + slotname.length();
int endOfContent = resultBuilder.indexOf(";;", startOfContent);
checkSlotIndex(endOfContent, slotname);
String paddedContent = " " + content + " "; // to prevent illegal ";;;"
if (append) {
resultBuilder.insert(endOfContent, paddedContent).toString();
} else {
resultBuilder.replace(startOfContent, endOfContent, paddedContent).toString();
}
return this;
}
public ArdenCodeBuilder replaceSlotContent(String slotname, String content) {
return insertSlotContent(slotname, content, false, true);
}
public ArdenCodeBuilder appendSlotContent(String slotname, String content) {
return insertSlotContent(slotname, content, true, true);
}
public ArdenCodeBuilder clearSlotContent(String slotname) {
return insertSlotContent(slotname, "", false, true);
}
public ArdenCodeBuilder addData(String dataCode) {
return appendSlotContent("data:", dataCode);
}
public ArdenCodeBuilder addLogic(String logicCode) {
return appendSlotContent("logic:", logicCode);
}
public ArdenCodeBuilder addEvoke(String evokeCode) {
return appendSlotContent("evoke:", evokeCode);
}
public ArdenCodeBuilder addAction(String actionCode) {
return appendSlotContent("action:", actionCode);
}
public ArdenCodeBuilder addExpression(String expression) {
return addData("return_expression__ := " + expression + ";").addAction("RETURN return_expression__;");
}
public ArdenCodeBuilder addMlm(String mlmCode) {
resultBuilder.append('\n');
resultBuilder.append(mlmCode);
return this;
}
public ArdenCodeBuilder setArdenVersion(ArdenVersion version) {
if (version == ArdenVersion.V1) {
return removeSlot("arden:");
}
return replaceSlotContent("arden:", version.toString());
}
public ArdenCodeBuilder setName(String name) {
if (resultBuilder.indexOf("mlmname:") != -1) {
replaceSlotContent("mlmname:", name);
} else {
replaceSlotContent("filename:", name + ".mlm");
}
return this;
}
public ArdenCodeBuilder removeResourceSlots() {
int resourceIndex = resultBuilder.indexOf("resources:");
if (resourceIndex == -1) {
throw new IllegalArgumentException("The resources category could not be found in the template");
}
int startIndex = resourceIndex + "resources:".length();
int endIndex = resultBuilder.indexOf("end:");
resultBuilder.replace(startIndex, endIndex, " " + System.lineSeparator());
return this;
}
public ArdenCodeBuilder appendDefaultSlot(String languageCode) {
String resourceSlot = "default: " + languageCode + ";;" + System.lineSeparator();
int endIndex = resultBuilder.indexOf("end:");
resultBuilder.insert(endIndex, resourceSlot);
return this;
}
public ArdenCodeBuilder appendLanguageSlot(String languageCode) {
String resourceSlot = "language: " + languageCode + ";; " + System.lineSeparator();
int endIndex = resultBuilder.indexOf("end:");
resultBuilder.insert(endIndex, resourceSlot);
return this;
}
public ArdenCodeBuilder addTextConstant(String languageCode, String key, String value) {
// Note: Will only work if one space is between "language:" and the code
String slotName = "language: " + languageCode;
String textConstant = "'" + key + "': \"" + value + "\";";
insertSlotContent(slotName, textConstant, true, false);
return this;
}
@Override
public String toString() {
return resultBuilder.toString();
}
}