diff options
Diffstat (limited to 'src/yellow/syntax.rs')
-rw-r--r-- | src/yellow/syntax.rs | 40 |
1 files changed, 10 insertions, 30 deletions
diff --git a/src/yellow/syntax.rs b/src/yellow/syntax.rs index 2ba9281fc..6e33310f1 100644 --- a/src/yellow/syntax.rs +++ b/src/yellow/syntax.rs | |||
@@ -1,25 +1,23 @@ | |||
1 | use std::{fmt, ops::Deref, ptr, sync::Arc}; | 1 | use std::{fmt, sync::Arc}; |
2 | 2 | ||
3 | use { | 3 | use { |
4 | yellow::{GreenNode, RedNode}, | 4 | yellow::{RedNode, TreeRoot, SyntaxRoot, RedPtr}, |
5 | SyntaxKind::{self, *}, | 5 | SyntaxKind::{self, *}, |
6 | TextRange, TextUnit, | 6 | TextRange, TextUnit, |
7 | }; | 7 | }; |
8 | 8 | ||
9 | pub trait TreeRoot: Deref<Target = SyntaxRoot> + Clone {} | ||
10 | |||
11 | impl TreeRoot for Arc<SyntaxRoot> {} | ||
12 | |||
13 | impl<'a> TreeRoot for &'a SyntaxRoot {} | ||
14 | 9 | ||
15 | #[derive(Clone, Copy)] | 10 | #[derive(Clone, Copy)] |
16 | pub struct SyntaxNode<R: TreeRoot = Arc<SyntaxRoot>> { | 11 | pub struct SyntaxNode<R: TreeRoot = Arc<SyntaxRoot>> { |
17 | pub(crate) root: R, | 12 | pub(crate) root: R, |
18 | // Guaranteed to not dangle, because `root` holds a | 13 | // Guaranteed to not dangle, because `root` holds a |
19 | // strong reference to red's ancestor | 14 | // strong reference to red's ancestor |
20 | red: ptr::NonNull<RedNode>, | 15 | red: RedPtr, |
21 | } | 16 | } |
22 | 17 | ||
18 | unsafe impl<R: TreeRoot> Send for SyntaxNode<R> {} | ||
19 | unsafe impl<R: TreeRoot> Sync for SyntaxNode<R> {} | ||
20 | |||
23 | impl<R1: TreeRoot, R2: TreeRoot> PartialEq<SyntaxNode<R1>> for SyntaxNode<R2> { | 21 | impl<R1: TreeRoot, R2: TreeRoot> PartialEq<SyntaxNode<R1>> for SyntaxNode<R2> { |
24 | fn eq(&self, other: &SyntaxNode<R1>) -> bool { | 22 | fn eq(&self, other: &SyntaxNode<R1>) -> bool { |
25 | self.red == other.red | 23 | self.red == other.red |
@@ -30,21 +28,6 @@ impl<R: TreeRoot> Eq for SyntaxNode<R> {} | |||
30 | 28 | ||
31 | pub type SyntaxNodeRef<'a> = SyntaxNode<&'a SyntaxRoot>; | 29 | pub type SyntaxNodeRef<'a> = SyntaxNode<&'a SyntaxRoot>; |
32 | 30 | ||
33 | #[derive(Debug)] | ||
34 | pub struct SyntaxRoot { | ||
35 | red: RedNode, | ||
36 | pub(crate) errors: Vec<SyntaxError>, | ||
37 | } | ||
38 | |||
39 | impl SyntaxRoot { | ||
40 | pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SyntaxRoot { | ||
41 | SyntaxRoot { | ||
42 | red: RedNode::new_root(green), | ||
43 | errors, | ||
44 | } | ||
45 | } | ||
46 | } | ||
47 | |||
48 | #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] | 31 | #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] |
49 | pub struct SyntaxError { | 32 | pub struct SyntaxError { |
50 | pub msg: String, | 33 | pub msg: String, |
@@ -54,11 +37,8 @@ pub struct SyntaxError { | |||
54 | impl SyntaxNode<Arc<SyntaxRoot>> { | 37 | impl SyntaxNode<Arc<SyntaxRoot>> { |
55 | pub(crate) fn new_owned(root: SyntaxRoot) -> Self { | 38 | pub(crate) fn new_owned(root: SyntaxRoot) -> Self { |
56 | let root = Arc::new(root); | 39 | let root = Arc::new(root); |
57 | let red_weak = ptr::NonNull::from(&root.red); | 40 | let red = RedPtr::new(&root.red); |
58 | SyntaxNode { | 41 | SyntaxNode { root, red } |
59 | root, | ||
60 | red: red_weak, | ||
61 | } | ||
62 | } | 42 | } |
63 | } | 43 | } |
64 | 44 | ||
@@ -66,7 +46,7 @@ impl<R: TreeRoot> SyntaxNode<R> { | |||
66 | pub fn as_ref<'a>(&'a self) -> SyntaxNode<&'a SyntaxRoot> { | 46 | pub fn as_ref<'a>(&'a self) -> SyntaxNode<&'a SyntaxRoot> { |
67 | SyntaxNode { | 47 | SyntaxNode { |
68 | root: &*self.root, | 48 | root: &*self.root, |
69 | red: ptr::NonNull::clone(&self.red), | 49 | red: self.red, |
70 | } | 50 | } |
71 | } | 51 | } |
72 | 52 | ||
@@ -120,7 +100,7 @@ impl<R: TreeRoot> SyntaxNode<R> { | |||
120 | } | 100 | } |
121 | 101 | ||
122 | fn red(&self) -> &RedNode { | 102 | fn red(&self) -> &RedNode { |
123 | unsafe { self.red.as_ref() } | 103 | unsafe { self.red.get(&self.root) } |
124 | } | 104 | } |
125 | } | 105 | } |
126 | 106 | ||