rust-analyzer/crates/mbe/src/subtree_source.rs

224 lines
7 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
2020-08-12 15:06:49 +00:00
use parser::{Token, TokenSource};
use std::cell::{Cell, Ref, RefCell};
2020-10-07 09:49:31 +00:00
use syntax::{lex_single_syntax_kind, SmolStr, SyntaxKind, SyntaxKind::*, T};
use tt::buffer::{Cursor, TokenBuffer};
2019-04-07 13:42:53 +00:00
#[derive(Debug, Clone, Eq, PartialEq)]
2019-04-07 13:42:53 +00:00
struct TtToken {
pub kind: SyntaxKind,
pub is_joint_to_next: bool,
pub text: SmolStr,
}
2019-05-27 14:56:21 +00:00
pub(crate) struct SubtreeTokenSource<'a> {
2019-05-27 16:28:46 +00:00
cached_cursor: Cell<Cursor<'a>>,
2019-04-22 14:46:39 +00:00
cached: RefCell<Vec<Option<TtToken>>>,
2019-05-27 14:56:21 +00:00
curr: (Token, usize),
}
impl<'a> SubtreeTokenSource<'a> {
// Helper function used in test
2019-05-28 02:55:08 +00:00
#[cfg(test)]
2019-05-27 14:56:21 +00:00
pub fn text(&self) -> SmolStr {
match *self.get(self.curr.1) {
Some(ref tt) => tt.text.clone(),
2019-05-27 14:56:21 +00:00
_ => SmolStr::new(""),
}
}
}
2019-05-27 14:56:21 +00:00
impl<'a> SubtreeTokenSource<'a> {
pub fn new(buffer: &'a TokenBuffer) -> SubtreeTokenSource<'a> {
let cursor = buffer.begin();
let mut res = SubtreeTokenSource {
curr: (Token { kind: EOF, is_jointed_to_next: false }, 0),
2019-05-27 16:28:46 +00:00
cached_cursor: Cell::new(cursor),
2019-04-22 14:46:39 +00:00
cached: RefCell::new(Vec::with_capacity(10)),
2019-05-27 14:56:21 +00:00
};
res.curr = (res.mk_token(0), 0);
res
}
fn mk_token(&self, pos: usize) -> Token {
match *self.get(pos) {
Some(ref tt) => Token { kind: tt.kind, is_jointed_to_next: tt.is_joint_to_next },
2019-05-27 14:56:21 +00:00
None => Token { kind: EOF, is_jointed_to_next: false },
2019-04-22 14:46:39 +00:00
}
2019-04-08 12:32:21 +00:00
}
fn get(&self, pos: usize) -> Ref<Option<TtToken>> {
2020-04-18 11:28:07 +00:00
fn is_lifetime(c: Cursor) -> Option<(Cursor, SmolStr)> {
let tkn = c.token_tree();
if let Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) = tkn {
if punct.char == '\'' {
let next = c.bump();
if let Some(tt::TokenTree::Leaf(tt::Leaf::Ident(ident))) = next.token_tree() {
let res_cursor = next.bump();
let text = SmolStr::new("'".to_string() + &ident.to_string());
return Some((res_cursor, text));
} else {
panic!("Next token must be ident : {:#?}", next.token_tree());
}
}
}
None
}
if pos < self.cached.borrow().len() {
return Ref::map(self.cached.borrow(), |c| &c[pos]);
2019-04-22 14:46:39 +00:00
}
{
let mut cached = self.cached.borrow_mut();
while pos >= cached.len() {
let cursor = self.cached_cursor.get();
if cursor.eof() {
cached.push(None);
continue;
2019-05-22 18:00:34 +00:00
}
2020-04-18 11:28:07 +00:00
if let Some((curr, text)) = is_lifetime(cursor) {
cached.push(Some(TtToken { kind: LIFETIME, is_joint_to_next: false, text }));
self.cached_cursor.set(curr);
continue;
}
match cursor.token_tree() {
Some(tt::TokenTree::Leaf(leaf)) => {
cached.push(Some(convert_leaf(&leaf)));
2019-05-27 16:28:46 +00:00
self.cached_cursor.set(cursor.bump());
2019-05-22 18:00:34 +00:00
}
Some(tt::TokenTree::Subtree(subtree)) => {
self.cached_cursor.set(cursor.subtree().unwrap());
2019-12-18 03:47:26 +00:00
cached.push(Some(convert_delim(subtree.delimiter_kind(), false)));
}
None => {
if let Some(subtree) = cursor.end() {
2019-12-18 03:47:26 +00:00
cached.push(Some(convert_delim(subtree.delimiter_kind(), true)));
self.cached_cursor.set(cursor.bump());
}
}
2019-05-22 18:00:34 +00:00
}
}
2019-04-22 14:46:39 +00:00
}
Ref::map(self.cached.borrow(), |c| &c[pos])
2019-04-07 13:42:53 +00:00
}
}
2019-04-07 13:42:53 +00:00
impl<'a> TokenSource for SubtreeTokenSource<'a> {
2019-05-25 12:31:53 +00:00
fn current(&self) -> Token {
self.curr.0
2019-04-07 13:42:53 +00:00
}
2019-05-25 12:31:53 +00:00
/// Lookahead n token
fn lookahead_nth(&self, n: usize) -> Token {
self.mk_token(self.curr.1 + n)
}
/// bump cursor to next token
fn bump(&mut self) {
if self.current().kind == EOF {
return;
}
2019-05-25 12:31:53 +00:00
2019-05-27 16:28:46 +00:00
self.curr = (self.mk_token(self.curr.1 + 1), self.curr.1 + 1);
2019-04-07 13:42:53 +00:00
}
2019-05-25 12:31:53 +00:00
/// Is the current token a specified keyword?
fn is_keyword(&self, kw: &str) -> bool {
match *self.get(self.curr.1) {
Some(ref t) => t.text == *kw,
_ => false,
}
2019-04-07 13:42:53 +00:00
}
}
2019-12-18 03:47:26 +00:00
fn convert_delim(d: Option<tt::DelimiterKind>, closing: bool) -> TtToken {
let (kinds, texts) = match d {
2019-12-12 17:41:44 +00:00
Some(tt::DelimiterKind::Parenthesis) => ([T!['('], T![')']], "()"),
Some(tt::DelimiterKind::Brace) => ([T!['{'], T!['}']], "{}"),
Some(tt::DelimiterKind::Bracket) => ([T!['['], T![']']], "[]"),
2019-12-13 13:53:34 +00:00
None => ([L_DOLLAR, R_DOLLAR], ""),
};
let idx = closing as usize;
let kind = kinds[idx];
let text = if !texts.is_empty() { &texts[idx..texts.len() - (1 - idx)] } else { "" };
2019-05-02 02:19:12 +00:00
TtToken { kind, is_joint_to_next: false, text: SmolStr::new(text) }
}
2019-04-07 13:42:53 +00:00
fn convert_literal(l: &tt::Literal) -> TtToken {
2020-10-07 09:49:31 +00:00
let is_negated = l.text.starts_with('-');
let inner_text = &l.text[if is_negated { 1 } else { 0 }..];
let kind = lex_single_syntax_kind(inner_text)
.map(|(kind, _error)| kind)
.filter(|kind| {
kind.is_literal() && (!is_negated || matches!(kind, FLOAT_NUMBER | INT_NUMBER))
})
.unwrap_or_else(|| panic!("Fail to convert given literal {:#?}", &l));
2019-04-24 15:01:32 +00:00
2019-05-02 02:19:12 +00:00
TtToken { kind, is_joint_to_next: false, text: l.text.clone() }
}
2019-04-07 13:42:53 +00:00
fn convert_ident(ident: &tt::Ident) -> TtToken {
let kind = match ident.text.as_ref() {
"true" => T![true],
"false" => T![false],
i if i.starts_with('\'') => LIFETIME,
_ => SyntaxKind::from_keyword(ident.text.as_str()).unwrap_or(IDENT),
};
2019-05-02 02:19:12 +00:00
TtToken { kind, is_joint_to_next: false, text: ident.text.clone() }
}
2019-04-07 13:42:53 +00:00
2019-07-05 16:02:32 +00:00
fn convert_punct(p: tt::Punct) -> TtToken {
2020-04-18 11:28:07 +00:00
let kind = match SyntaxKind::from_char(p.char) {
None => panic!("{:#?} is not a valid punct", p),
Some(kind) => kind,
};
2019-05-02 02:19:12 +00:00
let text = {
let mut buf = [0u8; 4];
let s: &str = p.char.encode_utf8(&mut buf);
SmolStr::new(s)
};
TtToken { kind, is_joint_to_next: p.spacing == tt::Spacing::Joint, text }
2019-04-07 13:42:53 +00:00
}
2019-05-02 02:19:12 +00:00
fn convert_leaf(leaf: &tt::Leaf) -> TtToken {
match leaf {
tt::Leaf::Literal(l) => convert_literal(l),
tt::Leaf::Ident(ident) => convert_ident(ident),
2019-07-05 16:02:32 +00:00
tt::Leaf::Punct(punct) => convert_punct(*punct),
}
}
#[cfg(test)]
mod tests {
use super::{convert_literal, TtToken};
use syntax::{SmolStr, SyntaxKind};
#[test]
fn test_negative_literal() {
assert_eq!(
convert_literal(&tt::Literal {
id: tt::TokenId::unspecified(),
text: SmolStr::new("-42.0")
}),
TtToken {
kind: SyntaxKind::FLOAT_NUMBER,
is_joint_to_next: false,
text: SmolStr::new("-42.0")
}
);
}
}