16
16
public class FileParser {
17
17
public static final Pattern LINE_PATTERN = Pattern .compile ("^((?:[^#]|##)*)(\\ s*#(?!#).*)$" ); // Might as well take that from Skript
18
18
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
+
19
22
/**
20
23
* Parses a {@linkplain List} of strings into a list of {@link FileElement}s. This creates {@link FileElement} and
21
24
* {@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
34
37
var line = lines .get (i );
35
38
String content = removeComments (line );
36
39
37
- if (content == null ) {
38
- content = line .replace ("##" , "#" ).strip ();
39
- } else if (content .isEmpty ()) {
40
+ if (content .isEmpty ()) {
40
41
elements .add (new VoidElement (fileName , lastLine + i , expectedIndentation ));
41
42
continue ;
42
43
}
@@ -92,39 +93,36 @@ private static int count(List<FileElement> elements) {
92
93
*/
93
94
@ Nullable
94
95
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 ();
101
97
for (int i = 0 ; i < string .length (); i ++) {
102
98
char c = string .charAt (i );
103
-
104
99
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 ;
116
106
}
107
+ }
117
108
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 ;
121
116
}
122
- } else {
123
- builder .append (c );
117
+
118
+ }
119
+ if (COMMENT_STATUS == 0 ) {
120
+ stringBuilder .append (c );
124
121
}
125
122
}
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
+
129
126
}
127
+
130
128
}
0 commit comments