Skip to content

Commit

Permalink
don't just unwrap number prase during grammer, fixes #28
Browse files Browse the repository at this point in the history
Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
  • Loading branch information
Ryex committed Apr 22, 2024
1 parent 05de776 commit 4333a43
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
4 changes: 2 additions & 2 deletions ic10emu/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,15 @@ fn write_enums() {
let output_file = File::create(dest_path).unwrap();
let mut writer = BufWriter::new(&output_file);

let mut enums_map: Vec<(String, EnumVariant<u16>)> = Vec::new();
let mut enums_map: Vec<(String, EnumVariant<u32>)> = Vec::new();
let e_infile = Path::new("data/enums.txt");
let e_contents = fs::read_to_string(e_infile).unwrap();

for line in e_contents.lines().filter(|l| !l.trim().is_empty()) {
let mut it = line.splitn(3, ' ');
let name = it.next().unwrap();
let val_str = it.next().unwrap();
let val: Option<u16> = val_str.parse().ok();
let val: Option<u32> = val_str.parse().ok();
let docs = it.next();
let deprecated = docs
.map(|docs| docs.trim().to_uppercase() == "DEPRECATED")
Expand Down
63 changes: 46 additions & 17 deletions ic10emu/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,11 +743,11 @@ impl FromStr for Operand {
if rest_iter.next().is_none() {
Ok(Some(connection))
} else {
let start = 1 + target_str.len() + 1 + connection_str.len();
let end = 1 + target_str.len() + 1 + connection_str.len();
Err(ParseError {
line: 0,
start,
end: start,
start: end - connection_str.len(),
end,
msg: "Invalid device connection specifier".to_owned(),
})
}
Expand All @@ -762,10 +762,11 @@ impl FromStr for Operand {
connection,
}))
} else {
let end = 1 + target_str.len();
Err(ParseError {
line: 0,
start: 0,
end: 0,
start: 1,
end,
msg: "Invalid device specifier".to_owned(),
})
}
Expand All @@ -780,8 +781,8 @@ impl FromStr for Operand {
} else {
Err(ParseError {
line: 0,
start: 0,
end: 0,
start: 6,
end: hash_str.len(),
msg: "Invalid hash string: Can not contain '\"'".to_owned(),
})
}
Expand Down Expand Up @@ -837,26 +838,40 @@ impl FromStr for Operand {
.collect::<String>();
if !decimal_str.is_empty() {
let float_str = float_str + "." + &decimal_str;
let num = f64::from_str(&float_str).unwrap();
Ok(Operand::Number(Number::Float(num)))
if let Ok(num) = f64::from_str(&float_str) {
Ok(Operand::Number(Number::Float(num)))
} else {
Err(ParseError {
line: 0,
start: 0,
end: 0,
msg: "Invalid Number".to_owned(),
})
}
} else {
let start = float_str.len() + 1;
Err(ParseError {
line: 0,
start,
end: start,
start: 0,
end: float_str.len(),
msg: "Invalid Decimal Number".to_owned(),
})
}
} else if rest_iter.next().is_none() {
let num = f64::from_str(&float_str).unwrap();
Ok(Operand::Number(Number::Float(num)))
if let Ok(num) = f64::from_str(&float_str) {
Ok(Operand::Number(Number::Float(num)))
} else {
Err(ParseError {
line: 0,
start: 0,
end: float_str.len(),
msg: "Invalid Number".to_owned(),
})
}
} else {
let start = float_str.len();
Err(ParseError {
line: 0,
start,
end: start,
start: 0,
end: float_str.len(),
msg: "Invalid Integer Number".to_owned(),
})
}
Expand Down Expand Up @@ -1484,5 +1499,19 @@ mod tests {
assert!(value.is_some());
assert!(value.unwrap().parse::<u8>().is_ok());
}
for le in LogicEnums::iter() {
println!("testing Enum.{le}");
let value = le.get_str("value");
assert!(value.is_some());
assert!(value.unwrap().parse::<u32>().is_ok());
}
}

#[test]
fn bad_parse_does_not_panic() {
let code = "move foo -";
let parsed = parse(code);
assert!(parsed.is_err());
println!("{}", parsed.unwrap_err());
}
}

0 comments on commit 4333a43

Please sign in to comment.