formalang 0.0.4-beta

FormaLang compiler frontend: lexer, parser, semantic analyzer, and IR lowering.
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
// Parser for FormaLang using chumsky parser combinator library
//
// This module converts tokens into an Abstract Syntax Tree (AST).
// It uses recursive descent parsing with excellent error recovery.

mod defs;
mod exprs;
mod span;
mod types;

use chumsky::input::{Stream, ValueInput};
use chumsky::prelude::*;

use crate::ast::{
    BlockStatement, Definition, Expr, File, Ident, LetBinding, Literal, Statement, UseItems,
    UseStmt, Visibility,
};
use crate::lexer::Token;
use crate::location::Span as CustomSpan;

use defs::{binding_pattern_parser, definition_parser};
use exprs::expr_parser;
use span::fill_file_spans;
use types::type_parser;

/// Main entry point for parsing
/// Returns the parsed AST or a vector of (`error_message`, span) tuples
///
/// # Errors
///
/// Returns a vector of `(message, span)` pairs if the token stream contains parse errors.
pub fn parse_file(tokens: &[(Token, CustomSpan)]) -> Result<File, Vec<(String, CustomSpan)>> {
    parse_file_internal(tokens, None)
}

/// Parse with source text for better error positions
///
/// # Errors
///
/// Returns a vector of `(message, span)` pairs if the token stream contains parse errors.
pub fn parse_file_with_source(
    tokens: &[(Token, CustomSpan)],
    source: &str,
) -> Result<File, Vec<(String, CustomSpan)>> {
    parse_file_internal(tokens, Some(source))
}

/// Internal parsing implementation
fn parse_file_internal(
    tokens: &[(Token, CustomSpan)],
    source: Option<&str>,
) -> Result<File, Vec<(String, CustomSpan)>> {
    // Convert our custom spans to SimpleSpan for chumsky
    let token_iter = tokens.iter().map(|(tok, span)| {
        (
            tok.clone(),
            SimpleSpan::new(span.start.offset, span.end.offset),
        )
    });

    // Create a stream with end-of-input span
    let end_offset = tokens.last().map_or(0, |(_, s)| s.end.offset);
    let end_span: SimpleSpan = (end_offset..end_offset).into();
    let token_stream = Stream::from_iter(token_iter).map(end_span, |(t, s)| (t, s));

    // Parse
    let mut file = file_parser()
        .parse(token_stream)
        .into_result()
        .map_err(|errors| {
            errors
                .into_iter()
                .map(|e| {
                    let simple_span = e.span();
                    let message = format_parse_error(&e);
                    let custom_span = source.map_or_else(
                        || {
                            // Use span from tokens if available
                            tokens
                                .iter()
                                .find(|(_, span)| {
                                    span.start.offset == simple_span.start
                                        && span.end.offset == simple_span.end
                                })
                                .map_or_else(|| span_from_simple(*simple_span), |(_, span)| *span)
                        },
                        |src| {
                            // Compute line/column from source text
                            CustomSpan::from_range_with_source(
                                simple_span.start,
                                simple_span.end,
                                src,
                            )
                        },
                    );
                    (message, custom_span)
                })
                .collect::<Vec<_>>()
        })?;

    // Post-process: Fill in line/column info for all spans if source is provided
    if let Some(src) = source {
        fill_file_spans(&mut file, src);
    }

    Ok(file)
}

/// Format a parse error with lowercase keywords and readable token names
#[expect(
    clippy::wildcard_enum_match_arm,
    reason = "RichPattern is defined in the chumsky library and cannot be exhaustively enumerated"
)]
fn format_parse_error(error: &Rich<'_, Token>) -> String {
    use chumsky::error::RichPattern;

    let found = error
        .found()
        .map_or_else(|| "end of input".to_string(), |t| format!("{t}"));

    let expected: Vec<String> = error
        .expected()
        .map(|exp| match exp {
            RichPattern::Token(tok) => {
                // tok is a Maybe<Token, &Token> which derefs to &Token
                format!("{}", &**tok)
            }
            RichPattern::Label(label) => label.to_string(),
            RichPattern::EndOfInput => "end of input".to_string(),
            _ => "<unknown>".to_string(),
        })
        .collect();

    let span = error.span();

    if expected.is_empty() {
        format!("found {} at {}..{}", found, span.start, span.end)
    } else if expected.len() == 1 {
        #[expect(
            clippy::indexing_slicing,
            reason = "bounds checked above: expected.len() == 1"
        )]
        let first = &expected[0];
        format!(
            "found {} at {}..{}, expected {}",
            found, span.start, span.end, first
        )
    } else {
        format!(
            "found {} at {}..{}, expected one of: {}",
            found,
            span.start,
            span.end,
            expected.join(", ")
        )
    }
}

/// Parse a complete file.
///
/// Each statement carries a recovery strategy that, on parse failure,
/// skips input tokens until the parser finds a token that can legally
/// start the next top-level statement (or end of input). This lets the
/// parser surface multiple independent syntax errors in one pass
/// instead of bailing on the first bad token.
fn file_parser<'tokens, I>(
) -> impl Parser<'tokens, I, File, extra::Err<Rich<'tokens, Token>>> + Clone
where
    I: ValueInput<'tokens, Token = Token, Span = SimpleSpan>,
{
    let statement_start = one_of([
        Token::Use,
        Token::Let,
        Token::Pub,
        Token::Struct,
        Token::Enum,
        Token::Trait,
        Token::Impl,
        Token::Fn,
        Token::Extern,
        Token::Module,
    ])
    .ignored();

    statement_parser()
        .recover_with(skip_then_retry_until(
            any().ignored(),
            statement_start.rewind().ignored().or(end()),
        ))
        .repeated()
        .collect::<Vec<_>>()
        .map_with(|statements, e| File {
            format_version: crate::ast::FORMAT_VERSION,
            statements,
            span: span_from_simple(e.span()),
        })
}

/// Parse a top-level statement, optionally preceded by `///` doc-comment
/// lines. The collected doc text is attached to the resulting definition
/// or let binding.
fn statement_parser<'tokens, I>(
) -> impl Parser<'tokens, I, Statement, extra::Err<Rich<'tokens, Token>>> + Clone
where
    I: ValueInput<'tokens, Token = Token, Span = SimpleSpan>,
{
    doc_comments_parser()
        .then(choice((
            use_stmt_parser().map(Statement::Use),
            let_binding_parser().map(|lb| Statement::Let(Box::new(lb))),
            definition_parser().map(|d| Statement::Definition(Box::new(d))),
        )))
        .map(|(doc, stmt)| attach_doc_to_statement(doc, stmt))
        .labelled("statement (use, let, or definition: struct, enum, trait, impl, fn, extern, mod)")
}

/// Consume zero or more leading `///` doc-comment lines and join them
/// with newlines. Returns `None` when no doc comments precede the next
/// item. Inner `//!` comments are skipped at this level — they belong
/// to the enclosing scope and are handled by the file-level parser.
pub(super) fn doc_comments_parser<'tokens, I>(
) -> impl Parser<'tokens, I, Option<String>, extra::Err<Rich<'tokens, Token>>> + Clone
where
    I: ValueInput<'tokens, Token = Token, Span = SimpleSpan>,
{
    select! { Token::DocComment(s) => s }
        .repeated()
        .collect::<Vec<_>>()
        .map(|lines| {
            if lines.is_empty() {
                None
            } else {
                Some(lines.join("\n"))
            }
        })
}

/// Attach a captured doc-comment string to whichever AST node the
/// statement carries. `Use` statements don't currently support docs and
/// silently drop the captured text.
fn attach_doc_to_statement(doc: Option<String>, stmt: Statement) -> Statement {
    let Some(doc) = doc else {
        return stmt;
    };
    match stmt {
        Statement::Let(mut lb) => {
            lb.doc = Some(doc);
            Statement::Let(lb)
        }
        Statement::Definition(def) => {
            Statement::Definition(Box::new(attach_doc_to_definition(doc, *def)))
        }
        Statement::Use(_) => stmt,
    }
}

fn attach_doc_to_definition(doc: String, def: Definition) -> Definition {
    match def {
        Definition::Function(mut f) => {
            f.doc = Some(doc);
            Definition::Function(f)
        }
        Definition::Struct(mut s) => {
            s.doc = Some(doc);
            Definition::Struct(s)
        }
        Definition::Trait(mut t) => {
            t.doc = Some(doc);
            Definition::Trait(t)
        }
        Definition::Enum(mut e) => {
            e.doc = Some(doc);
            Definition::Enum(e)
        }
        Definition::Impl(mut i) => {
            i.doc = Some(doc);
            Definition::Impl(i)
        }
        Definition::Module(mut m) => {
            m.doc = Some(doc);
            Definition::Module(m)
        }
    }
}

/// Parse a use statement
fn use_stmt_parser<'tokens, I>(
) -> impl Parser<'tokens, I, UseStmt, extra::Err<Rich<'tokens, Token>>> + Clone
where
    I: ValueInput<'tokens, Token = Token, Span = SimpleSpan>,
{
    visibility_parser()
        .then_ignore(just(Token::Use))
        .then(
            ident_parser()
                .separated_by(just(Token::DoubleColon))
                .at_least(1)
                .collect::<Vec<_>>(),
        )
        .then(
            just(Token::DoubleColon)
                .ignore_then(use_items_parser())
                .or_not(),
        )
        .map_with(|((visibility, mut path), items), e| {
            // When no trailing `::<items>` block is present, treat the last
            // path segment as the imported item. The path parser above is
            // `at_least(1)`, so `path.pop()` always yields a segment — the
            // fallback is only reachable if that invariant is ever broken,
            // in which case we surface an empty-name ident that downstream
            // symbol lookup will report as `ItemNotFound`.
            let items = items.unwrap_or_else(|| {
                path.pop().map_or_else(
                    || {
                        UseItems::Single(Ident {
                            name: String::new(),
                            span: CustomSpan::default(),
                        })
                    },
                    UseItems::Single,
                )
            });

            UseStmt {
                visibility,
                path,
                items,
                span: span_from_simple(e.span()),
            }
        })
}

/// Parse use items (single, multiple, or glob)
fn use_items_parser<'tokens, I>(
) -> impl Parser<'tokens, I, UseItems, extra::Err<Rich<'tokens, Token>>> + Clone
where
    I: ValueInput<'tokens, Token = Token, Span = SimpleSpan>,
{
    choice((
        // Glob import: *
        just(Token::Star).to(UseItems::Glob),
        // Multiple items: { A, B, C }
        ident_parser()
            .separated_by(just(Token::Comma))
            .at_least(1)
            .collect::<Vec<_>>()
            .delimited_by(just(Token::LBrace), just(Token::RBrace))
            .map(UseItems::Multiple),
        // Single item
        ident_parser().map(UseItems::Single),
    ))
}

/// Parse a let binding
fn let_binding_parser<'tokens, I>(
) -> impl Parser<'tokens, I, LetBinding, extra::Err<Rich<'tokens, Token>>> + Clone
where
    I: ValueInput<'tokens, Token = Token, Span = SimpleSpan>,
{
    visibility_parser()
        .then_ignore(just(Token::Let))
        .then(mutability_parser())
        .then(binding_pattern_parser())
        .then(just(Token::Colon).ignore_then(type_parser()).or_not()) // Optional type annotation
        .then_ignore(just(Token::Equals))
        .then(expr_parser())
        .map_with(
            |((((visibility, mutable), pattern), type_annotation), value), e| LetBinding {
                visibility,
                mutable,
                pattern,
                type_annotation,
                value,
                doc: None,
                span: span_from_simple(e.span()),
            },
        )
}

/// Parse visibility modifier (pub or private)
fn visibility_parser<'tokens, I>(
) -> impl Parser<'tokens, I, Visibility, extra::Err<Rich<'tokens, Token>>> + Clone
where
    I: ValueInput<'tokens, Token = Token, Span = SimpleSpan>,
{
    just(Token::Pub)
        .to(Visibility::Public)
        .or_not()
        .map(|v| v.unwrap_or(Visibility::Private))
}

/// Parse mutability modifier (mut or immutable)
fn mutability_parser<'tokens, I>(
) -> impl Parser<'tokens, I, bool, extra::Err<Rich<'tokens, Token>>> + Clone
where
    I: ValueInput<'tokens, Token = Token, Span = SimpleSpan>,
{
    just(Token::Mut).or_not().map(|m| m.is_some())
}

/// Parse an identifier
fn ident_parser<'tokens, I>(
) -> impl Parser<'tokens, I, Ident, extra::Err<Rich<'tokens, Token>>> + Clone
where
    I: ValueInput<'tokens, Token = Token, Span = SimpleSpan>,
{
    select! {
        Token::Ident(name) = e => Ident::new(name, span_from_simple(e.span())),
        Token::SelfKeyword = e => Ident::new("self".to_string(), span_from_simple(e.span()))
    }
    .labelled("identifier")
}

/// Parse an identifier (excluding 'self' keyword)
/// Used in type and enum contexts where 'self' is not valid
fn ident_no_self_parser<'tokens, I>(
) -> impl Parser<'tokens, I, Ident, extra::Err<Rich<'tokens, Token>>> + Clone
where
    I: ValueInput<'tokens, Token = Token, Span = SimpleSpan>,
{
    select! {
        Token::Ident(name) = e => Ident::new(name, span_from_simple(e.span()))
    }
    .labelled("identifier")
}

/// Parse an invocation target: identifier or self
fn invocation_target_parser<'tokens, I>(
) -> impl Parser<'tokens, I, Ident, extra::Err<Rich<'tokens, Token>>> + Clone
where
    I: ValueInput<'tokens, Token = Token, Span = SimpleSpan>,
{
    select! {
        Token::Ident(name) = e => Ident::new(name, span_from_simple(e.span())),
        Token::SelfKeyword = e => Ident::new("self".to_string(), span_from_simple(e.span())),
    }
    .labelled("identifier")
}

/// Convert a list of block statements to an Expr (Block or single expression).
///
/// This is shared logic used by both function bodies and block expressions.
fn block_statements_to_expr(mut statements: Vec<BlockStatement>, span: crate::Span) -> Expr {
    // Empty body -> Nil
    if statements.is_empty() {
        return Expr::Literal {
            value: Literal::Nil,
            span,
        };
    }

    // Last item becomes the result expression
    let Some(last) = statements.pop() else {
        return Expr::Literal {
            value: Literal::Nil,
            span,
        };
    };
    let result = match last {
        BlockStatement::Expr(expr) => expr,
        // If last is a statement (not expr), push it back and use Nil as result
        stmt @ (BlockStatement::Let { .. } | BlockStatement::Assign { .. }) => {
            statements.push(stmt);
            Expr::Literal {
                value: Literal::Nil,
                span,
            }
        }
    };

    // Single expression with no statements -> return it directly
    if statements.is_empty() {
        return result;
    }

    Expr::Block {
        statements,
        result: Box::new(result),
        span,
    }
}

/// Helper to convert `SimpleSpan` to our custom Span
const fn span_from_simple(s: SimpleSpan) -> CustomSpan {
    CustomSpan::from_range(s.start, s.end)
}

#[cfg(test)]
mod tests;