Commit 0bad861d

0bad861d7413Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Don't mis-parse a require with a computed argument

resolve_plain_requires grabbed the first quoted string anywhere on a require line as the lib name, so require File.expand_path('x', __dir__) took 'x' and rewrote only the require 'x' span, stranding , __dir__)) and producing a Prism unexpected ',' parse error. Guard the rewrite to bare string-literal arguments only (first non-space token after require must be a quote); a computed-argument require is left untouched and now degrades to a clean "unsupported call" diagnostic instead of corrupting the parse. A scan cursor lets the loop step past such a require without aborting or re-scanning it forever. Mixed string/computed requires on adjacent lines still inline the string ones.

Refs #1383.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
0bad861d7413
Parent
799dd9702a22

src/spinel_parse.cmodifiedView file

@@ -1875,6 +1875,9 @@
18751875
18761876 char *result = source;
18771877 char *pos;
1878+ /* `scan` lets us step past a computed-argument require without aborting
1879+ the loop; it resets to `result` after each rebuild below. */
1880+ char *scan = result;
18781881 /* Match a `require` at the very start of the buffer (offset 0) or
18791882 immediately after a newline. Re-checking offset 0 every iteration --
18801883 not just while `result == source` -- is what lets a first-line
@@ -1882,17 +1885,32 @@
18821885 buffer is rebuilt below, `result != source`, and the old condition
18831886 stranded the line-1 require. */
18841887 for (;;) {
1885- if (strncmp(result, "require ", 8) == 0) {
1888+ if (scan == result && strncmp(result, "require ", 8) == 0) {
18861889 pos = result;
18871890 }
18881891 else {
1889- pos = strstr(result, "\nrequire ");
1892+ pos = strstr(scan, "\nrequire ");
18901893 if (pos == NULL) break;
18911894 pos++; /* skip the matched newline */
18921895 }
18931896 char *line_end = strchr(pos, '\n');
18941897 if (!line_end) line_end = pos + strlen(pos);
18951898
1899+ /* Only a bare string-literal argument is inlined. A computed argument
1900+ (`require File.expand_path('x', __dir__)`, `require some_const`) is
1901+ left untouched: grabbing the first quote on the line would mistake an
1902+ inner string literal for the lib name and strand the rest of the
1903+ expression, producing invalid syntax. Skip past it and keep
1904+ scanning later lines. (#1383) */
1905+ {
1906+ const char *arg = pos + 8;
1907+ while (arg < line_end && (*arg == ' ' || *arg == '\t')) arg++;
1908+ if (arg >= line_end || (*arg != '"' && *arg != '\'')) {
1909+ scan = line_end;
1910+ continue;
1911+ }
1912+ }
1913+
18961914 /* Must be: require "name" or require 'name' */
18971915 char *q1 = strchr(pos + 7, '"');
18981916 char *q2 = strchr(pos + 7, '\'');
@@ -1987,6 +2005,7 @@
19872005 result_len - before_len - rl_len + 1);
19882006 free(result);
19892007 result = new_result;
2008+ scan = result;
19902009 continue;
19912010 }
19922011 }
@@ -2016,6 +2035,7 @@
20162035 memcpy(new_result + before_len + content_len, pos + line_len, result_len - before_len - line_len + 1);
20172036 free(result);
20182037 result = new_result;
2038+ scan = result;
20192039 free(content);
20202040 }
20212041 return result;