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
char *result = source; char *pos; /* `scan` lets us step past a computed-argument require without aborting the loop; it resets to `result` after each rebuild below. */ char *scan = result; /* Match a `require` at the very start of the buffer (offset 0) or immediately after a newline. Re-checking offset 0 every iteration -- not just while `result == source` -- is what lets a first-line buffer is rebuilt below, `result != source`, and the old condition stranded the line-1 require. */ for (;;) { if (strncmp(result, "require ", 8) == 0) { if (scan == result && strncmp(result, "require ", 8) == 0) { pos = result; }else { pos = strstr(result, "\nrequire "); pos = strstr(scan, "\nrequire "); if (pos == NULL) break; pos++; /* skip the matched newline */ } char *line_end = strchr(pos, '\n'); if (!line_end) line_end = pos + strlen(pos); /* Only a bare string-literal argument is inlined. A computed argument (`require File.expand_path('x', __dir__)`, `require some_const`) is left untouched: grabbing the first quote on the line would mistake an inner string literal for the lib name and strand the rest of the expression, producing invalid syntax. Skip past it and keep scanning later lines. (#1383) */ { const char *arg = pos + 8; while (arg < line_end && (*arg == ' ' || *arg == '\t')) arg++; if (arg >= line_end || (*arg != '"' && *arg != '\'')) { scan = line_end; continue; } } /* Must be: require "name" or require 'name' */ char *q1 = strchr(pos + 7, '"'); char *q2 = strchr(pos + 7, '\''); result_len - before_len - rl_len + 1); free(result); result = new_result; scan = result; continue; } } memcpy(new_result + before_len + content_len, pos + line_len, result_len - before_len - line_len + 1); free(result); result = new_result; scan = result; free(content); } return result;