Commit 4a4b506b
4a4b506b2ad6Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 5 files · +4 added · ~1 modified
Description
Fix two require line-scanning bugs in the parser
resolve_plain_requires scanned require "lib" lines with a fragile
line-oriented text match that mishandled two shapes:
require "x"; codeon one line silently droppedcode: the replacement spanned from the require to the end of the line, so anything after the;was deleted with it.- A
requireon the very first line (byte 0) was skipped when anotherrequirefollowed: the offset-0 fallback only fired whileresult == source, but the buffer is rebuilt after the first replacement, so the line-1 require was never processed and its call leaked to codegen (cannot resolve call to 'require').
Fix both:
- The scan now checks for a
requireat offset 0 every iteration (not just on the original buffer), so a first-line require survives rebuilds. - The replacement spans only the
require "name"statement, consuming trailing horizontal whitespace and a single terminating newline; a trailing;/comment/code is preserved and pushed onto its own line by the inserted content's newline.
Known remaining limitation: two requires on one line
(require "a"; require "b") still leaves the second unprocessed -- a
require after ; isn't matched. Rare; left for a follow-up.
Full suite green (709).
Fixes #1150
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 4a4b506b2ad6
- Parent
- b70cfc8adaac
spinel_parse.cmodifiedView file
@@ -1591,10 +1591,20 @@
15911591
15921592
char *result = source;15931593
char *pos;1594-
while ((pos = strstr(result, "\nrequire ")) != NULL ||1595-
(pos == NULL && result == source && strncmp(result, "require ", 8) == 0 && (pos = result))) {1596-
if (pos != result) pos++; /* skip \n */1597-
if (pos != result && *(pos - 1) != '\n') break;1594+
/* Match a `require` at the very start of the buffer (offset 0) or1595+
immediately after a newline. Re-checking offset 0 every iteration --1596+
not just while `result == source` -- is what lets a first-line1597+
`require` still be processed when a later `require` exists: once the1598+
buffer is rebuilt below, `result != source`, and the old condition1599+
stranded the line-1 require. */1600+
for (;;) {1601+
if (strncmp(result, "require ", 8) == 0) {1602+
pos = result;1603+
} else {1604+
pos = strstr(result, "\nrequire ");1605+
if (pos == NULL) break;1606+
pos++; /* skip the matched newline */1607+
}15981608
char *line_end = strchr(pos, '\n');15991609
if (!line_end) line_end = pos + strlen(pos);16001610
@@ -1649,7 +1659,16 @@
16491659
}16501660
}16511661
1652-
size_t line_len = (line_end - pos) + ((*line_end == '\n') ? 1 : 0);1662+
/* Replace only the `require "name"` statement itself, not the whole1663+
line, so `require "x"; code` keeps `code`. Consume trailing1664+
horizontal whitespace and a single terminating newline (so a1665+
require on its own line leaves no blank line); stop at `;` or any1666+
other trailing code, which the inserted content's trailing newline1667+
then pushes onto its own line. */1668+
char *stmt_end = end + 1; /* just past the closing quote */1669+
while (*stmt_end == ' ' || *stmt_end == '\t') stmt_end++;1670+
if (*stmt_end == '\n') stmt_end++;1671+
size_t line_len = stmt_end - pos;16531672
size_t content_len = strlen(content);16541673
size_t result_len = strlen(result);16551674
size_t before_len = pos - result;test/require_first_line.rbaddedView file
Loading diff…
test/require_first_line.rb.expectedaddedView file
Loading diff…
test/require_same_line.rbaddedView file
Loading diff…
test/require_same_line.rb.expectedaddedView file
Loading diff…