better error print

main
Flavien Henrion 2023-12-12 21:11:21 +01:00
parent 312aef4475
commit f96b6c058c
3 changed files with 18 additions and 6 deletions

View File

@ -10,7 +10,13 @@ pub fn main() {
let mut content = String::new();
f.read_to_string(&mut content)
.unwrap_or_else(|_| panic!("Error in reading file {}", path));
let ast = build_ast(&content).unwrap();
let ast = match build_ast(&content) {
Ok(ast) => ast,
Err(e) => {
eprintln!("Error in parsing file {}: {}", path, e);
exit(1);
}
};
let mut machine = Machine::new();
let ret = machine.run(ast).expect("execution failed");
exit(ret as i32);

View File

@ -314,6 +314,9 @@ pub fn build_ast(content: &str) -> Result<Vec<Stat>, String> {
Rule::op_neg => "-".to_owned(),
Rule::op_not => "!".to_owned(),
Rule::op_index => "[]".to_owned(),
Rule::string => "string".to_owned(),
Rule::ascii_string => "ascii string".to_owned(),
Rule::ascii_range => "ascii range".to_owned(),
Rule::WHITESPACE => "whitespace".to_owned(),
Rule::COMMENT => "comment".to_owned(),
Rule::EOI => "end of input".to_owned(),

View File

@ -1,5 +1,8 @@
alpha = { 'a'..'z' | 'A'..'Z' }
digit = { '0'..'9' }
ascii_range = { '\x00'..'\x7F' }
ascii_char = ${ !("'") ~ ascii_range }
ascii_string = ${ (!("\"") ~ ascii_range)* }
WHITESPACE = _{ " " | "\n" | "\t" }
COMMENT = _{ block_comment | line_comment }
@ -8,11 +11,11 @@ block_comment = @{ "/*" ~ ((!("*/") ~ ANY) | block_comment)* ~ "*/" }
args_call = { (expr ~ ("," ~ expr)*)? }
call = { variable ~ "(" ~ args_call ~ ")" }
string = @{ "\"" ~ (!("\"") ~ ANY)* ~ "\"" }
char = @{ "'" ~ (!("'") ~ ANY) ~ "'" }
variable = @ { (alpha) ~ (alpha | digit)* }
number = @ { (digit)+ }
boolean = @ { "true" | "false" }
string = ${ "\"" ~ ascii_string ~ "\"" }
char = ${ "'" ~ ascii_char ~ "'" }
variable = ${ (alpha) ~ (alpha | digit)* }
number = ${ (digit)+ }
boolean = ${ "true" | "false" }
op_add = { "+" }
op_sub = { "-" }