diff options
author | Edwin Cheng <[email protected]> | 2019-05-27 15:56:21 +0100 |
---|---|---|
committer | Edwin Cheng <[email protected]> | 2019-05-27 16:20:43 +0100 |
commit | d833ded3b4a12280f3e63e182ca7268a17cff90d (patch) | |
tree | 2e9fb66389163fa1924f0e5e8819fda968eee032 /crates/ra_mbe/src/subtree_source.rs | |
parent | 0d1c6076073c73f57340e256dc25da9d37311ef0 (diff) |
Remove Queier and SubtreeWalk
Diffstat (limited to 'crates/ra_mbe/src/subtree_source.rs')
-rw-r--r-- | crates/ra_mbe/src/subtree_source.rs | 86 |
1 files changed, 34 insertions, 52 deletions
diff --git a/crates/ra_mbe/src/subtree_source.rs b/crates/ra_mbe/src/subtree_source.rs index c4f79f38a..2ba0b1601 100644 --- a/crates/ra_mbe/src/subtree_source.rs +++ b/crates/ra_mbe/src/subtree_source.rs | |||
@@ -1,13 +1,8 @@ | |||
1 | use ra_parser::{TokenSource, Token}; | 1 | use ra_parser::{TokenSource, Token}; |
2 | use ra_syntax::{classify_literal, SmolStr, SyntaxKind, SyntaxKind::*, T}; | 2 | use ra_syntax::{classify_literal, SmolStr, SyntaxKind, SyntaxKind::*, T}; |
3 | use std::cell::{RefCell, Cell}; | 3 | use std::cell::{RefCell, Cell}; |
4 | use std::sync::Arc; | ||
5 | use tt::buffer::{TokenBuffer, Cursor}; | 4 | use tt::buffer::{TokenBuffer, Cursor}; |
6 | 5 | ||
7 | pub(crate) trait Querier { | ||
8 | fn token(&self, uidx: usize) -> (SyntaxKind, SmolStr, bool); | ||
9 | } | ||
10 | |||
11 | #[derive(Debug, Clone, Eq, PartialEq)] | 6 | #[derive(Debug, Clone, Eq, PartialEq)] |
12 | struct TtToken { | 7 | struct TtToken { |
13 | pub kind: SyntaxKind, | 8 | pub kind: SyntaxKind, |
@@ -15,20 +10,47 @@ struct TtToken { | |||
15 | pub text: SmolStr, | 10 | pub text: SmolStr, |
16 | } | 11 | } |
17 | 12 | ||
18 | // A wrapper class for ref cell | 13 | pub(crate) struct SubtreeTokenSource<'a> { |
19 | #[derive(Debug)] | ||
20 | pub(crate) struct SubtreeWalk<'a> { | ||
21 | start: Cursor<'a>, | 14 | start: Cursor<'a>, |
22 | cursor: Cell<Cursor<'a>>, | 15 | cursor: Cell<Cursor<'a>>, |
23 | cached: RefCell<Vec<Option<TtToken>>>, | 16 | cached: RefCell<Vec<Option<TtToken>>>, |
17 | curr: (Token, usize), | ||
18 | } | ||
19 | |||
20 | impl<'a> SubtreeTokenSource<'a> { | ||
21 | // Helper function used in test | ||
22 | #[allow(unused)] | ||
23 | pub fn text(&self) -> SmolStr { | ||
24 | match self.get(self.curr.1) { | ||
25 | Some(tt) => tt.text, | ||
26 | _ => SmolStr::new(""), | ||
27 | } | ||
28 | } | ||
24 | } | 29 | } |
25 | 30 | ||
26 | impl<'a> SubtreeWalk<'a> { | 31 | impl<'a> SubtreeTokenSource<'a> { |
27 | fn new(cursor: Cursor<'a>) -> Self { | 32 | pub fn new(buffer: &'a TokenBuffer) -> SubtreeTokenSource<'a> { |
28 | SubtreeWalk { | 33 | let cursor = buffer.begin(); |
34 | |||
35 | let mut res = SubtreeTokenSource { | ||
36 | curr: (Token { kind: EOF, is_jointed_to_next: false }, 0), | ||
29 | start: cursor, | 37 | start: cursor, |
30 | cursor: Cell::new(cursor), | 38 | cursor: Cell::new(cursor), |
31 | cached: RefCell::new(Vec::with_capacity(10)), | 39 | cached: RefCell::new(Vec::with_capacity(10)), |
40 | }; | ||
41 | res.curr = (res.mk_token(0), 0); | ||
42 | res | ||
43 | } | ||
44 | |||
45 | pub(crate) fn bump_n(&mut self, parsed_tokens: usize) -> Vec<tt::TokenTree> { | ||
46 | let res = self.collect_token_trees(parsed_tokens); | ||
47 | res | ||
48 | } | ||
49 | |||
50 | fn mk_token(&self, pos: usize) -> Token { | ||
51 | match self.get(pos) { | ||
52 | Some(tt) => Token { kind: tt.kind, is_jointed_to_next: tt.is_joint_to_next }, | ||
53 | None => Token { kind: EOF, is_jointed_to_next: false }, | ||
32 | } | 54 | } |
33 | } | 55 | } |
34 | 56 | ||
@@ -109,46 +131,6 @@ impl<'a> SubtreeWalk<'a> { | |||
109 | } | 131 | } |
110 | } | 132 | } |
111 | 133 | ||
112 | impl<'a> Querier for SubtreeWalk<'a> { | ||
113 | fn token(&self, uidx: usize) -> (SyntaxKind, SmolStr, bool) { | ||
114 | self.get(uidx) | ||
115 | .map(|tkn| (tkn.kind, tkn.text, tkn.is_joint_to_next)) | ||
116 | .unwrap_or_else(|| (SyntaxKind::EOF, "".into(), false)) | ||
117 | } | ||
118 | } | ||
119 | |||
120 | pub(crate) struct SubtreeTokenSource<'a> { | ||
121 | walker: Arc<SubtreeWalk<'a>>, | ||
122 | curr: (Token, usize), | ||
123 | } | ||
124 | |||
125 | impl<'a> SubtreeTokenSource<'a> { | ||
126 | pub fn new(buffer: &'a TokenBuffer) -> SubtreeTokenSource<'a> { | ||
127 | let mut res = SubtreeTokenSource { | ||
128 | walker: Arc::new(SubtreeWalk::new(buffer.begin())), | ||
129 | curr: (Token { kind: EOF, is_jointed_to_next: false }, 0), | ||
130 | }; | ||
131 | res.curr = (res.mk_token(0), 0); | ||
132 | res | ||
133 | } | ||
134 | |||
135 | pub fn querier(&self) -> Arc<SubtreeWalk<'a>> { | ||
136 | self.walker.clone() | ||
137 | } | ||
138 | |||
139 | pub(crate) fn bump_n(&mut self, parsed_tokens: usize) -> Vec<tt::TokenTree> { | ||
140 | let res = self.walker.collect_token_trees(parsed_tokens); | ||
141 | res | ||
142 | } | ||
143 | |||
144 | fn mk_token(&self, pos: usize) -> Token { | ||
145 | match self.walker.get(pos) { | ||
146 | Some(tt) => Token { kind: tt.kind, is_jointed_to_next: tt.is_joint_to_next }, | ||
147 | None => Token { kind: EOF, is_jointed_to_next: false }, | ||
148 | } | ||
149 | } | ||
150 | } | ||
151 | |||
152 | impl<'a> TokenSource for SubtreeTokenSource<'a> { | 134 | impl<'a> TokenSource for SubtreeTokenSource<'a> { |
153 | fn current(&self) -> Token { | 135 | fn current(&self) -> Token { |
154 | self.curr.0 | 136 | self.curr.0 |
@@ -170,7 +152,7 @@ impl<'a> TokenSource for SubtreeTokenSource<'a> { | |||
170 | 152 | ||
171 | /// Is the current token a specified keyword? | 153 | /// Is the current token a specified keyword? |
172 | fn is_keyword(&self, kw: &str) -> bool { | 154 | fn is_keyword(&self, kw: &str) -> bool { |
173 | match self.walker.get(self.curr.1) { | 155 | match self.get(self.curr.1) { |
174 | Some(t) => t.text == *kw, | 156 | Some(t) => t.text == *kw, |
175 | _ => false, | 157 | _ => false, |
176 | } | 158 | } |