xref: /DragonReach/src/error/parse_error/mod.rs (revision 236b9b4f4d4f527c482cad40d09674195023e5fb)
1 use std::format;
2 use std::string::String;
3 use std::string::ToString;
4 
5 use super::ErrorFormat;
6 /// 解析错误,错误信息应该包括文件名以及行号
7 #[repr(i32)]
8 #[derive(Debug, PartialEq, Eq, Clone)]
9 #[allow(dead_code, non_camel_case_types)]
10 pub enum ParseErrorType {
11     /// 不合法参数
12     EINVAL,
13     /// 结果过大 Result too large.
14     ERANGE,
15     /// 重复定义
16     EREDEF,
17     /// 未预料到的空值
18     EUnexpectedEmpty,
19     /// 语法错误
20     ESyntaxError,
21     /// 非法文件描述符
22     EBADF,
23     /// 非法文件
24     EFILE,
25     /// 不是目录
26     ENODIR,
27     /// 循环依赖
28     ECircularDependency,
29 }
30 /// 错误信息应该包括错误类型ParseErrorType,当前解析的文件名,当前解析的行号
31 #[derive(Debug, PartialEq, Eq, Clone)]
32 pub struct ParseError(ParseErrorType, String, usize);
33 
34 impl ParseError {
35     pub fn new(error_type: ParseErrorType, file_name: String, line_number: usize) -> ParseError {
36         ParseError(error_type, file_name, line_number)
37     }
38 
39     pub fn set_file(&mut self, path: &str) {
40         self.1 = path.to_string();
41     }
42 
43     pub fn set_linenum(&mut self, linenum: usize) {
44         self.2 = linenum;
45     }
46 }
47 
48 impl ErrorFormat for ParseError {
49     fn error_format(&self) -> String {
50         format!(
51             "Parse Error!,Error Type: {:?}, File: {}, Line: {}",
52             self.0, self.1, self.2
53         )
54     }
55 }
56