1#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub enum ErrorCode {
12 ExpectedSemicolon = 1001,
15 ExpectedClosingBrace = 1002,
17 ExpectedClosingParen = 1003,
19 ExpectedClosingBracket = 1004,
21 ExpectedOpeningBrace = 1005,
23 ExpectedOpeningParen = 1006,
25 ExpectedIdentifier = 1007,
27 ExpectedType = 1008,
29 ExpectedExpression = 1009,
31 ExpectedStatement = 1010,
33 ExpectedParameter = 1011,
35 ExpectedAssignment = 1012,
37 ExpectedComma = 1013,
39 ExpectedColon = 1014,
41 ExpectedEquals = 1015,
43 ExpectedFunctionBody = 1016,
45 ExpectedStructField = 1017,
47 ExpectedEof = 1018,
49 UnexpectedToken = 1019,
51 InvalidNumberLiteral = 1020,
53 InvalidStringLiteral = 1021,
55 InvalidCharLiteral = 1022,
57 InvalidEscapeSequence = 1023,
59 UnterminatedString = 1024,
61 UnterminatedChar = 1025,
63 MalformedComment = 1026,
65 InvalidToken = 1027,
67 NestedFunction = 1028,
69 InvalidSyntax = 1029,
71 UnknownType = 1030,
73 ExpectedElse = 1031,
75 ExpectedClosingQuote = 1032,
77
78 UndefinedVariable = 2001,
81 VariableRedefinition = 2002,
83 SymbolRedefinition = 2003,
85 InvalidFieldType = 2004,
87 TypeMismatch = 2005,
89 OperationTypeMismatch = 2006,
91 LogicalOperatorTypeMismatch = 2007,
93 ValueOutOfRange = 2008,
95 ArgumentCountMismatch = 2009,
97 ArgumentTypeMismatch = 2010,
99 ReturnOutsideFunction = 2011,
101 ReturnTypeMismatch = 2012,
103 MissingReturnValue = 2013,
105 UndefinedFunction = 2014,
107 InvalidUnaryOperation = 2015,
109 AssignmentToImmutableVariable = 2016,
111 InvalidExpression = 2017,
113 VariableNotCallable = 2018,
115
116 GenericCompileError = 3000,
119}
120
121impl ErrorCode {
122 pub fn code(&self) -> u16 {
124 *self as u16
125 }
126
127 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 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 pub fn is_parse_error(&self) -> bool {
188 let code = self.code();
189 code >= 1000 && code < 2000
190 }
191
192 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}