Switch token trees to use Symbols

This commit is contained in:
Lukas Wirth 2024-07-16 09:59:39 +02:00
parent 0c95aaa08e
commit 93024ad411
51 changed files with 593 additions and 399 deletions

View file

@ -28,8 +28,7 @@ span.workspace = true
# InternIds for the syntax context
base-db.workspace = true
la-arena.workspace = true
# only here to parse via token_to_literal
mbe.workspace = true
intern.workspace = true
[lints]
workspace = true

View file

@ -13,7 +13,6 @@ use base_db::Env;
use paths::{AbsPath, AbsPathBuf};
use span::Span;
use std::{fmt, io, sync::Arc};
use tt::SmolStr;
use serde::{Deserialize, Serialize};
@ -66,7 +65,7 @@ impl MacroDylib {
pub struct ProcMacro {
process: Arc<ProcMacroProcessSrv>,
dylib_path: Arc<AbsPathBuf>,
name: SmolStr,
name: Box<str>,
kind: ProcMacroKind,
}

View file

@ -158,6 +158,7 @@ type ProtocolWrite<W: Write> = for<'o, 'msg> fn(out: &'o mut W, msg: &'msg str)
#[cfg(test)]
mod tests {
use base_db::FileId;
use intern::{sym, Symbol};
use la_arena::RawIdx;
use span::{ErasedFileAstId, Span, SpanAnchor, SyntaxContextId};
use text_size::{TextRange, TextSize};
@ -174,7 +175,7 @@ mod tests {
let token_trees = Box::new([
TokenTree::Leaf(
Ident {
text: "struct".into(),
sym: Symbol::intern("struct"),
span: Span {
range: TextRange::at(TextSize::new(0), TextSize::of("struct")),
anchor,
@ -186,7 +187,7 @@ mod tests {
),
TokenTree::Leaf(
Ident {
text: "Foo".into(),
sym: Symbol::intern("Foo"),
span: Span {
range: TextRange::at(TextSize::new(5), TextSize::of("r#Foo")),
anchor,
@ -197,7 +198,7 @@ mod tests {
.into(),
),
TokenTree::Leaf(Leaf::Literal(Literal {
text: "Foo".into(),
symbol: Symbol::intern("Foo"),
span: Span {
range: TextRange::at(TextSize::new(10), TextSize::of("\"Foo\"")),
anchor,
@ -230,14 +231,14 @@ mod tests {
kind: DelimiterKind::Brace,
},
token_trees: Box::new([TokenTree::Leaf(Leaf::Literal(Literal {
text: "0".into(),
symbol: sym::INTEGER_0.clone(),
span: Span {
range: TextRange::at(TextSize::new(15), TextSize::of("0u32")),
anchor,
ctx: SyntaxContextId::ROOT,
},
kind: tt::LitKind::Integer,
suffix: Some(Box::new("u32".into())),
suffix: Some(sym::u32.clone()),
}))]),
}),
]);

View file

@ -37,6 +37,7 @@
use std::collections::VecDeque;
use intern::Symbol;
use la_arena::RawIdx;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
@ -433,8 +434,8 @@ impl<'a, 'span, S: InternableSpan> Writer<'a, 'span, S> {
let id = self.token_id_of(lit.span);
let (text, suffix) = if self.version >= EXTENDED_LEAF_DATA {
(
self.intern(&lit.text),
lit.suffix.as_ref().map(|s| self.intern(s)).unwrap_or(!0),
self.intern(lit.symbol.as_str()),
lit.suffix.as_ref().map(|s| self.intern(s.as_str())).unwrap_or(!0),
)
} else {
(self.intern_owned(format!("{lit}")), !0)
@ -469,11 +470,11 @@ impl<'a, 'span, S: InternableSpan> Writer<'a, 'span, S> {
let idx = self.ident.len() as u32;
let id = self.token_id_of(ident.span);
let text = if self.version >= EXTENDED_LEAF_DATA {
self.intern(&ident.text)
self.intern(ident.sym.as_str())
} else if ident.is_raw.yes() {
self.intern_owned(format!("r#{}", ident.text,))
self.intern_owned(format!("r#{}", ident.sym.as_str(),))
} else {
self.intern(&ident.text)
self.intern(ident.sym.as_str())
};
self.ident.push(IdentRepr { id, text, is_raw: ident.is_raw.yes() });
idx << 2 | 0b11
@ -555,7 +556,7 @@ impl<'span, S: InternableSpan> Reader<'span, S> {
let span = read_span(repr.id);
tt::Leaf::Literal(if self.version >= EXTENDED_LEAF_DATA {
tt::Literal {
text: text.into(),
symbol: Symbol::intern(text),
span,
kind: match u16::to_le_bytes(repr.kind) {
[0, _] => Err(()),
@ -572,15 +573,15 @@ impl<'span, S: InternableSpan> Reader<'span, S> {
_ => unreachable!(),
},
suffix: if repr.suffix != !0 {
Some(Box::new(
self.text[repr.suffix as usize].as_str().into(),
Some(Symbol::intern(
self.text[repr.suffix as usize].as_str(),
))
} else {
None
},
}
} else {
tt::token_to_literal(text.into(), span)
tt::token_to_literal(text, span)
})
.into()
}
@ -609,7 +610,7 @@ impl<'span, S: InternableSpan> Reader<'span, S> {
tt::IdentIsRaw::split_from_symbol(text)
};
tt::Leaf::Ident(tt::Ident {
text: text.into(),
sym: Symbol::intern(text),
span: read_span(repr.id),
is_raw,
})