Skip to content

Commit cac4d84

Browse files
committed
💩 need to be finished and squashed
1 parent 7a61292 commit cac4d84

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

src/main/java/io/github/syst3ms/skriptparser/file/FileParser.java

+39-18
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import java.util.ArrayList;
1010
import java.util.List;
11+
import java.util.regex.Matcher;
1112
import java.util.regex.Pattern;
1213

1314
/**
@@ -36,7 +37,7 @@ public static List<FileElement> parseFileLines(String fileName, List<String> lin
3637
for (var i = 0; i < lines.size(); i++) {
3738
var line = lines.get(i);
3839
String content = removeComments(line);
39-
40+
//System.out.println(content);
4041
if (content.isEmpty()) {
4142
elements.add(new VoidElement(fileName, lastLine + i, expectedIndentation));
4243
continue;
@@ -94,28 +95,48 @@ private static int count(List<FileElement> elements) {
9495
@Nullable
9596
private static String removeComments(String string) {
9697
StringBuilder stringBuilder = new StringBuilder();
97-
for (char c : string.toCharArray()) {
98+
for (int i = 0; i < string.length(); i++) {
99+
char c = string.charAt(i);
98100
if (c == '#') {
99-
int index = string.indexOf(c);
100-
if (index + 1 >= string.length()) return "";
101-
if (string.charAt(index + 1) != '#') { //double # escape the first #
102-
//Checking if it isn't color hex and if no double # (for escaping first #)
103-
for (int i : new int[]{3, 6, 9}) {
104-
if (index + i <= string.length() - 1 && !Color.COLOR_PATTERN.matcher(string.substring(index + 1, index + i + 1)).matches())
105-
COMMENT_STATUS = 1;
106-
}
107-
}
108-
//set start or end of a block comment ("###" characters)
109-
if (index + 2 <= string.length() && string.substring(index, index + 2).equals("##")) {
110-
COMMENT_STATUS = COMMENT_STATUS == 2 ? 0 : 2;
111-
}
101+
switch (COMMENT_STATUS) {
102+
case 0:
103+
//System.out.println("debug color match: " + Color.COLOR_PATTERN.matcher("ffff0000").matches());
104+
for (int j : new int[]{3, 6, 8}) {
105+
if (i + j <= string.length() - 1) {
106+
//System.out.println("debug string: " + string.substring(i + 1, i + j + 1));
107+
if (!Color.COLOR_PATTERN.matcher(string.substring(i + 1, i + j + 1)).matches()) {
108+
//System.out.println("yolo debug 1");
109+
COMMENT_STATUS = 1;
110+
}
111+
} else {
112+
COMMENT_STATUS = 1;
113+
break;
114+
}
115+
}
116+
break;
117+
case 1:
118+
if (string.length() <= i + 1 && string.charAt(i + 1) == '#')
119+
COMMENT_STATUS = 2;
112120

121+
else if(string.charAt(i - 1) == '#')
122+
COMMENT_STATUS = 0;
123+
124+
}
113125
}
114-
if (COMMENT_STATUS == 0) {
126+
/*
127+
System.out.println(
128+
"Caractere : " + c + '\n' + "Caractere numero : " + i
129+
+ '\n' + "Status commentaire: " + COMMENT_STATUS + '\n'
130+
);
131+
132+
*/
133+
if (COMMENT_STATUS == 0)
115134
stringBuilder.append(c);
116-
}
135+
136+
117137
}
118-
if (COMMENT_STATUS == 1) COMMENT_STATUS = 0;
138+
if (COMMENT_STATUS != 2) COMMENT_STATUS = 0;
139+
119140
return stringBuilder.toString().strip();
120141

121142
}

src/test/resources/general/comments.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ test:
99
throws "##BlackLivesMatter" = "#BlackLivesMatter"
1010

1111
throws true ###= false
12-
assert true # ##= false
12+
assert true # ##= false
13+
14+
assert true

0 commit comments

Comments
 (0)