mimium-lang 4.0.0-alpha

mimium(minimal-musical-medium) an infrastructural programming language for sound and music.
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
/// Tokenizer for mimium language using chumsky
/// Converts source text into a sequence of position-aware tokens
use super::token::{Token, TokenKind};
use chumsky::input::StrInput;
use chumsky::prelude::*;

type LexerError<'src> = chumsky::extra::Err<Rich<'src, char, SimpleSpan>>;

/// Parser for whitespace (not including newlines)
fn whitespace_parser<'src, I>() -> impl Parser<'src, I, TokenKind, LexerError<'src>> + Clone
where
    I: StrInput<'src, Token = char, Span = SimpleSpan, Slice = &'src str>,
{
    one_of(" \t\r")
        .repeated()
        .at_least(1)
        .to(TokenKind::Whitespace)
}

/// Parser for linebreaks
fn linebreak_parser<'src, I>() -> impl Parser<'src, I, TokenKind, LexerError<'src>> + Clone
where
    I: StrInput<'src, Token = char, Span = SimpleSpan, Slice = &'src str>,
{
    text::newline()
        .repeated()
        .at_least(1)
        .to(TokenKind::LineBreak)
}

/// Parser for comments
fn comment_parser<'src, I>() -> impl Parser<'src, I, TokenKind, LexerError<'src>> + Clone
where
    I: StrInput<'src, Token = char, Span = SimpleSpan, Slice = &'src str>,
{
    let endline = text::newline().or(end());
    let single_line = just("//")
        .ignore_then(any().and_is(endline.not()).repeated())
        .then_ignore(endline.rewind())
        .to(TokenKind::SingleLineComment);

    let multi_line = just("/*")
        .ignore_then(any().and_is(just("*/").not()).repeated())
        .then_ignore(just("*/"))
        .to(TokenKind::MultiLineComment);

    single_line.or(multi_line)
}

/// Parser for string literals
fn string_parser<'src, I>() -> impl Parser<'src, I, TokenKind, LexerError<'src>> + Clone
where
    I: StrInput<'src, Token = char, Span = SimpleSpan, Slice = &'src str>,
{
    none_of('"')
        .repeated()
        .delimited_by(just('"'), just('"'))
        .to(TokenKind::Str)
}

/// Parser for numbers
fn number_parser<'src, I>() -> impl Parser<'src, I, TokenKind, LexerError<'src>> + Clone
where
    I: StrInput<'src, Token = char, Span = SimpleSpan, Slice = &'src str>,
{
    let float = text::int::<I, _>(10)
        .then_ignore(just('.'))
        .then(text::digits::<I, _>(10))
        .then_ignore(just('.').not().ignored().or(end()).rewind())
        .to(TokenKind::Float);

    let int = text::int::<I, LexerError<'src>>(10).to(TokenKind::Int);

    float.or(int)
}

/// Parser for operators
fn operator_parser<'src, I>() -> impl Parser<'src, I, TokenKind, LexerError<'src>> + Clone
where
    I: StrInput<'src, Token = char, Span = SimpleSpan, Slice = &'src str>,
{
    choice((
        just("->").to(TokenKind::Arrow),
        just("<-").to(TokenKind::LeftArrow),
        just("=>").to(TokenKind::FatArrow),
        just("==").to(TokenKind::OpEqual),
        just("!=").to(TokenKind::OpNotEqual),
        just("<=").to(TokenKind::OpLessEqual),
        just(">=").to(TokenKind::OpGreaterEqual),
        just("&&").to(TokenKind::OpAnd),
        just("||").to(TokenKind::OpOr),
        just("|>").to(TokenKind::OpPipe),
        just("+").to(TokenKind::OpSum),
        just("-").to(TokenKind::OpMinus),
        just("*").to(TokenKind::OpProduct),
        just("/").to(TokenKind::OpDivide),
        just("%").to(TokenKind::OpModulo),
        just("^").to(TokenKind::OpExponent),
        just("@").to(TokenKind::OpAt),
        just("<").to(TokenKind::OpLessThan),
        just(">").to(TokenKind::OpGreaterThan),
        just("=").to(TokenKind::Assign),
        just("!").to(TokenKind::MacroExpand),
    ))
}

/// Parser for punctuation
fn punctuation_parser<'src, I>() -> impl Parser<'src, I, TokenKind, LexerError<'src>> + Clone
where
    I: StrInput<'src, Token = char, Span = SimpleSpan, Slice = &'src str>,
{
    choice((
        just("::").to(TokenKind::DoubleColon),
        just("..").to(TokenKind::DoubleDot),
        just(".").to(TokenKind::Dot),
        just(",").to(TokenKind::Comma),
        just(":").to(TokenKind::Colon),
        just(";").to(TokenKind::LineBreak), // Semicolon treated as linebreak
        just("(").to(TokenKind::ParenBegin),
        just(")").to(TokenKind::ParenEnd),
        just("[").to(TokenKind::ArrayBegin),
        just("]").to(TokenKind::ArrayEnd),
        just("{").to(TokenKind::BlockBegin),
        just("}").to(TokenKind::BlockEnd),
        just("`").to(TokenKind::BackQuote),
        just("$").to(TokenKind::Dollar),
        just("#").to(TokenKind::Sharp),
        just("|").to(TokenKind::LambdaArgBeginEnd),
    ))
}

/// Parser for identifiers and keywords
fn identifier_parser<'src, I>() -> impl Parser<'src, I, TokenKind, LexerError<'src>> + Clone
where
    I: StrInput<'src, Token = char, Span = SimpleSpan, Slice = &'src str>,
{
    // NOTE: MacroExpand (!) is now parsed as a separate operator token
    // The parser will handle Ident followed by MacroExpand

    text::ident()
        .to_slice()
        .map(|ident: &'src str| match ident {
            "fn" => TokenKind::Function,
            "macro" => TokenKind::Macro,
            "self" => TokenKind::SelfLit,
            "now" => TokenKind::Now,
            "samplerate" => TokenKind::SampleRate,
            "let" => TokenKind::Let,
            "letrec" => TokenKind::LetRec,
            "if" => TokenKind::If,
            "else" => TokenKind::Else,
            "match" => TokenKind::Match,
            "float" => TokenKind::FloatType,
            "int" => TokenKind::IntegerType,
            "string" => TokenKind::StringType,
            "struct" => TokenKind::StructType,
            "include" => TokenKind::Include,
            "stage" => TokenKind::StageKwd,
            "main" => TokenKind::Main,
            "mod" => TokenKind::Mod,
            "use" => TokenKind::Use,
            "pub" => TokenKind::Pub,
            "type" => TokenKind::Type,
            "alias" => TokenKind::Alias,
            "rec" => TokenKind::Rec,
            "_" => TokenKind::PlaceHolder,
            _ => TokenKind::Ident,
        })
}
/// Main tokenizer that combines all parsers
fn token_parser<'src, I>() -> impl Parser<'src, I, TokenKind, LexerError<'src>> + Clone
where
    I: StrInput<'src, Token = char, Span = SimpleSpan, Slice = &'src str>,
{
    choice((
        comment_parser(),
        linebreak_parser(),
        whitespace_parser(),
        string_parser(),
        number_parser(),
        identifier_parser(),
        operator_parser(), // Try operators before punctuation
        punctuation_parser(),
    ))
}

fn split_projection_float_tokens(tokens: Vec<Token>, source: &str) -> Vec<Token> {
    let mut result: Vec<Token> = Vec::with_capacity(tokens.len());
    tokens.into_iter().for_each(|token| {
        let maybe_split = result
            .last()
            .filter(|prev| prev.kind == TokenKind::Dot && prev.end() == token.start)
            .and_then(|_| {
                if token.kind != TokenKind::Float {
                    return None;
                }
                token.text(source).split_once('.').and_then(|(head, tail)| {
                    let is_digit_only =
                        |s: &str| !s.is_empty() && s.chars().all(|c| c.is_ascii_digit());
                    if is_digit_only(head) && is_digit_only(tail) {
                        Some((head.len(), tail.len()))
                    } else {
                        None
                    }
                })
            });

        if let Some((head_len, tail_len)) = maybe_split {
            let head = Token::new(TokenKind::Int, token.start, head_len);
            let dot = Token::new(TokenKind::Dot, token.start + head_len, 1);
            let tail = Token::new(TokenKind::Int, token.start + head_len + 1, tail_len);
            result.push(head);
            result.push(dot);
            result.push(tail);
        } else {
            result.push(token);
        }
    });
    result
}

/// Tokenize the source text into a sequence of tokens
/// Uses chumsky's error recovery to continue parsing after errors
pub fn tokenize(source: &str) -> Vec<Token> {
    // Error token parser - matches any character and creates an error token
    let error_token = any().map_with(|_, e| {
        let span: SimpleSpan = e.span();
        Token::new(TokenKind::Error, span.start, span.end - span.start)
    });

    let lexer = token_parser()
        .map_with(|kind, e| {
            let span: SimpleSpan = e.span();
            Token::new(kind, span.start, span.end - span.start)
        })
        // Try normal parsing, if it fails, create an error token for the invalid character
        .or(error_token)
        .repeated()
        .collect::<Vec<_>>()
        .then_ignore(end());

    // Use into_output_errors to get both tokens and errors
    let (tokens, errors) = lexer.parse(source).into_output_errors();

    // Log errors for debugging
    if !errors.is_empty() {
        eprintln!("Tokenization recovered from {} errors:", errors.len());
        for error in &errors {
            eprintln!("  - {error:?}");
        }
    }

    match tokens {
        Some(mut tokens) => {
            tokens = split_projection_float_tokens(tokens, source);
            // Add EOF token
            tokens.push(Token::new(TokenKind::Eof, source.len(), 0));
            tokens
        }
        None => {
            // If parsing completely failed, return just EOF
            vec![Token::new(TokenKind::Eof, source.len(), 0)]
        }
    }
}

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

    #[test]
    fn test_tokenize_simple() {
        let source = "fn dsp() { 42 }";
        let tokens = tokenize(source);

        assert_eq!(tokens[0].kind, TokenKind::Function);
        assert_eq!(tokens[0].text(source), "fn");

        assert_eq!(tokens[1].kind, TokenKind::Whitespace);

        assert_eq!(tokens[2].kind, TokenKind::Ident);
        assert_eq!(tokens[2].text(source), "dsp");
    }

    #[test]
    fn test_tokenize_numbers() {
        let source = "42 3.14";
        let tokens = tokenize(source);

        assert_eq!(tokens[0].kind, TokenKind::Int);
        assert_eq!(tokens[0].text(source), "42");

        assert_eq!(tokens[2].kind, TokenKind::Float);
        assert_eq!(tokens[2].text(source), "3.14");
    }

    #[test]
    fn test_tokenize_projection_chain_numbers() {
        let source = "c.0.0";
        let tokens = tokenize(source);

        let kinds: Vec<_> = tokens
            .iter()
            .filter(|t| !matches!(t.kind, TokenKind::Whitespace | TokenKind::Eof))
            .map(|t| t.kind)
            .collect();

        assert_eq!(
            kinds,
            vec![
                TokenKind::Ident,
                TokenKind::Dot,
                TokenKind::Int,
                TokenKind::Dot,
                TokenKind::Int
            ]
        );
    }

    #[test]
    fn test_tokenize_string() {
        let source = r#""hello world""#;
        let tokens = tokenize(source);

        assert_eq!(tokens[0].kind, TokenKind::Str);
        assert_eq!(tokens[0].text(source), r#""hello world""#);
    }

    #[test]
    fn test_tokenize_comments() {
        let source = "// single line\n/* multi\nline */";
        let tokens = tokenize(source);

        assert_eq!(tokens[0].kind, TokenKind::SingleLineComment);
        assert_eq!(tokens[0].text(source), "// single line");

        assert_eq!(tokens[1].kind, TokenKind::LineBreak);

        assert_eq!(tokens[2].kind, TokenKind::MultiLineComment);
        assert_eq!(tokens[2].text(source), "/* multi\nline */");
    }

    #[test]
    fn test_tokenize_operators() {
        let source = "+ - * / == != < <= > >= && || |>";
        let tokens = tokenize(source);

        let op_kinds: Vec<_> = tokens
            .iter()
            .filter(|t| !matches!(t.kind, TokenKind::Whitespace | TokenKind::Eof))
            .map(|t| t.kind)
            .collect();

        assert_eq!(
            op_kinds,
            vec![
                TokenKind::OpSum,
                TokenKind::OpMinus,
                TokenKind::OpProduct,
                TokenKind::OpDivide,
                TokenKind::OpEqual,
                TokenKind::OpNotEqual,
                TokenKind::OpLessThan,
                TokenKind::OpLessEqual,
                TokenKind::OpGreaterThan,
                TokenKind::OpGreaterEqual,
                TokenKind::OpAnd,
                TokenKind::OpOr,
                TokenKind::OpPipe,
            ]
        );
    }

    #[test]
    fn test_trivia_detection() {
        let source = "fn // comment\n dsp";
        let tokens = tokenize(source);

        assert!(!tokens[0].is_trivia()); // fn
        assert!(tokens[1].is_trivia()); // whitespace
        assert!(tokens[2].is_trivia()); // comment
        assert!(tokens[3].is_trivia()); // linebreak
        assert!(tokens[4].is_trivia()); // whitespace
        assert!(!tokens[5].is_trivia()); // dsp
    }

    #[test]
    fn test_error_recovery() {
        // Test with invalid character (unicode character that's not in grammar)
        let source = "fn dsp() { 42 § }";
        let tokens = tokenize(source);

        // Should recover and continue parsing
        let token_kinds: Vec<_> = tokens
            .iter()
            .filter(|t| !t.is_trivia() && t.kind != TokenKind::Eof)
            .map(|t| t.kind)
            .collect();

        // Should have: fn, dsp, (, ), {, 42, Error, }
        assert!(token_kinds.contains(&TokenKind::Function));
        assert!(token_kinds.contains(&TokenKind::Ident));
        assert!(token_kinds.contains(&TokenKind::Int));
        assert!(token_kinds.contains(&TokenKind::Error));
        assert!(token_kinds.contains(&TokenKind::BlockBegin));
        assert!(token_kinds.contains(&TokenKind::BlockEnd));
    }

    #[test]
    fn test_error_recovery_multiple_errors() {
        // Test with multiple invalid characters
        let source = "fn § dsp() { © }";
        let tokens = tokenize(source);

        let error_count = tokens.iter().filter(|t| t.is_error()).count();

        // Should have 2 error tokens
        assert_eq!(error_count, 2);

        // Should still parse valid tokens
        let has_fn = tokens.iter().any(|t| t.kind == TokenKind::Function);
        let has_dsp = tokens.iter().any(|t| t.kind == TokenKind::Ident);
        assert!(has_fn);
        assert!(has_dsp);
    }

    #[test]
    fn test_tokenize_module_keywords() {
        let source = "mod use pub";
        let tokens = tokenize(source);

        let kinds: Vec<_> = tokens
            .iter()
            .filter(|t| !matches!(t.kind, TokenKind::Whitespace | TokenKind::Eof))
            .map(|t| t.kind)
            .collect();

        assert_eq!(kinds, vec![TokenKind::Mod, TokenKind::Use, TokenKind::Pub]);
    }

    #[test]
    fn test_tokenize_double_colon() {
        let source = "mod::path::name";
        let tokens = tokenize(source);

        let kinds: Vec<_> = tokens
            .iter()
            .filter(|t| !matches!(t.kind, TokenKind::Whitespace | TokenKind::Eof))
            .map(|t| t.kind)
            .collect();

        assert_eq!(
            kinds,
            vec![
                TokenKind::Mod,
                TokenKind::DoubleColon,
                TokenKind::Ident,
                TokenKind::DoubleColon,
                TokenKind::Ident
            ]
        );
    }

    #[test]
    fn test_tokenize_module_declaration() {
        let source = "mod mymod { pub fn foo() { 42 } }";
        let tokens = tokenize(source);

        let has_mod = tokens.iter().any(|t| t.kind == TokenKind::Mod);
        let has_pub = tokens.iter().any(|t| t.kind == TokenKind::Pub);
        let has_fn = tokens.iter().any(|t| t.kind == TokenKind::Function);
        let has_ident = tokens.iter().any(|t| t.kind == TokenKind::Ident);

        assert!(has_mod);
        assert!(has_pub);
        assert!(has_fn);
        assert!(has_ident);
    }

    #[test]
    fn test_tokenize_use_statement() {
        let source = "use modA::modB::func";
        let tokens = tokenize(source);

        let kinds: Vec<_> = tokens
            .iter()
            .filter(|t| !matches!(t.kind, TokenKind::Whitespace | TokenKind::Eof))
            .map(|t| t.kind)
            .collect();

        assert_eq!(
            kinds,
            vec![
                TokenKind::Use,
                TokenKind::Ident,
                TokenKind::DoubleColon,
                TokenKind::Ident,
                TokenKind::DoubleColon,
                TokenKind::Ident
            ]
        );
    }
}