Skip to content

Commit

Permalink
refactor: organize Ast methods
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpombrio committed Mar 14, 2024
1 parent 1bf5a43 commit 1d6629c
Showing 1 changed file with 61 additions and 38 deletions.
99 changes: 61 additions & 38 deletions src/language/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ impl Location {
}
}

// TODO: put these methods in any order whatsoever
impl Ast {
/****************
* Constructors *
****************/

pub fn new_hole(s: &mut DocStorage, lang: Language) -> Ast {
Ast::new(s, lang.hole_construct(&s.language_set))
}
Expand Down Expand Up @@ -93,10 +96,49 @@ impl Ast {
}
}

/*************
* Node Data *
*************/

pub fn id(self, s: &DocStorage) -> AstId {
s.forest.data(self.0).id
}

pub fn arity(self, s: &DocStorage) -> Arity {
s.forest.data(self.0).construct.arity(&s.language_set)
}

pub fn is_comment_or_ws(self, s: &DocStorage) -> bool {
s.forest
.data(self.0)
.construct
.is_comment_or_ws(&s.language_set)
}

pub fn notation(self, s: &DocStorage) -> &ValidNotation {
s.forest.data(self.0).construct.notation(&s.language_set)
}

/// Borrow the text of a texty node.
pub fn text(self, s: &DocStorage) -> Option<&Text> {
s.forest.data(self.0).text.as_ref()
}

/// Mutably borrow the text of a texty node.
pub fn text_mut(self, s: &mut DocStorage) -> Option<&mut Text> {
s.forest.data_mut(self.0).text.as_mut()
}

/*************
* Relatives *
*************/

/// Returns `true` if this is the root of the tree, and `false` if
/// it isn't (and thus this node has a parent).
pub fn is_at_root(self, s: &DocStorage) -> bool {
s.forest.parent(self.0).is_none()
}

/// Determine the number of siblings that this node has, including itself.
pub fn num_siblings(&self, s: &DocStorage) -> usize {
if let Some(parent) = s.forest.parent(self.0) {
Expand All @@ -111,12 +153,6 @@ impl Ast {
s.forest.sibling_index(self.0)
}

/// Returns `true` if this is the root of the tree, and `false` if
/// it isn't (and thus this node has a parent).
pub fn is_at_root(self, s: &DocStorage) -> bool {
s.forest.parent(self.0).is_none()
}

/// Return the number of children this node has. For a Fixed node, this is
/// its arity. For a Listy node, this is its current number of children.
/// For text, this is None.
Expand All @@ -128,6 +164,10 @@ impl Ast {
}
}

/**************
* Navigation *
**************/

pub fn parent(self, s: &DocStorage) -> Option<Ast> {
s.forest.parent(self.0).map(Ast)
}
Expand All @@ -142,6 +182,12 @@ impl Ast {
.map(|n| Ast(s.forest.last_sibling(n)))
}

/// Go to this node's `n`'th child.
/// Panics if `n` is out of bounds, or if this node is texty.
pub fn nth_child(self, s: &DocStorage, n: usize) -> Ast {
Ast(s.forest.nth_child(self.0, n).bug_msg("Ast::nth_child"))
}

pub fn next_sibling(self, s: &DocStorage) -> Option<Ast> {
s.forest.next(self.0).map(Ast)
}
Expand Down Expand Up @@ -181,6 +227,10 @@ impl Ast {
}
}

/**************
* Acceptance *
**************/

/// Check if `other` is allowed where `self` currently is, according to its parent's arity.
fn accepts_replacement(self, s: &DocStorage, other: Ast) -> bool {
if let Some(parent) = s.forest.parent(self.0) {
Expand All @@ -205,6 +255,10 @@ impl Ast {
}
}

/************
* Mutation *
************/

// TODO: doc
pub fn swap(self, s: &mut DocStorage, other: Ast) -> bool {
if self.accepts_replacement(s, other) && other.accepts_replacement(s, self) {
Expand All @@ -214,37 +268,6 @@ impl Ast {
}
}

pub fn arity(self, s: &DocStorage) -> Arity {
s.forest.data(self.0).construct.arity(&s.language_set)
}

pub fn is_comment_or_ws(self, s: &DocStorage) -> bool {
s.forest
.data(self.0)
.construct
.is_comment_or_ws(&s.language_set)
}

pub fn notation(self, s: &DocStorage) -> &ValidNotation {
s.forest.data(self.0).construct.notation(&s.language_set)
}

/// Borrow the text of a texty node.
pub fn text(self, s: &DocStorage) -> Option<&Text> {
s.forest.data(self.0).text.as_ref()
}

/// Mutably borrow the text of a texty node.
pub fn text_mut(self, s: &mut DocStorage) -> Option<&mut Text> {
s.forest.data_mut(self.0).text.as_mut()
}

/// Go to this node's `n`'th child.
/// Panics if `n` is out of bounds, or if this node is texty.
pub fn nth_child(self, s: &DocStorage, n: usize) -> Ast {
Ast(s.forest.nth_child(self.0, n).bug_msg("Ast::nth_child"))
}

// TODO: doc (new_sibling must be root)
pub fn insert_before(self, s: &mut DocStorage, new_sibling: Ast) -> bool {
if let Some(parent) = self.parent(s) {
Expand Down

0 comments on commit 1d6629c

Please sign in to comment.