aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_tt
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_tt')
-rw-r--r--crates/ra_tt/src/lib.rs43
1 files changed, 27 insertions, 16 deletions
diff --git a/crates/ra_tt/src/lib.rs b/crates/ra_tt/src/lib.rs
index 20c251ff4..10f424aae 100644
--- a/crates/ra_tt/src/lib.rs
+++ b/crates/ra_tt/src/lib.rs
@@ -33,14 +33,14 @@ impl TokenId {
33 } 33 }
34} 34}
35 35
36#[derive(Debug, Clone, PartialEq, Eq)] 36#[derive(Debug, Clone, PartialEq, Eq, Hash)]
37pub enum TokenTree { 37pub enum TokenTree {
38 Leaf(Leaf), 38 Leaf(Leaf),
39 Subtree(Subtree), 39 Subtree(Subtree),
40} 40}
41impl_froms!(TokenTree: Leaf, Subtree); 41impl_froms!(TokenTree: Leaf, Subtree);
42 42
43#[derive(Debug, Clone, PartialEq, Eq)] 43#[derive(Debug, Clone, PartialEq, Eq, Hash)]
44pub enum Leaf { 44pub enum Leaf {
45 Literal(Literal), 45 Literal(Literal),
46 Punct(Punct), 46 Punct(Punct),
@@ -48,38 +48,45 @@ pub enum Leaf {
48} 48}
49impl_froms!(Leaf: Literal, Punct, Ident); 49impl_froms!(Leaf: Literal, Punct, Ident);
50 50
51#[derive(Debug, Clone, PartialEq, Eq)] 51#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
52pub struct Subtree { 52pub struct Subtree {
53 pub delimiter: Delimiter, 53 pub delimiter: Option<Delimiter>,
54 pub token_trees: Vec<TokenTree>, 54 pub token_trees: Vec<TokenTree>,
55} 55}
56 56
57#[derive(Clone, Copy, Debug, PartialEq, Eq)] 57#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
58pub enum Delimiter { 58pub struct Delimiter {
59 pub id: TokenId,
60 pub kind: DelimiterKind,
61}
62
63#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
64pub enum DelimiterKind {
59 Parenthesis, 65 Parenthesis,
60 Brace, 66 Brace,
61 Bracket, 67 Bracket,
62 None,
63} 68}
64 69
65#[derive(Debug, Clone, PartialEq, Eq)] 70#[derive(Debug, Clone, PartialEq, Eq, Hash)]
66pub struct Literal { 71pub struct Literal {
67 pub text: SmolStr, 72 pub text: SmolStr,
73 pub id: TokenId,
68} 74}
69 75
70#[derive(Debug, Clone, Copy, PartialEq, Eq)] 76#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
71pub struct Punct { 77pub struct Punct {
72 pub char: char, 78 pub char: char,
73 pub spacing: Spacing, 79 pub spacing: Spacing,
80 pub id: TokenId,
74} 81}
75 82
76#[derive(Debug, Clone, Copy, PartialEq, Eq)] 83#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
77pub enum Spacing { 84pub enum Spacing {
78 Alone, 85 Alone,
79 Joint, 86 Joint,
80} 87}
81 88
82#[derive(Debug, Clone, PartialEq, Eq)] 89#[derive(Debug, Clone, PartialEq, Eq, Hash)]
83pub struct Ident { 90pub struct Ident {
84 pub text: SmolStr, 91 pub text: SmolStr,
85 pub id: TokenId, 92 pub id: TokenId,
@@ -96,11 +103,11 @@ impl fmt::Display for TokenTree {
96 103
97impl fmt::Display for Subtree { 104impl fmt::Display for Subtree {
98 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 105 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
99 let (l, r) = match self.delimiter { 106 let (l, r) = match self.delimiter_kind() {
100 Delimiter::Parenthesis => ("(", ")"), 107 Some(DelimiterKind::Parenthesis) => ("(", ")"),
101 Delimiter::Brace => ("{", "}"), 108 Some(DelimiterKind::Brace) => ("{", "}"),
102 Delimiter::Bracket => ("[", "]"), 109 Some(DelimiterKind::Bracket) => ("[", "]"),
103 Delimiter::None => ("", ""), 110 None => ("", ""),
104 }; 111 };
105 f.write_str(l)?; 112 f.write_str(l)?;
106 let mut needs_space = false; 113 let mut needs_space = false;
@@ -164,6 +171,10 @@ impl Subtree {
164 171
165 self.token_trees.len() + children_count 172 self.token_trees.len() + children_count
166 } 173 }
174
175 pub fn delimiter_kind(&self) -> Option<DelimiterKind> {
176 self.delimiter.map(|it| it.kind)
177 }
167} 178}
168 179
169pub mod buffer; 180pub mod buffer;