tinytoken 0.1.4

Library for tokenizing text into words, numbers, symbols, and more, with customizable parsing options.
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
#![doc = include_str!("../README.md")]

use error::TokenizationError;

/// Contains error definitions specific to tokenization
pub mod error;

/// Represents the types of numeric tokens recognized by the tokenizer
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NumberType {
    /// Floating-point numbers (e.g., `3.14`, `.25`)
    Float,
    /// Hexadecimal numbers (e.g., `0x1A3F`)
    Hex,
    /// Binary numbers (e.g., `0b1010`)
    Binary,
    /// Octal numbers (e.g., `0o755`)
    Octal,
    /// Sequential integers (e.g., `12345`)
    Seq,
}

/// Represents all possible token types that can be parsed by the tokenizer
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TokenType {
    /// Any alphanumeric string
    Word,
    /// A numeric token, where [NumberType] specifies the format
    Number(NumberType),
    /// A sequence of characters surrounded by double quotes ("example")
    String,
    /// A single character surrounded by single quotes ('a')
    Char,
    /// A character recognized as a symbol
    Symbol,
    /// A character recognized as an operator
    Operator,
}

/// Represents the location of a token in the input text, with line and column values
///
/// Format: Formats Loc as `<line+1>`:`<column+1>`.
#[derive(Clone, Copy, Debug)]
pub struct Loc(
    /// Line number (0-based index)
    pub usize,
    /// Column number (0-based index)
    pub usize,
);

impl std::fmt::Display for Loc {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}:{}", self.0 + 1, self.1 + 1)
    }
}

/// Represents an individual token with type, value, and location
#[derive(Debug, Clone)]
pub struct Token {
    /// The [TokenType] of the token
    pub r#type: TokenType,
    /// The text of the token
    pub value: Box<String>,
    /// The location of the token in the input
    pub loc: Loc,
}

/// Primary struct for tokenizing an input string, with methods for parsing and generating tokens
pub struct Tokenizer {
    pub lines: Vec<Vec<char>>,
    ln: usize,
    col: usize,
    config: TokenizerConfig,
}

/// Configurable option for specific settings in [TokenizerConfig]
#[derive(Debug, Clone, Copy)]
pub enum Choice<T>
where
    T: Copy + Clone,
{
    /// An active choice with a specified value of type T
    Yes(T),
    /// No active choice
    No,
}

impl<T> Default for Choice<T>
where
    T: Copy + Clone,
{
    fn default() -> Self {
        Self::No
    }
}

/// Configuration struct for the tokenizer, allowing customization of tokenization behavior
#[derive(Default, Clone, Debug)]
pub struct TokenizerConfig {
    /// Whether single characters should be treated as strings and therefore may contains more than
    /// one character
    pub parse_char_as_string: bool,
    /// Considers numbers as words
    pub ignore_numbers: bool,
    /// Allows a specific character as a digit separator (e.g., `_`)
    pub allow_digit_separator: Choice<char>,
    /// List of characters to be treated as symbols
    pub consider_as_symbols: Vec<char>,
    /// List of characters to be treated as operators
    pub consider_as_operators: Vec<char>,
}

/// A builder struct for creating a [TokenizerConfig] instance with customized options
#[derive(Clone, Debug)]
pub struct TokenizerBuilder {
    conf: TokenizerConfig,
}

impl TokenizerBuilder {
    /// Creates a default [TokenizerBuilder]
    pub fn new() -> TokenizerBuilder {
        TokenizerBuilder {
            conf: TokenizerConfig {
                consider_as_symbols: vec!['.'],
                ..Default::default()
            },
        }
    }

    /// Configures character parsing behavior
    pub fn parse_char_as_string(self, set_to: bool) -> Self {
        let mut lb = TokenizerBuilder::new();
        lb.conf = self.conf;
        lb.conf.parse_char_as_string = set_to;
        lb
    }

    /// Configures number parsing behaviour
    pub fn ignore_numbers(self, set_to: bool) -> Self {
        let mut lb = TokenizerBuilder::new();
        lb.conf = self.conf;
        lb.conf.ignore_numbers = set_to;
        lb
    }

    /// Sets the digit separator
    pub fn allow_digit_separator(self, choice: Choice<char>) -> Self {
        let mut lb = TokenizerBuilder::new();
        lb.conf = self.conf;
        lb.conf.allow_digit_separator = choice;
        lb
    }

    /// Adds a symbol character
    pub fn add_symbol(self, sym: char) -> Self {
        let mut lb = TokenizerBuilder::new();
        lb.conf = self.conf;
        lb.conf.consider_as_symbols.push(sym);
        lb
    }

    /// Adds multiple symbol characters
    pub fn add_symbols(self, syms: &[char]) -> Self {
        let mut lb = TokenizerBuilder::new();
        lb.conf = self.conf;
        lb.conf.consider_as_symbols.extend(syms);
        lb
    }

    /// Adds an operator character
    pub fn add_operator(self, op: char) -> Self {
        let mut lb = TokenizerBuilder::new();
        lb.conf = self.conf;
        lb.conf.consider_as_operators.push(op);
        lb
    }

    /// Adds multiple operator characters
    pub fn add_operators(self, ops: &[char]) -> Self {
        let mut lb = TokenizerBuilder::new();
        lb.conf = self.conf;
        lb.conf.consider_as_operators.extend(ops);
        lb
    }

    /// Constructs a [Tokenizer] with the specified input and configuration.
    pub fn build<T>(self, with_input: T) -> Tokenizer
    where
        T: ToString,
    {
        Tokenizer::new(with_input, self.conf)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum OutOfBound {
    Empty,
    Out,
    Within,
}

impl Tokenizer {
    /// Creates a TokenizerBuilder instance for configuring and initializing the tokenizer
    pub fn builder() -> TokenizerBuilder {
        TokenizerBuilder::new()
    }
    /// Initializes the tokenizer with input text and a configuration
    pub fn new<T>(input: T, config: TokenizerConfig) -> Self
    where
        T: ToString,
    {
        let input = input.to_string();
        Self {
            lines: input.lines().map(|line| line.chars().collect()).collect(),
            ln: 0,
            col: 0,
            config,
        }
    }

    fn is_out_of_bound(&self) -> OutOfBound {
        if self.ln >= self.lines.len() {
            return OutOfBound::Out;
        } else if !self.lines[self.ln].is_empty() {
            if self.col >= self.lines[self.ln].len() {
                return OutOfBound::Out;
            }
        } else {
            return OutOfBound::Empty;
        }

        OutOfBound::Within
    }

    fn is_out_of_bound_for(&self, ln: usize, col: usize) -> OutOfBound {
        if ln >= self.lines.len() {
            return OutOfBound::Out;
        } else if !self.lines[ln].is_empty() {
            if col >= self.lines[self.ln].len() {
                return OutOfBound::Out;
            }
        } else {
            return OutOfBound::Empty;
        }

        OutOfBound::Within
    }

    #[track_caller]
    fn consume(&mut self, len: usize) {
        let mut len = len;
        let ln_max = self.lines.len();

        while len > 0 {
            if self.ln + 1 > ln_max {
                // At this point, calling consume will have no effect,
                // The cursor is already out of bound
                // NOTE: don't put `unreachable!()` to avoid panic
                if self.is_out_of_bound() == OutOfBound::Out {
                    let caller_location = std::panic::Location::caller();
                    eprintln!(
                        "WARNING at {} The consume function is called but we are out of characters",
                        caller_location
                    );
                    break;
                }

                self.ln += 1;
            } else {
                let col_max = self.lines[self.ln].len();
                if self.col + 1 >= col_max {
                    self.next_line();
                } else {
                    self.col += 1;
                }
            }

            len -= 1;
        }
    }

    fn fake_consume(&self, len: usize) -> (OutOfBound, Loc) {
        let mut len = len;
        let ln_max = self.lines.len();

        let mut ln = self.ln;
        let mut col = self.col;

        while len > 0 {
            if ln + 1 > ln_max {
                ln += 1;
            } else {
                let col_max = self.lines[ln].len();
                if col + 1 >= col_max {
                    ln += 1;
                    col = 0;
                } else {
                    col += 1;
                }
            }

            len -= 1;
        }

        (self.is_out_of_bound_for(ln, col), Loc(ln, col))
    }

    #[inline]
    fn next_line(&mut self) {
        self.ln += 1;
        self.col = 0;
    }

    fn get_next_char(&self) -> Option<&char> {
        if self.is_out_of_bound() == OutOfBound::Within {
            Some(&self.lines[self.ln][self.col])
        } else {
            None
        }
    }

    fn peek_tok(&self) -> Option<&char> {
        let (bound, loc) = self.fake_consume(1);
        match bound {
            OutOfBound::Empty => Some(&'\n'),
            OutOfBound::Out => None,
            OutOfBound::Within => Some(&self.lines[loc.0][loc.1]),
        }
    }

    fn parse_word(&mut self) -> Result<Token, TokenizationError> {
        let mut word = String::new();
        let start_ln = self.ln;
        let start_col = self.col;
        while let Some(c) = self.get_next_char() {
            if *c != ' ' {
                if !self.config.consider_as_symbols.contains(c)
                    && !self.config.consider_as_operators.contains(c)
                {
                    word.push(*c);
                } else {
                    break;
                }
            } else {
                break;
            }
            self.consume(1);
        }

        Ok(Token {
            r#type: TokenType::Word,
            value: Box::new(word),
            loc: Loc(start_ln, start_col),
        })
    }

    fn parse_float(&mut self) -> Result<Token, TokenizationError> {
        let mut float = String::new();
        let start_ln = self.ln;
        let start_col = self.col;
        let mut encountered_dot = false;

        if *self.get_next_char().unwrap() == '.' {
            float.push_str("0.");
            encountered_dot = true;
            self.consume(1);
        }

        while let Some(c) = self.get_next_char() {
            if c.is_ascii_digit() {
                float.push(*c);
            } else if *c == '.' {
                if encountered_dot {
                    break;
                } else {
                    float.push('.');
                    encountered_dot = true;
                }
            } else {
                break;
            }
            self.consume(1);
        }

        Ok(Token {
            r#type: TokenType::Number(NumberType::Float),
            value: Box::new(float),
            loc: Loc(start_ln, start_col),
        })
    }

    fn parse_number(&mut self) -> Result<Token, TokenizationError> {
        let mut num_type = NumberType::Seq;
        let mut parsing_float = false;
        let mut num = String::new();

        let start_ln = self.ln;
        let start_col = self.col;
        let mut inner_col = self.col;

        while let Some(c) = self.get_next_char() {
            if c.is_ascii_digit() {
                num.push(*c);
            } else if *c == '.' {
                if parsing_float {
                    break;
                } else {
                    parsing_float = true;
                    num.push('.');
                    num_type = NumberType::Float
                }
            } else if let Choice::Yes(with) = &self.config.allow_digit_separator {
                if *c == *with {
                    self.consume(1);
                    if let Some(next_char) = self.get_next_char() {
                        if !next_char.is_ascii_digit() {
                            return Err(TokenizationError::UnexpectedDigitSeparator(Loc(
                                start_ln, inner_col,
                            )));
                        } else {
                            num.push(*next_char);
                        }
                    } else {
                        return Err(TokenizationError::UnexpectedDigitSeparator(Loc(
                            start_ln, inner_col,
                        )));
                    }
                } else {
                    break;
                }
            } else {
                break;
            }
            inner_col += 1;
            self.consume(1);
        }

        Ok(Token {
            r#type: TokenType::Number(num_type),
            value: Box::new(num),
            loc: Loc(start_ln, start_col),
        })
    }

    fn parse_binary(&mut self) -> Result<Token, TokenizationError> {
        let mut num = String::new();

        let start_ln = self.ln;
        let start_col = self.col;
        self.consume(2);

        while let Some(c) = self.get_next_char() {
            if matches!(*c, '1' | '0') {
                num.push(*c);
            } else {
                break;
            }
            self.consume(1);
        }

        Ok(Token {
            r#type: TokenType::Number(NumberType::Binary),
            value: Box::new(num),
            loc: Loc(start_ln, start_col),
        })
    }

    fn parse_hex(&mut self) -> Result<Token, TokenizationError> {
        let mut num = String::new();

        let start_ln = self.ln;
        let start_col = self.col;
        self.consume(2);

        while let Some(c) = self.get_next_char() {
            if c.is_ascii_hexdigit() {
                num.push(*c);
            } else {
                break;
            }
            self.consume(1);
        }

        Ok(Token {
            r#type: TokenType::Number(NumberType::Hex),
            value: Box::new(num),
            loc: Loc(start_ln, start_col),
        })
    }

    fn parse_octal(&mut self) -> Result<Token, TokenizationError> {
        let mut num = String::new();

        let start_ln = self.ln;
        let start_col = self.col;
        self.consume(2);

        while let Some(c) = self.get_next_char() {
            if matches!(*c, '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7') {
                num.push(*c);
            } else {
                break;
            }
            self.consume(1);
        }

        Ok(Token {
            r#type: TokenType::Number(NumberType::Octal),
            value: Box::new(num),
            loc: Loc(start_ln, start_col),
        })
    }

    fn parse_string(&mut self, delim: Option<char>) -> Result<Token, TokenizationError> {
        let mut string = String::new();
        let mut is_escaped = false;
        let start_ln = self.ln;
        let start_col = self.col;
        let delim = delim.unwrap_or('"');

        self.consume(1);

        while let Some(c) = self.get_next_char() {
            if *c == delim && !is_escaped {
                self.consume(1);
                break;
            }
            if *c == '\\' && !is_escaped {
                is_escaped = true;
                self.consume(1);
                continue;
            }
            if is_escaped {
                match *c {
                    'n' => {
                        string.push('\n');
                    }
                    '0' => {
                        string.push('\0');
                    }
                    't' => {
                        string.push('\t');
                    }
                    'r' => {
                        string.push('\r');
                    }
                    '\\' => {
                        string.push('\\');
                    }
                    _ => {
                        string.push_str(&format!("\\{c}"));
                    }
                }

                is_escaped = false;
            } else {
                string.push(*c);
            }
            self.consume(1);
        }

        Ok(Token {
            r#type: TokenType::String,
            value: Box::new(string),
            loc: Loc(start_ln, start_col),
        })
    }

    fn parse_char(&mut self) -> Result<Token, TokenizationError> {
        if self.config.parse_char_as_string {
            self.parse_string(Some('\''))
        } else {
            let mut chr = String::new();
            let mut is_escaped = false;
            let start_ln = self.ln;
            let start_col = self.col;

            self.consume(1);

            while let Some(c) = self.get_next_char() {
                if *c == '\'' && !is_escaped {
                    self.consume(1);
                    break;
                }
                if *c == '\\' && !is_escaped {
                    is_escaped = true;
                    self.consume(1);
                    continue;
                }
                chr.push(*c);
                is_escaped = false;
                self.consume(1);
            }

            let out_type = if !self.config.parse_char_as_string {
                TokenType::Char
            } else {
                TokenType::String
            };

            if out_type == TokenType::Char && chr.len() > 1 {
                Err(TokenizationError::NotAValidChar(Loc(start_ln, start_col)))
            } else {
                Ok(Token {
                    r#type: out_type,
                    value: Box::new(chr),
                    loc: Loc(start_ln, start_col),
                })
            }
        }
    }

    /// Tokenizes the input and returns a list of Tokens or a [TokenizationError] if parsing fails
    pub fn tokenize(mut self) -> Result<Vec<Token>, TokenizationError> {
        let mut tokens = vec![];
        while self.is_out_of_bound() != OutOfBound::Out {
            if self.is_out_of_bound() == OutOfBound::Empty {
                self.next_line();
            } else {
                let next_char = self.get_next_char().unwrap();
                if matches!(next_char, ' ') {
                    self.consume(1);
                } else if matches!(next_char, '0'..='9') && !self.config.ignore_numbers {
                    let first_digit = *self.get_next_char().unwrap();
                    if first_digit == '0' {
                        if let Some(c) = self.peek_tok() {
                            match *c {
                                'x' => {
                                    tokens.push(self.parse_hex()?);
                                }
                                'o' => {
                                    tokens.push(self.parse_octal()?);
                                }
                                'b' => {
                                    tokens.push(self.parse_binary()?);
                                }
                                '.' => {
                                    tokens.push(self.parse_float()?);
                                }
                                _ => {
                                    tokens.push(self.parse_number()?);
                                }
                            };
                        } else {
                            tokens.push(self.parse_number()?);
                        }
                    } else {
                        tokens.push(self.parse_number()?);
                    }
                } else if matches!(next_char, '.') {
                    if let Some(c) = self.peek_tok() {
                        if *c == '\n' {
                            tokens.push(Token {
                                r#type: TokenType::Symbol,
                                value: Box::new(".".into()),
                                loc: Loc(self.ln, self.col),
                            });
                            self.consume(1);
                        } else if c.is_ascii_digit() && !self.config.ignore_numbers {
                            tokens.push(self.parse_float()?);
                        } else {
                            tokens.push(Token {
                                r#type: TokenType::Symbol,
                                value: Box::new(".".into()),
                                loc: Loc(self.ln, self.col),
                            });
                            self.consume(1);
                        }
                    } else {
                        tokens.push(Token {
                            r#type: TokenType::Symbol,
                            value: Box::new(".".into()),
                            loc: Loc(self.ln, self.col),
                        });
                        self.consume(1);
                    }
                } else if matches!(next_char, '"') {
                    tokens.push(self.parse_string(None)?);
                } else if matches!(next_char, '\'') {
                    tokens.push(self.parse_char()?);
                } else if self.config.consider_as_symbols.contains(&next_char) {
                    tokens.push(Token {
                        r#type: TokenType::Symbol,
                        value: Box::new(next_char.to_string()),
                        loc: Loc(self.ln, self.col),
                    });
                    self.consume(1);
                } else if self.config.consider_as_operators.contains(&next_char) {
                    tokens.push(Token {
                        r#type: TokenType::Operator,
                        value: Box::new(next_char.to_string()),
                        loc: Loc(self.ln, self.col),
                    });
                    self.consume(1);
                } else {
                    tokens.push(self.parse_word()?)
                }
            }
        }

        Ok(tokens)
    }
}