shithub: rgbds

Download patch

ref: 365484ef1898a392d7a201de313a0f93e74ff5bf
parent: 5e2bd352395584264d22660da2070cf1c527bdcf
author: Rangi <remy.oukaour+rangi42@gmail.com>
date: Tue Mar 2 18:26:43 EST 2021

Factor out handleCRLF()

This logic repeats ten times

--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -1021,6 +1021,12 @@
 	return c;
 }
 
+static void handleCRLF(int c)
+{
+	if (c == '\r' && peek(0) == '\n')
+		shiftChars(1);
+}
+
 /* "Services" provided by the lexer to the rest of the program */
 
 char const *lexer_GetFileName(void)
@@ -1081,8 +1087,7 @@
 			goto finish;
 		case '\r':
 			/* Handle CRLF before nextLine() since shiftChars updates colNo */
-			if (peek(0) == '\n')
-				shiftChars(1);
+			handleCRLF(c);
 			/* fallthrough */
 		case '\n':
 			if (!lexerState->expansions || lexerState->expansions->distance)
@@ -1140,10 +1145,8 @@
 		} 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)
+			handleCRLF(c);
+			if (!lexerState->expansions || lexerState->expansions->distance)
 				nextLine();
 			return;
 		} else if (c == ';') {
@@ -1532,8 +1535,7 @@
 		// Handle '\r' or '\n' (in multiline strings only, already handled above otherwise)
 		if (c == '\r' || c == '\n') {
 			/* Handle CRLF before nextLine() since shiftChars updates colNo */
-			if (c == '\r' && peek(0) == '\n')
-				shiftChars(1);
+			handleCRLF(c);
 			nextLine();
 			c = '\n';
 		}
@@ -1684,8 +1686,7 @@
 		// Handle '\r' or '\n' (in multiline strings only, already handled above otherwise)
 		if (c == '\r' || c == '\n') {
 			/* Handle CRLF before nextLine() since shiftChars updates colNo */
-			if (c == '\r' && peek(0) == '\n')
-				shiftChars(1);
+			handleCRLF(c);
 			nextLine();
 			c = '\n';
 		}
@@ -2041,9 +2042,7 @@
 		/* Handle newlines and EOF */
 
 		case '\r':
-			// Handle CRLF
-			if (peek(0) == '\n')
-				shiftChars(1);
+			handleCRLF(c);
 			/* fallthrough */
 		case '\n':
 			return T_NEWLINE;
@@ -2239,9 +2238,7 @@
 
 	if (c == '\r' || c == '\n') {
 		shiftChars(1);
-		/* Handle CRLF */
-		if (c == '\r' && peek(0) == '\n')
-			shiftChars(1);
+		handleCRLF(c);
 		return T_NEWLINE;
 	}
 
@@ -2328,8 +2325,7 @@
 
 			if (c == '\r' || c == '\n') {
 				/* Handle CRLF before nextLine() since shiftChars updates colNo */
-				if (c == '\r' && peek(0) == '\n')
-					shiftChars(1);
+				handleCRLF(c);
 				/* Do this both on line continuations and plain EOLs */
 				nextLine();
 			}
@@ -2416,8 +2412,7 @@
 
 			if (c == '\r' || c == '\n') {
 				/* Handle CRLF before nextLine() since shiftChars updates colNo */
-				if (c == '\r' && peek(0) == '\n')
-					shiftChars(1);
+				handleCRLF(c);
 				/* Do this both on line continuations and plain EOLs */
 				nextLine();
 			}
@@ -2430,7 +2425,7 @@
 	lexerState->atLineStart = false;
 
 	/* yywrap() will finish the REPT/FOR loop */
-	return 0;
+	return T_EOF;
 }
 
 int yylex(void)
@@ -2472,7 +2467,6 @@
 		}
 	}
 	lexerState->lastToken = token;
-
 	lexerState->atLineStart = token == T_NEWLINE;
 
 	return token;
@@ -2543,8 +2537,7 @@
 				error("Unterminated REPT/FOR block\n");
 				goto finish;
 			} else if (c == '\n' || c == '\r') {
-				if (c == '\r' && peek(0) == '\n')
-					shiftChars(1);
+				handleCRLF(c);
 				break;
 			}
 			c = nextChar();
@@ -2604,8 +2597,7 @@
 				error("Unterminated macro definition\n");
 				goto finish;
 			} else if (c == '\n' || c == '\r') {
-				if (c == '\r' && peek(0) == '\n')
-					shiftChars(1);
+				handleCRLF(c);
 				break;
 			}
 			c = nextChar();