numbat 1.23.0

A statically typed programming language for scientific computations with first class support for physical dimensions and units.
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
use std::str::{FromStr, SplitWhitespace};

use compact_str::ToCompactString;

use crate::{
    Context, ParseError, RuntimeError,
    diagnostic::{ErrorDiagnostic, ResolverDiagnostic},
    help::basic_help_markup,
    markup::{self as m, Markup},
    parser::ParseErrorKind,
    resolver::CodeSource,
    session_history::{SessionHistory, SessionHistoryOptions},
    span::{ByteIndex, Span},
};

type PrintMarkupFn<'a> = Option<Box<dyn FnMut(&Markup) + 'a>>;
type ClearFn<'a, Editor> = Option<Box<dyn FnMut(&mut Editor) -> CommandControlFlow + 'a>>;

#[derive(Debug, Clone, PartialEq)]
pub enum ListItems {
    Functions,
    Dimensions,
    Variables,
    Units,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum QuitAlias {
    Quit,
    Exit,
}

#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CommandKind {
    Help,
    Info,
    List,
    Clear,
    Save,
    Reset,
    Quit(QuitAlias),
}

impl FromStr for CommandKind {
    type Err = ();

    fn from_str(word: &str) -> Result<Self, Self::Err> {
        use CommandKind::*;
        Ok(match word {
            "help" | "?" => Help,
            "info" => Info,
            "list" => List,
            "clear" => Clear,
            "save" => Save,
            "reset" => Reset,
            "quit" => Quit(QuitAlias::Quit),
            "exit" => Quit(QuitAlias::Exit),
            _ => return Err(()),
        })
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
enum HelpKind {
    BasicHelp,
    AllCommands,
}

#[derive(Debug, PartialEq, Eq)]
#[must_use]
pub enum CommandControlFlow {
    Continue,
    Return,
    Reset,
    NotACommand,
}

#[derive(Debug)]
pub enum CommandError {
    Parse(ParseError),
    Runtime(Box<RuntimeError>),
}

impl From<ParseError> for CommandError {
    fn from(err: ParseError) -> Self {
        Self::Parse(err)
    }
}

impl From<RuntimeError> for CommandError {
    fn from(err: RuntimeError) -> Self {
        Self::Runtime(Box::new(err))
    }
}

impl ErrorDiagnostic for ResolverDiagnostic<'_, CommandError> {
    fn diagnostics(&self) -> Vec<crate::Diagnostic> {
        match self.error {
            CommandError::Parse(parse_error) => parse_error.diagnostics(),
            CommandError::Runtime(runtime_error) => ResolverDiagnostic {
                resolver: self.resolver,
                error: runtime_error.as_ref(),
            }
            .diagnostics(),
        }
    }
}

enum ParsedCommand<'session, 'input> {
    Help {
        help_kind: HelpKind,
    },
    Info {
        item: &'input str,
    },
    List {
        items: Option<ListItems>,
    },
    Clear,
    Save {
        session_history: &'session SessionHistory,
        dst: &'input str,
    },
    Reset,
    Quit,
}

pub struct CommandRunner<'a, Editor = ()> {
    print_markup: PrintMarkupFn<'a>,
    clear: ClearFn<'a, Editor>,
    session_history: Option<SessionHistory>,
    reset: Option<()>,
    quit: Option<()>,
}

// cannot be derived because `#[derive(Default)]` introduces the bound `Editor:
// Default`, which is not necessary
impl<Editor> Default for CommandRunner<'_, Editor> {
    fn default() -> Self {
        Self {
            print_markup: None,
            clear: None,
            session_history: None,
            reset: None,
            quit: None,
        }
    }
}

impl<'a, Editor> CommandRunner<'a, Editor> {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn print_with(mut self, action: impl FnMut(&Markup) + 'a) -> Self {
        self.print_markup = Some(Box::new(action));
        self
    }

    pub fn enable_clear(
        mut self,
        action: impl FnMut(&mut Editor) -> CommandControlFlow + 'a,
    ) -> Self {
        self.clear = Some(Box::new(action));
        self
    }

    pub fn enable_save(mut self, session_history: SessionHistory) -> Self {
        self.session_history = Some(session_history);
        self
    }

    pub fn enable_reset(mut self) -> Self {
        self.reset = Some(());
        self
    }

    pub fn enable_quit(mut self) -> Self {
        self.quit = Some(());
        self
    }

    pub fn push_to_history(&mut self, line: &str, result: Result<(), ()>) {
        let Some(session_history) = self.session_history.as_mut() else {
            return;
        };
        session_history.push(line.to_compact_string(), result);
    }

    fn help_markup(&self, help_kind: HelpKind) -> m::Markup {
        match help_kind {
            HelpKind::BasicHelp => basic_help_markup(),
            HelpKind::AllCommands => self.all_commands_markup(),
        }
    }

    fn all_commands_markup(&self) -> m::Markup {
        fn m_cmd(cmd: &'static str) -> m::Markup {
            m::keyword(cmd)
        }

        fn m_arg(arg: &'static str) -> m::Markup {
            m::value(arg)
        }

        // Column width for command + args (not including indent)
        const CMD_COLUMN_WIDTH: usize = 18;

        fn cmd(
            cmd: &'static str,
            args: impl AsRef<[&'static str]>,
            aliases: impl AsRef<[&'static str]>,
            help: &'static str,
        ) -> m::Markup {
            cmd_fmt(cmd, args, aliases, m::text(help))
        }

        fn cmd_fmt(
            cmd: &'static str,
            args: impl AsRef<[&'static str]>,
            aliases: impl AsRef<[&'static str]>,
            help: m::Markup,
        ) -> m::Markup {
            let indent = m::text("  ");
            let mut output = indent;
            output += m_cmd(cmd);

            // Calculate the width of command + args
            let args = args.as_ref();
            let mut cmd_width = cmd.len();
            for arg in args {
                output += m::space();
                output += m_arg(arg);
                cmd_width += 1 + arg.len(); // space + arg
            }

            // Pad to align descriptions
            let padding = CMD_COLUMN_WIDTH.saturating_sub(cmd_width);
            let padding_str = " ".repeat(padding).to_compact_string();
            output += m::whitespace(padding_str);
            output += help;

            let aliases = aliases.as_ref();
            let mut aliases_iter = aliases.iter();
            if let Some(first_alias) = aliases_iter.next() {
                if aliases.len() == 1 {
                    output += m::text(" (alias: ");
                } else {
                    output += m::text(" (aliases: ");
                }
                output += m_cmd(first_alias);

                for alias in aliases_iter {
                    output += m::text(", ");
                    output += m_cmd(alias);
                }
                output += m::text(")");
            }

            output += m::nl();

            output
        }

        let mut output = m::nl()
            + cmd("help", [], ["?"], "show a basic introduction to Numbat")
            + cmd(
                "help",
                ["commands"],
                [],
                "brief description of all available commands",
            )
            + cmd(
                "info",
                ["<identifier>"],
                [],
                "get more information about functions, variables, units or dimensions",
            )
            + cmd("list", [], [], "show all currently defined items")
            + cmd_fmt(
                "list",
                ["<what>"],
                [],
                m::text("show all currently defined ")
                    + m_arg("functions")
                    + m::text(", ")
                    + m_arg("definitions")
                    + m::text(", ")
                    + m_arg("variables")
                    + m::text(", or ")
                    + m_arg("units"),
            );

        if self.session_history.is_some() {
            output += cmd_fmt(
                "save",
                [],
                [],
                m::text("save the current session history to ")
                    + m::string("history.nbt")
                    + m::text(" in the current directory"),
            );
            output += cmd_fmt(
                "save",
                ["<path>"],
                [],
                m::text("save the current session history to file")
                    + m::text("(the recommended file extension is ")
                    + m::string(".nbt")
                    + m::text(")"),
            );
        }

        if self.clear.is_some() {
            output += cmd("clear", [], [], "clear the console output");
        }

        if self.reset.is_some() {
            output += cmd("reset", [], [], "completely reset the interpreter state");
        }

        if self.quit.is_some() {
            output += cmd("exit", [], ["quit"], "close this session");
        }

        output
    }

    fn parse_command<'s, 'b>(
        &'s self,
        line: &'b str,
        ctx: &mut Context,
    ) -> Result<Option<ParsedCommand<'s, 'b>>, Box<CommandError>> {
        let Some(mut parser) = CommandParser::new(
            line,
            ctx.resolver_mut().add_code_source(CodeSource::Text, line),
        ) else {
            return Ok(None);
        };

        let Self {
            print_markup,
            clear,
            session_history,
            reset,
            quit,
        } = self;

        // todo: replace all of the initial `let Some(...) = ... else { return Ok(None) };`
        // with an if-let guard. https://github.com/rust-lang/rust/issues/51114
        Ok(Some(match &parser.command_kind {
            CommandKind::Help => {
                if print_markup.is_none() {
                    return Ok(None);
                }

                let help_arg = parser.args.next();

                if parser.args.next().is_some() {
                    return Err(Box::new(
                        parser
                            .err_through_end_from(2, "`help` takes at most one argument")
                            .into(),
                    ));
                }

                let help_kind = match help_arg {
                    None => HelpKind::BasicHelp,
                    Some("commands") => HelpKind::AllCommands,
                    _ => {
                        return Err(Box::new(
                            parser
                                .err_at_idx(
                                    1,
                                    "if provided, the argument to `help` must be \"commands\"",
                                )
                                .into(),
                        ));
                    }
                };

                ParsedCommand::Help { help_kind }
            }
            CommandKind::Info => {
                if print_markup.is_none() {
                    return Ok(None);
                }
                let err_msg = "`info` requires exactly one argument, the item to get info on";
                let Some(item) = parser.args.next() else {
                    return Err(Box::new(parser.err_at_idx(0, err_msg).into()));
                };

                if parser.args.next().is_some() {
                    return Err(Box::new(parser.err_through_end_from(1, err_msg).into()));
                }

                ParsedCommand::Info { item }
            }
            CommandKind::List => {
                if print_markup.is_none() {
                    return Ok(None);
                }

                let items = parser.args.next();

                if parser.args.next().is_some() {
                    return Err(Box::new(
                        parser
                            .err_through_end_from(2, "`list` takes at most one argument")
                            .into(),
                    ));
                }

                let items = match items {
                    None => None,
                    Some("functions") => Some(ListItems::Functions),
                    Some("dimensions") => Some(ListItems::Dimensions),
                    Some("variables") => Some(ListItems::Variables),
                    Some("units") => Some(ListItems::Units),
                    _ => {
                        return Err(Box::new(
                            parser
                                .err_at_idx(
                                    1,
                                    "if provided, the argument to `list` must be \
                                             one of: functions, dimensions, variables, units",
                                )
                                .into(),
                        ));
                    }
                };

                ParsedCommand::List { items }
            }
            CommandKind::Clear => {
                if clear.is_none() {
                    return Ok(None);
                }

                parser
                    .ensure_zero_args("clear", "")
                    .map_err(|err| Box::new(err.into()))?;

                ParsedCommand::Clear
            }
            CommandKind::Save => {
                let Some(session_history) = session_history else {
                    return Ok(None);
                };

                let dst = parser.args.next().unwrap_or("history.nbt");

                if parser.args.next().is_some() {
                    return Err(Box::new(
                        parser
                            .err_through_end_from(
                                2,
                                "`save` takes at most one argument (the destination, which will be history.nbt if omitted)",
                            )
                            .into(),
                    ));
                }

                ParsedCommand::Save {
                    session_history,
                    dst,
                }
            }
            CommandKind::Reset => {
                if reset.is_none() {
                    return Ok(None);
                }

                parser
                    .ensure_zero_args("reset", "")
                    .map_err(|err| Box::new(err.into()))?;

                ParsedCommand::Reset
            }
            CommandKind::Quit(quit_alias) => {
                if quit.is_none() {
                    return Ok(None);
                }

                parser
                    .ensure_zero_args(
                        match quit_alias {
                            QuitAlias::Quit => "quit",
                            QuitAlias::Exit => "exit",
                        },
                        "",
                    )
                    .map_err(|err| Box::new(err.into()))?;

                ParsedCommand::Quit
            }
        }))
    }

    /// Try to run the input line as a command.
    ///
    /// If the line is recognized as an (enabled) command, this handles the happy path,
    /// but it is up to frontends to handle the error path if this returns an error,
    /// which can happen because arguments to the command were incorrect (eg `list
    /// foobar`) or because the command failed at runtime (eg `save /`). If the command
    /// was recognized then this returns one of `CommandControlFlow::Continue` or
    /// `CommandControlFlow::Return`. If the line is not an enabled command (whether
    /// that's because it's not a command at all, or it's a disabled command), then this
    /// returns `CommandControlFlow::NotACommand`.
    pub fn try_run_command(
        &mut self,
        line: &str,
        ctx: &mut Context,
        editor: &mut Editor,
    ) -> Result<CommandControlFlow, Box<CommandError>> {
        let Some(parsed) = self.parse_command(line, ctx)? else {
            return Ok(CommandControlFlow::NotACommand);
        };

        Ok(match parsed {
            ParsedCommand::Help { help_kind } => {
                let markup = self.help_markup(help_kind);
                if let Some(print_fn) = self.print_markup.as_mut() {
                    print_fn(&markup);
                }
                CommandControlFlow::Continue
            }
            ParsedCommand::Info { item } => {
                let markup = ctx.print_info_for_keyword(item);
                if let Some(print_fn) = self.print_markup.as_mut() {
                    print_fn(&markup);
                }
                CommandControlFlow::Continue
            }
            ParsedCommand::List { items } => {
                let markup = match items {
                    None => ctx.print_environment(),
                    Some(ListItems::Functions) => ctx.print_functions(),
                    Some(ListItems::Dimensions) => ctx.print_dimensions(),
                    Some(ListItems::Variables) => ctx.print_variables(),
                    Some(ListItems::Units) => ctx.print_units(),
                };
                if let Some(print_fn) = self.print_markup.as_mut() {
                    print_fn(&markup);
                }
                CommandControlFlow::Continue
            }
            ParsedCommand::Clear => {
                if let Some(clear_fn) = self.clear.as_mut() {
                    clear_fn(editor)
                } else {
                    CommandControlFlow::Continue
                }
            }
            ParsedCommand::Save {
                session_history,
                dst,
            } => {
                session_history
                    .save(
                        dst,
                        SessionHistoryOptions {
                            include_err_lines: false,
                            trim_lines: true,
                        },
                    )
                    .map_err(|err| {
                        CommandError::Runtime(Box::new(ctx.interpreter.runtime_error(*err)))
                    })?;

                if let Some(print_fn) = self.print_markup.as_mut() {
                    let markup = m::text("successfully saved session history to")
                        + m::space()
                        + m::string(dst.to_compact_string());
                    print_fn(&markup);
                }
                CommandControlFlow::Continue
            }
            ParsedCommand::Reset => {
                if let Some(clear_fn) = self.clear.as_mut() {
                    let _ = clear_fn(editor);
                }
                CommandControlFlow::Reset
            }
            ParsedCommand::Quit => CommandControlFlow::Return,
        })
    }
}

/// The command parser
///
/// This contains the words of the input and, for reporting errors, their boundaries'
/// indices and the input's code_source_id. The args of the input are not validated for
/// correctness until running  [`CommandRunner::try_run_line`].
///
/// This can only be successfully constructed if the first word of the input is a valid
/// command name.
pub struct CommandParser<'a> {
    command_kind: CommandKind,
    /// The words in the input, not including the command itself, which has already been
    /// parsed into `self.command_kind`.
    args: SplitWhitespace<'a>,
    /// For tracking spans. Contains `(start, start+len)` for each (whitespace-separated)
    /// word in the input
    word_boundaries: Vec<(u32, u32)>,
    /// The source id of the input
    code_source_id: usize,
}

impl<'a> CommandParser<'a> {
    /// Construct a new `CommandParser` from the input line and a `code_source_id`
    ///
    /// This breaks the input down into words and parses the first into a
    /// `command_kind`, but doesn't check the subsequent ones for correctness. For error
    /// reporting, it also stores the word boundaries and code_source_id.
    ///
    /// Returns `Some(_)` if the first word of the input is a valid command, `None`
    /// otherwise. This is not aware of commands enabled by a command runner; that check
    /// happens later.
    pub fn new(input: &'a str, code_source_id: usize) -> Option<Self> {
        let mut words: SplitWhitespace<'_> = input.split_whitespace();
        let command_kind = words.next()?.parse().ok()?;

        let mut word_boundaries = Vec::new();
        let mut prev_char_was_whitespace = true;
        let mut start_idx = 0;

        for (i, c) in input
            .char_indices()
            // force trailing whitespace to get last word
            .chain(std::iter::once((input.len(), ' ')))
        {
            if prev_char_was_whitespace && !c.is_whitespace() {
                start_idx = u32::try_from(i).unwrap();
            } else if !prev_char_was_whitespace && c.is_whitespace() {
                word_boundaries.push((start_idx, u32::try_from(i).unwrap()));
            }
            prev_char_was_whitespace = c.is_whitespace();
        }

        Some(Self {
            command_kind,
            args: words,
            word_boundaries,
            code_source_id,
        })
    }

    /// Get the span starting at the start of the word at `word_index`, through the end of
    /// the last word represented by `word_boundaries`
    ///
    /// ## Panics
    /// If `word_index` is out of bounds, ie `word_index >= word_boundaries.len()`
    fn span_through_end(&self, word_index: usize) -> Span {
        let start = self.word_boundaries[word_index].0;
        let end = self.word_boundaries.last().unwrap().1;
        self.span_from_boundary((start, end))
    }

    /// Get the span between indices given by `start` and `end`
    ///
    /// The only role of `&self` here is to provide the `code_source_id`
    fn span_from_boundary(&self, (start, end): (u32, u32)) -> Span {
        Span {
            start: ByteIndex(start),
            end: ByteIndex(end),
            code_source_id: self.code_source_id,
        }
    }

    fn err_at_idx(&self, index: usize, err_msg: impl Into<String>) -> ParseError {
        ParseError {
            kind: ParseErrorKind::InvalidCommand(err_msg.into()),
            span: self.span_from_boundary(self.word_boundaries[index]),
        }
    }

    fn err_through_end_from(&self, index: usize, err_msg: impl Into<String>) -> ParseError {
        ParseError {
            kind: ParseErrorKind::InvalidCommand(err_msg.into()),
            span: self.span_through_end(index),
        }
    }

    fn ensure_zero_args(
        &mut self,
        cmd: &'static str,
        err_msg_suffix: &'static str,
    ) -> Result<(), ParseError> {
        if self.args.next().is_some() {
            let message = format!("`{}` takes 0 arguments{}", cmd, err_msg_suffix);
            return Err(self.err_through_end_from(1, message));
        }
        Ok(())
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[derive(Debug, Clone, PartialEq)]
    pub enum BareCommand<'a> {
        Help { help_kind: HelpKind },
        Info { item: &'a str },
        List { items: Option<ListItems> },
        Clear,
        Save { dst: &'a str },
        Reset,
        Quit,
    }

    impl<'a> From<ParsedCommand<'_, 'a>> for BareCommand<'a> {
        fn from(cmd: ParsedCommand<'_, 'a>) -> Self {
            match cmd {
                ParsedCommand::Help { help_kind } => BareCommand::Help { help_kind },
                ParsedCommand::Info { item } => BareCommand::Info { item },
                ParsedCommand::List { items } => BareCommand::List { items },
                ParsedCommand::Clear => BareCommand::Clear,
                ParsedCommand::Save { dst, .. } => BareCommand::Save { dst },
                ParsedCommand::Reset => BareCommand::Reset,
                ParsedCommand::Quit => BareCommand::Quit,
            }
        }
    }

    fn new_runner() -> CommandRunner<'static, ()> {
        CommandRunner::new()
            .print_with(|_| {})
            .enable_clear(|_| CommandControlFlow::Continue)
            .enable_save(SessionHistory::new())
            .enable_reset()
            .enable_quit()
    }

    fn parser(input: &'static str) -> Option<CommandParser<'static>> {
        CommandParser::new(input, 0)
    }

    #[track_caller]
    fn expect_word_boundaries(input: &'static str) -> Vec<(u32, u32)> {
        parser(input).unwrap().word_boundaries
    }

    #[track_caller]
    fn expect_ok(
        runner: &CommandRunner<()>,
        ctx: &mut Context,
        input: &'static str,
        expected: BareCommand,
    ) {
        let cmd = runner.parse_command(input, ctx).unwrap().unwrap();
        assert_eq!(expected, cmd.into());
    }

    #[track_caller]
    fn expect_fail(runner: &CommandRunner<()>, ctx: &mut Context, input: &'static str) {
        assert!(runner.parse_command(input, ctx).is_err());
    }

    #[test]
    fn test_command_parser() {
        assert!(parser("").is_none());
        assert!(parser(" ").is_none());
        assert!(parser("  ").is_none());

        assert!(parser("x").is_none());
        assert!(parser("x ").is_none());
        assert!(parser(" x").is_none());
        assert!(parser(" x ").is_none());

        assert!(parser("xyz").is_none());
        assert!(parser("xyz  ").is_none());
        assert!(parser("  xyz").is_none());
        assert!(parser("  xyz  ").is_none());

        assert!(parser("abc x").is_none(),);
        assert!(parser("abc  x ").is_none(),);
        assert!(parser(" abc   x").is_none());
        assert!(parser("  abc   x  ").is_none());

        assert_eq!(expect_word_boundaries("list"), [(0, 4)]);
        assert_eq!(expect_word_boundaries("list "), [(0, 4)]);
        assert_eq!(expect_word_boundaries(" list"), [(1, 5)]);
        assert_eq!(expect_word_boundaries(" list "), [(1, 5)]);

        assert_eq!(expect_word_boundaries("list   ab"), [(0, 4), (7, 9)]);
        assert_eq!(expect_word_boundaries("list   ab "), [(0, 4), (7, 9)]);
        assert_eq!(expect_word_boundaries(" list   ab"), [(1, 5), (8, 10)]);
        assert_eq!(expect_word_boundaries(" list   ab "), [(1, 5), (8, 10)]);

        assert_eq!(
            expect_word_boundaries("list   ab xy"),
            [(0, 4), (7, 9), (10, 12)]
        );
        assert_eq!(
            expect_word_boundaries("list   ab   xy "),
            [(0, 4), (7, 9), (12, 14)]
        );
        assert_eq!(
            expect_word_boundaries("   list   ab    xy"),
            [(3, 7), (10, 12), (16, 18)]
        );
        assert_eq!(
            expect_word_boundaries("   list   ab    xy   "),
            [(3, 7), (10, 12), (16, 18)]
        );
    }

    #[test]
    fn test_existent_commands() {
        // these shouldn't happen at runtime because the REPL skips over all
        // whitespace lines, but we still want to handle them just in case
        assert!(parser("").is_none());
        assert!(parser(" ").is_none());

        // valid commands
        assert!(parser("help").is_some());
        assert!(parser("help arg").is_some());
        assert!(parser("help arg1 arg2").is_some());

        assert!(parser("info").is_some());
        assert!(parser("info arg").is_some());
        assert!(parser("info arg1 arg2").is_some());

        assert!(parser("clear").is_some());
        assert!(parser("clear arg").is_some());
        assert!(parser("clear arg1 arg2").is_some());

        assert!(parser("list").is_some());
        assert!(parser("list arg").is_some());
        assert!(parser("list arg1 arg2").is_some());

        assert!(parser("quit").is_some());
        assert!(parser("quit arg").is_some());
        assert!(parser("quit arg1 arg2").is_some());

        assert!(parser("exit").is_some());
        assert!(parser("exit arg").is_some());
        assert!(parser("exit arg1 arg2").is_some());

        assert!(parser("save").is_some());
        assert!(parser("save arg").is_some());
        assert!(parser("save arg1 arg2").is_some());

        assert!(parser("reset").is_some());
        assert!(parser("reset arg").is_some());
        assert!(parser("reset arg1 arg2").is_some());

        // invalid (nonempty) command names are all None so that parsing can continue on
        // what is presumably a math expression. case matters
        assert!(parser(".").is_none());
        assert!(parser(",").is_none());
        assert!(parser(";").is_none());
        assert!(parser("ls").is_none());
        assert!(parser("HELP").is_none());
        assert!(parser("List xyz").is_none());
        assert!(parser("qUIt abc").is_none());
        assert!(parser("listfunctions").is_none());
        assert!(parser("exitquit").is_none());
    }

    #[test]
    fn test_whitespace() {
        let mut ctx = Context::new_without_importer();
        let runner = new_runner();

        expect_ok(&runner, &mut ctx, "list", BareCommand::List { items: None });
        expect_ok(
            &runner,
            &mut ctx,
            " list",
            BareCommand::List { items: None },
        );
        expect_ok(
            &runner,
            &mut ctx,
            "list ",
            BareCommand::List { items: None },
        );
        expect_ok(
            &runner,
            &mut ctx,
            " list ",
            BareCommand::List { items: None },
        );
        expect_ok(
            &runner,
            &mut ctx,
            "list functions  ",
            BareCommand::List {
                items: Some(ListItems::Functions),
            },
        );
        expect_ok(
            &runner,
            &mut ctx,
            "  list    functions  ",
            BareCommand::List {
                items: Some(ListItems::Functions),
            },
        );
        expect_ok(
            &runner,
            &mut ctx,
            "  list    functions  ",
            BareCommand::List {
                items: Some(ListItems::Functions),
            },
        );
        expect_ok(
            &runner,
            &mut ctx,
            "list    functions",
            BareCommand::List {
                items: Some(ListItems::Functions),
            },
        );
    }

    #[test]
    fn test_args() {
        let mut ctx = Context::new_without_importer();
        let runner = new_runner();

        expect_ok(
            &runner,
            &mut ctx,
            "help",
            BareCommand::Help {
                help_kind: HelpKind::BasicHelp,
            },
        );
        expect_ok(
            &runner,
            &mut ctx,
            "help commands",
            BareCommand::Help {
                help_kind: HelpKind::AllCommands,
            },
        );
        expect_fail(&runner, &mut ctx, "help arg");
        expect_fail(&runner, &mut ctx, "help arg1 arg2");

        expect_fail(&runner, &mut ctx, "info");
        expect_ok(
            &runner,
            &mut ctx,
            "info arg",
            BareCommand::Info { item: "arg" },
        );
        expect_ok(&runner, &mut ctx, "info .", BareCommand::Info { item: "." });
        expect_fail(&runner, &mut ctx, "info arg1 arg2");

        expect_ok(&runner, &mut ctx, "clear", BareCommand::Clear);
        expect_fail(&runner, &mut ctx, "clear arg");
        expect_fail(&runner, &mut ctx, "clear arg1 arg2");

        expect_ok(&runner, &mut ctx, "list", BareCommand::List { items: None });
        expect_ok(
            &runner,
            &mut ctx,
            "list functions",
            BareCommand::List {
                items: Some(ListItems::Functions),
            },
        );
        expect_ok(
            &runner,
            &mut ctx,
            "list dimensions",
            BareCommand::List {
                items: Some(ListItems::Dimensions),
            },
        );
        expect_ok(
            &runner,
            &mut ctx,
            "list variables",
            BareCommand::List {
                items: Some(ListItems::Variables),
            },
        );
        expect_ok(
            &runner,
            &mut ctx,
            "list units",
            BareCommand::List {
                items: Some(ListItems::Units),
            },
        );

        expect_ok(&runner, &mut ctx, "reset", BareCommand::Reset);
        expect_fail(&runner, &mut ctx, "reset arg");
        expect_fail(&runner, &mut ctx, "reset arg1 arg2");

        expect_ok(&runner, &mut ctx, "quit", BareCommand::Quit);
        expect_fail(&runner, &mut ctx, "quit arg");
        expect_fail(&runner, &mut ctx, "quit arg1 arg2");

        expect_ok(&runner, &mut ctx, "exit", BareCommand::Quit);
        expect_fail(&runner, &mut ctx, "exit arg");
        expect_fail(&runner, &mut ctx, "exit arg1 arg2");

        expect_ok(
            &runner,
            &mut ctx,
            "save",
            BareCommand::Save { dst: "history.nbt" },
        );
        expect_ok(
            &runner,
            &mut ctx,
            "save arg",
            BareCommand::Save { dst: "arg" },
        );
        expect_ok(&runner, &mut ctx, "save .", BareCommand::Save { dst: "." });
        expect_fail(&runner, &mut ctx, "save arg1 arg2");
    }

    #[test]
    fn test_runner() {
        fn test_case(
            runner: &mut CommandRunner<()>,
            ctx: &mut Context,
            input: &'static str,
            expected: CommandControlFlow,
        ) {
            let editor = &mut ();
            assert_eq!(
                expected,
                runner.try_run_command(input, ctx, editor).unwrap()
            );
        }

        let mut ctx = Context::new_without_importer();

        let mut runner = CommandRunner::new().print_with(|_| {}).enable_quit();

        test_case(&mut runner, &mut ctx, "help", CommandControlFlow::Continue);
        test_case(&mut runner, &mut ctx, "list", CommandControlFlow::Continue);
        test_case(
            &mut runner,
            &mut ctx,
            // won't be found, but that's fine
            "info item",
            CommandControlFlow::Continue,
        );
        test_case(
            &mut runner,
            &mut ctx,
            "clear",
            CommandControlFlow::NotACommand,
        );
        test_case(
            &mut runner,
            &mut ctx,
            "save dst",
            CommandControlFlow::NotACommand,
        );
        test_case(&mut runner, &mut ctx, "quit", CommandControlFlow::Return);
        test_case(&mut runner, &mut ctx, "exit", CommandControlFlow::Return);
    }
}