lambdust 0.1.1

A Scheme dialect with gradual typing and effect systems
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
//! Error handling and diagnostics for the Lambdust language.
//!
//! This module provides comprehensive error reporting with source location information,
//! helpful error messages, and integration with the `miette` and `ariadne` crates
//! for beautiful error display.

#![allow(missing_docs)]

pub mod error;
pub mod position;
pub mod span;
pub mod source_map;
pub mod stack_trace;
pub mod suggestions;
pub mod gc_diagnostics;
pub mod custom_error;
pub mod lightweight_diagnostic;

pub use error::*;
pub use position::*;
pub use span::{Span, Spanned, spanned};
pub use source_map::*;
pub use stack_trace::*;
pub use suggestions::*;
pub use gc_diagnostics::{
    GcDiagnosticManager, GcDiagnosticConfig, DiagnosticId, PreservedError,
    ErrorKind, ErrorContext, GcAwareError, DiagnosticStatistics
};
pub use custom_error::{LambdustError, ErrorLabel, LabelStyle, RuntimeError, utils as error_utils};
pub use lightweight_diagnostic::{
    LightweightDiagnostic, DiagnosticLabel, DiagnosticLabelStyle, DiagnosticSeverity,
    DiagnosticReporter, report_diagnostic
};

/// Result type used throughout the Lambdust implementation.
pub type Result<T> = std::result::Result<T, Box<Error>>;

/// Error types for the Lambdust language implementation.
#[derive(Debug, Clone)]
pub enum Error {
    /// Lexical analysis errors
    LexError {
        message: String,
        span: Span,
    },

    /// Parsing errors
    ParseError {
        message: String,
        span: Span,
    },

    /// Type checking errors
    TypeError {
        message: String,
        span: Span,
    },

    /// Macro expansion errors
    MacroError {
        message: String,
        span: Span,
    },

    /// Runtime evaluation errors
    RuntimeError {
        message: String,
        span: Option<Span>,
    },

    /// FFI errors
    FfiError {
        message: String,
    },

    /// IO and system errors
    IoError {
        message: String,
    },

    /// Internal compiler errors (bugs)
    InternalError {
        message: String,
    },

    /// R7RS Exception (raised by raise/error procedures)
    Exception {
        exception: crate::stdlib::exceptions::ExceptionObject,
        span: Option<Span>,
    },
}

impl Error {
    /// Creates a new lexical error.
    pub fn lex_error(message: impl Into<String>, span: Span) -> Self {
        Self::LexError {
            message: message.into(),
            span,
        }
    }

    /// Creates a new parse error.
    pub fn parse_error(message: impl Into<String>, span: Span) -> Self {
        Self::ParseError {
            message: message.into(),
            span,
        }
    }

    /// Creates a new type error.
    pub fn type_error(message: impl Into<String>, span: Span) -> Self {
        Self::TypeError {
            message: message.into(),
            span,
        }
    }

    /// Creates a new macro error.
    pub fn macro_error(message: impl Into<String>, span: Span) -> Self {
        Self::MacroError {
            message: message.into(),
            span,
        }
    }

    /// Creates a new runtime error.
    pub fn runtime_error(message: impl Into<String>, span: Option<Span>) -> Self {
        Self::RuntimeError {
            message: message.into(),
            span,
        }
    }

    /// Creates a new FFI error.
    pub fn ffi_error(message: impl Into<String>) -> Self {
        Self::FfiError {
            message: message.into(),
        }
    }

    /// Creates a new IO error.
    pub fn io_error(message: impl Into<String>) -> Self {
        Self::IoError {
            message: message.into(),
        }
    }

    /// Creates a new internal error.
    pub fn internal_error(message: impl Into<String>) -> Self {
        Self::InternalError {
            message: message.into(),
        }
    }

    /// Creates a new syntax error (alias for parse_error).
    pub fn syntax_error(message: impl Into<String>, span: Option<Span>) -> Self {
        Self::ParseError {
            message: message.into(),
            span: span.unwrap_or(Span::new(0, 0)),
        }
    }

    /// Creates a new exception error.
    pub fn exception(exception: crate::stdlib::exceptions::ExceptionObject) -> Self {
        Self::Exception {
            exception,
            span: None,
        }
    }

    /// Creates a new exception error with span.
    pub fn exception_with_span(exception: crate::stdlib::exceptions::ExceptionObject, span: Span) -> Self {
        Self::Exception {
            exception,
            span: Some(span),
        }
    }

    /// Creates an arity error for a function called with wrong number of arguments.
    pub fn arity_error(function_name: &str, expected: usize, actual: usize) -> Self {
        Self::RuntimeError {
            message: format!("Function '{function_name}' expects {expected} arguments, got {actual}"),
            span: None,
        }
    }
}

// Manual Display implementation to replace thiserror
impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::LexError { message, .. } => write!(f, "Lexical error: {message}"),
            Self::ParseError { message, .. } => write!(f, "Parse error: {message}"),
            Self::TypeError { message, .. } => write!(f, "Type error: {message}"),
            Self::MacroError { message, .. } => write!(f, "Macro error: {message}"),
            Self::RuntimeError { message, .. } => write!(f, "Runtime error: {message}"),
            Self::FfiError { message } => write!(f, "FFI error: {message}"),
            Self::IoError { message } => write!(f, "IO error: {message}"),
            Self::InternalError { message } => write!(f, "Internal error: {message}"),
            Self::Exception { exception, .. } => write!(f, "Exception: {exception}"),
        }
    }
}

// LambdustError trait implementation
impl LambdustError for Error {
    fn error_code(&self) -> &'static str {
        match self {
            Self::LexError { .. } => "lambdust::lexer::error",
            Self::ParseError { .. } => "lambdust::parser::error",
            Self::TypeError { .. } => "lambdust::types::error",
            Self::MacroError { .. } => "lambdust::macros::error",
            Self::RuntimeError { .. } => "lambdust::runtime::error",
            Self::FfiError { .. } => "lambdust::ffi::error",
            Self::IoError { .. } => "lambdust::io::error",
            Self::InternalError { .. } => "lambdust::internal::error",
            Self::Exception { .. } => "lambdust::exception::error",
        }
    }
    
    fn help(&self) -> Option<&str> {
        match self {
            Self::InternalError { .. } => Some("This is likely a bug in the Lambdust implementation. Please report it."),
            _ => None,
        }
    }
    
    fn labels(&self) -> Vec<ErrorLabel> {
        match self {
            Self::LexError { span, .. } => vec![ErrorLabel::primary(*span, "here")],
            Self::ParseError { span, .. } => vec![ErrorLabel::primary(*span, "here")],
            Self::TypeError { span, .. } => vec![ErrorLabel::primary(*span, "here")],
            Self::MacroError { span, .. } => vec![ErrorLabel::primary(*span, "here")],
            Self::RuntimeError { span: Some(span), .. } => vec![ErrorLabel::primary(*span, "here")],
            Self::Exception { span: Some(span), .. } => vec![ErrorLabel::primary(*span, "raised here")],
            _ => Vec::new(),
        }
    }
    
    fn is_critical(&self) -> bool {
        matches!(self, Self::InternalError { .. })
    }
}

// Standard Error trait implementation
impl std::error::Error for Error {}

// LightweightDiagnostic trait implementation
impl LightweightDiagnostic for Error {
    fn code(&self) -> Option<&str> {
        match self {
            Self::LexError { .. } => Some("lambdust::lexer::error"),
            Self::ParseError { .. } => Some("lambdust::parser::error"),
            Self::TypeError { .. } => Some("lambdust::types::error"),
            Self::MacroError { .. } => Some("lambdust::macros::error"),
            Self::RuntimeError { .. } => Some("lambdust::runtime::error"),
            Self::FfiError { .. } => Some("lambdust::ffi::error"),
            Self::IoError { .. } => Some("lambdust::io::error"),
            Self::InternalError { .. } => Some("lambdust::internal::error"),
            Self::Exception { .. } => Some("lambdust::exception::error"),
        }
    }
    
    fn help(&self) -> Option<&str> {
        match self {
            Self::InternalError { .. } => Some("This is likely a bug in the Lambdust implementation. Please report it."),
            _ => None,
        }
    }
    
    fn labels(&self) -> Vec<DiagnosticLabel> {
        match self {
            Self::LexError { span, .. } => vec![DiagnosticLabel::primary(*span, "here")],
            Self::ParseError { span, .. } => vec![DiagnosticLabel::primary(*span, "here")],
            Self::TypeError { span, .. } => vec![DiagnosticLabel::primary(*span, "here")],
            Self::MacroError { span, .. } => vec![DiagnosticLabel::primary(*span, "here")],
            Self::RuntimeError { span: Some(span), .. } => vec![DiagnosticLabel::primary(*span, "here")],
            Self::Exception { span: Some(span), .. } => vec![DiagnosticLabel::primary(*span, "raised here")],
            _ => Vec::new(),
        }
    }
    
    fn severity(&self) -> DiagnosticSeverity {
        match self {
            Self::InternalError { .. } => DiagnosticSeverity::Error,
            _ => DiagnosticSeverity::Error,
        }
    }
}

impl Error {
    /// Creates a compilation error.
    pub fn compilation_error(message: impl Into<String>) -> Self {
        Self::InternalError {
            message: format!("Compilation error: {}", message.into()),
        }
    }

    /// Creates a type mismatch error with expected type information.
    pub fn type_mismatch_error(expected: &str, actual: impl std::fmt::Debug) -> Self {
        Self::RuntimeError {
            message: format!("Expected {expected}, got {actual:?}"),
            span: None,
        }
    }

    /// Creates an unexpected end-of-file error.
    pub fn unexpected_eof(span: Span) -> Self {
        Self::ParseError {
            message: "Unexpected end of file".to_string(),
            span,
        }
    }

    /// Creates an unexpected token error.
    pub fn unexpected_token(token: &crate::lexer::Token, expected: &str) -> Self {
        Self::ParseError {
            message: format!("Unexpected token '{}', expected {}", token.lexeme, expected),
            span: token.span,
        }
    }

    /// Creates an expected token error.
    pub fn expected_token(token: &crate::lexer::Token, expected: &crate::lexer::TokenKind, context: &str) -> Self {
        Self::ParseError {
            message: format!("{}, found '{}'", context, token.lexeme),
            span: token.span,
        }
    }

    /// Creates a not-implemented error for development.
    pub fn not_implemented(feature: &str, span: Span) -> Self {
        Self::InternalError {
            message: format!("Feature '{feature}' not yet implemented"),
        }
    }

    /// Converts this Error into a Box<Error> for use with the Result type.
    pub fn boxed(self) -> Box<Error> {
        Box::new(self)
    }

    /// Creates a boxed lexical error.
    pub fn boxed_lex_error(message: impl Into<String>, span: Span) -> Box<Error> {
        Self::lex_error(message, span).boxed()
    }

    /// Creates a boxed parse error.
    pub fn boxed_parse_error(message: impl Into<String>, span: Span) -> Box<Error> {
        Self::parse_error(message, span).boxed()
    }

    /// Creates a boxed runtime error.
    pub fn boxed_runtime_error(message: impl Into<String>, span: Option<Span>) -> Box<Error> {
        Self::runtime_error(message, span).boxed()
    }
}

impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Self {
        Self::IoError {
            message: err.to_string(),
        }
    }
}

impl From<std::io::Error> for Box<Error> {
    fn from(err: std::io::Error) -> Self {
        Error::from(err).into()
    }
}

/// Helper trait for adding span information to results.
pub trait WithSpan<T> {
    /// Adds span information to an error result.
    fn with_span(self, span: Span) -> Result<T>;
}

impl<T, E> WithSpan<T> for std::result::Result<T, E>
where
    E: Into<String>,
{
    fn with_span(self, span: Span) -> Result<T> {
        self.map_err(|e| Error::runtime_error(e.into(), Some(span)).boxed())
    }
}

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

    #[test]
    fn test_span_creation() {
        let span = Span::new(10, 5);
        assert_eq!(span.start, 10);
        assert_eq!(span.len, 5);
        assert_eq!(span.end(), 15);
    }

    #[test]
    fn test_span_combine() {
        let span1 = Span::new(5, 3);
        let span2 = Span::new(10, 2);
        let combined = span1.combine(span2);
        
        assert_eq!(combined.start, 5);
        assert_eq!(combined.end(), 12);
        assert_eq!(combined.len, 7);
    }

    #[test]
    fn test_error_creation() {
        let span = Span::new(0, 5);
        let error = Error::lex_error("test error", span);
        
        match error {
            Error::LexError { message, span: error_span } => {
                assert_eq!(message, "test error");
                assert_eq!(error_span, span);
            }
            _ => panic!("Wrong error type"),
        }
    }
}