File size: 2,869 Bytes
9425aed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//! Parser error types
//!
//! Clean-room implementation - not derived from Qiskit

use std::fmt;

/// Result type for parser operations
pub type ParseResult<T> = Result<T, ParseError>;

/// Parser error types
#[derive(Debug, Clone, PartialEq)]
pub enum ParseError {
    /// Lexical error - invalid token
    LexError {
        message: String,
        line: usize,
        column: usize,
    },
    
    /// Syntax error - unexpected token
    SyntaxError {
        expected: String,
        found: String,
        line: usize,
        column: usize,
    },
    
    /// Semantic error - valid syntax but invalid meaning
    SemanticError {
        message: String,
        line: usize,
        column: usize,
    },
    
    /// Version mismatch
    VersionError {
        expected: String,
        found: String,
    },
    
    /// Unsupported feature
    UnsupportedFeature {
        feature: String,
        line: usize,
        column: usize,
    },
    
    /// End of file reached unexpectedly
    UnexpectedEof {
        expected: String,
    },
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ParseError::LexError { message, line, column } => {
                write!(f, "Lexical error at {}:{}: {}", line, column, message)
            }
            ParseError::SyntaxError { expected, found, line, column } => {
                write!(
                    f,
                    "Syntax error at {}:{}: expected {}, found {}",
                    line, column, expected, found
                )
            }
            ParseError::SemanticError { message, line, column } => {
                write!(f, "Semantic error at {}:{}: {}", line, column, message)
            }
            ParseError::VersionError { expected, found } => {
                write!(f, "Version error: expected {}, found {}", expected, found)
            }
            ParseError::UnsupportedFeature { feature, line, column } => {
                write!(
                    f,
                    "Unsupported feature at {}:{}: {}",
                    line, column, feature
                )
            }
            ParseError::UnexpectedEof { expected } => {
                write!(f, "Unexpected end of file: expected {}", expected)
            }
        }
    }
}

impl std::error::Error for ParseError {}

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

    #[test]
    fn test_error_display() {
        let err = ParseError::LexError {
            message: "invalid character".to_string(),
            line: 1,
            column: 5,
        };
        assert_eq!(
            err.to_string(),
            "Lexical error at 1:5: invalid character"
        );
    }
}

// Made with Bob