WIP support nested suffixes

This commit is contained in:
Luke Boswell 2024-04-07 14:40:58 +10:00
parent 2200916c2b
commit 5a91a4f78a
No known key found for this signature in database
GPG key ID: F6DB3C9DB47377B0
6 changed files with 1053 additions and 433 deletions

View file

@ -346,6 +346,13 @@ pub enum Expr<'a> {
UnappliedRecordBuilder(&'a Loc<Expr<'a>>),
}
pub fn split_loc_exprs_around<'a>(items: &'a [&Loc<Expr<'a>>], index: usize) -> (&'a [&'a Loc<Expr<'a>>], &'a [&'a Loc<Expr<'a>>]) {
let (before, rest) = items.split_at(index);
let after = &rest[1..]; // Skip the index element
(before, after)
}
pub fn is_loc_expr_suffixed(loc_expr: &Loc<Expr>) -> bool {
match loc_expr.value.extract_spaces().item {
// expression without arguments, `read!`
@ -370,6 +377,27 @@ pub fn is_loc_expr_suffixed(loc_expr: &Loc<Expr>) -> bool {
is_loc_expr_suffixed(sub_loc_expr) || sum > 0
}
// // expression in a if-then-else, `if isOk! then "ok" else doSomething!`
// Expr::If(if_thens, final_else) => {
// let sum: isize = if_thens
// .iter()
// .map(|(if_then, else_expr)| -> isize {
// if is_loc_expr_suffixed(if_then) || is_loc_expr_suffixed(else_expr) {
// 1
// } else {
// 0
// }
// })
// .sum();
// is_loc_expr_suffixed(final_else) || sum > 0
// }
// // expression in parens `(read!)`
// Expr::ParensAround(sub_loc_expr) => {
// is_loc_expr_suffixed(&Loc::at(loc_expr.region, *sub_loc_expr))
// }
_ => false,
}
}
@ -522,6 +550,13 @@ impl<'a> Defs<'a> {
})
}
pub fn list_value_defs(&self) -> impl Iterator<Item = (usize, &ValueDef<'a>)> {
self.tags.iter().enumerate().filter_map(|(tag_index, tag)| match tag.split() {
Ok(_) => None,
Err(value_index) => Some((tag_index, &self.value_defs[value_index.index()])),
})
}
pub fn last(&self) -> Option<Result<&TypeDef<'a>, &ValueDef<'a>>> {
self.tags.last().map(|tag| match tag.split() {
Ok(type_index) => Ok(&self.type_defs[type_index.index()]),
@ -694,17 +729,19 @@ impl<'a> Defs<'a> {
}
// For desugaring Suffixed Defs we need to split the defs around the Suffixed value
pub fn split_values_either_side_of(&self, index: usize) -> SplitDefsAround {
// TODO be smarter with the spaces and comments, at the moment these are duplicated
// for both sides of the split, but not all of them are needed
pub fn split_defs_around(&self, tag_index: usize) -> SplitDefsAround {
let mut before = self.clone();
let mut after = self.clone();
before.tags = self.tags[0..index].to_vec();
before.tags = self.tags[0..tag_index].to_vec();
if index >= self.tags.len() {
if tag_index >= self.tags.len() {
after.tags = self.tags.clone();
after.tags.clear();
} else {
after.tags = self.tags[(index + 1)..].to_vec();
after.tags = self.tags[(tag_index + 1)..].to_vec();
}
SplitDefsAround { before, after }
@ -1219,6 +1256,16 @@ impl<'a> Pattern<'a> {
}
}
}
// used to check if a pattern is suffixed to report as an error
pub fn is_suffixed(&self) -> bool {
match self {
Pattern::Identifier { suffixed, .. } => *suffixed > 0,
Pattern::QualifiedIdentifier { suffixed, .. } => *suffixed > 0,
_ => false,
}
}
}
#[derive(Copy, Clone)]
pub struct Collection<'a, T> {