From 54edd72314972b5b9e677c1b5c8d1facca7a77c9 Mon Sep 17 00:00:00 2001 From: WeeskyBDW Date: Sat, 28 Aug 2021 03:43:47 +0200 Subject: [PATCH 1/3] :construction: WIP improve comment system adding block comments --- .../syst3ms/skriptparser/file/FileParser.java | 61 ++++++++++--------- 1 file changed, 31 insertions(+), 30 deletions(-) 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..7dd2bb49 100644 --- a/src/main/java/io/github/syst3ms/skriptparser/file/FileParser.java +++ b/src/main/java/io/github/syst3ms/skriptparser/file/FileParser.java @@ -16,6 +16,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. @@ -36,11 +39,14 @@ public static List parseFileLines(String fileName, List lin if (content == null) { content = line.replace("##", "#").strip(); - } else if (content.isEmpty()) { + } + else if (content.isEmpty()) { elements.add(new VoidElement(fileName, lastLine + i, expectedIndentation)); continue; } + //System.out.println(content); + var lineIndentation = FileUtils.getIndentationLevel(line, false); if (lineIndentation > expectedIndentation) { // The line is indented too much logger.error( @@ -92,39 +98,34 @@ 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: - for (int i = 0; i < string.length(); i++) { - char c = string.charAt(i); - + StringBuilder stringBuilder = new StringBuilder(); + for (char c : string.toCharArray()) { 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; + int index = string.indexOf(c); + //System.out.println(c + " : " + COMMENT_STATUS); + //Checking if it isn't color hex and if no double # (for escaping first #) + for (int i : new int[]{3, 6, 9}) { + if (index + i <= string.length()) + if (!Color.COLOR_PATTERN.matcher(string.substring(index, index + i)).matches() || string.charAt(index + 1) != '#') { + COMMENT_STATUS = 1; + System.out.println(COMMENT_STATUS); } - } - - // Comment was found. Erase it from the string - assert !builder.toString().isEmpty(); - return builder.toString().strip(); } - } else { - builder.append(c); + //System.out.println(string.substring(index, index + 2)); + if (index + 2 <= string.length() && string.substring(index, index + 2).equals("##")) { + COMMENT_STATUS = COMMENT_STATUS == 2 ? 0 : 2; + System.out.println(COMMENT_STATUS); + } + + } + if (COMMENT_STATUS == 0) { + stringBuilder.append(c); } } - if (builder.toString().equals(string)) - return null; - return builder.toString().strip(); + if (COMMENT_STATUS == 1) COMMENT_STATUS = 0; + //System.out.println(stringBuilder.toString()); + return stringBuilder.toString().strip(); + } + } From 7a61292f25693fbc5b0676ae0d519b5734e88c99 Mon Sep 17 00:00:00 2001 From: WeeskyBDW Date: Sat, 28 Aug 2021 22:43:22 +0200 Subject: [PATCH 2/3] :construction: Add escape one line comment & remove color clash --- .../syst3ms/skriptparser/file/FileParser.java | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) 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 7dd2bb49..e08aff1d 100644 --- a/src/main/java/io/github/syst3ms/skriptparser/file/FileParser.java +++ b/src/main/java/io/github/syst3ms/skriptparser/file/FileParser.java @@ -37,16 +37,11 @@ public static List parseFileLines(String fileName, List lin var line = lines.get(i); String content = removeComments(line); - if (content == null) { - content = line.replace("##", "#").strip(); - } - else if (content.isEmpty()) { + if (content.isEmpty()) { elements.add(new VoidElement(fileName, lastLine + i, expectedIndentation)); continue; } - //System.out.println(content); - var lineIndentation = FileUtils.getIndentationLevel(line, false); if (lineIndentation > expectedIndentation) { // The line is indented too much logger.error( @@ -102,19 +97,17 @@ private static String removeComments(String string) { for (char c : string.toCharArray()) { if (c == '#') { int index = string.indexOf(c); - //System.out.println(c + " : " + COMMENT_STATUS); - //Checking if it isn't color hex and if no double # (for escaping first #) - for (int i : new int[]{3, 6, 9}) { - if (index + i <= string.length()) - if (!Color.COLOR_PATTERN.matcher(string.substring(index, index + i)).matches() || string.charAt(index + 1) != '#') { + if (index + 1 >= string.length()) return ""; + if (string.charAt(index + 1) != '#') { //double # escape the first # + //Checking if it isn't color hex and if no double # (for escaping first #) + for (int i : new int[]{3, 6, 9}) { + if (index + i <= string.length() - 1 && !Color.COLOR_PATTERN.matcher(string.substring(index + 1, index + i + 1)).matches()) COMMENT_STATUS = 1; - System.out.println(COMMENT_STATUS); - } + } } - //System.out.println(string.substring(index, index + 2)); + //set start or end of a block comment ("###" characters) if (index + 2 <= string.length() && string.substring(index, index + 2).equals("##")) { COMMENT_STATUS = COMMENT_STATUS == 2 ? 0 : 2; - System.out.println(COMMENT_STATUS); } } @@ -123,7 +116,6 @@ private static String removeComments(String string) { } } if (COMMENT_STATUS == 1) COMMENT_STATUS = 0; - //System.out.println(stringBuilder.toString()); return stringBuilder.toString().strip(); } From cac4d8497b04fad7a916bae8f7211a0c559cc4b9 Mon Sep 17 00:00:00 2001 From: WeeskyBDW Date: Sun, 7 Nov 2021 23:16:23 +0100 Subject: [PATCH 3/3] :poop: need to be finished and squashed --- .../syst3ms/skriptparser/file/FileParser.java | 57 +++++++++++++------ src/test/resources/general/comments.txt | 4 +- 2 files changed, 42 insertions(+), 19 deletions(-) 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 e08aff1d..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; /** @@ -36,7 +37,7 @@ 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); - + //System.out.println(content); if (content.isEmpty()) { elements.add(new VoidElement(fileName, lastLine + i, expectedIndentation)); continue; @@ -94,28 +95,48 @@ private static int count(List elements) { @Nullable private static String removeComments(String string) { StringBuilder stringBuilder = new StringBuilder(); - for (char c : string.toCharArray()) { + for (int i = 0; i < string.length(); i++) { + char c = string.charAt(i); if (c == '#') { - int index = string.indexOf(c); - if (index + 1 >= string.length()) return ""; - if (string.charAt(index + 1) != '#') { //double # escape the first # - //Checking if it isn't color hex and if no double # (for escaping first #) - for (int i : new int[]{3, 6, 9}) { - if (index + i <= string.length() - 1 && !Color.COLOR_PATTERN.matcher(string.substring(index + 1, index + i + 1)).matches()) - COMMENT_STATUS = 1; - } - } - //set start or end of a block comment ("###" characters) - if (index + 2 <= string.length() && string.substring(index, index + 2).equals("##")) { - COMMENT_STATUS = COMMENT_STATUS == 2 ? 0 : 2; - } + 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; + + } } - if (COMMENT_STATUS == 0) { + /* + System.out.println( + "Caractere : " + c + '\n' + "Caractere numero : " + i + + '\n' + "Status commentaire: " + COMMENT_STATUS + '\n' + ); + + */ + if (COMMENT_STATUS == 0) stringBuilder.append(c); - } + + } - if (COMMENT_STATUS == 1) COMMENT_STATUS = 0; + 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