1616public 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.
@@ -34,9 +37,7 @@ public static List<FileElement> parseFileLines(String fileName, List<String> lin
3437 var line = lines .get (i );
3538 String content = removeComments (line );
3639
37- if (content == null ) {
38- content = line .replace ("##" , "#" ).strip ();
39- } else if (content .isEmpty ()) {
40+ if (content .isEmpty ()) {
4041 elements .add (new VoidElement (fileName , lastLine + i , expectedIndentation ));
4142 continue ;
4243 }
@@ -92,39 +93,36 @@ private static int count(List<FileElement> elements) {
9293 */
9394 @ Nullable
9495 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 :
96+ StringBuilder stringBuilder = new StringBuilder ();
10197 for (int i = 0 ; i < string .length (); i ++) {
10298 char c = string .charAt (i );
103-
10499 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 ;
115- }
100+ if (i + 1 >= string .length ()) return "" ;
101+ if (COMMENT_STATUS == 0 ) {
102+ //Checking if it isn't color hex and if no double # (for escaping first #)
103+ for (int j : new int []{3 , 6 , 9 }) {
104+ if (i + j <= string .length () - 1 && !Color .COLOR_PATTERN .matcher (string .substring (i +1 , i + j )).matches ())
105+ COMMENT_STATUS = 1 ;
116106 }
107+ }
117108
118- // Comment was found. Erase it from the string
119- assert !builder .toString ().isEmpty ();
120- return builder .toString ().strip ();
109+ if (i + 3 <= string .length ()) {
110+ //set start or end of a block comment ("###" characters)
111+ if (string .substring (i , i + 3 ).equals ("###" ))
112+ COMMENT_STATUS = COMMENT_STATUS == 2 ? 0 : 2 ;
113+ else if (string .substring (i , i + 2 ).equals ("##" ))
114+ COMMENT_STATUS = 0 ;
115+ i += 2 ;
121116 }
122- } else {
123- builder .append (c );
117+
118+ }
119+ if (COMMENT_STATUS == 0 ) {
120+ stringBuilder .append (c );
124121 }
125122 }
126- if (builder . toString (). equals ( string ))
127- return null ;
128- return builder . toString (). strip ();
123+ if (COMMENT_STATUS == 1 ) COMMENT_STATUS = 0 ;
124+ return stringBuilder . toString (). strip () ;
125+
129126 }
127+
130128}
0 commit comments