shithub: rgbds

Download patch

ref: d51ab3520328fe7bd82f37da276180c3d7d4059d
parent: 3a71910312a58fafb497c50795dede0ae7010b06
author: ISSOtm <eldredhabert0@gmail.com>
date: Wed Jun 8 20:01:19 EDT 2022

Trim macro arg whitespace after line continuations

--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -2052,9 +2052,23 @@
 	size_t i = 0;
 	int c;
 
-	/* Trim left whitespace (stops at a block comment or line continuation) */
-	while (isWhitespace(peek()))
-		shiftChar();
+	/* Trim left whitespace (stops at a block comment) */
+	for (;;) {
+		c = peek();
+		if (isWhitespace(c)) {
+			shiftChar();
+		} else if (c == '\\') {
+			shiftChar();
+			c = peek();
+			// If not a line continuation, handle as a normal char
+			if (!isWhitespace(c) && c != '\n' && c != '\r')
+				goto backslash;
+			// Line continuations count as "whitespace"
+			readLineContinuation();
+		} else {
+			break;
+		}
+	}
 
 	for (;;) {
 		c = peek();
@@ -2103,6 +2117,7 @@
 			shiftChar();
 			c = peek();
 
+backslash:
 			switch (c) {
 			case ',': /* Escapes only valid inside a macro arg */
 			case '(':
--- a/test/asm/macro-arguments.out
+++ b/test/asm/macro-arguments.out
@@ -4,8 +4,8 @@
 \1: < 1>
 \2: <2>
 
-'mac 	c,d':
-\1: <	c>
+'mac c,d':
+\1: <c>
 \2: <d>
 
 'mac 1,2 +  2,3':
--- /dev/null
+++ b/test/asm/trimmed-macro-args.asm
@@ -1,0 +1,19 @@
+MACRO print_all
+	REPT _NARG
+		PRINTLN "{d:_NARG}: \"\1\""
+		SHIFT
+	ENDR
+ENDM
+
+	print_all a, \
+	          b \
+	        , c
+
+DEF EMPTY equs ""
+	print_all a, \
+	  {EMPTY} b \
+	 {EMPTY}, c
+
+	print_all a, \
+	  /* . */ b \
+	 /* . */, c
--- /dev/null
+++ b/test/asm/trimmed-macro-args.out
@@ -1,0 +1,9 @@
+3: "a"
+2: "b"
+1: "c"
+3: "a"
+2: "b"
+1: "c"
+3: "a"
+2: " b"
+1: "c"