-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
114 lines (85 loc) · 2.21 KB
/
error.rs
File metadata and controls
114 lines (85 loc) · 2.21 KB
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
use rusty_common::Positioned;
use rusty_pc::ParserErrorTrait;
/// Represents parser errors.
/// All errors except `Miss` and `Expected` are fatal.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub enum ParserError {
/// Indicates a generic miss in parsing (soft error).
#[default]
Miss,
/// A soft error with a description of what was expected.
Expected(String),
// 1
NextWithoutFor,
// 2
SyntaxError(String),
// 6
Overflow,
// 26
ForWithoutNext,
// 29
WhileWithoutWend,
// 30
WendWithoutWhile,
ParseNumError(String),
// 52
BadFileNameOrNumber,
// 53
FileNotFound,
// 57
DeviceIOError(String),
// 62
InputPastEndOfFile,
ElseWithoutIf,
IdentifierCannotIncludePeriod,
IdentifierTooLong,
ElementNotDefined,
LoopWithoutDo,
}
impl ParserError {
pub fn syntax_error(msg: &str) -> Self {
Self::SyntaxError(msg.to_string())
}
/// Creates a syntax error that starts with "Expected: "
/// followed by the given string.
pub fn expected(expectation: &str) -> Self {
Self::from(expectation)
}
}
impl ParserErrorTrait for ParserError {
fn is_soft(&self) -> bool {
matches!(self, Self::Miss | Self::Expected(_))
}
fn is_fatal(&self) -> bool {
!self.is_soft()
}
fn to_fatal(self) -> Self {
match self {
Self::Expected(msg) => Self::SyntaxError(msg),
Self::Miss => Self::SyntaxError("Unknown error".to_string()),
_ => self,
}
}
}
pub type ParseErrorPos = Positioned<ParserError>;
impl From<std::num::ParseFloatError> for ParserError {
fn from(e: std::num::ParseFloatError) -> Self {
Self::ParseNumError(e.to_string())
}
}
impl From<std::num::ParseIntError> for ParserError {
fn from(e: std::num::ParseIntError) -> Self {
Self::ParseNumError(e.to_string())
}
}
// Needed in order to support with_expected_message
impl From<String> for ParserError {
fn from(e: String) -> Self {
Self::Expected(format!("Expected: {}", e))
}
}
impl From<&str> for ParserError {
fn from(e: &str) -> Self {
Self::Expected(format!("Expected: {}", e))
}
}