aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/subtree_source.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_mbe/src/subtree_source.rs')
-rw-r--r--crates/ra_mbe/src/subtree_source.rs86
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 @@
1use ra_parser::{TokenSource, Token}; 1use ra_parser::{TokenSource, Token};
2use ra_syntax::{classify_literal, SmolStr, SyntaxKind, SyntaxKind::*, T}; 2use ra_syntax::{classify_literal, SmolStr, SyntaxKind, SyntaxKind::*, T};
3use std::cell::{RefCell, Cell}; 3use std::cell::{RefCell, Cell};
4use std::sync::Arc;
5use tt::buffer::{TokenBuffer, Cursor}; 4use tt::buffer::{TokenBuffer, Cursor};
6 5
7pub(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)]
12struct TtToken { 7struct 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 13pub(crate) struct SubtreeTokenSource<'a> {
19#[derive(Debug)]
20pub(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
20impl<'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
26impl<'a> SubtreeWalk<'a> { 31impl<'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
112impl<'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
120pub(crate) struct SubtreeTokenSource<'a> {
121 walker: Arc<SubtreeWalk<'a>>,
122 curr: (Token, usize),
123}
124
125impl<'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
152impl<'a> TokenSource for SubtreeTokenSource<'a> { 134impl<'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 }