2019-04-07 13:42:53 +00:00
|
|
|
use ra_parser::{TokenSource};
|
|
|
|
use ra_syntax::{classify_literal, SmolStr, SyntaxKind, SyntaxKind::*};
|
2019-04-08 07:58:02 +00:00
|
|
|
use std::cell::{RefCell};
|
2019-04-07 13:42:53 +00:00
|
|
|
|
2019-04-08 07:58:02 +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,
|
|
|
|
pub n_tokens: usize,
|
|
|
|
}
|
|
|
|
|
2019-04-08 07:58:02 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2019-04-08 12:32:21 +00:00
|
|
|
enum WalkCursor {
|
2019-04-08 07:58:02 +00:00
|
|
|
DelimiterBegin(Option<TtToken>),
|
|
|
|
Token(usize, Option<TtToken>),
|
|
|
|
DelimiterEnd(Option<TtToken>),
|
|
|
|
Eof,
|
2019-04-07 13:42:53 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 10:21:48 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct SubTreeWalker<'a> {
|
|
|
|
pos: usize,
|
|
|
|
stack: Vec<(&'a tt::Subtree, Option<usize>)>,
|
2019-04-08 12:32:21 +00:00
|
|
|
cursor: WalkCursor,
|
2019-04-08 10:21:48 +00:00
|
|
|
last_steps: Vec<usize>,
|
|
|
|
subtree: &'a tt::Subtree,
|
|
|
|
}
|
|
|
|
|
2019-04-08 07:58:02 +00:00
|
|
|
impl<'a> SubTreeWalker<'a> {
|
|
|
|
fn new(subtree: &tt::Subtree) -> SubTreeWalker {
|
|
|
|
let mut res = SubTreeWalker {
|
|
|
|
pos: 0,
|
|
|
|
stack: vec![],
|
2019-04-08 12:32:21 +00:00
|
|
|
cursor: WalkCursor::Eof,
|
2019-04-08 07:58:02 +00:00
|
|
|
last_steps: vec![],
|
|
|
|
subtree,
|
|
|
|
};
|
|
|
|
|
|
|
|
res.reset();
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2019-04-08 12:32:21 +00:00
|
|
|
fn is_eof(&self) -> bool {
|
|
|
|
self.cursor == WalkCursor::Eof
|
|
|
|
}
|
|
|
|
|
2019-04-08 07:58:02 +00:00
|
|
|
fn reset(&mut self) {
|
|
|
|
self.pos = 0;
|
|
|
|
self.stack = vec![(self.subtree, None)];
|
2019-04-08 12:32:21 +00:00
|
|
|
self.cursor = WalkCursor::DelimiterBegin(convert_delim(self.subtree.delimiter, false));
|
2019-04-08 07:58:02 +00:00
|
|
|
self.last_steps = vec![];
|
|
|
|
|
|
|
|
while self.is_empty_delimiter() {
|
|
|
|
self.forward_unchecked();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 12:32:21 +00:00
|
|
|
// This funciton will fast forward the cursor,
|
2019-04-08 07:58:02 +00:00
|
|
|
// Such that backward will stop at `start_pos` point
|
|
|
|
fn start_from_nth(&mut self, start_pos: usize) {
|
|
|
|
self.reset();
|
|
|
|
self.pos = start_pos;
|
2019-04-08 12:32:21 +00:00
|
|
|
self.cursor = self.walk_token(start_pos, 0, false);
|
2019-04-08 07:58:02 +00:00
|
|
|
|
|
|
|
while self.is_empty_delimiter() {
|
|
|
|
self.forward_unchecked();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn current(&self) -> Option<&TtToken> {
|
2019-04-08 12:32:21 +00:00
|
|
|
match &self.cursor {
|
|
|
|
WalkCursor::DelimiterBegin(t) => t.as_ref(),
|
|
|
|
WalkCursor::Token(_, t) => t.as_ref(),
|
|
|
|
WalkCursor::DelimiterEnd(t) => t.as_ref(),
|
|
|
|
WalkCursor::Eof => None,
|
2019-04-08 07:58:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_empty_delimiter(&self) -> bool {
|
2019-04-08 12:32:21 +00:00
|
|
|
match &self.cursor {
|
|
|
|
WalkCursor::DelimiterBegin(None) => true,
|
|
|
|
WalkCursor::DelimiterEnd(None) => true,
|
2019-04-08 07:58:02 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 12:32:21 +00:00
|
|
|
/// Move cursor backward by 1 step with empty checking
|
2019-04-08 07:58:02 +00:00
|
|
|
fn backward(&mut self) {
|
|
|
|
if self.last_steps.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.pos -= 1;
|
|
|
|
loop {
|
|
|
|
self.backward_unchecked();
|
|
|
|
// Skip Empty delimiter
|
|
|
|
if self.last_steps.is_empty() || !self.is_empty_delimiter() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 10:21:48 +00:00
|
|
|
|
2019-04-08 12:32:21 +00:00
|
|
|
// Move forward if it is empty delimiter
|
2019-04-08 10:21:48 +00:00
|
|
|
if self.last_steps.is_empty() {
|
|
|
|
while self.is_empty_delimiter() {
|
|
|
|
self.forward_unchecked();
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 07:58:02 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 12:32:21 +00:00
|
|
|
/// Move cursor backward by 1 step without empty check
|
|
|
|
///
|
|
|
|
/// Depends on the current state of cursor:
|
|
|
|
///
|
|
|
|
/// * Delimiter Begin => Pop the stack, goto last walking token (`walk_token`)
|
|
|
|
/// * Token => Goto prev token (`walk_token`)
|
|
|
|
/// * Delimiter End => Goto the last child token (`walk_token`)
|
|
|
|
/// * Eof => push the root subtree, and set it as Delimiter End
|
2019-04-08 07:58:02 +00:00
|
|
|
fn backward_unchecked(&mut self) {
|
|
|
|
if self.last_steps.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let last_step = self.last_steps.pop().unwrap();
|
2019-04-08 12:32:21 +00:00
|
|
|
let do_walk_token = match self.cursor {
|
|
|
|
WalkCursor::DelimiterBegin(_) => None,
|
|
|
|
WalkCursor::Token(u, _) => Some(u),
|
|
|
|
WalkCursor::DelimiterEnd(_) => {
|
2019-04-08 07:58:02 +00:00
|
|
|
let (top, _) = self.stack.last().unwrap();
|
|
|
|
Some(top.token_trees.len())
|
|
|
|
}
|
2019-04-08 12:32:21 +00:00
|
|
|
WalkCursor::Eof => None,
|
2019-04-08 07:58:02 +00:00
|
|
|
};
|
|
|
|
|
2019-04-08 12:32:21 +00:00
|
|
|
self.cursor = match do_walk_token {
|
|
|
|
Some(u) => self.walk_token(u, last_step, true),
|
|
|
|
None => match self.cursor {
|
|
|
|
WalkCursor::Eof => {
|
2019-04-08 07:58:02 +00:00
|
|
|
self.stack.push((self.subtree, None));
|
2019-04-08 12:32:21 +00:00
|
|
|
WalkCursor::DelimiterEnd(convert_delim(
|
2019-04-08 07:58:02 +00:00
|
|
|
self.stack.last().unwrap().0.delimiter,
|
|
|
|
true,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
_ => {
|
2019-04-08 12:32:21 +00:00
|
|
|
let (_, last_top_cursor) = self.stack.pop().unwrap();
|
2019-04-08 07:58:02 +00:00
|
|
|
assert!(!self.stack.is_empty());
|
|
|
|
|
2019-04-08 12:32:21 +00:00
|
|
|
self.walk_token(last_top_cursor.unwrap(), last_step, true)
|
2019-04-08 07:58:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-08 12:32:21 +00:00
|
|
|
/// Move cursor forward by 1 step with empty checking
|
2019-04-08 07:58:02 +00:00
|
|
|
fn forward(&mut self) {
|
2019-04-08 12:32:21 +00:00
|
|
|
if self.is_eof() {
|
2019-04-08 10:21:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-08 07:58:02 +00:00
|
|
|
self.pos += 1;
|
|
|
|
loop {
|
|
|
|
self.forward_unchecked();
|
|
|
|
if !self.is_empty_delimiter() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 12:32:21 +00:00
|
|
|
/// Move cursor forward by 1 step without empty checking
|
|
|
|
///
|
|
|
|
/// Depends on the current state of cursor:
|
|
|
|
///
|
|
|
|
/// * Delimiter Begin => Goto the first child token (`walk_token`)
|
|
|
|
/// * Token => Goto next token (`walk_token`)
|
|
|
|
/// * Delimiter End => Pop the stack, goto last walking token (`walk_token`)
|
|
|
|
///
|
2019-04-08 07:58:02 +00:00
|
|
|
fn forward_unchecked(&mut self) {
|
2019-04-08 12:32:21 +00:00
|
|
|
if self.is_eof() {
|
2019-04-08 07:58:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let step = self.current().map(|x| x.n_tokens).unwrap_or(1);
|
|
|
|
self.last_steps.push(step);
|
|
|
|
|
2019-04-08 12:32:21 +00:00
|
|
|
let do_walk_token = match self.cursor {
|
|
|
|
WalkCursor::DelimiterBegin(_) => Some((0, 0)),
|
|
|
|
WalkCursor::Token(u, _) => Some((u, step)),
|
|
|
|
WalkCursor::DelimiterEnd(_) => None,
|
2019-04-08 07:58:02 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
2019-04-08 12:32:21 +00:00
|
|
|
self.cursor = match do_walk_token {
|
|
|
|
Some((u, step)) => self.walk_token(u, step, false),
|
2019-04-08 07:58:02 +00:00
|
|
|
None => {
|
|
|
|
let (_, last_top_idx) = self.stack.pop().unwrap();
|
|
|
|
match self.stack.last() {
|
2019-04-08 12:32:21 +00:00
|
|
|
Some(_) => self.walk_token(last_top_idx.unwrap(), 1, false),
|
|
|
|
None => WalkCursor::Eof,
|
2019-04-08 07:58:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-08 12:32:21 +00:00
|
|
|
/// Traversal child token
|
|
|
|
/// Depends on the new position, it returns:
|
|
|
|
///
|
|
|
|
/// * new position < 0 => DelimiterBegin
|
|
|
|
/// * new position > token_tree.len() => DelimiterEnd
|
|
|
|
/// * if new position is a subtree, depends on traversal direction:
|
|
|
|
/// ** backward => DelimiterEnd
|
|
|
|
/// ** forward => DelimiterBegin
|
|
|
|
/// * if new psoition is a leaf, return walk_leaf()
|
|
|
|
fn walk_token(&mut self, pos: usize, offset: usize, backward: bool) -> WalkCursor {
|
2019-04-08 07:58:02 +00:00
|
|
|
let (top, _) = self.stack.last().unwrap();
|
2019-04-08 12:32:21 +00:00
|
|
|
|
|
|
|
if backward && pos < offset {
|
|
|
|
return WalkCursor::DelimiterBegin(convert_delim(
|
|
|
|
self.stack.last().unwrap().0.delimiter,
|
|
|
|
false,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
if !backward && pos + offset >= top.token_trees.len() {
|
|
|
|
return WalkCursor::DelimiterEnd(convert_delim(
|
|
|
|
self.stack.last().unwrap().0.delimiter,
|
|
|
|
true,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let pos = if backward { pos - offset } else { pos + offset };
|
|
|
|
|
2019-04-08 07:58:02 +00:00
|
|
|
match &top.token_trees[pos] {
|
|
|
|
tt::TokenTree::Subtree(subtree) => {
|
|
|
|
self.stack.push((subtree, Some(pos)));
|
|
|
|
let delim = convert_delim(self.stack.last().unwrap().0.delimiter, backward);
|
|
|
|
if backward {
|
2019-04-08 12:32:21 +00:00
|
|
|
WalkCursor::DelimiterEnd(delim)
|
2019-04-08 07:58:02 +00:00
|
|
|
} else {
|
2019-04-08 12:32:21 +00:00
|
|
|
WalkCursor::DelimiterBegin(delim)
|
2019-04-08 07:58:02 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-08 12:32:21 +00:00
|
|
|
tt::TokenTree::Leaf(leaf) => WalkCursor::Token(pos, Some(self.walk_leaf(leaf, pos))),
|
2019-04-08 07:58:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn walk_leaf(&mut self, leaf: &tt::Leaf, pos: usize) -> TtToken {
|
|
|
|
match leaf {
|
|
|
|
tt::Leaf::Literal(l) => convert_literal(l),
|
|
|
|
tt::Leaf::Ident(ident) => convert_ident(ident),
|
|
|
|
tt::Leaf::Punct(punct) => {
|
|
|
|
let (top, _) = self.stack.last().unwrap();
|
|
|
|
convert_punct(punct, top, pos)
|
|
|
|
}
|
|
|
|
}
|
2019-04-07 13:42:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 07:58:02 +00:00
|
|
|
pub(crate) trait Querier {
|
|
|
|
fn token(&self, uidx: usize) -> (SyntaxKind, SmolStr);
|
2019-04-07 13:42:53 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 07:58:02 +00:00
|
|
|
// A wrapper class for ref cell
|
2019-04-08 10:21:48 +00:00
|
|
|
#[derive(Debug)]
|
2019-04-08 07:58:02 +00:00
|
|
|
pub(crate) struct WalkerOwner<'a> {
|
|
|
|
walker: RefCell<SubTreeWalker<'a>>,
|
|
|
|
offset: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> WalkerOwner<'a> {
|
2019-04-08 12:32:21 +00:00
|
|
|
fn new(subtree: &'a tt::Subtree) -> Self {
|
|
|
|
WalkerOwner { walker: RefCell::new(SubTreeWalker::new(subtree)), offset: 0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get<'b>(&self, pos: usize) -> Option<TtToken> {
|
2019-04-08 07:58:02 +00:00
|
|
|
self.set_walker_pos(pos);
|
2019-04-08 10:21:48 +00:00
|
|
|
let walker = self.walker.borrow();
|
2019-04-08 11:21:07 +00:00
|
|
|
walker.current().cloned()
|
2019-04-07 13:42:53 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 07:58:02 +00:00
|
|
|
fn start_from_nth(&mut self, pos: usize) {
|
|
|
|
self.offset = pos;
|
|
|
|
self.walker.borrow_mut().start_from_nth(pos);
|
|
|
|
}
|
2019-04-07 13:42:53 +00:00
|
|
|
|
2019-04-08 07:58:02 +00:00
|
|
|
fn set_walker_pos(&self, mut pos: usize) {
|
|
|
|
pos += self.offset;
|
|
|
|
let mut walker = self.walker.borrow_mut();
|
2019-04-08 12:32:21 +00:00
|
|
|
while pos > walker.pos && !walker.is_eof() {
|
2019-04-08 07:58:02 +00:00
|
|
|
walker.forward();
|
|
|
|
}
|
|
|
|
while pos < walker.pos {
|
|
|
|
walker.backward();
|
2019-04-07 13:42:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 12:32:21 +00:00
|
|
|
fn collect_token_trees(&mut self, n: usize) -> Vec<&tt::TokenTree> {
|
2019-04-08 07:58:02 +00:00
|
|
|
self.start_from_nth(self.offset);
|
|
|
|
|
2019-04-07 13:42:53 +00:00
|
|
|
let mut res = vec![];
|
2019-04-08 07:58:02 +00:00
|
|
|
let mut walker = self.walker.borrow_mut();
|
|
|
|
|
|
|
|
while walker.pos - self.offset < n {
|
2019-04-08 12:32:21 +00:00
|
|
|
if let WalkCursor::Token(u, tt) = &walker.cursor {
|
2019-04-08 07:58:02 +00:00
|
|
|
if walker.stack.len() == 1 {
|
|
|
|
// We only collect the topmost child
|
|
|
|
res.push(&walker.stack[0].0.token_trees[*u]);
|
|
|
|
if let Some(tt) = tt {
|
|
|
|
for i in 0..tt.n_tokens - 1 {
|
|
|
|
res.push(&walker.stack[0].0.token_trees[u + i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
walker.forward();
|
2019-04-07 13:42:53 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 16:12:07 +00:00
|
|
|
res
|
|
|
|
}
|
2019-04-08 07:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Querier for WalkerOwner<'a> {
|
|
|
|
fn token(&self, uidx: usize) -> (SyntaxKind, SmolStr) {
|
2019-04-08 12:32:21 +00:00
|
|
|
let tkn = self.get(uidx).unwrap();
|
2019-04-08 07:58:02 +00:00
|
|
|
(tkn.kind, tkn.text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct SubtreeTokenSource<'a> {
|
|
|
|
walker: WalkerOwner<'a>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> SubtreeTokenSource<'a> {
|
|
|
|
pub fn new(subtree: &tt::Subtree) -> SubtreeTokenSource {
|
|
|
|
SubtreeTokenSource { walker: WalkerOwner::new(subtree) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn start_from_nth(&mut self, n: usize) {
|
|
|
|
self.walker.start_from_nth(n);
|
|
|
|
}
|
2019-04-07 16:12:07 +00:00
|
|
|
|
2019-04-08 07:58:02 +00:00
|
|
|
pub fn querier<'b>(&'a self) -> &'b WalkerOwner<'a>
|
|
|
|
where
|
|
|
|
'a: 'b,
|
|
|
|
{
|
|
|
|
&self.walker
|
2019-04-07 16:12:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 12:32:21 +00:00
|
|
|
pub(crate) fn bump_n(&mut self, parsed_tokens: usize) -> Vec<&tt::TokenTree> {
|
|
|
|
let res = self.walker.collect_token_trees(parsed_tokens);
|
2019-04-08 07:58:02 +00:00
|
|
|
res
|
2019-04-07 13:42:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> TokenSource for SubtreeTokenSource<'a> {
|
|
|
|
fn token_kind(&self, pos: usize) -> SyntaxKind {
|
2019-04-08 12:32:21 +00:00
|
|
|
if let Some(tok) = self.walker.get(pos) {
|
2019-04-07 13:42:53 +00:00
|
|
|
tok.kind
|
|
|
|
} else {
|
|
|
|
SyntaxKind::EOF
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn is_token_joint_to_next(&self, pos: usize) -> bool {
|
2019-04-08 12:32:21 +00:00
|
|
|
self.walker.get(pos).unwrap().is_joint_to_next
|
2019-04-07 13:42:53 +00:00
|
|
|
}
|
|
|
|
fn is_keyword(&self, pos: usize, kw: &str) -> bool {
|
2019-04-08 12:32:21 +00:00
|
|
|
self.walker.get(pos).unwrap().text == *kw
|
2019-04-07 13:42:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TokenPeek<'a, I>
|
|
|
|
where
|
|
|
|
I: Iterator<Item = &'a tt::TokenTree>,
|
|
|
|
{
|
|
|
|
iter: itertools::MultiPeek<I>,
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function
|
|
|
|
fn to_punct(tt: &tt::TokenTree) -> Option<&tt::Punct> {
|
|
|
|
if let tt::TokenTree::Leaf(tt::Leaf::Punct(pp)) = tt {
|
|
|
|
return Some(pp);
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, I> TokenPeek<'a, I>
|
|
|
|
where
|
|
|
|
I: Iterator<Item = &'a tt::TokenTree>,
|
|
|
|
{
|
|
|
|
pub fn new(iter: I) -> Self {
|
|
|
|
TokenPeek { iter: itertools::multipeek(iter) }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn current_punct2(&mut self, p: &tt::Punct) -> Option<((char, char), bool)> {
|
|
|
|
if p.spacing != tt::Spacing::Joint {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.iter.reset_peek();
|
|
|
|
let p1 = to_punct(self.iter.peek()?)?;
|
|
|
|
Some(((p.char, p1.char), p1.spacing == tt::Spacing::Joint))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn current_punct3(&mut self, p: &tt::Punct) -> Option<((char, char, char), bool)> {
|
|
|
|
self.current_punct2(p).and_then(|((p0, p1), last_joint)| {
|
|
|
|
if !last_joint {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let p2 = to_punct(*self.iter.peek()?)?;
|
|
|
|
Some(((p0, p1, p2.char), p2.spacing == tt::Spacing::Joint))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 07:58:02 +00:00
|
|
|
fn convert_multi_char_punct<'b, I>(
|
|
|
|
p: &tt::Punct,
|
|
|
|
iter: &mut TokenPeek<'b, I>,
|
|
|
|
) -> Option<(SyntaxKind, bool, &'static str, usize)>
|
|
|
|
where
|
|
|
|
I: Iterator<Item = &'b tt::TokenTree>,
|
|
|
|
{
|
|
|
|
if let Some((m, is_joint_to_next)) = iter.current_punct3(p) {
|
|
|
|
if let Some((kind, text)) = match m {
|
|
|
|
('<', '<', '=') => Some((SHLEQ, "<<=")),
|
|
|
|
('>', '>', '=') => Some((SHREQ, ">>=")),
|
|
|
|
('.', '.', '.') => Some((DOTDOTDOT, "...")),
|
|
|
|
('.', '.', '=') => Some((DOTDOTEQ, "..=")),
|
|
|
|
_ => None,
|
|
|
|
} {
|
|
|
|
return Some((kind, is_joint_to_next, text, 3));
|
2019-04-07 13:42:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 07:58:02 +00:00
|
|
|
if let Some((m, is_joint_to_next)) = iter.current_punct2(p) {
|
|
|
|
if let Some((kind, text)) = match m {
|
|
|
|
('<', '<') => Some((SHL, "<<")),
|
|
|
|
('>', '>') => Some((SHR, ">>")),
|
|
|
|
|
|
|
|
('|', '|') => Some((PIPEPIPE, "||")),
|
|
|
|
('&', '&') => Some((AMPAMP, "&&")),
|
|
|
|
('%', '=') => Some((PERCENTEQ, "%=")),
|
|
|
|
('*', '=') => Some((STAREQ, "*=")),
|
|
|
|
('/', '=') => Some((SLASHEQ, "/=")),
|
|
|
|
('^', '=') => Some((CARETEQ, "^=")),
|
|
|
|
|
|
|
|
('&', '=') => Some((AMPEQ, "&=")),
|
|
|
|
('|', '=') => Some((PIPEEQ, "|=")),
|
|
|
|
('-', '=') => Some((MINUSEQ, "-=")),
|
|
|
|
('+', '=') => Some((PLUSEQ, "+=")),
|
|
|
|
('>', '=') => Some((GTEQ, ">=")),
|
|
|
|
('<', '=') => Some((LTEQ, "<=")),
|
|
|
|
|
|
|
|
('-', '>') => Some((THIN_ARROW, "->")),
|
|
|
|
('!', '=') => Some((NEQ, "!=")),
|
|
|
|
('=', '>') => Some((FAT_ARROW, "=>")),
|
|
|
|
('=', '=') => Some((EQEQ, "==")),
|
|
|
|
('.', '.') => Some((DOTDOT, "..")),
|
|
|
|
(':', ':') => Some((COLONCOLON, "::")),
|
|
|
|
|
|
|
|
_ => None,
|
|
|
|
} {
|
|
|
|
return Some((kind, is_joint_to_next, text, 2));
|
2019-04-07 13:42:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 07:58:02 +00:00
|
|
|
None
|
|
|
|
}
|
2019-04-07 13:42:53 +00:00
|
|
|
|
2019-04-08 07:58:02 +00:00
|
|
|
fn convert_delim(d: tt::Delimiter, closing: bool) -> Option<TtToken> {
|
|
|
|
let (kinds, texts) = match d {
|
|
|
|
tt::Delimiter::Parenthesis => ([L_PAREN, R_PAREN], "()"),
|
|
|
|
tt::Delimiter::Brace => ([L_CURLY, R_CURLY], "{}"),
|
|
|
|
tt::Delimiter::Bracket => ([L_BRACK, R_BRACK], "[]"),
|
|
|
|
tt::Delimiter::None => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let idx = closing as usize;
|
|
|
|
let kind = kinds[idx];
|
|
|
|
let text = &texts[idx..texts.len() - (1 - idx)];
|
|
|
|
Some(TtToken { kind, is_joint_to_next: false, text: SmolStr::new(text), n_tokens: 1 })
|
|
|
|
}
|
2019-04-07 13:42:53 +00:00
|
|
|
|
2019-04-08 07:58:02 +00:00
|
|
|
fn convert_literal(l: &tt::Literal) -> TtToken {
|
|
|
|
TtToken {
|
|
|
|
kind: classify_literal(&l.text).unwrap().kind,
|
|
|
|
is_joint_to_next: false,
|
|
|
|
text: l.text.clone(),
|
|
|
|
n_tokens: 1,
|
2019-04-07 13:42:53 +00:00
|
|
|
}
|
2019-04-08 07:58:02 +00:00
|
|
|
}
|
2019-04-07 13:42:53 +00:00
|
|
|
|
2019-04-08 07:58:02 +00:00
|
|
|
fn convert_ident(ident: &tt::Ident) -> TtToken {
|
|
|
|
let kind = SyntaxKind::from_keyword(ident.text.as_str()).unwrap_or(IDENT);
|
|
|
|
TtToken { kind, is_joint_to_next: false, text: ident.text.clone(), n_tokens: 1 }
|
|
|
|
}
|
2019-04-07 13:42:53 +00:00
|
|
|
|
2019-04-08 07:58:02 +00:00
|
|
|
fn convert_punct(p: &tt::Punct, parent: &tt::Subtree, next: usize) -> TtToken {
|
2019-04-08 11:21:07 +00:00
|
|
|
let iter = parent.token_trees[next + 1..].iter();
|
2019-04-08 07:58:02 +00:00
|
|
|
let mut peek = TokenPeek::new(iter);
|
|
|
|
|
|
|
|
if let Some((kind, is_joint_to_next, text, size)) = convert_multi_char_punct(p, &mut peek) {
|
|
|
|
TtToken { kind, is_joint_to_next, text: text.into(), n_tokens: size }
|
|
|
|
} else {
|
|
|
|
let kind = match p.char {
|
|
|
|
// lexer may produce combpund tokens for these ones
|
|
|
|
'.' => DOT,
|
|
|
|
':' => COLON,
|
|
|
|
'=' => EQ,
|
|
|
|
'!' => EXCL,
|
|
|
|
'-' => MINUS,
|
|
|
|
c => SyntaxKind::from_char(c).unwrap(),
|
|
|
|
};
|
|
|
|
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, n_tokens: 1 }
|
2019-04-07 13:42:53 +00:00
|
|
|
}
|
|
|
|
}
|