slang_error/
error_codes.rs

1/// Comprehensive error codes for all compilation errors in the Slang compiler.
2///
3/// This enum provides unified error codes and descriptions for both parsing and semantic analysis errors.
4/// Each variant maps to a unique u16 code and has an associated description.
5///
6/// Error code ranges:
7/// - 1000-1999: Parse errors (syntax and structural issues)
8/// - 2000-2999: Semantic analysis errors (type checking, scope resolution)
9/// - 3000-3999: Generic compile errors (not specifically categorized)
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub enum ErrorCode {
12    // Parse Errors (1000-1999)
13    /// Expected a semicolon after a statement
14    ExpectedSemicolon = 1001,
15    /// Expected a closing brace
16    ExpectedClosingBrace = 1002,
17    /// Expected a closing parenthesis
18    ExpectedClosingParen = 1003,
19    /// Expected a closing bracket
20    ExpectedClosingBracket = 1004,
21    /// Expected an opening brace
22    ExpectedOpeningBrace = 1005,
23    /// Expected an opening parenthesis
24    ExpectedOpeningParen = 1006,
25    /// Expected an identifier
26    ExpectedIdentifier = 1007,
27    /// Expected a type annotation
28    ExpectedType = 1008,
29    /// Expected an expression
30    ExpectedExpression = 1009,
31    /// Expected a statement
32    ExpectedStatement = 1010,
33    /// Expected a function parameter
34    ExpectedParameter = 1011,
35    /// Expected an assignment operator
36    ExpectedAssignment = 1012,
37    /// Expected a comma separator
38    ExpectedComma = 1013,
39    /// Expected a colon
40    ExpectedColon = 1014,
41    /// Expected an equals sign
42    ExpectedEquals = 1015,
43    /// Expected a function body
44    ExpectedFunctionBody = 1016,
45    /// Expected a struct field
46    ExpectedStructField = 1017,
47    /// Expected end of file
48    ExpectedEof = 1018,
49    /// Unexpected token encountered
50    UnexpectedToken = 1019,
51    /// Invalid number literal format
52    InvalidNumberLiteral = 1020,
53    /// Invalid string literal format
54    InvalidStringLiteral = 1021,
55    /// Invalid character literal format
56    InvalidCharLiteral = 1022,
57    /// Invalid escape sequence in string
58    InvalidEscapeSequence = 1023,
59    /// Unterminated string literal
60    UnterminatedString = 1024,
61    /// Unterminated character literal
62    UnterminatedChar = 1025,
63    /// Malformed comment
64    MalformedComment = 1026,
65    /// Invalid token in input
66    InvalidToken = 1027,
67    /// Nested function definitions not allowed
68    NestedFunction = 1028,
69    /// Invalid syntax in expression
70    InvalidSyntax = 1029,
71    /// Encountered unknow type during parsing
72    UnknownType = 1030,
73    /// Expected 'else' keyword after if expression
74    ExpectedElse = 1031,
75    /// Expected a closing quote for a string literal
76    ExpectedClosingQuote = 1032,
77
78    // Semantic Analysis Errors (2000-2999)
79    /// Variable used before being defined
80    UndefinedVariable = 2001,
81    /// Variable redefinition in the same scope
82    VariableRedefinition = 2002,
83    /// Symbol redefinition conflict
84    SymbolRedefinition = 2003,
85    /// Invalid type for struct field
86    InvalidFieldType = 2004,
87    /// Type mismatch between expected and actual types
88    TypeMismatch = 2005,
89    /// Incompatible types for operation
90    OperationTypeMismatch = 2006,
91    /// Logical operator used with non-boolean types
92    LogicalOperatorTypeMismatch = 2007,
93    /// Value out of range for target type
94    ValueOutOfRange = 2008,
95    /// Function called with wrong number of arguments
96    ArgumentCountMismatch = 2009,
97    /// Function argument has wrong type
98    ArgumentTypeMismatch = 2010,
99    /// Return statement used outside function
100    ReturnOutsideFunction = 2011,
101    /// Return type doesn't match function declaration
102    ReturnTypeMismatch = 2012,
103    /// Missing return value for non-void function
104    MissingReturnValue = 2013,
105    /// Function called but not defined
106    UndefinedFunction = 2014,
107    /// Invalid unary operation for type
108    InvalidUnaryOperation = 2015,
109    /// Assignment to immutable variable
110    AssignmentToImmutableVariable = 2016,
111    /// Expression has invalid form or context
112    InvalidExpression = 2017,
113    /// Attempt to call a non-callable value (non-function)
114    VariableNotCallable = 2018,
115
116    // Generic Compile Errors (3000-3999)
117    /// Generic compile error not categorized
118    GenericCompileError = 3000,
119}
120
121impl ErrorCode {
122    /// Get the numeric error code as a u16
123    pub fn code(&self) -> u16 {
124        *self as u16
125    }
126
127    /// Get a short description of the error
128    pub fn description(&self) -> &'static str {
129        match self {
130            ErrorCode::ExpectedSemicolon => "Expected semicolon after statement",
131            ErrorCode::ExpectedClosingBrace => "Expected closing brace '}'",
132            ErrorCode::ExpectedClosingParen => "Expected closing parenthesis ')'",
133            ErrorCode::ExpectedClosingBracket => "Expected closing bracket ']'",
134            ErrorCode::ExpectedOpeningBrace => "Expected opening brace '{'",
135            ErrorCode::ExpectedOpeningParen => "Expected opening parenthesis '('",
136            ErrorCode::ExpectedIdentifier => "Expected identifier",
137            ErrorCode::ExpectedType => "Expected type annotation",
138            ErrorCode::ExpectedExpression => "Expected expression",
139            ErrorCode::ExpectedStatement => "Expected statement",
140            ErrorCode::ExpectedParameter => "Expected function parameter",
141            ErrorCode::ExpectedAssignment => "Expected assignment operator",
142            ErrorCode::ExpectedComma => "Expected comma separator",
143            ErrorCode::ExpectedColon => "Expected colon ':'",
144            ErrorCode::ExpectedEquals => "Expected equals sign '='",
145            ErrorCode::ExpectedFunctionBody => "Expected function body",
146            ErrorCode::ExpectedStructField => "Expected struct field",
147            ErrorCode::ExpectedEof => "Expected end of file",
148            ErrorCode::UnexpectedToken => "Unexpected token",
149            ErrorCode::InvalidNumberLiteral => "Invalid number literal format",
150            ErrorCode::InvalidStringLiteral => "Invalid string literal format",
151            ErrorCode::InvalidCharLiteral => "Invalid character literal format",
152            ErrorCode::InvalidEscapeSequence => "Invalid escape sequence",
153            ErrorCode::UnterminatedString => "Unterminated string literal",
154            ErrorCode::UnterminatedChar => "Unterminated character literal",
155            ErrorCode::MalformedComment => "Malformed comment",
156            ErrorCode::InvalidToken => "Invalid token",
157            ErrorCode::NestedFunction => "Nested function definitions not allowed",
158            ErrorCode::InvalidSyntax => "Invalid syntax",
159            ErrorCode::UnknownType => "Unknow type",
160            ErrorCode::ExpectedElse => "Expected 'else' after if expression",
161            ErrorCode::ExpectedClosingQuote => "Expected closing quote for string literal",
162
163            // Semantic Analysis Errors
164            ErrorCode::UndefinedVariable => "Undefined variable",
165            ErrorCode::VariableRedefinition => "Variable already defined",
166            ErrorCode::SymbolRedefinition => "Symbol redefinition",
167            ErrorCode::InvalidFieldType => "Invalid field type",
168            ErrorCode::TypeMismatch => "Type mismatch",
169            ErrorCode::OperationTypeMismatch => "Incompatible types for operation",
170            ErrorCode::LogicalOperatorTypeMismatch => "Logical operator requires boolean types",
171            ErrorCode::ValueOutOfRange => "Value out of range for type",
172            ErrorCode::ArgumentCountMismatch => "Wrong number of function arguments",
173            ErrorCode::ArgumentTypeMismatch => "Function argument type mismatch",
174            ErrorCode::ReturnOutsideFunction => "Return statement outside function",
175            ErrorCode::ReturnTypeMismatch => "Return type mismatch",
176            ErrorCode::MissingReturnValue => "Missing return value",
177            ErrorCode::UndefinedFunction => "Undefined function",
178            ErrorCode::InvalidUnaryOperation => "Invalid unary operation for type",
179            ErrorCode::AssignmentToImmutableVariable => "Assignment to immutable variable",
180            ErrorCode::InvalidExpression => "Invalid expression",
181            ErrorCode::VariableNotCallable => "Variable is not callable",
182            ErrorCode::GenericCompileError => "Generic compile error",
183        }
184    }
185
186    /// Check if this is a parse error (1000-1999 range)
187    pub fn is_parse_error(&self) -> bool {
188        let code = self.code();
189        code >= 1000 && code < 2000
190    }
191
192    /// Check if this is a semantic error (2000-2999 range)
193    pub fn is_semantic_error(&self) -> bool {
194        let code = self.code();
195        code >= 2000 && code < 3000
196    }
197}
198
199impl std::fmt::Display for ErrorCode {
200    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
201        write!(f, "[E{:04}]", self.code())
202    }
203}