shelly-shell 0.1.0

A Rust based Unix style shell with a typed and structured language syntax.
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
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232

use std::fmt::{ self, Display, Formatter };

use crate::language::text::{ buffer::Buffer, location::Location };



/**
 * Represents all the different types of tokens that can be recognized in the Shelly script
 * language.
 */
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TokenKind
{
    /**
     * A simple literal value, such as a number or string.
     */
    Literal,

    /**
     * A variable identifier and always starts with a $ character.
     */
    Identifier,

    /**
     * A symbol text representing blocks of text as found during parsing. Can be a name of an
     * executable or a command line parameter being passed to something. These can end up resolving
     * to simple strings.
     */
    Symbol,

    /**
     * The auto variable that is automatically supplied by the language in certain contexts.
     */
    AutoIdentifier,

    /**
     * The `let` keyword used for variable declaration.
     */
    Let,

    /**
     * The `export` keyword used for marking variables as exported.
     */
    Export,

    /**
     * The `alias` keyword used for defining an alias.
     */
    Alias,

    /**
     * The `,` character used to separate items in a list or function parameters.
     */
    Comma,

    /**
     * The `sub` keyword used for defining a sub-process block.
     */
    Sub,

    /**
     * The `if` keyword used for conditional branching.
     */
    If,

    /**
     * The `else` keyword used for conditional branching when the `if` condition is not met.
     */
    Else,

    /**
     * The `while` keyword used for looping while a condition is true.
     */
    While,

    /**
     * A unbound loop construct used for indefinite looping until explicitly broken.
     */
    Loop,

    /**
     * The `match` keyword used for pattern matching against values.
     */
    Match,

    /**
     * The `return` keyword used for returning a value from a function.
     */
    Return,

    /**
     * Keyword used for defining a function.
     */
    Function,

    /**
     * The `struct` keyword used for defining a new structure.
     */
    Struct,

    /**
     * The `enum` keyword used for defining a new enumeration.
     */
    Enum,

    /**
     * Keyword used for importing modules or other resources into the current scope.
     */
    Import,

    /**
     * The `:` character used for type annotations or other type-related syntax.
     */
    TypeDelimiter,

    /**
     * The `=` character used for assignment of values to variables.
     */
    Assign,

    /**
     * The `-` character used for subtraction or negation.
     */
    Minus,

    /**
     * The `+` character used for addition.
     */
    Plus,

    /**
     * The `*` character used for multiplication or file gathering operations.
     */
    Asterisk,

    /**
     * The `**` character used for globbing or file pattern matching.
     */
    Glob,

    /**
     * The `/` character used for division or file path operations.
     */
    Slash,

    /**
     * The `%` character used for modulo operations or other percentage-related syntax.
     */
    Percent,

    /**
     * The `;` character used to indicate the end of a statement, without requiring a newline.
     */
    StatementBreak,

    /**
     * The `\n` character used to indicate a line break in the source code. If the file includes any
     * `\r` characters they are ignored.
     */
    LineBreak,

    /**
     * The `\` character used to indicate that the current line continues onto the next line. It is
     * illegal for anything other than whitespace and comments to follow it.
     */
    LineContinue,

    /**
     * The scope symbol, `::` used to indicate the a scope delimiter for a keyword or to indicate
     * the executable should be taken from an outer scope.
     */
    Scope,

    /**
     * The `\`` character used to indicate that we're taking the value of an executable instead of
     * actually executing it.
     *
     * Used for assigning functions and executable references to variables for later use.
     */
    ExecEscape,

    /**
     * Open a block of code with `{`.
     */
    BlockOpen,

    /**
     * Close a block of code with `}`.
     */
    BlockClose,

    /**
     * Represents an empty block of code, `{}`.
     */
    EmptyBlock,

    /**
     * Open a square bracket with `[`. Used for array indexing or defining array literals.
     */
    SquareOpen,

    /**
     * Close a square bracket with `]`. Used for array indexing or defining array literals.
     */
    SquareClose,

    /**
     * Open a parenthesis with `(`. Used for grouping expressions, defining function parameters, and
     * grouping expressions/executable calls.
     */
    ParenOpen,

    /**
     * Close a parenthesis closing a group of expressions or an executable call.
     */
    ParenClose,

    /**
     * Symbol for piping IO between commands, represented by the `|` character.
     */
    Pipe,

    /**
     * Represents stderr in the source code, represented by the `~` character.
     */
    ErrorSource,

    /**
     * Represents redirection in the source code to a file or variable, the `->` symbol.
     */
    RedirectTo,

    /**
     * Represents redirection in the source code from a file or variable, the `<-` symbol.
     */
    RedirectFrom,

    /**
     * Represents redirection of stderr to a file or variable, the `~->` symbol.
     */
    RedirectErrorTo,

    /**
     * Represents redirection of both stdout and stderr to a file or variable, the `+~->` symbol.
     */
    RedirectBothTo,

    /**
     * Represents redirection of stderr from a file or variable, the `<-~` symbol.
     */
    RedirectErrorFrom,

    /**
     * Represents redirection of both stdout and stderr from a file or variable, the `<-+~` symbol.
     */
    RedirectBothFrom,

    /**
     * Represents the splat operator, `...`, used for argument expansion.
     */
    Splat
}


impl Display for TokenKind
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result
    {
        write!(f, "{:?}", self)
    }
}


/**
 * Indicate if the string should participate in interpolation or not.
 */
#[derive(Clone, PartialEq, Eq)]
pub enum StringFlag
{
    Interpolated,
    NonInterpolated
}


/**
 * Represents the different types of literals that can be associated with a token.
 */
#[derive(Clone)]
pub enum TokenLiteral
{
    /**
     * An integer literal value.
     */
    Integer(i64, String),

    /**
     * A floating-point literal value.
     */
    Float(f64, String),

    /**
     * A boolean literal value.
     */
    Boolean(bool),

    /**
     * A string literal value.
     */
    String(String, StringFlag)
}


impl Display for TokenLiteral
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result
    {
        match self
        {
            TokenLiteral::Integer(value, text) => write!(f, "{}:{}", value, text),
            TokenLiteral::Float(value, text)   => write!(f, "{}:{}", value, text),
            TokenLiteral::Boolean(value)       => write!(f, "{}", value),
            TokenLiteral::String(text, _)      => write!(f, "{:?}", text)
        }
    }
}


/**
 * Represents a value that can be associated with a token, such as a literal, identifier, or none.
 */
#[derive(Clone)]
pub enum TokenValue
{
    None,
    Literal(TokenLiteral),
    Identifier(String),
    Symbol(String)
}


impl Display for TokenValue
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result
    {
        match self
        {
            TokenValue::None                   => write!(f, "_"),
            TokenValue::Literal(literal)       => write!(f, "{}", literal),
            TokenValue::Identifier(identifier) => write!(f, "{}", identifier),
            TokenValue::Symbol(symbol)         => write!(f, "{}", symbol)
        }
    }
}


/**
 * Represents a single token of language as extracted from the input source code.
 */
#[derive(Clone)]
pub struct Token
{
    /**
     * The logical location in the source code this token was extracted from.
     */
    pub location: Location,

    /**
     * The type of the token, indicating its role in the language syntax.
     */
    pub kind: TokenKind,

    /**
     * The value of the token, which may be a literal, identifier, or none depending on the token
     * type.
     */
    pub value: TokenValue
}


impl Token
{
    /**
     * Returns the textual representation of the token's original value.
     */
    pub fn token_value_text(&self) -> String
    {
        match &self.kind
        {
            TokenKind::Literal =>
                {
                    if let TokenValue::Literal(literal) = &self.value
                    {
                        match literal
                        {
                            TokenLiteral::Integer(_, text) => return text.clone(),
                            TokenLiteral::Float(_, text)   => return text.clone(),
                            TokenLiteral::Boolean(value)   => return value.to_string(),
                            TokenLiteral::String(text, _)  => return text.clone()
                        }
                    }
                    else
                    {
                        panic!("A literal token does not have a literal value.");
                    }
                },

            TokenKind::Identifier =>
                {
                    if let TokenValue::Identifier(text) = &self.value
                    {
                        text.clone()
                    }
                    else
                    {
                        panic!("An identifier token does not have an identifier value.");
                    }
                },

            TokenKind::Symbol =>
                {
                    if let TokenValue::Symbol(text) = &self.value
                    {
                        text.clone()
                    }
                    else
                    {
                        panic!("A symbol token does not have a symbol value.");
                    }
                },

            TokenKind::AutoIdentifier    => "$".to_string(),
            TokenKind::Let               => "let".to_string(),
            TokenKind::Export            => "export".to_string(),
            TokenKind::Alias             => "alias".to_string(),
            TokenKind::Comma             => ",".to_string(),
            TokenKind::Sub               => "sub".to_string(),
            TokenKind::If                => "if".to_string(),
            TokenKind::Else              => "else".to_string(),
            TokenKind::While             => "while".to_string(),
            TokenKind::Loop              => "loop".to_string(),
            TokenKind::Match             => "match".to_string(),
            TokenKind::Return            => "return".to_string(),
            TokenKind::Function          => "fn".to_string(),
            TokenKind::Struct            => "struct".to_string(),
            TokenKind::Enum              => "enum".to_string(),
            TokenKind::Import            => "import".to_string(),
            TokenKind::TypeDelimiter     => ":".to_string(),
            TokenKind::Assign            => "=".to_string(),
            TokenKind::Minus             => "-".to_string(),
            TokenKind::Plus              => "+".to_string(),
            TokenKind::Asterisk          => "*".to_string(),
            TokenKind::Glob              => "**".to_string(),
            TokenKind::Slash             => "/".to_string(),
            TokenKind::Percent           => "%".to_string(),
            TokenKind::StatementBreak    => ";".to_string(),
            TokenKind::LineBreak         => "\n".to_string(),
            TokenKind::LineContinue      => "\\".to_string(),
            TokenKind::Scope             => "::".to_string(),
            TokenKind::ExecEscape        => "`".to_string(),
            TokenKind::BlockOpen         => "{".to_string(),
            TokenKind::BlockClose        => "}".to_string(),
            TokenKind::EmptyBlock        => "{}".to_string(),
            TokenKind::SquareOpen        => "[".to_string(),
            TokenKind::SquareClose       => "]".to_string(),
            TokenKind::ParenOpen         => "(".to_string(),
            TokenKind::ParenClose        => ")".to_string(),
            TokenKind::Pipe              => "|".to_string(),
            TokenKind::ErrorSource       => "~".to_string(),
            TokenKind::RedirectTo        => "->".to_string(),
            TokenKind::RedirectFrom      => "<-".to_string(),
            TokenKind::RedirectErrorTo   => "~->".to_string(),
            TokenKind::RedirectBothTo    => "+~->".to_string(),
            TokenKind::RedirectErrorFrom => "<-~".to_string(),
            TokenKind::RedirectBothFrom  => "<-+~".to_string(),
            TokenKind::Splat             => "...".to_string()
        }
    }
}


impl Display for Token
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result
    {
        write!(f, "{}: ({}, {})", self.location, self.kind, self.value)
    }
}


/**
 * Represents an error encountered during tokenization.
 */
pub struct TokenizerError
{
    /**
     * The logical location in the source code the error occurred at.
     */
    pub location: Location,

    /**
     * A descriptive message explaining the error.
     */
    pub message: String
}


impl Display for TokenizerError
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result
    {
        write!(f, "TokenizerError at {}: {}", self.location, self.message)
    }
}


/**
 * Indicates whether comments should be skipped during tokenization.
 */
#[derive(PartialEq, Eq, Clone, Copy)]
enum SkipComments
{
    /**
     * Skip comments during tokenization.
     */
    Yes,

    /**
     * Do not skip comments during tokenization.
     */
    No
}


#[derive(PartialEq, Eq, Clone, Copy)]
enum SkipNewlines
{
    /**
     * Skip newlines during tokenization.
     */
    Yes,

    /**
     * Do not skip newlines during tokenization.
     */
    No
}


/**
 * A Shelly script tokenizer that lazily extracts tokens from the input source code buffer as
 * needed.
 */
pub struct Tokenizer<'a>
{
    /**
     * The source code input buffer to read our tokens from.
     */
    input: &'a mut dyn Buffer
}


impl<'a> Tokenizer<'a>
{
    /**
     * Construct a new tokenizer with the given input buffer.
     */
    pub fn new(input: &'a mut dyn Buffer) -> Self
    {
        Self { input }
    }

    /**
     * Attempt to extract the next token from the input stream. Returns `Ok(Some(Token))` if a token
     * is successfully parsed, `Ok(None)` if the end of the input stream is reached, or
     * `Err(TokenizerError)` if an error is encountered during tokenization.
     */
    pub fn next_token(&mut self) -> Result<Option<Token>, TokenizerError>
    {
        // Skip past any whitespace and comments.
        self.skip_whitespace(SkipComments::Yes, SkipNewlines::No);

        // Check to see what the next character is, if any.
        let next = self.input.peek_next();

        if let None = next
        {
            return Ok(None);
        }

        // Unwrap the next character since we know it exists then proceed with matching it. Attempt
        // to return the extracted token to the caller, if any.
        let next = next.unwrap();

        match next
        {
            '_' | 'a'..='z' | 'A'..='Z' => Ok(Some(self.parse_symbol())),
            '$'                         => self.parse_identifier().map(Some),
            '0'..='9'                   => Ok(Some(self.try_parse_number())),
            '"'                         => self.parse_string(StringFlag::Interpolated),
            '\''                        => self.parse_string(StringFlag::NonInterpolated),
            _                           => self.try_parse_operator()
        }
    }

    /**
     * Extract a text symbol from the input stream. A symbol typically consists of alphabetic
     * characters, digits, and underscores, and is used to represent identifiers, keywords, or
     * other executables in the system where an executable can be an external program or function.
     */
    fn parse_symbol(&mut self) -> Token
    {
        let location = self.input.location().clone();
        let symbol = self.extract_to_separator(None);

        Self::symbol_str_to_token(location, symbol)
    }

    /**
     * Internal method to convert the symbol string into a corresponding token. This is used to
     * differentiate between keywords and general symbols.
     */
    fn symbol_str_to_token(location: Location, symbol: String) -> Token
    {
        let kind = match symbol.as_str()
            {
                "let"    => TokenKind::Let,
                "export" => TokenKind::Export,
                "alias"  => TokenKind::Alias,
                "sub"    => TokenKind::Sub,
                "if"     => TokenKind::If,
                "else"   => TokenKind::Else,
                "while"  => TokenKind::While,
                "loop"   => TokenKind::Loop,
                "match"  => TokenKind::Match,
                "return" => TokenKind::Return,
                "fn"     => TokenKind::Function,
                "struct" => TokenKind::Struct,
                "enum"   => TokenKind::Enum,
                "import" => TokenKind::Import,
                "true"   => TokenKind::Literal,
                "false"  => TokenKind::Literal,
                _        => TokenKind::Symbol
            };

        let value = match &kind
            {
                TokenKind::Symbol  => TokenValue::Symbol(symbol),
                TokenKind::Literal => TokenValue::Literal(TokenLiteral::Boolean(symbol == "true")),
                _                  => TokenValue::None
            };

         Token { location, kind, value }
    }

    /**
     * Parse a variable identifier from the input stream. A variable identifier starts with a `$`
     * sign followed by a name, optionally enclosed in braces.
     */
    fn parse_identifier(&mut self) -> Result<Token, TokenizerError>
    {
        let location = self.input.location().clone();
        let mut identifier = "$".to_string();

        // Ensure that the next character is a '$' before proceeding with parsing the identifier.
        assert!(self.input.next() == Some('$'),
                "Expected '$' at the beginning of an identifier");

        let braced = self.input.peek_next() == Some('{');
        if braced
        {
            self.input.next();
            identifier += &self.extract_to_separator(Some(&['{', '}', '.', '/']));

            if identifier.len() == 1 || self.input.next() != Some('}')
            {
                return Err(TokenizerError
                    {
                        location,
                        message: "Expected a variable name and closing '}' in variable reference."
                            .to_string()
                    });
            }
        }
        else
        {
            identifier += &self.extract_to_separator(Some(&['.', '/']));
        }

        // A variable-prefixed path is one interpolated word, not a variable name.
        if self.input.peek_next() == Some('/')
        {
            // Keep braces in interpolated paths to preserve the variable boundary.
            if braced { identifier = format!("${{{}}}", &identifier[1..]); }
            identifier += &self.extract_to_separator(None);
            return Ok(Self::symbol_str_to_token(location, identifier));
        }

        if identifier.len() == 1
        {
            return Ok(Token
                {
                    location,
                    kind: TokenKind::AutoIdentifier,
                    value: TokenValue::None
                });
        }

        Ok(Token
            {
                location,
                kind: TokenKind::Identifier,
                value: TokenValue::Identifier(identifier)
            })
    }

    /**
     * Attempt to parse a literal number from the input stream. It will handle integers and
     * floating-point numbers, and other numeric formats as defined by the language's syntax.
     */
    fn try_parse_number(&mut self) -> Token
    {
        let location = self.input.location().clone();
        let number_str = self.extract_to_separator(None);

        // Attempt to parse the collected string as an integer first. If that fails, try parsing it
        // as a float. If both fail, treat it as a symbol.
        if let Ok(number) = number_str.parse::<i64>()
        {
            Token
                {
                    location,
                    kind: TokenKind::Literal,
                    value: TokenValue::Literal(TokenLiteral::Integer(number, number_str))
                }
        }
        else if let Ok(number) = number_str.parse::<f64>()
        {
            Token
                {
                    location,
                    kind: TokenKind::Literal,
                    value: TokenValue::Literal(TokenLiteral::Float(number, number_str))
                }
        }
        else
        {
            Self::symbol_str_to_token(location, number_str)
        }
    }

    /**
     * Extract characters from the input stream until a separator or an optional additional
     * separator is encountered.
     */
    fn extract_to_separator(&mut self, additional_separators: Option<&[char]>) -> String
    {
        let mut result = String::new();

        while let Some(next) = self.input.peek_next()
        {
            if    !Self::is_separator_char(&next)
               && !additional_separators.map_or(false, |separators| separators.contains(&next))
            {
                let _ = &self.input.next();
                result.push(next);
            }
            else
            {
                break;
            }
        }

        result
    }

    /**
     * Parse a string literal from the input stream. It will attempt to extract the string as a
     * single line string. If it is a multi-line string, it will call out for multi-line handling.
     *
     * In single line strings `\n`, new lines are illegal and will result in a parsing error. An
     * error is also generated if the closing quote is never encountered in the source text.
     */
    fn parse_string(&mut self, flag: StringFlag) -> Result<Option<Token>, TokenizerError>
    {
        let location = self.input.location().clone();
        let next = self.input.next().unwrap();

        assert!(matches!(next, '"' | '\''),
                "Internal error, expected a quote to start a string literal.");

        if    let Some(next) = self.input.peek_next()
           && next == '*'
        {
            return self.parse_multiline_string(location, flag);
        }
        else
        {
            let mut literal_string = String::new();
            let mut closed = false;

            while let Some(next) = self.input.next()
            {
                match next
                {
                    '"' if flag == StringFlag::Interpolated =>
                        {
                            closed = true;
                            break;
                        },

                    '\'' if flag == StringFlag::NonInterpolated =>
                        {
                            closed = true;
                            break;
                        },

                    '\n' =>
                        {
                            return Err(TokenizerError
                                {
                                    location,
                                    message: "Newline encountered in single-line string literal."
                                        .to_string()
                                });
                        },

                    '\\' => literal_string.push(self.process_string_escape()?),

                    _    => literal_string.push(next)
                }
            }

            if !closed
            {
                return Err(TokenizerError
                    {
                        location,
                        message: "Missing ending quote for string literal.".to_string()
                    })
            }

            Ok(Some(Token
                {
                    location,
                    kind: TokenKind::Literal,
                    value: TokenValue::Literal(TokenLiteral::String(literal_string, flag))
                }))
        }
    }

    /**
     * Called from parse string when a multi-line string is detected. The string parsed will have
     * leading whitespace removed while preserving the relative indentation of the subsequent lines.
     */
    fn parse_multiline_string(&mut self,
                              location: Location,
                              flag: StringFlag) -> Result<Option<Token>, TokenizerError>
    {
        // Helper for skipping extra whitespace at the beginning of each line.  If there is no text
        // on a given line it is skipped entirely.
        fn skip_whitespace_until_column<'a>(location: &Location, buffer: &mut dyn Buffer,
                                            target_column: usize)
                                            -> Result<(), TokenizerError>
        {
            while   let Some(next) = buffer.peek_next()
                 && Tokenizer::<'a>::is_whitespace_char(&next)
                 && buffer.location().column < target_column
            {
                let _ = buffer.next();
            }

            if buffer.peek_next().is_none()
            {
                return Err(TokenizerError
                    {
                        location: location.clone(),
                        message: "Unexpected end of file in string literal.".to_string()
                    });
            }

            Ok(())
        }

        // Append newlines for skipped empty lines.
        fn append_newlines(text: &mut String, count: usize)
        {
            for _ in 0..count
            {
                text.push('\n');
            }
        }

        // We expect that the " has already be processed and that we need to consume the following *.
        let next = self.input.next().unwrap();
        assert!(next == '*');

        // Skip over any whitespace at the beginning of the string.  Using the location of the first
        // textual character to calibrate what we will consider the beginning of the actual line of
        // text.  This way we can remove any extra whitespace at the beginning of each line while
        // allowing for any extra indentation the user may want to add.
        self.skip_whitespace(SkipComments::No, SkipNewlines::Yes);

        let target_column = self.input.location().column;

        let mut closed = false;
        let mut text = String::new();

        // Keep going until we either hit the end of the buffer or the closing *" pair.
        while let Some(next) = self.input.next()
        {
            match next
            {
                // We found the * but did we find the "?
                '*' =>
                {
                    if let Some(quote) = self.input.peek_next()
                    {
                        // We're at the end of the string.
                        if   (quote == '"' && flag == StringFlag::Interpolated)
                          || (quote == '\'' && flag == StringFlag::NonInterpolated)
                        {
                            let _ = self.input.next();
                            closed = true;
                            break;
                        }
                        else
                        {
                            // Looks like a stray * so we'll just add it to the text.
                            text.push('*');
                        }
                    }
                    else
                    {
                        return Err(TokenizerError
                            {
                                location: location.clone(),
                                message: "Unexpected end of file in string literal.".to_string()
                            });
                    }
                }

                // Process the escape sequence.
                '\\' => text.push(self.process_string_escape()?),

                // Process the new line skipping any extra whitespace until we hit the target column.
                '\n' =>
                {
                    text.push('\n');

                    // Keep track of the starting line so that we can add newlines for skipped empty
                    // lines.  Then start skipping until we find something useful or we hit the
                    // target column.
                    let start_line = self.input.location().line;

                    skip_whitespace_until_column(&location, self.input, target_column)?;

                    // If we skipped any empty lines then we need to backfill the newlines.
                    let current_line = self.input.location().line;

                    if current_line > start_line
                    {
                        append_newlines(&mut text, current_line - start_line);
                    }
                }

                // Just add the character to the text.
                _ =>
                {
                    text.push(next);
                }
            }
        }

        if !closed
        {
            return Err(TokenizerError
                {
                    location: location.clone(),
                    message: "Unterminated string literal.".to_string()
                });
        }

        Ok(Some(Token
            {
                location,
                kind: TokenKind::Literal,
                value: TokenValue::Literal(TokenLiteral::String(text, flag))
            }))
    }

    /**
     * Processes an escape sequence within a string literal and returns the corresponding character.
     *
     * The sequence can be a single known character like \n or \\ or a numeric escape sequence like
     * \xNN or \o{NNNN}.
     */
    fn process_string_escape(&mut self) -> Result<char, TokenizerError>
    {
        // Attempt to parse the numeric value from the escape sequence. If not parsed correctly, an
        // error will be returned.
        fn parse_number(location: &Location, input: &mut dyn Buffer,
                        is_digit: fn(char) -> bool, radix: u32)
                        -> Result<char, TokenizerError>
        {
            let number_str = read_digits(input, is_digit);
            let number = u32::from_str_radix(&number_str, radix)
                .map_err(|_| TokenizerError
                    {
                        location: location.clone(),
                        message: format!("Failed to parse numeric literal from '{}'.",
                                         number_str)
                    })?;

            char::from_u32(number)
                .ok_or_else(|| TokenizerError
                    {
                        location: location.clone(),
                        message: format!("Numeric literal '{}' is not a valid character.",
                                         number_str)
                    })
        }

        // Read a sequence of digits from the input buffer that satisfy the `is_digit` predicate
        // breaking when a non-digit character is encountered.
        fn read_digits(input: &mut dyn Buffer, is_digit: fn(char) -> bool) -> String
        {
            let mut number_str = String::new();

            while    let Some(next) = input.peek_next()
                  && is_digit(next)
            {
                number_str.push(input.next().unwrap());
            }

            number_str
        }

        let location = self.input.location().clone();

        match self.input.next()
        {
            // Perform a simple translation of the escape sequence.
            Some('n') => Ok('\n'),
            Some('r') => Ok('\r'),
            Some('t') => Ok('\t'),

            // Parse a decimal numeric literal for the character.
            Some('0') =>
                {
                    parse_number(&location, self.input, |next| next.is_ascii_digit(), 10)
                }

            // Parse a hexadecimal numeric literal for the character.
            Some('x') | Some('X') =>
                {
                    parse_number(&location, self.input, |next| next.is_ascii_hexdigit(), 16)
                }

            // Parse an octal numeric literal for the character.
            Some('o') | Some('O') =>
                {
                    parse_number(&location, self.input, |next| ('0'..='7').contains(&next), 8)
                }

            // The escape was on a non-special character so just pass it through without translation.
            Some(next) => Ok(next),

            // Looks like we hit the end of the buffer while processing a string.
            None =>
                {
                    Err(TokenizerError
                        {
                            location,
                            message: "Unexpected end of file in string literal.".to_string()
                        })
                }
        }
    }

    /**
     * Attempt to extract a single or multiple character operator from the input stream. Returns
     * `Ok(Some(Token))` if an operator is successfully parsed, `Ok(None)` if no operator is found,
     * or `Err(TokenizerError)` if an error is encountered during parsing.
     */
    fn try_parse_operator(&mut self) -> Result<Option<Token>, TokenizerError>
    {
        fn operator_token(location: Location, kind: TokenKind) -> Result<Option<Token>,
                                                                       TokenizerError>
        {
            Ok(Some(Token{ location, kind, value: TokenValue::None }))
        }

        // First, check to see if the next character is a separator operator that can be immediately
        // returned. If it isn't a separator operator, we will need to parse it as a multi-character
        // operator. This disambiguates "-" from "-f" or "--foo" where the latter two can be treated
        // as text to be passed through.
        let location = self.input.location().clone();
        let next = self.input.next().unwrap();

        match next
        {
            '|'  => return operator_token(location, TokenKind::Pipe),
            ';'  => return operator_token(location, TokenKind::StatementBreak),
            '('  => return operator_token(location, TokenKind::ParenOpen),
            ')'  => return operator_token(location, TokenKind::ParenClose),
            '['  => return operator_token(location, TokenKind::SquareOpen),
            ']'  => return operator_token(location, TokenKind::SquareClose),
            '`'  => return operator_token(location, TokenKind::ExecEscape),
            '\n' => return operator_token(location, TokenKind::LineBreak),
            '\\' => return operator_token(location, TokenKind::LineContinue),
            ','  => return operator_token(location, TokenKind::Comma),
            ':'  =>
                {
                    if let Some(':') = self.input.peek_next()
                    {
                        let _ = self.input.next();
                        return operator_token(location, TokenKind::Scope);
                    }

                    return operator_token(location, TokenKind::TypeDelimiter);
                }

            // Nothing to do here, pass through to non-separator operator parsing.
            _ => {}
        }

        // Ok we know it isn't a separator operator, so we will attempt to parse it as a
        // non-separator operator, some of which can be multi-character.
        let mut operator_str = String::new();

        operator_str.push(next);
        operator_str += &self.extract_to_separator(None);

        match operator_str.as_str()
        {
            "="    => return operator_token(location, TokenKind::Assign),
            "{"    => return operator_token(location, TokenKind::BlockOpen),
            "}"    => return operator_token(location, TokenKind::BlockClose),
            "-"    => return operator_token(location, TokenKind::Minus),
            "~"    => return operator_token(location, TokenKind::ErrorSource),
            "+"    => return operator_token(location, TokenKind::Plus),
            "*"    => return operator_token(location, TokenKind::Asterisk),
            "/"    => return operator_token(location, TokenKind::Slash),
            "%"    => return operator_token(location, TokenKind::Percent),
            "->"   => return operator_token(location, TokenKind::RedirectTo),
            "<-"   => return operator_token(location, TokenKind::RedirectFrom),
            "~->"  => return operator_token(location, TokenKind::RedirectErrorTo),
            "~+->" => return operator_token(location, TokenKind::RedirectBothTo),
            "<-~"  => return operator_token(location, TokenKind::RedirectErrorFrom),
            "<-+~" => return operator_token(location, TokenKind::RedirectBothFrom),
            "{}"   => return operator_token(location, TokenKind::EmptyBlock),
            "**"   => return operator_token(location, TokenKind::Glob),
            "..."  => return operator_token(location, TokenKind::Splat),
            _      => Ok(Some(Self::symbol_str_to_token(location, operator_str)))
        }
    }

    /**
     * Skip all text that is considered whitespace, including comments, until a non-whitespace
     * character is encountered. In this syntax new lines are not specifically considered
     * whitespace. Because newlines are significant in terminating certain statements like
     * executable object calls.
     */
    fn skip_whitespace(&mut self, skip_comments: SkipComments, skip_newlines: SkipNewlines)
    {
        // Peek at the next character and skip it if it's whitespace or part of a comment.
        while let Some(next) = self.input.peek_next()
        {
            // Check to see if the next character is not whitespace. If it isn't, break out of the
            // loop, without consuming the non-whitespace character.
            if    !Self::is_whitespace_char(&next)
               && !(next == '#' && skip_comments == SkipComments::Yes)
               && !(next == '\n' && skip_newlines == SkipNewlines::Yes)
            {
                break;
            }

            // It's a whitespace character or part of a comment, so consume it.
            let _ = self.input.next();

            // If the next character is a comment indicator and we are supposed to skip comments,
            // skip the entire comment up until the next newline character. We don't consume the
            // newline itself so that it can be handled separately as a significant token.
            if next == '#' && skip_comments == SkipComments::Yes
            {
                // Consume any characters in the comment until a newline is encountered.
                while let Some(next) = self.input.peek_next()
                {
                    if next == '\n'
                    {
                        break;
                    }

                    let _ = self.input.next();
                }
            }
        }
    }

    /**
     * Is the given character a separator character? Separator characters are those that are used as
     * operators or punctuation in the language's syntax. They also act as separator characters
     * between tokens. So `$var|$var` appear as separate tokens in the stream despite the lack of
     * whitespace.
     */
    fn is_separator_char(next: &char) -> bool
    {
           *next == '|'
        || *next == ';'
        || *next == '('
        || *next == ')'
        || *next == '['
        || *next == ']'
        || *next == ':'
        || *next == '#'
        || *next == '`'
        || *next == ','
        || *next == '\n'
        || *next == '\\'
        || Self::is_whitespace_char(next)
    }

    /**
     * Determines if the given character is considered whitespace, excluding newline characters.
     * This is because newlines are considered significant in the language's syntax.
     */
    fn is_whitespace_char(next: &char) -> bool
    {
        *next != '\n' && next.is_whitespace()
    }
}