You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.4 KiB
45 lines
1.4 KiB
use sexp::{Atom, Sexp, SourcePosition};
|
|
|
|
use crate::asm::error::CrsnError;
|
|
|
|
pub fn expect_list(expr: Sexp, allow_empty: bool) -> Result<(Vec<Sexp>, SourcePosition), CrsnError> {
|
|
match expr {
|
|
Sexp::Atom(_, pos) => {
|
|
return Err(CrsnError::Parse("Expected a list".into(), pos));
|
|
}
|
|
Sexp::List(list, pos) => {
|
|
if !allow_empty && list.is_empty() {
|
|
return Err(CrsnError::Parse("Routine: Empty list".into(), pos));
|
|
}
|
|
|
|
Ok((list, pos))
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn expect_atom(expr: Sexp) -> Result<(Atom, SourcePosition), CrsnError> {
|
|
match expr {
|
|
Sexp::Atom(a, pos) => {
|
|
Ok((a, pos))
|
|
}
|
|
Sexp::List(_, pos) => {
|
|
return Err(CrsnError::Parse("Expected atom got list".into(), pos));
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn expect_string_atom(expr: Sexp) -> Result<(String, SourcePosition), CrsnError> {
|
|
match expect_atom(expr) {
|
|
Ok((Atom::S(s), pos)) => Ok((s, pos)),
|
|
Ok((_, pos)) => Err(CrsnError::Parse("Expected string atom".into(), pos)),
|
|
Err(e) => Err(e),
|
|
}
|
|
}
|
|
|
|
// pub fn expect_int_atom(expr: Option<Sexp>) -> Result<i64, Error> {
|
|
// match expect_atom(expr) {
|
|
// Ok(Atom::I(v)) => Ok(v),
|
|
// Ok(atom) => Err(Error::ParseIn("Expected int atom".into(), Sexp::Atom(atom))),
|
|
// Err(e) => Err(e),
|
|
// }
|
|
// }
|
|
|