diff options
Diffstat (limited to 'src/syntax')
| -rw-r--r-- | src/syntax/mod.rs | 71 | ||||
| -rw-r--r-- | src/syntax/span.rs | 6 |
2 files changed, 36 insertions, 41 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) diff --git a/src/syntax/span.rs b/src/syntax/span.rs index a707d3d9..430d5f1d 100644 --- a/src/syntax/span.rs +++ b/src/syntax/span.rs @@ -127,12 +127,12 @@ impl Span { /// Create a new span with n characters inserted inside of this span. pub fn inserted(mut self, other: Self, n: usize) -> Self { - if !self.contains(other.start) || !self.contains(other.end) { + if !self.surrounds(other) { panic!(); } - let len_change = (n as isize - other.len() as isize) as usize; - self.end += len_change; + let len_change = n as isize - other.len() as isize; + self.end += len_change as usize; self } |
