blob: 8d509fd47e66770bfdbaea664e172f454a0184cc [file] [log] [blame]
//! proc-macro server implementation
//!
//! Based on idea from <https://github.com/fedochet/rust-proc-macro-expander>
//! The lib-proc-macro server backend is `TokenStream`-agnostic, such that
//! we could provide any TokenStream implementation.
//! The original idea from fedochet is using proc-macro2 as backend,
//! we use tt instead for better integration with RA.
pub mod rust_analyzer_span;
pub mod token_id;
pub(super) fn literal_from_str<Span: Copy>(
s: &str,
span: Span,
) -> Result<proc_macro::bridge::Literal<Span, intern::Symbol>, ()> {
use rustc_lexer::{LiteralKind, Token, TokenKind};
let mut tokens = rustc_lexer::tokenize(s, rustc_lexer::FrontmatterAllowed::No);
let minus_or_lit = tokens.next().unwrap_or(Token { kind: TokenKind::Eof, len: 0 });
let lit = if minus_or_lit.kind == TokenKind::Minus {
let lit = tokens.next().ok_or(())?;
if !matches!(
lit.kind,
TokenKind::Literal { kind: LiteralKind::Int { .. } | LiteralKind::Float { .. }, .. }
) {
return Err(());
}
lit
} else {
minus_or_lit
};
if tokens.next().is_some() {
return Err(());
}
let TokenKind::Literal { kind, suffix_start } = lit.kind else { return Err(()) };
Ok(crate::tt::literal_from_lexer(s, span, kind, suffix_start))
}