Skip to content

Commit 36823f8

Browse files
authored
rewrite preprocessor
1 parent 52f78ec commit 36823f8

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

src/main/java/mods/Hileb/shotaasm/api/ScriptFile.java

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,39 @@
1717
public record ScriptFile(String name, String text, Multimap<String, String> property, HashMap<String, Object> data) {
1818

1919
@Nullable
20-
public static ScriptFile create(String name, String rawText){
20+
public static ScriptFile create(String name, String rawText) {
2121
try (BufferedReader reader = new BufferedReader(new StringReader(rawText))) {
22-
List<String> lines = new ArrayList<>();
22+
List<String> commentLines = new ArrayList<>();
23+
StringBuilder nonCommentText = new StringBuilder();
2324
String line;
25+
2426
while ((line = reader.readLine()) != null) {
2527
line = line.trim();
2628
if (StringUtils.startsWith(line, "#")) {
27-
lines.add(line.substring(1));
29+
commentLines.add(line.substring(1));
2830
} else {
29-
Multimap<String, String> map = HashMultimap.create();
30-
for (String s : lines) {
31-
int i = s.indexOf(' ');
32-
if (i > 0) {
33-
map.put(s.substring(0, i), s.substring(i + 1));
34-
}
35-
}
36-
if (!map.containsKey("compiler")) map.put("compiler", "javaShota");
37-
38-
byte[] b1 = IOUtils.toByteArray(reader, StandardCharsets.UTF_8);
39-
byte[] b2 = new byte[line.length() + 1 + b1.length];
40-
System.arraycopy(line.getBytes(), 0, b2, 0, line.length());
41-
b2[line.length()] = '\n';
42-
System.arraycopy(b1, line.length(), b2, 0, b1.length);
43-
44-
return new ScriptFile(
45-
name,
46-
new String(b2),
47-
map, new HashMap<>()
48-
);
31+
nonCommentText.append(line).append("\n");
4932
}
5033
}
51-
return null;
34+
35+
if(nonCommentText.length() > 0 && nonCommentText.charAt(nonCommentText.length()-1) == '\n'){
36+
nonCommentText.deleteCharAt(nonCommentText.length()-1);
37+
}
38+
39+
Multimap<String, String> map = HashMultimap.create();
40+
for (String s : commentLines) {
41+
int i = s.indexOf(' ');
42+
if (i > 0) {
43+
map.put(s.substring(0, i), s.substring(i + 1));
44+
}
45+
}
46+
if (!map.containsKey("compiler")) {
47+
map.put("compiler", "javaShota");
48+
}
49+
50+
return new ScriptFile(name, nonCommentText.toString(), map, new HashMap<>());
5251
} catch (Exception e) {
53-
ShotaASM.LOGGER.error("error when locate script with", e);
52+
ShotaASM.LOGGER.error("Error creating ScriptFile for '{}': {}", name, e.getMessage(), e);
5453
return null;
5554
}
5655
}

0 commit comments

Comments
 (0)