repl-lang 1.1.1

Read-eval-print loop scaffolding: line editing, session state, and incremental input feed.
Documentation
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//! Property tests: the editor and the session checked against simple reference
//! models over arbitrary inputs.
//!
//! The editor model works on a `Vec<char>` with a cursor counted in characters,
//! so it shares none of the implementation's byte-offset arithmetic or string
//! surgery. The session model is a plain string accumulator. After every step
//! the real type and its model must agree exactly.

#![allow(clippy::unwrap_used, clippy::panic)]

use std::collections::VecDeque;

use diag_lang::{Diagnostic, Label, Severity};
use proptest::prelude::*;
use repl_lang::{Edit, Editor, Feed, Session, SessionError, Span, Status};
use unicode_lang::{char_width, is_xid_continue, str_width};

// ---------------------------------------------------------------------------
// Editor
// ---------------------------------------------------------------------------

/// Characters chosen to stress the unit and word rules: ASCII word and
/// separator characters, a precomposed accent, a combining accent, a joiner, a
/// wide ideograph, an astral emoji, control characters and bidirectional
/// formatting characters: refused when typed or pasted, while a history entry
/// keeps tabs and newlines (and turns `\r` into a newline).
fn any_char() -> impl Strategy<Value = char> {
    prop::sample::select(vec![
        'a', 'b', 'Z', '_', '7', ' ', '.', '(', 'é', '\u{0301}', '\u{200D}', '世', '😀', '\t',
        '\n', '\r', '\u{1b}', '\u{202E}', '\u{2066}', '\u{200F}',
    ])
}

/// Whether the editor keeps `c` out of the line: control characters and the
/// bidirectional formatting characters, listed out here independently of the
/// crate.
fn refused(c: char) -> bool {
    c.is_control()
        || [
            '\u{061C}', '\u{200E}', '\u{200F}', '\u{202A}', '\u{202B}', '\u{202C}', '\u{202D}',
            '\u{202E}', '\u{2066}', '\u{2067}', '\u{2068}', '\u{2069}',
        ]
        .contains(&c)
}

/// Columns a character takes in the model: a tab counts one, as the editor
/// documents; everything else follows the Unicode tables.
fn model_width(c: char) -> usize {
    if c == '\t' { 1 } else { char_width(c) }
}

/// Whether `c` begins a display unit in the model: anything with width, and a
/// newline.
fn starts_unit(c: char) -> bool {
    model_width(c) != 0 || c == '\n'
}

/// What a history entry keeps of `line`, written as plain string replacement.
fn recallable(line: &str) -> String {
    line.replace("\r\n", "\n")
        .replace('\r', "\n")
        .chars()
        .filter(|&c| !refused(c) || c == '\n' || c == '\t')
        .collect()
}

#[derive(Clone, Debug)]
enum Op {
    Apply(Edit),
    Insert(String),
    Submit,
    Clear,
    AddHistory(String),
}

fn any_edit() -> impl Strategy<Value = Edit> {
    prop_oneof![
        any_char().prop_map(Edit::Insert),
        prop::sample::select(vec![
            Edit::Backspace,
            Edit::Delete,
            Edit::Left,
            Edit::Right,
            Edit::WordLeft,
            Edit::WordRight,
            Edit::Home,
            Edit::End,
            Edit::KillToStart,
            Edit::KillToEnd,
            Edit::KillWordLeft,
            Edit::KillWordRight,
            Edit::Yank,
            Edit::HistoryPrev,
            Edit::HistoryNext,
        ]),
    ]
}

fn any_text() -> impl Strategy<Value = String> {
    prop::collection::vec(any_char(), 0..6).prop_map(|chars| chars.into_iter().collect())
}

fn any_op() -> impl Strategy<Value = Op> {
    prop_oneof![
        8 => any_edit().prop_map(Op::Apply),
        2 => any_text().prop_map(Op::Insert),
        1 => Just(Op::Submit),
        1 => Just(Op::Clear),
        1 => any_text().prop_map(Op::AddHistory),
    ]
}

/// The reference editor.
struct Model {
    chars: Vec<char>,
    cursor: usize,
    kill: Vec<char>,
    history: VecDeque<String>,
    capacity: usize,
    /// The history position being shown; `-1` once the shown entry was the
    /// oldest and has been dropped.
    browsing: Option<isize>,
    draft: Vec<char>,
}

impl Model {
    fn new(capacity: usize) -> Self {
        Self {
            chars: Vec::new(),
            cursor: 0,
            kill: Vec::new(),
            history: VecDeque::new(),
            capacity,
            browsing: None,
            draft: Vec::new(),
        }
    }

    fn line(&self) -> String {
        self.chars.iter().collect()
    }

    fn byte_cursor(&self) -> usize {
        self.chars[..self.cursor].iter().map(|c| c.len_utf8()).sum()
    }

    fn next(&self, mut i: usize) -> usize {
        if i >= self.chars.len() {
            return i;
        }
        i += 1;
        while i < self.chars.len() && !starts_unit(self.chars[i]) {
            i += 1;
        }
        i
    }

    fn prev(&self, mut i: usize) -> usize {
        if i == 0 {
            return 0;
        }
        i -= 1;
        while i > 0 && !starts_unit(self.chars[i]) {
            i -= 1;
        }
        i
    }

    fn word_right(&self, mut i: usize) -> usize {
        while i < self.chars.len() && !is_xid_continue(self.chars[i]) {
            i = self.next(i);
        }
        while i < self.chars.len() && is_xid_continue(self.chars[i]) {
            i = self.next(i);
        }
        i
    }

    fn word_left(&self, mut i: usize) -> usize {
        while i > 0 && !is_xid_continue(self.chars[self.prev(i)]) {
            i = self.prev(i);
        }
        while i > 0 && is_xid_continue(self.chars[self.prev(i)]) {
            i = self.prev(i);
        }
        i
    }

    fn moved(&mut self, to: usize) -> bool {
        let changed = to != self.cursor;
        self.cursor = to;
        changed
    }

    fn kill(&mut self, start: usize, end: usize) -> bool {
        if start == end {
            return false;
        }
        self.kill = self.chars.drain(start..end).collect();
        self.cursor = start;
        true
    }

    fn insert(&mut self, text: &[char]) -> bool {
        let mut any = false;
        for &c in text.iter().filter(|&&c| !refused(c)) {
            self.chars.insert(self.cursor, c);
            self.cursor += 1;
            any = true;
        }
        any
    }

    fn record(&mut self, line: &str) {
        if self.capacity == 0
            || line.trim().is_empty()
            || self.history.back().is_some_and(|newest| newest == line)
        {
            return;
        }
        if self.history.len() == self.capacity {
            self.history.pop_front();
            self.browsing = self.browsing.map(|i| (i - 1).max(-1));
        }
        self.history.push_back(line.to_owned());
    }

    fn load(&mut self, index: usize) {
        self.chars = self.history[index].chars().collect();
        self.cursor = self.chars.len();
        self.browsing = Some(index as isize);
    }

    fn apply(&mut self, edit: Edit) -> bool {
        match edit {
            Edit::Insert(c) => self.insert(&[c]),
            Edit::Backspace => {
                if self.cursor == 0 {
                    return false;
                }
                let start = self.prev(self.cursor);
                self.chars.drain(start..self.cursor);
                self.cursor = start;
                true
            }
            Edit::Delete => {
                let end = self.next(self.cursor);
                if end == self.cursor {
                    return false;
                }
                self.chars.drain(self.cursor..end);
                true
            }
            Edit::Left => self.moved(self.prev(self.cursor)),
            Edit::Right => self.moved(self.next(self.cursor)),
            Edit::WordLeft => self.moved(self.word_left(self.cursor)),
            Edit::WordRight => self.moved(self.word_right(self.cursor)),
            Edit::Home => self.moved(0),
            Edit::End => self.moved(self.chars.len()),
            Edit::KillToStart => self.kill(0, self.cursor),
            Edit::KillToEnd => self.kill(self.cursor, self.chars.len()),
            Edit::KillWordLeft => self.kill(self.word_left(self.cursor), self.cursor),
            Edit::KillWordRight => self.kill(self.cursor, self.word_right(self.cursor)),
            Edit::Yank => {
                // The kill buffer goes back verbatim, newlines and tabs from
                // a recalled entry included.
                if self.kill.is_empty() {
                    return false;
                }
                let kill = self.kill.clone();
                let n = kill.len();
                self.chars.splice(self.cursor..self.cursor, kill);
                self.cursor += n;
                true
            }
            Edit::HistoryPrev => match self.browsing {
                None if self.history.is_empty() => false,
                None => {
                    self.draft = std::mem::take(&mut self.chars);
                    self.load(self.history.len() - 1);
                    true
                }
                Some(i) if i <= 0 => false,
                Some(i) => {
                    self.load(i as usize - 1);
                    true
                }
            },
            Edit::HistoryNext => match self.browsing {
                None => false,
                Some(i) if i + 1 < self.history.len() as isize => {
                    self.load((i + 1) as usize);
                    true
                }
                Some(_) => {
                    self.chars = std::mem::take(&mut self.draft);
                    self.cursor = self.chars.len();
                    self.browsing = None;
                    true
                }
            },
            other => panic!("model does not know {other:?}"),
        }
    }

    fn submit(&mut self) -> String {
        let line = self.line();
        self.chars.clear();
        self.cursor = 0;
        self.browsing = None;
        self.draft.clear();
        self.record(&line);
        line
    }

    fn clear(&mut self) -> bool {
        self.browsing = None;
        self.draft.clear();
        let changed = !self.chars.is_empty();
        self.chars.clear();
        self.cursor = 0;
        changed
    }
}

fn assert_same(editor: &Editor, model: &Model, step: &Op) {
    assert_eq!(editor.line(), model.line(), "line after {step:?}");
    assert_eq!(
        editor.cursor(),
        model.byte_cursor(),
        "cursor after {step:?}"
    );
    assert!(editor.line().is_char_boundary(editor.cursor()));
    let before = &editor.line()[..editor.cursor()];
    let own_line = before.rsplit('\n').next().unwrap_or("");
    assert_eq!(
        editor.column(),
        own_line.chars().map(model_width).sum::<usize>(),
        "column after {step:?}"
    );
    if !before.contains(['\n', '\t']) {
        assert_eq!(editor.column(), str_width(before));
    }
    assert!(
        editor
            .history()
            .eq(model.history.iter().map(String::as_str)),
        "history after {step:?}"
    );
}

/// Runs `ops` on an editor and on the model, both with history `capacity`,
/// and checks that they agree after every step.
fn check_editor(capacity: usize, ops: &[Op]) -> Result<(), TestCaseError> {
    let mut editor = Editor::with_history(capacity);
    let mut model = Model::new(capacity);
    for op in ops {
        match op {
            Op::Apply(edit) => {
                prop_assert_eq!(
                    editor.apply(*edit),
                    model.apply(*edit),
                    "result of {:?}",
                    op
                );
            }
            Op::Insert(text) => {
                let chars: Vec<char> = text.chars().collect();
                prop_assert_eq!(editor.insert(text), model.insert(&chars));
            }
            Op::Submit => prop_assert_eq!(editor.submit(), model.submit()),
            Op::Clear => prop_assert_eq!(editor.clear(), model.clear()),
            Op::AddHistory(line) => {
                editor.add_history(line);
                model.record(&recallable(line));
            }
        }
        assert_same(&editor, &model, op);
    }
    Ok(())
}

proptest! {
    #![proptest_config(ProptestConfig::with_cases(512))]

    /// Every operation, in any order, does exactly what the reference does.
    #[test]
    fn prop_editor_matches_model(
        capacity in 0_usize..4,
        ops in prop::collection::vec(any_op(), 0..80),
    ) {
        check_editor(capacity, &ops)?;
    }

    /// The same, starting from a recalled multi-line history entry, so the
    /// line holds newlines and tabs for the operations that follow.
    #[test]
    fn prop_editor_matches_model_on_recalled_multi_line_entries(
        entry in prop::collection::vec(
            prop::sample::select(vec!['a', 'b', ' ', '(', 'é', '\u{0301}', '世', '\t', '\n', '\r']),
            1..12,
        ),
        ops in prop::collection::vec(any_op(), 0..40),
    ) {
        let entry: String = entry.into_iter().collect();
        let mut all = vec![Op::AddHistory(format!("x{entry}")), Op::Apply(Edit::HistoryPrev)];
        all.extend(ops);
        check_editor(3, &all)?;
    }

    /// Typing a string and deleting it unit by unit leaves an empty line, and
    /// the number of units equals the number of width-bearing characters.
    #[test]
    fn prop_backspace_undoes_typing(text in any_text()) {
        let mut editor = Editor::new();
        editor.insert(&text);
        let typed: String = text.chars().filter(|&c| !refused(c)).collect();
        prop_assert_eq!(editor.line(), typed.as_str());
        let mut units = 0;
        while editor.apply(Edit::Backspace) {
            units += 1;
        }
        prop_assert_eq!(editor.line(), "");
        let starts = typed.chars().enumerate().filter(|&(i, c)| i == 0 || char_width(c) != 0).count();
        prop_assert_eq!(units, starts);
    }
}

// ---------------------------------------------------------------------------
// Session
// ---------------------------------------------------------------------------

/// Lines over a small alphabet with brackets, blanks, and both terminators.
fn any_line() -> impl Strategy<Value = String> {
    let body = prop::collection::vec(prop::sample::select(vec!['a', ' ', '(', ')', '\t']), 0..8)
        .prop_map(|chars| chars.into_iter().collect::<String>());
    (body, prop::sample::select(vec!["", "\n", "\r\n"])).prop_map(|(b, end)| b + end)
}

/// Incomplete while `(` outnumber `)`; complete otherwise, echoing the text.
fn brackets(input: repl_lang::Input<'_>) -> Status<(String, u32, u32)> {
    let open = input.text().matches('(').count();
    let close = input.text().matches(')').count();
    if open > close {
        Status::Incomplete
    } else {
        Status::Complete((
            input.text().to_owned(),
            input.base().to_u32(),
            input.number(),
        ))
    }
}

proptest! {
    #![proptest_config(ProptestConfig::with_cases(512))]

    /// The session agrees with a plain accumulator on every feed: what is
    /// pending, what is committed, at which base, under which number, and when
    /// the limit refuses a line.
    #[test]
    fn prop_session_matches_model(
        limit in prop_oneof![Just(usize::MAX), 0_usize..40],
        lines in prop::collection::vec(any_line(), 0..40),
    ) {
        let mut session = Session::with_limit(limit);
        let mut pending = String::new();
        let mut committed: Vec<String> = Vec::new();
        let mut base = 0_u32;

        for line in &lines {
            let result = session.feed(line, brackets);

            let stripped = line.strip_suffix('\n').map_or(line.as_str(), |l| l.strip_suffix('\r').unwrap_or(l));
            if pending.is_empty() && stripped.trim().is_empty() {
                prop_assert_eq!(result, Ok(Feed::Empty));
                continue;
            }
            let len = pending.len() + stripped.len() + 1;
            if len > limit {
                prop_assert_eq!(result, Err(SessionError::TooLong { len, limit }));
                pending.clear();
                continue;
            }
            pending.push_str(stripped);
            pending.push('\n');
            if pending.matches('(').count() > pending.matches(')').count() {
                prop_assert_eq!(result, Ok(Feed::Incomplete));
                continue;
            }
            let number = u32::try_from(committed.len() + 1).unwrap();
            let span = Span::new(base, base + u32::try_from(pending.len()).unwrap());
            match result {
                Ok(Feed::Complete { entry, value: (text, seen_base, seen_number) }) => {
                    prop_assert_eq!(&text, &pending);
                    prop_assert_eq!(seen_base, base);
                    prop_assert_eq!(seen_number, number);
                    prop_assert_eq!(entry.number(), number);
                    prop_assert_eq!(entry.span(), span);
                }
                other => prop_assert!(false, "expected Complete, got {:?}", other),
            }
            base = span.end().to_u32();
            committed.push(std::mem::take(&mut pending));
        }

        prop_assert_eq!(session.pending(), pending.as_str());
        prop_assert_eq!(session.len(), committed.len());
        for (i, text) in committed.iter().enumerate() {
            let number = u32::try_from(i + 1).unwrap();
            let entry = session.entry(number).unwrap();
            prop_assert_eq!(entry.text(), text.as_str());
            prop_assert_eq!(entry.name(), format!("<repl:{number}>"));
        }
    }

    /// `is_incomplete` holds exactly when there is an error and every error
    /// starts at or after the end of the non-whitespace text.
    #[test]
    fn prop_is_incomplete_matches_definition(
        body in "[a-z (]{0,6}[ \t]{0,3}",
        errors in prop::collection::vec((0_u32..12, 0_usize..4), 0..4),
    ) {
        // Two earlier entries, so the input under test does not start at 0.
        let mut session = Session::new();
        session.feed("pad
", |_| Status::Complete(())).unwrap(); // 0..4
        session.feed("x", |_| Status::Complete(())).unwrap(); // 4..6
        let base = 6_u32;

        let severities = [Severity::Error, Severity::Warning, Severity::Note, Severity::Help];
        let diagnostics: Vec<Diagnostic> = errors
            .iter()
            .map(|&(at, sev)| {
                Diagnostic::new(severities[sev], "m", Label::new(Span::empty(base + at), "l"))
            })
            .collect();

        let end = base + u32::try_from(body.trim_end().len()).unwrap();
        let errs: Vec<u32> = errors.iter().filter(|e| e.1 == 0).map(|e| base + e.0).collect();
        let expected = !errs.is_empty() && errs.iter().all(|&at| at >= end);

        session.feed(&body, |input| {
            assert_eq!(input.base().to_u32(), base);
            assert_eq!(input.is_incomplete(&diagnostics), expected, "{body:?} {errors:?}");
            Status::Complete(())
        }).unwrap();
    }

    /// A parser that reports spans relative to the entry text gets, through
    /// `is_incomplete_relative` and in any entry, exactly the verdict of the
    /// definition applied to the text alone.
    #[test]
    fn prop_is_incomplete_relative_matches_definition(
        pad in "[a-z]{0,24}",
        body in "[a-z (]{0,10}[ \t]{0,3}",
        errors in prop::collection::vec((0_u32..16, 0_u32..4, 0_usize..4), 0..4),
    ) {
        let mut session = Session::new();
        session.feed(&pad, |_| Status::Complete(())).unwrap();
        let end = u32::try_from(body.trim_end().len()).unwrap();
        let severities = [Severity::Error, Severity::Warning, Severity::Note, Severity::Help];
        let diagnostics: Vec<Diagnostic> = errors
            .iter()
            .map(|&(s, w, sev)| Diagnostic::new(severities[sev], "m", Label::new(Span::new(s, s + w), "l")))
            .collect();
        let errs: Vec<u32> = errors.iter().filter(|e| e.2 == 0).map(|e| e.0).collect();
        let expected = !errs.is_empty() && errs.iter().all(|&at| at >= end);

        session.feed(&body, |input| {
            assert_eq!(input.is_incomplete_relative(&diagnostics), expected, "{pad:?} {body:?} {errors:?}");
            Status::Complete(())
        }).unwrap();
    }

    /// The two methods are one rule in two coordinate spaces: shifting every
    /// span by the entry's base turns one verdict into the other.
    #[test]
    fn prop_is_incomplete_global_equals_relative_shifted_by_base(
        pad in "[a-z]{0,24}",
        body in "[a-z (]{0,10}[ \t]{0,3}",
        errors in prop::collection::vec((0_u32..16, 0_u32..4), 0..4),
    ) {
        let mut session = Session::new();
        session.feed(&pad, |_| Status::Complete(())).unwrap();
        session.feed(&body, |input| {
            let base = input.base().to_u32();
            let make = |shift: u32| -> Vec<Diagnostic> {
                errors
                    .iter()
                    .map(|&(s, w)| {
                        Diagnostic::new(Severity::Error, "m", Label::new(Span::new(s + shift, s + w + shift), "l"))
                    })
                    .collect()
            };
            assert_eq!(input.is_incomplete(&make(base)), input.is_incomplete_relative(&make(0)));
            Status::Complete(())
        }).unwrap();
    }
}