Refactor lexer_CaptureRept and lexer_CaptureMacro

This commit is contained in:
Rangi42
2025-08-04 23:21:11 -04:00
parent 0c1b422c36
commit ac75a085fa

View File

@@ -2316,13 +2316,10 @@ Capture lexer_CaptureRept() {
Defer reenableExpansions = scopedDisableExpansions(); Defer reenableExpansions = scopedDisableExpansions();
for (size_t depth = 0;;) { for (size_t depth = 0;;) {
nextLine(); nextLine();
int c = skipChars(isWhitespace);
if (c != EOF) {
shiftChar();
}
// We're at line start, so attempt to match a `REPT`, `FOR`, or `ENDR` token // We're at line start, so attempt to match a `REPT`, `FOR`, or `ENDR` token
if (startsIdentifier(c)) { if (int c = skipChars(isWhitespace); startsIdentifier(c)) {
shiftChar();
switch (readIdentifier(c, false).type) { switch (readIdentifier(c, false).type) {
case T_(POP_REPT): case T_(POP_REPT):
case T_(POP_FOR): case T_(POP_FOR):
@@ -2342,20 +2339,15 @@ Capture lexer_CaptureRept() {
} }
// Just consume characters until EOL or EOF // Just consume characters until EOL or EOF
for (;;) { if (int c = skipChars([](int d) { return d != EOF && !isNewline(d); }); c == EOF) {
if (c == EOF) { error("Unterminated REPT/FOR block");
error("Unterminated REPT/FOR block"); endCapture(capture);
endCapture(capture); capture.span.ptr = nullptr; // Indicates that it reached EOF before an ENDR
capture.span.ptr = nullptr; // Indicates that it reached EOF before an ENDR return capture;
return capture; } else {
} else if (isNewline(c)) { assume(isNewline(c));
handleCRLF(c); shiftChar();
break; handleCRLF(c);
}
c = peek();
if (c != EOF) {
shiftChar();
}
} }
} }
} }
@@ -2366,35 +2358,29 @@ Capture lexer_CaptureMacro() {
Defer reenableExpansions = scopedDisableExpansions(); Defer reenableExpansions = scopedDisableExpansions();
for (;;) { for (;;) {
nextLine(); nextLine();
int c = skipChars(isWhitespace);
if (c != EOF) {
shiftChar();
}
// We're at line start, so attempt to match an `ENDM` token // We're at line start, so attempt to match an `ENDM` token
if (startsIdentifier(c) && readIdentifier(c, false).type == T_(POP_ENDM)) { if (int c = skipChars(isWhitespace); startsIdentifier(c)) {
endCapture(capture); shiftChar();
// The ENDM has been captured, but we don't want it! if (readIdentifier(c, false).type == T_(POP_ENDM)) {
// We know we have read exactly "ENDM", not e.g. an EQUS endCapture(capture);
capture.span.size -= literal_strlen("ENDM"); // The ENDM has been captured, but we don't want it!
return capture; // We know we have read exactly "ENDM", not e.g. an EQUS
capture.span.size -= literal_strlen("ENDM");
return capture;
}
} }
// Just consume characters until EOL or EOF // Just consume characters until EOL or EOF
for (;;) { if (int c = skipChars([](int d) { return d != EOF && !isNewline(d); }); c == EOF) {
if (c == EOF) { error("Unterminated macro definition");
error("Unterminated macro definition"); endCapture(capture);
endCapture(capture); capture.span.ptr = nullptr; // Indicates that it reached EOF before an ENDM
capture.span.ptr = nullptr; // Indicates that it reached EOF before an ENDM return capture;
return capture; } else {
} else if (isNewline(c)) { assume(isNewline(c));
handleCRLF(c); shiftChar();
break; handleCRLF(c);
}
c = peek();
if (c != EOF) {
shiftChar();
}
} }
} }
} }