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-12 17:50:05 +00:00
|
|
|
// A Sequece of Token,
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
|
|
pub(super) enum TokenSeq<'a> {
|
|
|
|
Subtree(&'a tt::Subtree),
|
|
|
|
Seq(&'a [tt::TokenTree]),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a tt::Subtree> for TokenSeq<'a> {
|
|
|
|
fn from(s: &'a tt::Subtree) -> TokenSeq<'a> {
|
|
|
|
TokenSeq::Subtree(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a [tt::TokenTree]> for TokenSeq<'a> {
|
|
|
|
fn from(s: &'a [tt::TokenTree]) -> TokenSeq<'a> {
|
|
|
|
TokenSeq::Seq(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 20:17:49 +00:00
|
|
|
#[derive(Debug)]
|
2019-04-12 17:50:05 +00:00
|
|
|
enum DelimToken<'a> {
|
|
|
|
Delim(&'a tt::Delimiter, bool),
|
|
|
|
Token(&'a tt::TokenTree),
|
|
|
|
End,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> TokenSeq<'a> {
|
|
|
|
fn get(&self, pos: usize) -> DelimToken<'a> {
|
|
|
|
match self {
|
|
|
|
TokenSeq::Subtree(subtree) => {
|
|
|
|
let len = subtree.token_trees.len() + 2;
|
|
|
|
match pos {
|
|
|
|
p if p >= len => DelimToken::End,
|
|
|
|
p if p == len - 1 => DelimToken::Delim(&subtree.delimiter, true),
|
|
|
|
0 => DelimToken::Delim(&subtree.delimiter, false),
|
|
|
|
p => DelimToken::Token(&subtree.token_trees[p - 1]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TokenSeq::Seq(tokens) => {
|
|
|
|
tokens.get(pos).map(DelimToken::Token).unwrap_or(DelimToken::End)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
match self {
|
|
|
|
TokenSeq::Subtree(subtree) => subtree.token_trees.len() + 2,
|
|
|
|
TokenSeq::Seq(tokens) => tokens.len(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 20:17:49 +00:00
|
|
|
fn child_slice(&self, pos: usize) -> &[tt::TokenTree] {
|
2019-04-12 17:50:05 +00:00
|
|
|
match self {
|
2019-04-21 20:17:49 +00:00
|
|
|
TokenSeq::Subtree(subtree) => &subtree.token_trees[pos - 1..],
|
|
|
|
TokenSeq::Seq(tokens) => &tokens[pos..],
|
2019-04-12 17:50:05 +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-12 17:50:05 +00:00
|
|
|
Token(usize, TtToken),
|
2019-04-08 07:58:02 +00:00
|
|
|
Eof,
|
2019-04-07 13:42:53 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 10:21:48 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct SubTreeWalker<'a> {
|
|
|
|
pos: usize,
|
2019-04-12 17:50:05 +00:00
|
|
|
stack: Vec<(TokenSeq<'a>, usize)>,
|
2019-04-08 12:32:21 +00:00
|
|
|
cursor: WalkCursor,
|
2019-04-08 10:21:48 +00:00
|
|
|
last_steps: Vec<usize>,
|
2019-04-12 17:50:05 +00:00
|
|
|
ts: TokenSeq<'a>,
|
2019-04-08 10:21:48 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 07:58:02 +00:00
|
|
|
impl<'a> SubTreeWalker<'a> {
|
2019-04-12 17:50:05 +00:00
|
|
|
fn new(ts: TokenSeq<'a>) -> SubTreeWalker {
|
2019-04-08 07:58:02 +00:00
|
|
|
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![],
|
2019-04-12 17:50:05 +00:00
|
|
|
ts,
|
2019-04-08 07:58:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2019-04-12 17:50:05 +00:00
|
|
|
self.stack = vec![];
|
2019-04-08 07:58:02 +00:00
|
|
|
self.last_steps = vec![];
|
|
|
|
|
2019-04-12 17:50:05 +00:00
|
|
|
self.cursor = match self.ts.get(0) {
|
|
|
|
DelimToken::Token(token) => match token {
|
|
|
|
tt::TokenTree::Subtree(subtree) => {
|
2019-04-13 10:38:31 +00:00
|
|
|
let ts = TokenSeq::from(subtree);
|
|
|
|
self.stack.push((ts, 0));
|
2019-04-12 17:50:05 +00:00
|
|
|
WalkCursor::Token(0, convert_delim(subtree.delimiter, false))
|
|
|
|
}
|
|
|
|
tt::TokenTree::Leaf(leaf) => {
|
2019-04-21 20:17:49 +00:00
|
|
|
let next_tokens = self.ts.child_slice(0);
|
2019-04-12 17:50:05 +00:00
|
|
|
WalkCursor::Token(0, convert_leaf(&next_tokens, leaf))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
DelimToken::Delim(delim, is_end) => {
|
|
|
|
assert!(!is_end);
|
|
|
|
WalkCursor::Token(0, convert_delim(*delim, false))
|
|
|
|
}
|
|
|
|
DelimToken::End => WalkCursor::Eof,
|
2019-04-08 07:58:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn current(&self) -> Option<&TtToken> {
|
2019-04-08 12:32:21 +00:00
|
|
|
match &self.cursor {
|
2019-04-12 17:50:05 +00:00
|
|
|
WalkCursor::Token(_, t) => Some(t),
|
2019-04-08 12:32:21 +00:00
|
|
|
WalkCursor::Eof => None,
|
2019-04-08 07:58:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 17:50:05 +00:00
|
|
|
fn top(&self) -> &TokenSeq {
|
|
|
|
self.stack.last().map(|(t, _)| t).unwrap_or(&self.ts)
|
2019-04-08 07:58:02 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 17:50:05 +00:00
|
|
|
/// Move cursor backward by 1 step
|
2019-04-08 07:58:02 +00:00
|
|
|
fn backward(&mut self) {
|
|
|
|
if self.last_steps.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-12 17:50:05 +00:00
|
|
|
|
2019-04-08 07:58:02 +00:00
|
|
|
self.pos -= 1;
|
2019-04-12 17:50:05 +00:00
|
|
|
let last_step = self.last_steps.pop().unwrap();
|
2019-04-08 10:21:48 +00:00
|
|
|
|
2019-04-12 17:50:05 +00:00
|
|
|
self.cursor = match self.cursor {
|
|
|
|
WalkCursor::Token(idx, _) => self.walk_token(idx, last_step, true),
|
|
|
|
WalkCursor::Eof => {
|
|
|
|
let len = self.top().len();
|
|
|
|
self.walk_token(len, last_step, true)
|
2019-04-08 10:21:48 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-08 07:58:02 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 17:50:05 +00:00
|
|
|
/// Move cursor forward by 1 step
|
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;
|
|
|
|
|
|
|
|
let step = self.current().map(|x| x.n_tokens).unwrap_or(1);
|
|
|
|
self.last_steps.push(step);
|
|
|
|
|
2019-04-12 17:50:05 +00:00
|
|
|
if let WalkCursor::Token(u, _) = self.cursor {
|
|
|
|
self.cursor = self.walk_token(u, step, false)
|
|
|
|
}
|
2019-04-08 07:58:02 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 12:32:21 +00:00
|
|
|
/// Traversal child token
|
|
|
|
fn walk_token(&mut self, pos: usize, offset: usize, backward: bool) -> WalkCursor {
|
2019-04-12 17:50:05 +00:00
|
|
|
let top = self.stack.last().map(|(t, _)| t).unwrap_or(&self.ts);
|
2019-04-08 12:32:21 +00:00
|
|
|
|
|
|
|
if backward && pos < offset {
|
2019-04-12 17:50:05 +00:00
|
|
|
let (_, last_idx) = self.stack.pop().unwrap();
|
|
|
|
return self.walk_token(last_idx, offset, backward);
|
2019-04-08 12:32:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let pos = if backward { pos - offset } else { pos + offset };
|
|
|
|
|
2019-04-12 17:50:05 +00:00
|
|
|
match top.get(pos) {
|
|
|
|
DelimToken::Token(token) => match token {
|
|
|
|
tt::TokenTree::Subtree(subtree) => {
|
|
|
|
let ts = TokenSeq::from(subtree);
|
|
|
|
let new_idx = if backward { ts.len() - 1 } else { 0 };
|
|
|
|
self.stack.push((ts, pos));
|
|
|
|
WalkCursor::Token(new_idx, convert_delim(subtree.delimiter, backward))
|
|
|
|
}
|
|
|
|
tt::TokenTree::Leaf(leaf) => {
|
2019-04-21 20:17:49 +00:00
|
|
|
let next_tokens = top.child_slice(pos);
|
|
|
|
WalkCursor::Token(pos, convert_leaf(&next_tokens, leaf))
|
2019-04-08 07:58:02 +00:00
|
|
|
}
|
2019-04-12 17:50:05 +00:00
|
|
|
},
|
|
|
|
DelimToken::Delim(delim, is_end) => {
|
|
|
|
WalkCursor::Token(pos, convert_delim(*delim, is_end))
|
2019-04-08 07:58:02 +00:00
|
|
|
}
|
2019-04-12 17:50:05 +00:00
|
|
|
DelimToken::End => {
|
|
|
|
// it is the top level
|
|
|
|
if let Some((_, last_idx)) = self.stack.pop() {
|
|
|
|
assert!(!backward);
|
|
|
|
self.walk_token(last_idx, offset, backward)
|
|
|
|
} else {
|
|
|
|
WalkCursor::Eof
|
|
|
|
}
|
2019-04-08 07:58:02 +00:00
|
|
|
}
|
|
|
|
}
|
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>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> WalkerOwner<'a> {
|
2019-04-12 17:50:05 +00:00
|
|
|
fn new<I: Into<TokenSeq<'a>>>(ts: I) -> Self {
|
|
|
|
WalkerOwner { walker: RefCell::new(SubTreeWalker::new(ts.into())) }
|
2019-04-08 12:32:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get<'b>(&self, pos: usize) -> Option<TtToken> {
|
2019-04-12 17:50:05 +00:00
|
|
|
self.set_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-12 17:50:05 +00:00
|
|
|
fn set_pos(&self, pos: usize) {
|
2019-04-08 07:58:02 +00:00
|
|
|
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-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();
|
2019-04-12 17:50:05 +00:00
|
|
|
walker.reset();
|
2019-04-08 07:58:02 +00:00
|
|
|
|
2019-04-12 17:50:05 +00:00
|
|
|
while walker.pos < n {
|
2019-04-08 12:32:21 +00:00
|
|
|
if let WalkCursor::Token(u, tt) = &walker.cursor {
|
2019-04-12 17:50:05 +00:00
|
|
|
// We only collect the topmost child
|
|
|
|
if walker.stack.len() == 0 {
|
|
|
|
for i in 0..tt.n_tokens {
|
|
|
|
if let DelimToken::Token(token) = walker.ts.get(u + i) {
|
|
|
|
res.push(token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if walker.stack.len() == 1 {
|
2019-04-13 10:38:31 +00:00
|
|
|
if let DelimToken::Delim(_, is_end) = walker.top().get(*u) {
|
2019-04-12 17:50:05 +00:00
|
|
|
if !is_end {
|
|
|
|
let (_, last_idx) = &walker.stack[0];
|
|
|
|
if let DelimToken::Token(token) = walker.ts.get(*last_idx) {
|
|
|
|
res.push(token);
|
|
|
|
}
|
2019-04-08 07:58:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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> {
|
2019-04-12 17:50:05 +00:00
|
|
|
pub fn new<I: Into<TokenSeq<'a>>>(ts: I) -> SubtreeTokenSource<'a> {
|
|
|
|
SubtreeTokenSource { walker: WalkerOwner::new(ts) }
|
2019-04-08 07:58:02 +00:00
|
|
|
}
|
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-13 10:38:31 +00:00
|
|
|
match self.walker.get(pos) {
|
|
|
|
Some(t) => t.is_joint_to_next,
|
|
|
|
_ => false,
|
|
|
|
}
|
2019-04-07 13:42:53 +00:00
|
|
|
}
|
|
|
|
fn is_keyword(&self, pos: usize, kw: &str) -> bool {
|
2019-04-13 10:38:31 +00:00
|
|
|
match self.walker.get(pos) {
|
|
|
|
Some(t) => t.text == *kw,
|
|
|
|
_ => false,
|
|
|
|
}
|
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((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((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-12 17:50:05 +00:00
|
|
|
fn convert_delim(d: tt::Delimiter, closing: bool) -> TtToken {
|
2019-04-08 07:58:02 +00:00
|
|
|
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], "[]"),
|
2019-04-12 17:50:05 +00:00
|
|
|
tt::Delimiter::None => ([L_DOLLAR, R_DOLLAR], ""),
|
2019-04-08 07:58:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let idx = closing as usize;
|
|
|
|
let kind = kinds[idx];
|
2019-04-12 17:50:05 +00:00
|
|
|
let text = if texts.len() > 0 { &texts[idx..texts.len() - (1 - idx)] } else { "" };
|
|
|
|
TtToken { kind, is_joint_to_next: false, text: SmolStr::new(text), n_tokens: 1 }
|
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_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 {
|
2019-04-21 04:32:39 +00:00
|
|
|
let kind = if let Some('\'') = ident.text.chars().next() {
|
|
|
|
LIFETIME
|
|
|
|
} else {
|
|
|
|
SyntaxKind::from_keyword(ident.text.as_str()).unwrap_or(IDENT)
|
|
|
|
};
|
|
|
|
|
2019-04-08 07:58:02 +00:00
|
|
|
TtToken { kind, is_joint_to_next: false, text: ident.text.clone(), n_tokens: 1 }
|
|
|
|
}
|
2019-04-07 13:42:53 +00:00
|
|
|
|
2019-04-12 17:50:05 +00:00
|
|
|
fn convert_punct(p: &tt::Punct, next_tokens: &[tt::TokenTree]) -> TtToken {
|
|
|
|
let mut iter = next_tokens.iter();
|
|
|
|
iter.next();
|
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
|
|
|
}
|
|
|
|
}
|
2019-04-12 17:50:05 +00:00
|
|
|
|
|
|
|
fn convert_leaf(tokens: &[tt::TokenTree], leaf: &tt::Leaf) -> TtToken {
|
|
|
|
match leaf {
|
|
|
|
tt::Leaf::Literal(l) => convert_literal(l),
|
|
|
|
tt::Leaf::Ident(ident) => convert_ident(ident),
|
|
|
|
tt::Leaf::Punct(punct) => convert_punct(punct, tokens),
|
|
|
|
}
|
|
|
|
}
|