Commit 1b54a7ef

1b54a7ef4eabMerged

Ryan Seys <[email protected]> · 6 months ago · 5 files · +4 added · ~1 modified

Description

feat(parse): factor .send rewrite, add string-literal arg shape

Factor the ad-hoc .send(:sym, args) -> .sym(args) rewrite loop in rewrite_syntax_sugar into a REWRITE_SEND_CALL macro that takes a prefix string and a string_form flag. Use it to also rewrite the plain-string form .send("sym", args) -> .sym(args).

Robustness fixes (also retroactively cover the pre-existing arm):

  • sp_is_method_name_char gains [ and ] so .send(:[], idx) rewrites to .[](idx) and .send(:[]=, idx, val) to .[]=(idx, val). Prism parses both as the index operators.
  • Macro rejects digit-leading names (.123foo() is invalid Ruby even though the digits sit in the char class for body chars).
  • Args copy loop tracks "..." and '...' quote state with \ escape skipping so parens inside string literals do not prematurely close the call (.send(:m, "a)b") previously emitted broken .m("a)).

Interpolated strings parse as InterpolatedStringNode and are left untouched -- Spinel cannot resolve those names statically.

  • spinel_parse.c: hoist the method-name char class into sp_is_method_name_char(); extract REWRITE_SEND_CALL macro; refactor the existing .send(: arm to use it; add the new .send(" arm; add digit-leading guard and quote-state tracking in the macro.

Tests:

  • send_literal_symbol.rb covers the basic shapes, the operator methods [] / []=, and three quoted-paren cases that would have corrupted the source under the pre-existing parser.
  • send_literal_string.rb covers the new "sym" arm.

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

Details

Commit
1b54a7ef4eab
Parent
097825fd48d1

spinel_parse.cmodifiedView file

@@ -1462,8 +1462,21 @@
14621462 }
14631463
14641464 /* ---- Syntax sugar rewriting ---- */
1465+
1466+/* Ruby method-name char class: idents, digits, `?` / `!` / `=` suffixes,
1467+ operator-method chars (`+`, `-`, `*`, etc.), and `[` / `]` for the
1468+ index operators `[]` and `[]=`. Digits are allowed in the body but
1469+ the macro rejects digit-leading names (invalid Ruby method syntax). */
1470+static int sp_is_method_name_char(char c) {
1471+ return c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
1472+ (c >= '0' && c <= '9') || c == '?' || c == '!' || c == '+' ||
1473+ c == '-' || c == '*' || c == '/' || c == '<' || c == '>' ||
1474+ c == '=' || c == '&' || c == '|' || c == '^' || c == '~' ||
1475+ c == '%' || c == '[' || c == ']';
1476+}
1477+
14651478 static char *rewrite_syntax_sugar(char *source) {
1466- /* Rewrite .send(:method, args) → .method(args) */
1479+ /* Rewrite .send(:foo, args) / .send("foo", args) → .foo(args) */
14671480 /* Rewrite &:symbol → { |_spx| _spx.symbol } */
14681481 size_t len = strlen(source);
14691482 size_t cap = len * 2 + 256;
@@ -1474,42 +1487,68 @@
14741487 #define OUT_CHAR(c) do { if (oi >= cap - 1) { cap *= 2; out = realloc(out, cap); } out[oi++] = (c); } while(0)
14751488 #define OUT_STR(s) do { const char *_s = (s); while (*_s) { OUT_CHAR(*_s); _s++; } } while(0)
14761489
1490+ /* Rewrite one .send(:foo / .send("foo dispatch. `string_form` is 1
1491+ for the double-quoted variant (requires a closing `"`) and 0 for
1492+ the colon-symbol variant. On mismatch (empty / digit-leading
1493+ name, unclosed quote), emits the prefix verbatim and advances
1494+ past it so the outer loop resumes scanning. The args copy tracks
1495+ `"..."` and `'...'` state with `\` escape skipping so parens
1496+ inside string literals do not prematurely close the call. */
1497+ #define REWRITE_SEND_CALL(prefix_str, prefix_len, string_form) do { \
1498+ size_t _save_i = i; \
1499+ i += (prefix_len); \
1500+ size_t _ns = i; \
1501+ while (i < len && sp_is_method_name_char(source[i])) i++; \
1502+ size_t _name_len = i - _ns; \
1503+ int _ok = (_name_len > 0); \
1504+ /* Reject digit-leading names: `.123foo()` is not valid Ruby. */ \
1505+ if (_ok && source[_ns] >= '0' && source[_ns] <= '9') { _ok = 0; } \
1506+ if (_ok && (string_form)) { \
1507+ if (i >= len || source[i] != '"') { _ok = 0; } \
1508+ else { i++; /* skip closing quote */ } \
1509+ } \
1510+ if (!_ok) { i = _save_i; OUT_STR(prefix_str); i += (prefix_len); } \
1511+ else { \
1512+ OUT_CHAR('.'); \
1513+ { size_t _k; for (_k = 0; _k < _name_len; _k++) OUT_CHAR(source[_ns + _k]); } \
1514+ if (i < len && source[i] == ')') { \
1515+ i++; /* no args */ \
1516+ } else if (i < len && source[i] == ',') { \
1517+ i++; \
1518+ while (i < len && source[i] == ' ') i++; \
1519+ OUT_CHAR('('); \
1520+ { int _depth = 1; char _qc = 0; \
1521+ while (i < len && _depth > 0) { \
1522+ char _ac = source[i]; \
1523+ if (_qc != 0) { \
1524+ /* Inside string: `\` escapes next char, skip it as a pair. */ \
1525+ if (_ac == '\\' && i + 1 < len) { \
1526+ OUT_CHAR(_ac); OUT_CHAR(source[i + 1]); i += 2; continue; \
1527+ } \
1528+ if (_ac == _qc) _qc = 0; \
1529+ OUT_CHAR(_ac); i++; continue; \
1530+ } \
1531+ if (_ac == '"' || _ac == '\'') { _qc = _ac; OUT_CHAR(_ac); i++; continue; } \
1532+ if (_ac == '(') _depth++; \
1533+ else if (_ac == ')') { _depth--; if (_depth == 0) { i++; break; } } \
1534+ OUT_CHAR(_ac); i++; \
1535+ } } \
1536+ OUT_CHAR(')'); \
1537+ } \
1538+ } \
1539+ } while(0)
1540+
14771541 while (i < len) {
1478- /* .send(:symbol ...) */
1542+ /* .send(:foo, args) → .foo(args) */
14791543 if (i + 7 < len && strncmp(source + i, ".send(:", 7) == 0) {
1480- i += 7; /* skip .send(: */
1481- /* Extract method name */
1482- size_t ns = i;
1483- while (i < len && (source[i] == '_' || (source[i] >= 'a' && source[i] <= 'z') ||
1484- (source[i] >= 'A' && source[i] <= 'Z') || (source[i] >= '0' && source[i] <= '9') ||
1485- source[i] == '?' || source[i] == '!' || source[i] == '+' || source[i] == '-' ||
1486- source[i] == '*' || source[i] == '/' || source[i] == '<' || source[i] == '>' ||
1487- source[i] == '=' || source[i] == '&' || source[i] == '|' || source[i] == '^' ||
1488- source[i] == '~' || source[i] == '%')) i++;
1489- size_t name_len = i - ns;
1490- if (name_len > 0) {
1491- OUT_CHAR('.');
1492- size_t k; for (k = 0; k < name_len; k++) OUT_CHAR(source[ns + k]);
1493- /* Skip optional comma + space, then copy remaining args until ) */
1494- if (i < len && source[i] == ')') {
1495- i++; /* no args */
1496- } else if (i < len && source[i] == ',') {
1497- i++; /* skip comma */
1498- while (i < len && source[i] == ' ') i++;
1499- OUT_CHAR('(');
1500- /* Copy args until matching ) */
1501- int depth = 1;
1502- while (i < len && depth > 0) {
1503- if (source[i] == '(') depth++;
1504- else if (source[i] == ')') { depth--; if (depth == 0) { i++; break; } }
1505- OUT_CHAR(source[i]); i++;
1506- }
1507- OUT_CHAR(')');
1508- }
1509- continue;
1510- }
1511- /* Failed to parse, output original */
1512- OUT_STR(".send(:");
1544+ REWRITE_SEND_CALL(".send(:", 7, 0);
1545+ continue;
1546+ }
1547+ /* .send("foo", args) → .foo(args). Plain string literal only;
1548+ interpolated strings parse as InterpolatedStringNode and are
1549+ left untouched (Spinel can't resolve the name statically). */
1550+ if (i + 7 < len && strncmp(source + i, ".send(\"", 7) == 0) {
1551+ REWRITE_SEND_CALL(".send(\"", 7, 1);
15131552 continue;
15141553 }
15151554 /* (&:symbol) → { |_spx| _spx.symbol } — also remove enclosing parens */