-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherr_lox.rs
201 lines (182 loc) · 5.91 KB
/
err_lox.rs
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use std::error::Error;
use std::fmt;
use crate::interpreter::token::Token;
use crate::interpreter::AST_Node::AST_Node;
use crate::runtime::lox_variable::LoxVariable;
use clap::error::ErrorKind;
use colored::*;
use std::convert::From;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
use std::sync::{Arc, Mutex};
// DEBUG:
use log::{debug, error, info, trace, warn};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ErrorType {
ParseErr,
ScanErr,
UnterminatedDelimiter,
UnKnown,
}
#[derive(Debug)]
pub enum Source {
NoSource,
FileName(String),
Stdin,
}
impl Source {
pub fn from_filename(filename: &str) -> Self {
Source::FileName(filename.to_string())
}
}
#[derive(Debug)]
pub struct ErrorLox {
description: String,
error_type: ErrorType,
row: usize,
column: usize,
source: Source,
}
impl ErrorLox {
pub fn from_filename(description: &str, row: usize, column: usize, filename: &str) -> Self {
let source = Source::from_filename(filename);
ErrorLox {
description: description.to_string(),
error_type: ErrorType::UnKnown,
row,
column,
source,
}
}
// TODO: remove filename, as token already has it
pub fn from_token(token: &Token, description: &str) -> Self {
let column = token.column;
let row = token.line;
let source = Source::from_filename(&token.source_file);
ErrorLox {
description: description.to_string(),
error_type: ErrorType::UnKnown,
row,
column,
source,
}
}
pub fn from_lox_variable(variable: &LoxVariable, description: &str) -> Self {
// TODO: UNFINISHED
match variable.get_ref_node() {
None => {
return ErrorLox {
description: description.to_string(),
error_type: ErrorType::UnKnown,
row: 0,
column: 0,
source: Source::NoSource,
}
}
Some(node) => {
let token = AST_Node::get_token_from_arc(node);
let ref_token = token.lock().unwrap();
return ErrorLox {
description: description.to_string(),
error_type: ErrorType::UnKnown,
row: ref_token.line,
column: ref_token.column,
source: Source::from_filename(&ref_token.source_file),
};
}
}
}
pub fn from_arc_mutex_token(token: Arc<Mutex<Token>>, description: &str) -> Self {
let tmp = token.lock().unwrap();
ErrorLox::from_token(&tmp, description)
}
pub fn from_ast_node(node: &AST_Node, description: &str) -> Self {
let token = node.get_token();
ErrorLox::from_arc_mutex_token(token, description)
}
pub fn from_arc_mutex_ast_node(node: Arc<Mutex<AST_Node>>, description: &str) -> Self {
let node = node.lock().unwrap();
ErrorLox::from_ast_node(&node, description)
}
pub fn panic(&self) {
// TODO: WHAT IS A BETTER MAY TO HANDLE THIS?
match &self.source {
Source::Stdin => {
panic!("ERROR: {}", self.description);
}
Source::FileName(f) => match Path::new(&f).try_exists() {
Ok(exists) => {
if !exists {
panic!("ERROR: {}", self.description);
}
}
_ => {
panic!("ERROR: {}", self.description);
}
},
Source::NoSource => {
panic!("ERROR: {}", self.description);
}
}
println!("{}", self);
std::process::exit(1);
}
pub fn set_error_type(&mut self, error_type: ErrorType) {
self.error_type = error_type;
}
pub fn get_error_type(&self) -> ErrorType {
self.error_type.clone()
}
}
impl fmt::Display for ErrorLox {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let source_name: String = match &self.source {
Source::FileName(name) => format!("{}", name.underline()),
Source::Stdin => "stdin".underline().to_string(),
// TODO: needs better handling
Source::NoSource => {
panic!("{}", self.description);
}
};
// error!("{:?}", self);
let detailed_desr: String = match &self.source {
Source::FileName(name) => {
let reader =
BufReader::new(File::open(name).expect(&format!("Cannot open file {name}")));
let mut content_at_nth = reader
.lines()
.nth(self.row - 1)
.expect(&format!(
"Internal Error: {} is not {} lines long!",
name, self.row
))
.expect(&format!(
"Internal Error: Could not read the {}th line long from {}!",
self.row, name
));
let mut content_second_line = String::new();
for _ in 1..self.column {
content_second_line.push_str(" ");
}
let red_tick = "^".red().to_string();
content_second_line.push_str(&red_tick);
content_at_nth.push_str("\n");
content_at_nth.push_str(&content_second_line);
content_at_nth
}
_ => String::new(),
};
write!(
f,
"{}: {} \n--> {}.{}:{}\n{}",
"Error".bold().red(),
self.description.bold(),
source_name,
self.row,
self.column,
detailed_desr
)
}
}
impl Error for ErrorLox {}