neovm-core 0.0.1

Core runtime structures for NeoVM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! Search and regex builtins for the Elisp interpreter.
//!
//! Pure builtins:
//! - `string-match`, `string-match-p`, `regexp-quote`
//! - `match-beginning`, `match-end`, `match-data`, `set-match-data`
//! - `looking-at`, `looking-at-p`, `replace-regexp-in-string`
//!
//! Eval-dependent builtins:
//! - `search-forward`, `search-backward`
//! - `re-search-forward`, `re-search-backward`
//! - `posix-search-forward`, `posix-search-backward`
//! - `replace-match`
//! - `word-search-forward`, `word-search-backward`

use super::error::{EvalResult, Flow, signal};
use super::intern::intern;
use super::value::*;
use crate::emacs_core::value::ValueKind;

// ---------------------------------------------------------------------------
// Argument helpers
// ---------------------------------------------------------------------------

fn expect_args(name: &str, args: &[Value], n: usize) -> Result<(), Flow> {
    if args.len() != n {
        Err(signal(
            "wrong-number-of-arguments",
            vec![Value::symbol(name), Value::fixnum(args.len() as i64)],
        ))
    } else {
        Ok(())
    }
}

fn expect_min_args(name: &str, args: &[Value], min: usize) -> Result<(), Flow> {
    if args.len() < min {
        Err(signal(
            "wrong-number-of-arguments",
            vec![Value::symbol(name), Value::fixnum(args.len() as i64)],
        ))
    } else {
        Ok(())
    }
}

fn expect_range_args(name: &str, args: &[Value], min: usize, max: usize) -> Result<(), Flow> {
    if args.len() < min || args.len() > max {
        Err(signal(
            "wrong-number-of-arguments",
            vec![Value::symbol(name), Value::fixnum(args.len() as i64)],
        ))
    } else {
        Ok(())
    }
}

fn expect_int(val: &Value) -> Result<i64, Flow> {
    match val.kind() {
        ValueKind::Fixnum(n) => Ok(n),
        other => Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("integerp"), *val],
        )),
    }
}

fn expect_integer_or_marker(val: &Value) -> Result<i64, Flow> {
    match val.kind() {
        ValueKind::Fixnum(n) => Ok(n),
        other => Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("integer-or-marker-p"), *val],
        )),
    }
}

fn expect_string(val: &Value) -> Result<String, Flow> {
    match val.kind() {
        ValueKind::String => Ok(val.as_str().unwrap().to_owned()),
        other => Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("stringp"), *val],
        )),
    }
}

fn normalize_string_start_arg(string: &str, start: Option<&Value>) -> Result<usize, Flow> {
    let Some(start_val) = start else {
        return Ok(0);
    };
    if start_val.is_nil() {
        return Ok(0);
    }

    let raw_start = expect_int(start_val)?;
    let len = super::string_escape::storage_char_len(string) as i64;
    let normalized = if raw_start < 0 {
        len.checked_add(raw_start)
    } else {
        Some(raw_start)
    };

    let Some(start_idx) = normalized else {
        return Err(signal(
            "args-out-of-range",
            vec![Value::string(string), Value::fixnum(raw_start)],
        ));
    };

    if !(0..=len).contains(&start_idx) {
        return Err(signal(
            "args-out-of-range",
            vec![Value::string(string), Value::fixnum(raw_start)],
        ));
    }

    let start_char_idx = start_idx as usize;
    if start_char_idx == len as usize {
        return Ok(string.len());
    }

    Ok(super::string_escape::storage_char_to_byte(
        string,
        start_char_idx,
    ))
}

pub(crate) fn normalize_lisp_string_start_arg(
    string: &crate::heap_types::LispString,
    start: Option<&Value>,
) -> Result<usize, Flow> {
    let Some(start_val) = start else {
        return Ok(0);
    };
    if start_val.is_nil() {
        return Ok(0);
    }

    let raw_start = expect_int(start_val)?;
    if string.is_ascii() {
        let len = string.byte_len() as i64;
        let normalized = if raw_start < 0 {
            len.checked_add(raw_start)
        } else {
            Some(raw_start)
        };
        let Some(start_idx) = normalized else {
            return Err(signal(
                "args-out-of-range",
                vec![Value::string(string.as_str()), Value::fixnum(raw_start)],
            ));
        };
        if !(0..=len).contains(&start_idx) {
            return Err(signal(
                "args-out-of-range",
                vec![Value::string(string.as_str()), Value::fixnum(raw_start)],
            ));
        }
        return Ok(start_idx as usize);
    }

    normalize_string_start_arg(string.as_str(), start)
}

fn preserve_case(replacement: &str, matched: &str) -> String {
    super::casefiddle::apply_replace_match_case(replacement, matched)
}

fn expand_emacs_replacement(
    rep: &str,
    groups: &[Option<(usize, usize)>],
    source: &str,
    literal: bool,
) -> String {
    if literal {
        return rep.to_string();
    }

    let mut out = String::with_capacity(rep.len());
    let mut chars = rep.chars().peekable();
    while let Some(ch) = chars.next() {
        if ch != '\\' {
            out.push(ch);
            continue;
        }

        let Some(next) = chars.next() else {
            out.push('\\');
            break;
        };

        match next {
            '&' => {
                if let Some(Some((start, end))) = groups.first()
                    && let Some(text) = source.get(*start..*end)
                {
                    out.push_str(text);
                }
            }
            '1'..='9' => {
                let idx = next.to_digit(10).unwrap() as usize;
                if let Some(Some((start, end))) = groups.get(idx)
                    && let Some(text) = source.get(*start..*end)
                {
                    out.push_str(text);
                }
            }
            '\\' => out.push('\\'),
            other => out.push(other),
        }
    }

    out
}

fn flatten_match_data(md: &super::regex::MatchData) -> Value {
    let mut trailing = md.groups.len();
    while trailing > 0 && md.groups[trailing - 1].is_none() {
        trailing -= 1;
    }

    let mut flat: Vec<Value> = Vec::with_capacity(trailing * 2);
    for grp in md.groups.iter().take(trailing) {
        match grp {
            Some((start, end)) => {
                // For string searches, positions are already character positions.
                // For buffer searches, positions are byte positions (returned as-is).
                flat.push(Value::fixnum(*start as i64));
                flat.push(Value::fixnum(*end as i64));
            }
            None => {
                flat.push(Value::NIL);
                flat.push(Value::NIL);
            }
        }
    }
    Value::list(flat)
}

// ---------------------------------------------------------------------------
// Pure builtins
// ---------------------------------------------------------------------------

/// `(regexp-quote STRING)` -- return a regexp that matches STRING literally,
/// quoting all special regex characters.
pub(crate) fn builtin_regexp_quote(args: Vec<Value>) -> EvalResult {
    crate::emacs_core::perf_trace::time_op(
        crate::emacs_core::perf_trace::HotpathOp::RegexpQuote,
        || {
            expect_args("regexp-quote", &args, 1)?;
            let s = expect_string(&args[0])?;
            let mut result = String::with_capacity(s.len() + 8);
            for ch in s.chars() {
                match ch {
                    '.' | '*' | '+' | '?' | '[' | '^' | '$' | '\\' => {
                        result.push('\\');
                        result.push(ch);
                    }
                    _ => result.push(ch),
                }
            }
            Ok(Value::string(result))
        },
    )
}

/// Parse SUBEXP and START args (positions 5 and 6) for replace-regexp-in-string.
fn parse_replace_regexp_subexp_start(args: &[Value], s: &str) -> Result<(i64, usize), Flow> {
    // args[5] = SUBEXP (optional), args[6] = START (optional)
    let subexp = match args.get(5) {
        Some(v) if v.is_nil() => 0i64,
        None => 0i64,
        Some(value) => expect_int(value)?,
    };
    if subexp < 0 {
        return Err(signal(
            "args-out-of-range",
            vec![
                Value::fixnum(subexp),
                Value::fixnum(0),
                Value::fixnum(s.len() as i64),
            ],
        ));
    }
    let start = normalize_string_start_arg(s, args.get(6))?;
    Ok((subexp, start))
}

/// Core implementation for replace-regexp-in-string with string replacement.
fn replace_regexp_in_string_core(
    args: &[Value],
    case_fold: bool,
    rep: &str,
    start_override: Option<usize>,
) -> EvalResult {
    let pattern = expect_string(&args[0])?;
    let s = expect_string(&args[2])?;
    let fixedcase = args.get(3).is_some_and(|v| v.is_truthy());
    let literal = args.get(4).is_some_and(|v| v.is_truthy());

    let (subexp, start) = if let Some(so) = start_override {
        let sub = match args.get(5) {
            Some(v) if v.is_nil() => 0i64,
            None => 0i64,
            Some(value) => expect_int(value)?,
        };
        (sub, so)
    } else {
        parse_replace_regexp_subexp_start(args, &s)?
    };

    let iterated =
        super::regex::iterate_string_matches_with_case_fold(&pattern, &s, start, case_fold)
            .map_err(|msg| signal("invalid-regexp", vec![Value::string(msg)]))?;

    let max_subexp = iterated.capture_count.saturating_sub(1);
    if (subexp as usize) > max_subexp {
        return Err(signal(
            "error",
            vec![
                Value::string("replace-match subexpression does not exist"),
                Value::fixnum(subexp),
            ],
        ));
    }

    let mut out = String::with_capacity(s.len().saturating_sub(start));
    let mut cursor = start;

    for groups in iterated.matches {
        let Some((_, full_end)) = groups.first().and_then(|group| *group) else {
            continue;
        };
        let (replace_start, replace_end, case_source) = if subexp == 0 {
            let Some((match_start, match_end)) = groups.first().and_then(|group| *group) else {
                continue;
            };
            let Some(src) = s.get(match_start..match_end) else {
                continue;
            };
            (match_start, match_end, src)
        } else if let Some(Some((group_start, group_end))) = groups.get(subexp as usize) {
            let Some(src) = s.get(*group_start..*group_end) else {
                continue;
            };
            (*group_start, *group_end, src)
        } else {
            return Err(signal(
                "error",
                vec![
                    Value::string("replace-match subexpression does not exist"),
                    Value::fixnum(subexp),
                ],
            ));
        };

        out.push_str(&s[cursor..replace_start]);
        let base = expand_emacs_replacement(rep, &groups, &s, literal);
        let replacement = if fixedcase {
            base
        } else {
            preserve_case(&base, case_source)
        };
        out.push_str(&replacement);
        cursor = if subexp == 0 { full_end } else { replace_end };
    }

    out.push_str(&s[cursor..]);
    Ok(Value::string(out))
}

fn dynamic_or_global_symbol_value(eval: &super::eval::Context, name: &str) -> Option<Value> {
    eval.obarray.symbol_value(name).cloned()
}

pub(crate) fn builtin_replace_regexp_in_string(
    eval: &mut super::eval::Context,
    args: Vec<Value>,
) -> EvalResult {
    expect_range_args("replace-regexp-in-string", &args, 3, 7)?;
    let case_fold = dynamic_or_global_symbol_value(eval, "case-fold-search")
        .map(|value| !value.is_nil())
        .unwrap_or(false);

    // Check if REP is a string or a function
    let rep_is_string = args[1].as_str().is_some();

    if rep_is_string {
        let rep = expect_string(&args[1])?;
        return replace_regexp_in_string_core(&args, case_fold, &rep, None);
    }

    // REP is a function — call it with each matched string.
    let func = args[1];
    let pattern = expect_string(&args[0])?;
    let s = expect_string(&args[2])?;
    let fixedcase = args.get(3).is_some_and(|v| v.is_truthy());
    let _literal = args.get(4).is_some_and(|v| v.is_truthy());
    let (subexp, start) = parse_replace_regexp_subexp_start(&args, &s)?;

    let iterated =
        super::regex::iterate_string_matches_with_case_fold(&pattern, &s, start, case_fold)
            .map_err(|msg| signal("invalid-regexp", vec![Value::string(msg)]))?;

    let max_subexp = iterated.capture_count.saturating_sub(1);
    if (subexp as usize) > max_subexp {
        return Err(signal(
            "error",
            vec![
                Value::string("replace-match subexpression does not exist"),
                Value::fixnum(subexp),
            ],
        ));
    }

    let search_region = &s[start..];
    let mut out = String::with_capacity(search_region.len());
    let mut cursor = start;
    let prefix_chars = s[..start].chars().count();
    let searched_string = match args[2].kind() {
        ValueKind::String => super::regex::SearchedString::Heap(args[2]),
        _ => super::regex::SearchedString::Owned(s.clone()),
    };

    eval.with_gc_scope_result(|ctx| {
        ctx.root(func);

        for groups in &iterated.matches {
            let Some((full_start, full_end)) = groups.first().and_then(|group| *group) else {
                continue;
            };

            let (replace_start, replace_end, case_source) = if subexp == 0 {
                let Some(src) = s.get(full_start..full_end) else {
                    continue;
                };
                (full_start, full_end, src.to_string())
            } else if let Some(Some((group_start, group_end))) = groups.get(subexp as usize) {
                let Some(src) = s.get(*group_start..*group_end) else {
                    continue;
                };
                (*group_start, *group_end, src.to_string())
            } else {
                return Err(signal(
                    "error",
                    vec![
                        Value::string("replace-match subexpression does not exist"),
                        Value::fixnum(subexp),
                    ],
                ));
            };

            // Set match-data so the function can call match-string etc.
            // In Emacs, replace-regexp-in-string calls string-match on the
            // whole STRING with START, so match positions are character
            // positions relative to the whole string.
            let mut match_groups = Vec::with_capacity(groups.len());
            for group in groups {
                match_groups.push(group.map(|(group_start, group_end)| {
                    let cs = search_region[..group_start - start].chars().count() + prefix_chars;
                    let ce = search_region[..group_end - start].chars().count() + prefix_chars;
                    (cs, ce)
                }));
            }
            ctx.match_data = Some(super::regex::MatchData {
                groups: match_groups,
                searched_string: Some(searched_string.clone()),
                searched_buffer: None,
            });

            out.push_str(&s[cursor..replace_start]);

            // Call the function with the matched string
            let matched_str = &s[full_start..full_end];
            let func_result = ctx.apply(func, vec![Value::string(matched_str)])?;
            let base = match func_result.as_str() {
                Some(s) => s.to_string(),
                None => {
                    return Err(signal(
                        "wrong-type-argument",
                        vec![Value::symbol("stringp"), func_result],
                    ));
                }
            };

            let replacement = if fixedcase {
                base
            } else {
                preserve_case(&base, &case_source)
            };
            out.push_str(&replacement);
            cursor = if subexp == 0 { full_end } else { replace_end };
        }

        out.push_str(&s[cursor..]);
        Ok(Value::string(out))
    })
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
#[path = "search_test.rs"]
mod tests;