Implement block / indent based parsing

... and enforce that defs can only occur in blocks (or, inside parenthesized expressions)
This commit is contained in:
Joshua Warner 2024-07-08 21:14:51 -07:00
parent d5db3137a3
commit 4f32f43048
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
304 changed files with 12050 additions and 8876 deletions

View file

@ -21,6 +21,12 @@ pub struct Spaces<'a, T> {
pub after: &'a [CommentOrNewline<'a>],
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct SpacesBefore<'a, T> {
pub before: &'a [CommentOrNewline<'a>],
pub item: T,
}
#[derive(Copy, Clone, PartialEq)]
pub enum Spaced<'a, T> {
Item(T),
@ -1204,6 +1210,21 @@ impl<'a> Defs<'a> {
})
}
pub fn loc_defs<'b>(
&'b self,
) -> impl Iterator<Item = Result<Loc<TypeDef<'a>>, Loc<ValueDef<'a>>>> + 'b {
self.tags
.iter()
.enumerate()
.map(|(i, tag)| match tag.split() {
Ok(type_index) => Ok(Loc::at(self.regions[i], self.type_defs[type_index.index()])),
Err(value_index) => Err(Loc::at(
self.regions[i],
self.value_defs[value_index.index()],
)),
})
}
pub fn list_value_defs(&self) -> impl Iterator<Item = (usize, &ValueDef<'a>)> {
self.tags
.iter()
@ -2072,6 +2093,28 @@ pub trait Spaceable<'a> {
fn before(&'a self, _: &'a [CommentOrNewline<'a>]) -> Self;
fn after(&'a self, _: &'a [CommentOrNewline<'a>]) -> Self;
fn maybe_before(self, arena: &'a Bump, spaces: &'a [CommentOrNewline<'a>]) -> Self
where
Self: Sized + 'a,
{
if spaces.is_empty() {
self
} else {
arena.alloc(self).before(spaces)
}
}
fn maybe_after(self, arena: &'a Bump, spaces: &'a [CommentOrNewline<'a>]) -> Self
where
Self: Sized + 'a,
{
if spaces.is_empty() {
self
} else {
arena.alloc(self).after(spaces)
}
}
fn with_spaces_before(&'a self, spaces: &'a [CommentOrNewline<'a>], region: Region) -> Loc<Self>
where
Self: Sized,