diff --git a/src/tree/location.rs b/src/tree/location.rs index 66a459f..129f9d1 100644 --- a/src/tree/location.rs +++ b/src/tree/location.rs @@ -49,8 +49,9 @@ impl Location { } /// Returns the location at the beginning of the child sequence of the given node. + /// (Returns `None` for a texty node, or a fixed node with no children.) pub fn before_children(s: &Storage, node: Node) -> Option { - if node.is_texty(s) { + if !node.can_have_children(s) { return None; } if let Some(first_child) = node.first_child(s) { @@ -61,8 +62,9 @@ impl Location { } /// Returns the location at the end of the child sequence of the given node. + /// (Returns `None` for a texty node, or a fixed node with no children.) pub fn after_children(s: &Storage, node: Node) -> Option { - if node.is_texty(s) { + if !node.can_have_children(s) { return None; } if let Some(last_child) = node.last_child(s) { diff --git a/src/tree/node.rs b/src/tree/node.rs index 17c26f2..72f43be 100644 --- a/src/tree/node.rs +++ b/src/tree/node.rs @@ -185,6 +185,14 @@ impl Node { s.forest().data(self.0).text.is_some() } + pub fn can_have_children(self, s: &Storage) -> bool { + match self.arity(s) { + Arity::Texty => false, + Arity::Fixed(sorts) => sorts.len(s) > 0, + Arity::Listy(_) => true, + } + } + /// Borrow the text of a texty node. `None` if it's not texty. pub fn text(self, s: &Storage) -> Option<&Text> { s.forest().data(self.0).text.as_ref() @@ -218,9 +226,9 @@ impl Node { s.forest().sibling_index(self.0) } - /// 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. Requires iterating over all the children. + /// Return the number of children this node has. For a Fixed node, this is the length of its + /// arity. For a Listy node, this is its current number of children. For text, this is None. + /// Requires iterating over all the children. pub fn num_children(self, s: &Storage) -> Option { if self.is_texty(s) { None