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 if (isNewline(c)) { } else {
handleCRLF(c); assume(isNewline(c));
break;
}
c = peek();
if (c != EOF) {
shiftChar(); shiftChar();
} handleCRLF(c);
} }
} }
} }
@@ -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)) {
shiftChar();
if (readIdentifier(c, false).type == T_(POP_ENDM)) {
endCapture(capture); endCapture(capture);
// The ENDM has been captured, but we don't want it! // The ENDM has been captured, but we don't want it!
// We know we have read exactly "ENDM", not e.g. an EQUS // We know we have read exactly "ENDM", not e.g. an EQUS
capture.span.size -= literal_strlen("ENDM"); capture.span.size -= literal_strlen("ENDM");
return capture; 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 if (isNewline(c)) { } else {
handleCRLF(c); assume(isNewline(c));
break;
}
c = peek();
if (c != EOF) {
shiftChar(); shiftChar();
} handleCRLF(c);
} }
} }
} }