summaryrefslogtreecommitdiff
path: root/src/syntax/mod.rs
diff options
context:
space:
mode:
authorMartin Haug <mhaug@live.de>2021-11-03 11:03:00 +0100
committerMartin Haug <mhaug@live.de>2021-11-03 11:03:00 +0100
commit7016ab0d123ba06d0bbc6ed5001fa02fbd261bfa (patch)
tree12e71a43e51a863509b71724ef882e3903d51357 /src/syntax/mod.rs
parent1e4cab393e55df8875c6303ebb7bde8f09f911c9 (diff)
Make stuff more elegant
Diffstat (limited to 'src/syntax/mod.rs')
-rw-r--r--src/syntax/mod.rs71
1 files changed, 33 insertions, 38 deletions
diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs
index b0911c63..5da690ab 100644
--- a/src/syntax/mod.rs
+++ b/src/syntax/mod.rs
@@ -98,7 +98,7 @@ pub struct GreenNode {
/// This node's children, losslessly make up this node.
children: Vec<Green>,
/// Whether this node or any of its children are erroneous.
- erroneous: bool,
+ pub erroneous: bool,
}
impl GreenNode {
@@ -148,12 +148,9 @@ impl GreenNode {
self.data().len()
}
- /// Find the parent of the deepest incremental-safe node and the index of
- /// the found child.
- pub fn incremental_parent(
- &mut self,
- span: Span,
- ) -> Option<(usize, &mut GreenNode, usize)> {
+ /// Find the deepest incremental-safe node and its offset in the source
+ /// code.
+ pub fn incremental_parent(&mut self, span: Span) -> Option<(&mut GreenNode, usize)> {
self.incremental_parent_internal(span, 0)
}
@@ -161,10 +158,8 @@ impl GreenNode {
&mut self,
span: Span,
mut offset: usize,
- ) -> Option<(usize, &mut GreenNode, usize)> {
- let x = unsafe { &mut *(self as *mut _) };
-
- for (i, child) in self.children.iter_mut().enumerate() {
+ ) -> Option<(&mut GreenNode, usize)> {
+ for child in self.children.iter_mut() {
match child {
Green::Token(n) => {
if offset < span.start {
@@ -187,15 +182,15 @@ impl GreenNode {
&& span.end > offset
{
// the node is within the span.
- if n.kind().is_incremental_safe() {
- let res =
- Rc::make_mut(n).incremental_parent_internal(span, offset);
+ let safe = n.kind().is_incremental_safe();
+ let mut_n = Rc::make_mut(n);
+ if safe {
+ let res = mut_n.incremental_parent_internal(span, offset);
if res.is_none() {
- return Some((i, x, offset));
+ return Some((mut_n, offset));
}
} else {
- return Rc::make_mut(n)
- .incremental_parent_internal(span, offset);
+ return mut_n.incremental_parent_internal(span, offset);
}
} else {
// the node is overlapping or after after the span; nodes are
@@ -208,11 +203,6 @@ impl GreenNode {
return None;
}
-
- /// Replace one of the node's children.
- pub fn replace_child(&mut self, index: usize, child: impl Into<Green>) {
- self.children[index] = child.into();
- }
}
impl From<GreenNode> for Green {
@@ -352,7 +342,7 @@ impl Debug for RedNode {
}
}
-/// A borrowed wrapper for a green node with span information.
+/// A borrowed wrapper for a [`GreenNode`] with span information.
///
/// Borrowed variant of [`RedNode`]. Can be [cast](Self::cast) to an AST node.
#[derive(Copy, Clone, PartialEq)]
@@ -387,6 +377,26 @@ impl<'a> RedRef<'a> {
Span::new(self.id, self.offset, self.offset + self.green.len())
}
+ /// Whether the node or its children contain an error.
+ pub fn erroneous(self) -> bool {
+ self.green.erroneous()
+ }
+
+ /// The node's children.
+ pub fn children(self) -> Children<'a> {
+ let children = match &self.green {
+ Green::Node(node) => node.children(),
+ Green::Token(_) => &[],
+ };
+
+ Children {
+ id: self.id,
+ iter: children.iter(),
+ front: self.offset,
+ back: self.offset + self.len(),
+ }
+ }
+
/// The error messages for this node and its descendants.
pub fn errors(self) -> Vec<Error> {
if !self.green.erroneous() {
@@ -419,21 +429,6 @@ impl<'a> RedRef<'a> {
T::from_red(self)
}
- /// The node's children.
- pub fn children(self) -> Children<'a> {
- let children = match &self.green {
- Green::Node(node) => node.children(),
- Green::Token(_) => &[],
- };
-
- Children {
- id: self.id,
- iter: children.iter(),
- front: self.offset,
- back: self.offset + self.len(),
- }
- }
-
/// Get the first child that can cast to some AST type.
pub fn cast_first_child<T: TypedNode>(self) -> Option<T> {
self.children().find_map(RedRef::cast)