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,39 @@ 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+ System .out .println ("popo : " + i );
101+ if (i + 1 >= string .length ()) return "" ;
102+ if (COMMENT_STATUS == 0 ) {
103+ //Checking if it isn't color hex and if no double # (for escaping first #)
104+ for (int j : new int []{3 , 6 , 9 }) {
105+ if (i + j <= string .length () - 1 && !Color .COLOR_PATTERN .matcher (string .substring (i +1 , i + j )).matches ())
106+ COMMENT_STATUS = 1 ;
116107 }
108+ }
117109
118- // Comment was found. Erase it from the string
119- assert !builder .toString ().isEmpty ();
120- return builder .toString ().strip ();
110+ //set start or end of a block comment ("###" characters)
111+ if (i + 3 <= string .length ()) {
112+ if (string .substring (i , i + 3 ).equals ("###" ))
113+ COMMENT_STATUS = COMMENT_STATUS == 2 ? 0 : 2 ;
114+ else if (string .substring (i , i + 2 ).equals ("##" ))
115+ COMMENT_STATUS = 0 ;
116+ System .out .println ("toto : " + string .substring (i , i + 2 ));
117+ i += 2 ;
121118 }
122- } else {
123- builder .append (c );
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 ("---------------------------------------" );
127+ return stringBuilder .toString ().strip ();
128+
129129 }
130+
130131}
0 commit comments