Browse code

Support nested comments in uxnasm.

Previously, code like this would fail with an error
about an unrecognized ) token:

( this is a ( nested ) comment )

With this patch, the above code will now work.

Relatedly, it was previously possible to write code
that compiled but was confusing:

(open parenthesis should have a space )
( in this case the ADD2 will be ignored )ADD2
( this comment with ( would have been fine )

With this commit, the first example will emit a warning
but continue to work as intended. The second and third
examples will continue searching for a matching ) token,
which due to the new nested coment behavior will probably
mean the rest of the file gets commented out.

Erik Osheim authored on 29/12/2021 02:38:05 • neauoire committed on 29/12/2021 03:28:15
Showing 1 changed files
... ...
@@ -243,8 +243,13 @@ parse(char *w, FILE *f)
243 243
 		return error("Invalid token", w);
244 244
 	switch(w[0]) {
245 245
 	case '(': /* comment */
246
-		while(fscanf(f, "%63s", word) == 1)
247
-			if(word[0] == ')') break;
246
+		if(slen(w) != 1) fprintf(stderr, "-- Malformed comment: %s\n", w);
247
+		i = 1; /* track nested comment depth */
248
+		while(fscanf(f, "%63s", word) == 1) {
249
+			if(slen(word) != 1) continue;
250
+			else if(word[0] == '(') i++;
251
+			else if(word[0] == ')' && --i < 1) break;
252
+		}
248 253
 		break;
249 254
 	case '~': /* include */
250 255
 		if(!doinclude(w + 1))