shithub: rgbds

Download patch

ref: 7a587eb7d624d717d4af44682f59c4c39fc83392
parent: 7ac8bd6e240618fbfacfe9f0987bf089b7721b9c
author: Rangi <remy.oukaour+rangi42@gmail.com>
date: Mon Apr 19 05:23:12 EDT 2021

Use midrule action values for captures' terminated status

Bison 3.1 introduces "typed midrule values", which would write
`<captureTerminated>{ ... }` and `$$` instead of `{ ... }` and
`$<captureTerminated>[1-9]`, but rgbds supports 3.0 or even lower.

--- a/include/asm/lexer.h
+++ b/include/asm/lexer.h
@@ -81,7 +81,6 @@
 	uint32_t lineNo;
 	char *body;
 	size_t size;
-	bool unterminated;
 };
 
 char const *lexer_GetFileName(void);
@@ -89,8 +88,8 @@
 uint32_t lexer_GetColNo(void);
 void lexer_DumpStringExpansions(void);
 int yylex(void);
-void lexer_CaptureRept(struct CaptureBody *capture);
-void lexer_CaptureMacroBody(struct CaptureBody *capture);
+bool lexer_CaptureRept(struct CaptureBody *capture);
+bool lexer_CaptureMacroBody(struct CaptureBody *capture);
 
 #define INITIAL_DS_ARG_SIZE 2
 struct DsArgList {
--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -2336,12 +2336,12 @@
 	}
 }
 
-void lexer_CaptureRept(struct CaptureBody *capture)
+bool lexer_CaptureRept(struct CaptureBody *capture)
 {
-	capture->unterminated = false;
 	capture->lineNo = lexer_GetLineNo();
 
 	char *captureStart = startCapture();
+	bool terminated = false;
 	unsigned int level = 0;
 	int c;
 
@@ -2373,6 +2373,7 @@
 					 * We know we have read exactly "ENDR", not e.g. an EQUS
 					 */
 					lexerState->captureSize -= strlen("ENDR");
+					terminated = true;
 					goto finish;
 				}
 				level--;
@@ -2383,7 +2384,6 @@
 		for (;;) {
 			if (c == EOF) {
 				error("Unterminated REPT/FOR block\n");
-				capture->unterminated = true;
 				goto finish;
 			} else if (c == '\n' || c == '\r') {
 				handleCRLF(c);
@@ -2401,14 +2401,15 @@
 	lexerState->disableMacroArgs = false;
 	lexerState->disableInterpolation = false;
 	lexerState->atLineStart = false;
+	return terminated;
 }
 
-void lexer_CaptureMacroBody(struct CaptureBody *capture)
+bool lexer_CaptureMacroBody(struct CaptureBody *capture)
 {
-	capture->unterminated = false;
 	capture->lineNo = lexer_GetLineNo();
 
 	char *captureStart = startCapture();
+	bool terminated = false;
 	int c;
 
 	/* If the file is `mmap`ed, we need not to unmap it to keep access to the macro */
@@ -2436,6 +2437,7 @@
 				 * We know we have read exactly "ENDM", not e.g. an EQUS
 				 */
 				lexerState->captureSize -= strlen("ENDM");
+				terminated = true;
 				goto finish;
 			}
 		}
@@ -2444,7 +2446,6 @@
 		for (;;) {
 			if (c == EOF) {
 				error("Unterminated macro definition\n");
-				capture->unterminated = true;
 				goto finish;
 			} else if (c == '\n' || c == '\r') {
 				handleCRLF(c);
@@ -2462,4 +2463,5 @@
 	lexerState->disableMacroArgs = false;
 	lexerState->disableInterpolation = false;
 	lexerState->atLineStart = false;
+	return terminated;
 }
--- a/src/asm/parser.y
+++ b/src/asm/parser.y
@@ -463,6 +463,7 @@
 		int32_t step;
 	} forArgs;
 	struct StrFmtArgList strfmtArgs;
+	bool captureTerminated;
 }
 
 %type	<expr>		relocexpr
@@ -987,9 +988,9 @@
 ;
 
 rept		: T_POP_REPT uconst T_NEWLINE {
-			lexer_CaptureRept(&captureBody);
+			$<captureTerminated>$ = lexer_CaptureRept(&captureBody);
 		} endofline {
-			if (!captureBody.unterminated)
+			if ($<captureTerminated>4)
 				fstk_RunRept($2, captureBody.lineNo, captureBody.body,
 					     captureBody.size);
 		}
@@ -1000,9 +1001,9 @@
 		} T_ID {
 			lexer_ToggleStringExpansion(true);
 		} T_COMMA for_args T_NEWLINE {
-			lexer_CaptureRept(&captureBody);
+			$<captureTerminated>$ = lexer_CaptureRept(&captureBody);
 		} endofline {
-			if (!captureBody.unterminated)
+			if ($<captureTerminated>8)
 				fstk_RunFor($3, $6.start, $6.stop, $6.step, captureBody.lineNo,
 					    captureBody.body, captureBody.size);
 		}
@@ -1035,16 +1036,16 @@
 		} T_ID {
 			lexer_ToggleStringExpansion(true);
 		} T_NEWLINE {
-			lexer_CaptureMacroBody(&captureBody);
+			$<captureTerminated>$ = lexer_CaptureMacroBody(&captureBody);
 		} endofline {
-			if (!captureBody.unterminated)
+			if ($<captureTerminated>6)
 				sym_AddMacro($3, captureBody.lineNo, captureBody.body,
 					     captureBody.size);
 		}
 		| T_LABEL T_COLON T_POP_MACRO T_NEWLINE {
-			lexer_CaptureMacroBody(&captureBody);
+			$<captureTerminated>$ = lexer_CaptureMacroBody(&captureBody);
 		} endofline {
-			if (!captureBody.unterminated)
+			if ($<captureTerminated>5)
 				sym_AddMacro($1, captureBody.lineNo, captureBody.body,
 					     captureBody.size);
 		}