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
pub const VERSION: &[usize] = &[0, 1, 1];
pub const PRELUDE_FILENAME: &str = ".atom-prelude";
pub const HISTORY_FILENAME: &str = ".atom-history";
use std::cmp::min;
pub use asciicolor::Colorize;
mod env;
pub use env::{CWD, Environment, REPORT, PROMPT, INCOMPLETE_PROMPT};
mod value;
pub use value::{Value, Size};
mod error;
pub use error::Error;
use comment::atom::strip;
use lalrpop_util::{lalrpop_mod, ParseError};
lalrpop_mod!(parser);
use parser::ProgramParser;
pub fn parse(code: impl ToString) -> Result<Value, Error> {
let code = code.to_string();
let code = match strip(&code) {
Ok(s) => s,
Err(_) => code.clone(),
};
match ProgramParser::new().parse(&code) {
Ok(val) => Ok(val),
Err(e) => Err(Error::SyntaxError(format!("\n{}", format_error(&code, e))))
}
}
pub type SyntaxError<'a, T> = ParseError<usize, T, &'a str>;
pub fn make_error(line: &str, unexpected: &str, line_number: usize, column_number: usize) -> String {
let underline = format!(
"{}^{}",
" ".repeat(column_number),
"-".repeat(unexpected.len() - 1)
);
format!(
"{WS} |
{line_number} | {line}
{WS} | {underline}
{WS} |
{WS} = unexpected `{unexpected}`",
WS = " ".repeat(line_number.to_string().len()),
line_number = line_number,
line = line.bright_yellow().underline(),
underline = underline,
unexpected = unexpected.bright_yellow().underline()
)
}
pub fn get_line(script: &str, location: usize) -> (usize, String, usize) {
let line_number = script[..min(location + 1, script.len())].lines().count();
let line = match script.lines().nth(line_number - 1) {
Some(line) => line,
None => {
if let Some(line) = script.lines().last() {
line
} else {
""
}
}
};
let mut column = {
let mut current_column = 0;
for ch in script[..location].chars() {
if ch == '\n' {
current_column = 0;
} else {
current_column += 1;
}
}
current_column
};
let trimmed_line = line.trim_start();
column -= (line.len() - trimmed_line.len()) as i32;
(line_number, String::from(trimmed_line), column as usize)
}
pub fn format_error<T: core::fmt::Debug>(script: &str, err: SyntaxError<T>) -> String {
match err {
SyntaxError::InvalidToken { location } => {
let (line_number, line, column) = get_line(script, location);
make_error(
&line,
&(script.as_bytes()[location] as char).to_string(),
line_number,
column,
)
}
SyntaxError::UnrecognizedEOF { location, .. } => {
let (line_number, line, _) = get_line(script, location);
make_error(&line, "EOF", line_number, line.len())
}
SyntaxError::UnrecognizedToken { token, .. } => {
let start = token.0;
let end = token.2;
let (line_number, line, column) = get_line(script, start);
let unexpected = &script[start..end];
make_error(&line, unexpected, line_number, column)
}
SyntaxError::ExtraToken { token } => {
let start = token.0;
let end = token.2;
let (line_number, line, column) = get_line(script, start);
let unexpected = &script[start..end];
make_error(&line, unexpected, line_number, column)
}
SyntaxError::User { error } => format!(
" |\n? | {}\n | {}\n |\n = unexpected compiling error",
error,
format!("^{}", "-".repeat(error.len() - 1))
),
}
}