Skip to content

Commit 54edd72

Browse files
committed
🚧 WIP improve comment system
adding block comments
1 parent d2c9293 commit 54edd72

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

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

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
public class FileParser {
1717
public static final Pattern LINE_PATTERN = Pattern.compile("^((?:[^#]|##)*)(\\s*#(?!#).*)$"); // Might as well take that from Skript
1818

19+
// 0 = not in a comment, 1 = in a one-line comment, 2 = in a block comment
20+
private static int COMMENT_STATUS = 0;
21+
1922
/**
2023
* Parses a {@linkplain List} of strings into a list of {@link FileElement}s. This creates {@link FileElement} and
2124
* {@link FileSection} objects from the lines, effectively structuring the lines into a tree.
@@ -36,11 +39,14 @@ public static List<FileElement> parseFileLines(String fileName, List<String> lin
3639

3740
if (content == null) {
3841
content = line.replace("##", "#").strip();
39-
} else if (content.isEmpty()) {
42+
}
43+
else if (content.isEmpty()) {
4044
elements.add(new VoidElement(fileName, lastLine + i, expectedIndentation));
4145
continue;
4246
}
4347

48+
//System.out.println(content);
49+
4450
var lineIndentation = FileUtils.getIndentationLevel(line, false);
4551
if (lineIndentation > expectedIndentation) { // The line is indented too much
4652
logger.error(
@@ -92,39 +98,34 @@ private static int count(List<FileElement> elements) {
9298
*/
9399
@Nullable
94100
private static String removeComments(String string) {
95-
if (string.matches("^[\\s\\t]*#[^#]+") || string.startsWith("#") || string.isBlank()) {
96-
return ""; // Whole string is a comment
97-
}
98-
99-
var builder = new StringBuilder();
100-
outer:
101-
for (int i = 0; i < string.length(); i++) {
102-
char c = string.charAt(i);
103-
101+
StringBuilder stringBuilder = new StringBuilder();
102+
for (char c : string.toCharArray()) {
104103
if (c == '#') {
105-
if (string.charAt(i + 1) == '#') {
106-
builder.append(c).append(string.charAt(++i));
107-
} else {
108-
var checked = string.substring(i + 1);
109-
for (int j : new int[] {3, 6, 8}) {
110-
if (checked.length() >= j
111-
&& Color.COLOR_PATTERN.matcher(checked.substring(0, j)).matches()) {
112-
builder.append(c).append(checked, 0, j);
113-
i+=j;
114-
continue outer;
104+
int index = string.indexOf(c);
105+
//System.out.println(c + " : " + COMMENT_STATUS);
106+
//Checking if it isn't color hex and if no double # (for escaping first #)
107+
for (int i : new int[]{3, 6, 9}) {
108+
if (index + i <= string.length())
109+
if (!Color.COLOR_PATTERN.matcher(string.substring(index, index + i)).matches() || string.charAt(index + 1) != '#') {
110+
COMMENT_STATUS = 1;
111+
System.out.println(COMMENT_STATUS);
115112
}
116-
}
117-
118-
// Comment was found. Erase it from the string
119-
assert !builder.toString().isEmpty();
120-
return builder.toString().strip();
121113
}
122-
} else {
123-
builder.append(c);
114+
//System.out.println(string.substring(index, index + 2));
115+
if (index + 2 <= string.length() && string.substring(index, index + 2).equals("##")) {
116+
COMMENT_STATUS = COMMENT_STATUS == 2 ? 0 : 2;
117+
System.out.println(COMMENT_STATUS);
118+
}
119+
120+
}
121+
if (COMMENT_STATUS == 0) {
122+
stringBuilder.append(c);
124123
}
125124
}
126-
if (builder.toString().equals(string))
127-
return null;
128-
return builder.toString().strip();
125+
if (COMMENT_STATUS == 1) COMMENT_STATUS = 0;
126+
//System.out.println(stringBuilder.toString());
127+
return stringBuilder.toString().strip();
128+
129129
}
130+
130131
}

0 commit comments

Comments
 (0)