shithub: rgbds

Download patch

ref: 2a9d52871b0bc17f4056cdba71729085a1db8866
parent: c4fb5591eeddd2baad12fc32400dfea2bdd20fcf
author: Rangi <[email protected]>
date: Fri Jan 1 07:44:47 EST 2021

Make dbgPrint in lexer.c report the correct colNo (#676)

Fixes #656

--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -366,6 +366,12 @@
 	state->expansionOfs = 0;
 }
 
+static void nextLine(void)
+{
+	lexerState->lineNo++;
+	lexerState->colNo = 1;
+}
+
 struct LexerState *lexer_OpenFile(char const *path)
 {
 	dbgPrint("Opening file \"%s\"\n", path);
@@ -900,11 +906,12 @@
 		/* Now, `distance` is how many bytes to move forward **in the file** */
 	}
 
+	lexerState->colNo += distance;
+
 	if (lexerState->isMmapped) {
 		lexerState->offset += distance;
 	} else {
 		lexerState->index += distance;
-		lexerState->colNo += distance;
 		/* Wrap around if necessary */
 		if (lexerState->index >= LEXER_BUF_SIZE)
 			lexerState->index %= LEXER_BUF_SIZE;
@@ -1030,11 +1037,12 @@
 			shiftChars(1);
 		} else if (c == '\r' || c == '\n') {
 			shiftChars(1);
+			/* Handle CRLF before nextLine() since shiftChars updates colNo */
 			if (c == '\r' && peek(0) == '\n')
 				shiftChars(1);
 			if (!lexerState->expansions
 			 || lexerState->expansions->distance)
-				lexerState->lineNo++;
+				nextLine();
 			return;
 		} else if (c == ';') {
 			discardComment();
@@ -2007,12 +2015,13 @@
 				atLineStart = true;
 			}
 
-			if (c == '\r' || c == '\n')
+			if (c == '\r' || c == '\n') {
+				/* Handle CRLF before nextLine() since shiftChars updates colNo */
+				if (c == '\r' && peek(0) == '\n')
+					shiftChars(1);
 				/* Do this both on line continuations and plain EOLs */
-				lexerState->lineNo++;
-			/* Handle CRLF */
-			if (c == '\r' && peek(0) == '\n')
-				shiftChars(1);
+				nextLine();
+			}
 		} while (!atLineStart);
 	}
 finish:
@@ -2043,10 +2052,8 @@
 	}
 	if (lexerState->atLineStart) {
 		/* Newlines read within an expansion should not increase the line count */
-		if (!lexerState->expansions || lexerState->expansions->distance) {
-			lexerState->lineNo++;
-			lexerState->colNo = 0;
-		}
+		if (!lexerState->expansions || lexerState->expansions->distance)
+			nextLine();
 	}
 
 	static int (* const lexerModeFuncs[])(void) = {
@@ -2111,7 +2118,7 @@
 	 */
 	assert(lexerState->atLineStart);
 	for (;;) {
-		lexerState->lineNo++;
+		nextLine();
 		/* We're at line start, so attempt to match a `REPT` or `ENDR` token */
 		do { /* Discard initial whitespace */
 			c = nextChar();
@@ -2224,7 +2231,7 @@
 				goto finish;
 			}
 		}
-		lexerState->lineNo++;
+		nextLine();
 	}
 
 finish: