diff --git a/src/main/java/io/github/syst3ms/skriptparser/file/FileParser.java b/src/main/java/io/github/syst3ms/skriptparser/file/FileParser.java index 47b23e1f..dbcb14ef 100644 --- a/src/main/java/io/github/syst3ms/skriptparser/file/FileParser.java +++ b/src/main/java/io/github/syst3ms/skriptparser/file/FileParser.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.regex.Matcher; import java.util.regex.Pattern; /** @@ -16,6 +17,9 @@ public class FileParser { public static final Pattern LINE_PATTERN = Pattern.compile("^((?:[^#]|##)*)(\\s*#(?!#).*)$"); // Might as well take that from Skript + // 0 = not in a comment, 1 = in a one-line comment, 2 = in a block comment + private static int COMMENT_STATUS = 0; + /** * Parses a {@linkplain List} of strings into a list of {@link FileElement}s. This creates {@link FileElement} and * {@link FileSection} objects from the lines, effectively structuring the lines into a tree. @@ -33,10 +37,8 @@ public static List parseFileLines(String fileName, List lin for (var i = 0; i < lines.size(); i++) { var line = lines.get(i); String content = removeComments(line); - - if (content == null) { - content = line.replace("##", "#").strip(); - } else if (content.isEmpty()) { + //System.out.println(content); + if (content.isEmpty()) { elements.add(new VoidElement(fileName, lastLine + i, expectedIndentation)); continue; } @@ -92,39 +94,51 @@ private static int count(List elements) { */ @Nullable private static String removeComments(String string) { - if (string.matches("^[\\s\\t]*#[^#]+") || string.startsWith("#") || string.isBlank()) { - return ""; // Whole string is a comment - } - - var builder = new StringBuilder(); - outer: + StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < string.length(); i++) { char c = string.charAt(i); - if (c == '#') { - if (string.charAt(i + 1) == '#') { - builder.append(c).append(string.charAt(++i)); - } else { - var checked = string.substring(i + 1); - for (int j : new int[] {3, 6, 8}) { - if (checked.length() >= j - && Color.COLOR_PATTERN.matcher(checked.substring(0, j)).matches()) { - builder.append(c).append(checked, 0, j); - i+=j; - continue outer; + switch (COMMENT_STATUS) { + case 0: + //System.out.println("debug color match: " + Color.COLOR_PATTERN.matcher("ffff0000").matches()); + for (int j : new int[]{3, 6, 8}) { + if (i + j <= string.length() - 1) { + //System.out.println("debug string: " + string.substring(i + 1, i + j + 1)); + if (!Color.COLOR_PATTERN.matcher(string.substring(i + 1, i + j + 1)).matches()) { + //System.out.println("yolo debug 1"); + COMMENT_STATUS = 1; + } + } else { + COMMENT_STATUS = 1; + break; + } } - } + break; + case 1: + if (string.length() <= i + 1 && string.charAt(i + 1) == '#') + COMMENT_STATUS = 2; + + else if(string.charAt(i - 1) == '#') + COMMENT_STATUS = 0; - // Comment was found. Erase it from the string - assert !builder.toString().isEmpty(); - return builder.toString().strip(); } - } else { - builder.append(c); } + /* + System.out.println( + "Caractere : " + c + '\n' + "Caractere numero : " + i + + '\n' + "Status commentaire: " + COMMENT_STATUS + '\n' + ); + + */ + if (COMMENT_STATUS == 0) + stringBuilder.append(c); + + } - if (builder.toString().equals(string)) - return null; - return builder.toString().strip(); + if (COMMENT_STATUS != 2) COMMENT_STATUS = 0; + + return stringBuilder.toString().strip(); + } + } diff --git a/src/test/resources/general/comments.txt b/src/test/resources/general/comments.txt index 85198842..c9a1ae7d 100644 --- a/src/test/resources/general/comments.txt +++ b/src/test/resources/general/comments.txt @@ -9,4 +9,6 @@ test: throws "##BlackLivesMatter" = "#BlackLivesMatter" throws true ###= false - assert true # ##= false \ No newline at end of file + assert true # ##= false + + assert true \ No newline at end of file