tachyonfx 0.25.0

A ratatui library for creating shader-like effects in TUIs.
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
use core::{fmt, fmt::Formatter, ops::Range};

use anpa::{
    combinators::{
        attempt, count_consumed, get_parsed, many, many_to_vec, middle, no_separator, not_empty,
        or_diff, right, succeed, times,
    },
    core::{ParserExt, StrParser},
    greedy_or,
    number::float,
    or,
    parsers::{item_if, item_while, until},
    right, skip, take,
};

use crate::dsl::{expressions::ExprSpan, DslError};

/// Represents the type of a token in the DSL
#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) enum TokenKind {
    // literals
    Identifier,
    StringLiteral,
    IntLiteral,
    HexLiteral,
    FloatLiteral,

    // keywords
    Keyword,

    // structural
    LeftParen,    // (
    RightParen,   // )
    LeftBracket,  // [
    RightBracket, // ]
    LeftBrace,    // {
    RightBrace,   // }
    Comma,        // ,
    Dot,          // .
    Colon,        // :
    Semicolon,    // ;
    Equals,       // =
    Ampersand,    // &
    DoubleColon,  // ::
    Minus,        // -
    Bang,         // !

    // comments
    LineComment,  // discarded
    BlockComment, // discarded

    // special
    Whitespace, // discarded
}

/// A token in the DSL with its kind, value, and source position
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) struct Token<'a> {
    /// The type of token
    pub kind: TokenKind,
    /// The text content of the token
    pub text: &'a str,
    /// The byte range in the source text
    pub span: (u32, u32),
}

impl<'a> Token<'a> {
    /// Creates a new token with the given kind, text, and span
    pub(super) fn new(kind: TokenKind, text: &'a str, span: Range<usize>) -> Self {
        Self { kind, text, span: (span.start as _, span.end as _) }
    }

    /// Checks if the token's span contains the given index
    pub(super) fn contains_index(&self, idx: u32) -> bool {
        let (start, end) = self.span;
        idx >= start && idx < end
    }
}

pub(super) fn sanitize_tokens(tokens: Vec<Token>) -> Vec<Token> {
    const DISCARD: &[TokenKind] =
        &[TokenKind::Whitespace, TokenKind::LineComment, TokenKind::BlockComment];

    tokens
        .into_iter()
        .filter(|t| !DISCARD.contains(&t.kind))
        .collect::<Vec<_>>()
}

pub(super) fn tokenize(input: &str) -> Result<Vec<Token<'_>>, DslError> {
    let result = anpa::core::parse(tokens(), input);
    if !result.state.is_empty() {
        let consumed = input.len() - result.state.len();
        return Err(DslError::TokenizationError {
            location: ExprSpan::new(consumed.saturating_sub(1) as _, input.len() as _),
        });
    };

    result
        .result
        .map(|tokens| {
            let mut tokens = tokens;
            let mut offset = 0;
            for token in &mut tokens {
                let (start, end) = token.span;
                token.span = (start + offset, end + offset);
                offset = token.span.1;
            }

            tokens
        })
        .ok_or(DslError::OhNoError)
}

fn tokens<'a>() -> impl StrParser<'a, Vec<Token<'a>>> {
    let p = or!(
        whitespace(),
        double_colon(),
        hex_literal(),
        greedy_or!(int_literal(), float_literal()),
        structural(),
        string_literal(),
        keyword(),
        line_comment(),
        block_comment(),
        identifier(),
    );

    many_to_vec(p, true, no_separator())
}

fn snake_case_str<'a>() -> impl StrParser<'a, &'a str> {
    let p = not_empty(item_while(|c: char| c.is_ascii_alphanumeric() || c == '_'));
    get_parsed(p)
}

fn identifier<'a>() -> impl StrParser<'a, Token<'a>> {
    token(TokenKind::Identifier, snake_case_str())
}

fn keyword<'a>() -> impl StrParser<'a, Token<'a>> {
    let p = attempt(snake_case_str().map_if(|s| {
        Some(match s {
            "let" => TokenKind::Keyword,
            "return" => TokenKind::Keyword,
            "if" => TokenKind::Keyword,
            "else" => TokenKind::Keyword,
            "while" => TokenKind::Keyword,
            "for" => TokenKind::Keyword,
            "in" => TokenKind::Keyword,
            "break" => TokenKind::Keyword,
            "continue" => TokenKind::Keyword,
            "true" => TokenKind::Keyword,
            "false" => TokenKind::Keyword,
            _ => None?,
        })
    }));

    token(TokenKind::Keyword, get_parsed(p))
}

fn float_literal<'a>() -> impl StrParser<'a, Token<'a>> {
    let sign = attempt(succeed(or!(skip!('-'), skip!('+'))));
    let plain = float::<f32, char, &str, ()>();
    token(TokenKind::FloatLiteral, get_parsed(right!(sign, plain)))
}

fn int_literal<'a>() -> impl StrParser<'a, Token<'a>> {
    let sign = attempt(succeed(or!(skip!('-'), skip!('+'))));
    let plain = many(item_if(|c: char| c.is_ascii_digit()), false, no_separator());
    token(TokenKind::IntLiteral, get_parsed(right!(sign, plain)))
}

fn hex_literal<'a>() -> impl StrParser<'a, Token<'a>> {
    let hex = get_parsed(right(
        skip!("0x"),
        item_while(|c: char| c.is_ascii_hexdigit()),
    ));
    token(TokenKind::HexLiteral, hex)
}

fn string_literal<'a>() -> impl StrParser<'a, Token<'a>> {
    let unicode = right(
        skip!('u'),
        times(4, item_if(|c: char| c.is_ascii_hexdigit())),
    );
    let escaped = right(
        skip!('\\'),
        or_diff(unicode, item_if(|c: char| "\"\\/bfnrt".contains(c))),
    );
    let valid_char = item_if(|c: char| c != '"' && c != '\\' && !c.is_control());
    let not_end = or_diff(valid_char, escaped);

    let string_literal = middle(skip!('"'), many(not_end, true, no_separator()), skip!('"'));
    token(TokenKind::StringLiteral, string_literal)
}

fn line_comment<'a>() -> impl StrParser<'a, Token<'a>> {
    let line_comment = get_parsed(right!(skip!("//"), item_while(|c: char| c != '\n')));
    token(TokenKind::LineComment, line_comment)
}

fn block_comment<'a>() -> impl StrParser<'a, Token<'a>> {
    let block_comment = get_parsed(right!(skip!("/*"), until("*/")));
    token(TokenKind::BlockComment, block_comment)
}

fn double_colon<'a>() -> impl StrParser<'a, Token<'a>> {
    token(TokenKind::DoubleColon, take!("::"))
}

fn structural<'a>() -> impl StrParser<'a, Token<'a>> {
    let p = get_parsed(item_if(|c: char| "()[]{},.:;=&-!".contains(c))).map(|t: &str| {
        (t, match t {
            "(" => TokenKind::LeftParen,
            ")" => TokenKind::RightParen,
            "[" => TokenKind::LeftBracket,
            "]" => TokenKind::RightBracket,
            "{" => TokenKind::LeftBrace,
            "}" => TokenKind::RightBrace,
            "," => TokenKind::Comma,
            "." => TokenKind::Dot,
            ":" => TokenKind::Colon,
            ";" => TokenKind::Semicolon,
            "=" => TokenKind::Equals,
            "&" => TokenKind::Ampersand,
            "-" => TokenKind::Minus,
            "!" => TokenKind::Bang,
            _ => unreachable!(),
        })
    });

    count_consumed(p).map(|(len, (s, kind))| Token::new(kind, s, 0..len))
}

fn whitespace<'a>() -> impl StrParser<'a, Token<'a>> {
    token(
        TokenKind::Whitespace,
        not_empty(anpa::whitespace::whitespace()),
    )
}

fn token<'a>(kind: TokenKind, p: impl StrParser<'a, &'a str>) -> impl StrParser<'a, Token<'a>> {
    count_consumed(p).map(move |(len, s): (_, &str)| Token::new(kind, s, 0..len))
}

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

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

    use super::*;

    // Enhanced helper function to test both token kinds and text
    fn test_tokens(input: &str, expected_tokens: &[(TokenKind, &str)]) {
        let result = tokenize(input);
        assert!(result.is_ok(), "Failed to parse tokens from input: {input}");

        let tokens = result.unwrap();

        for (token, (expected_kind, expected_text)) in tokens.iter().zip(expected_tokens.iter()) {
            assert_eq!(
                token.kind, *expected_kind,
                "Expected token kind {expected_kind:?}, but got {:?} for token text: '{}'",
                token.kind, token.text
            );

            assert_eq!(
                token.text,
                *expected_text,
                "Expected token text '{expected_text}', but got '{}' for token kind: {kind:?}",
                token.text,
                kind = token.kind
            );
        }

        assert_eq!(
            tokens.len(),
            expected_tokens.len(),
            "Expected {} tokens, but got {} for input: {}",
            expected_tokens.len(),
            tokens.len(),
            input
        );
    }

    #[test]
    fn test_identifiers() {
        test_tokens("identifier snake_case_id _leading_underscore", &[
            (Identifier, "identifier"),
            (Whitespace, " "),
            (Identifier, "snake_case_id"),
            (Whitespace, " "),
            (Identifier, "_leading_underscore"),
        ]);
    }

    #[test]
    fn test_keywords() {
        test_tokens("let if else while for in break continue true false", &[
            (Keyword, "let"),
            (Whitespace, " "),
            (Keyword, "if"),
            (Whitespace, " "),
            (Keyword, "else"),
            (Whitespace, " "),
            (Keyword, "while"),
            (Whitespace, " "),
            (Keyword, "for"),
            (Whitespace, " "),
            (Keyword, "in"),
            (Whitespace, " "),
            (Keyword, "break"),
            (Whitespace, " "),
            (Keyword, "continue"),
            (Whitespace, " "),
            (Keyword, "true"),
            (Whitespace, " "),
            (Keyword, "false"),
        ]);
    }

    #[test]
    #[allow(clippy::approx_constant)]
    fn test_literals() {
        test_tokens("123 -456 3.14 - -2.718 0x1a2b \"string literal\"", &[
            (IntLiteral, "123"),
            (Whitespace, " "),
            (IntLiteral, "-456"),
            (Whitespace, " "),
            (FloatLiteral, "3.14"),
            (Whitespace, " "),
            (Minus, "-"),
            (Whitespace, " "),
            (FloatLiteral, "-2.718"),
            (Whitespace, " "),
            (HexLiteral, "0x1a2b"),
            (Whitespace, " "),
            (StringLiteral, "string literal"),
        ]);
    }

    #[test]
    fn test_string_literals_with_escapes() {
        test_tokens(
            r#""simple" "with \"escaped quotes\"" "with \\backslash" "with \u1234 unicode""#,
            &[
                (StringLiteral, r#"simple"#),
                (Whitespace, " "),
                (StringLiteral, r#"with \"escaped quotes\""#),
                (Whitespace, " "),
                (StringLiteral, r#"with \\backslash"#),
                (Whitespace, " "),
                (StringLiteral, r#"with \u1234 unicode"#),
            ],
        );
    }

    #[test]
    fn test_operators_and_punctuation() {
        test_tokens("( ) [ ] { } , . : ; = & :: -", &[
            (LeftParen, "("),
            (Whitespace, " "),
            (RightParen, ")"),
            (Whitespace, " "),
            (LeftBracket, "["),
            (Whitespace, " "),
            (RightBracket, "]"),
            (Whitespace, " "),
            (LeftBrace, "{"),
            (Whitespace, " "),
            (RightBrace, "}"),
            (Whitespace, " "),
            (Comma, ","),
            (Whitespace, " "),
            (Dot, "."),
            (Whitespace, " "),
            (Colon, ":"),
            (Whitespace, " "),
            (Semicolon, ";"),
            (Whitespace, " "),
            (Equals, "="),
            (Whitespace, " "),
            (Ampersand, "&"),
            (Whitespace, " "),
            (DoubleColon, "::"),
            (Whitespace, " "),
            (Minus, "-"),
        ]);
    }

    #[test]
    fn test_comments() {
        let line_comment = "// This is a line comment";
        let block_comment = "/* This is a block comment */";
        test_tokens(&format!("{line_comment}\n{block_comment}"), &[
            (LineComment, line_comment),
            (Whitespace, "\n"),
            (BlockComment, block_comment),
        ]);
    }

    #[test]
    fn test_whitespace() {
        let ws = "  \t\n\r  ";
        test_tokens(ws, &[(Whitespace, ws)]);
    }

    #[test]
    #[allow(clippy::approx_constant)]
    fn test_mixed_tokens() {
        let input = "let x = 42; // assign value\nfn::call(true, 3.14);";
        test_tokens(input, &[
            (Keyword, "let"),
            (Whitespace, " "),
            (Identifier, "x"),
            (Whitespace, " "),
            (Equals, "="),
            (Whitespace, " "),
            (IntLiteral, "42"),
            (Semicolon, ";"),
            (Whitespace, " "),
            (LineComment, "// assign value"),
            (Whitespace, "\n"),
            (Identifier, "fn"),
            (DoubleColon, "::"),
            (Identifier, "call"),
            (LeftParen, "("),
            (Keyword, "true"),
            (Comma, ","),
            (Whitespace, " "),
            (FloatLiteral, "3.14"),
            (RightParen, ")"),
            (Semicolon, ";"),
        ]);
    }

    #[test]
    fn test_token_spans() {
        let input = "x = 42";
        let result = tokenize(input);
        let tokens = result.unwrap();

        assert_eq!(tokens[0].span, (0, 1)); // "x"
        assert_eq!(tokens[1].span, (1, 2)); // " "
        assert_eq!(tokens[2].span, (2, 3)); // "="
        assert_eq!(tokens[3].span, (3, 4)); // " "
        assert_eq!(tokens[4].span, (4, 6)); // "42"

        // Check that token text matches the spans
        for token in &tokens {
            let (start, end) = token.span;
            assert_eq!(token.text, &input[start as _..end as _]);
        }
    }

    #[test]
    fn test_complex_expression() {
        let input = "fx::fade_to(Color::Red, (500, CircOut))";
        test_tokens(input, &[
            (Identifier, "fx"),
            (DoubleColon, "::"),
            (Identifier, "fade_to"),
            (LeftParen, "("),
            (Identifier, "Color"),
            (DoubleColon, "::"),
            (Identifier, "Red"),
            (Comma, ","),
            (Whitespace, " "),
            (LeftParen, "("),
            (IntLiteral, "500"),
            (Comma, ","),
            (Whitespace, " "),
            (Identifier, "CircOut"),
            (RightParen, ")"),
            (RightParen, ")"),
        ]);
    }

    #[test]
    fn test_effect_declaration() {
        let input =
            r#"let fade /* yolo */ = fx::fade_to_fg(Color::from_u32(0x504945), (1000, CircOut));"#;
        test_tokens(input, &[
            (Keyword, "let"),
            (Whitespace, " "),
            (Identifier, "fade"),
            (Whitespace, " "),
            (BlockComment, "/* yolo */"),
            (Whitespace, " "),
            (Equals, "="),
            (Whitespace, " "),
            (Identifier, "fx"),
            (DoubleColon, "::"),
            (Identifier, "fade_to_fg"),
            (LeftParen, "("),
            (Identifier, "Color"),
            (DoubleColon, "::"),
            (Identifier, "from_u32"),
            (LeftParen, "("),
            (HexLiteral, "0x504945"),
            (RightParen, ")"),
            (Comma, ","),
            (Whitespace, " "),
            (LeftParen, "("),
            (IntLiteral, "1000"),
            (Comma, ","),
            (Whitespace, " "),
            (Identifier, "CircOut"),
            (RightParen, ")"),
            (RightParen, ")"),
            (Semicolon, ";"),
        ]);
    }

    #[test]
    fn test_edge_cases() {
        // Empty input
        let result = tokenize("");
        assert_eq!(result.unwrap().len(), 0);

        // Only whitespace
        test_tokens(" \t\n", &[(Whitespace, " \t\n")]);

        // Only comments
        let line_comment = "// comment";
        test_tokens(line_comment, &[(LineComment, line_comment)]);

        let block_comment = "/* comment */";
        test_tokens(block_comment, &[(BlockComment, block_comment)]);

        // Unicode characters in string literals
        test_tokens("\"Unicode: \u{1234} \u{5678}\"", &[(
            StringLiteral,
            "Unicode: \u{1234} \u{5678}",
        )]);
    }

    #[test]
    fn test_debug_implementation() {
        // Verify that TokenKind implements Debug correctly
        assert_eq!(format!("{Identifier:?}"), "Identifier");
        assert_eq!(format!("{StringLiteral:?}"), "StringLiteral");
        assert_eq!(format!("{LeftBrace:?}"), "LeftBrace");
    }
}