Croissant Runtime
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.
 
 
crsn/crsn/src/asm/parse/sexp_expect.rs

55 lines
1.7 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_quoted_string_atom(expr: Sexp) -> Result<(String, SourcePosition), CrsnError> {
match expect_atom(expr) {
Ok((Atom::QS(s), pos)) => Ok((s, pos)),
Ok((_, pos)) => Err(CrsnError::Parse("Expected quoted string atom".into(), pos)),
Err(e) => Err(e),
}
}
/// Quoted or simple string
pub fn expect_any_string_atom(expr: Sexp) -> Result<(String, SourcePosition), CrsnError> {
match expect_atom(expr) {
Ok((Atom::S(s), pos)) => Ok((s, pos)),
Ok((Atom::QS(s), pos)) => Ok((s, pos)),
Ok((_, pos)) => Err(CrsnError::Parse("Expected quoted string atom".into(), pos)),
Err(e) => Err(e),
}
}