2019-04-02 10:02:23 +00:00
|
|
|
//! There are many AstNodes, but only a few tokens, so we hand-write them here.
|
|
|
|
|
2020-07-09 17:21:41 +00:00
|
|
|
use std::{
|
|
|
|
borrow::Cow,
|
|
|
|
convert::{TryFrom, TryInto},
|
|
|
|
};
|
2020-04-24 22:57:47 +00:00
|
|
|
|
2020-07-23 15:23:01 +00:00
|
|
|
use rustc_lexer::unescape::{unescape_literal, Mode};
|
|
|
|
|
2019-04-02 07:23:18 +00:00
|
|
|
use crate::{
|
2020-11-06 17:39:09 +00:00
|
|
|
ast::{self, AstToken},
|
2020-04-24 21:40:41 +00:00
|
|
|
TextRange, TextSize,
|
2019-04-02 07:23:18 +00:00
|
|
|
};
|
|
|
|
|
2020-11-06 17:39:09 +00:00
|
|
|
impl ast::Comment {
|
2019-04-02 09:18:52 +00:00
|
|
|
pub fn kind(&self) -> CommentKind {
|
|
|
|
kind_by_prefix(self.text())
|
2019-04-02 07:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn prefix(&self) -> &'static str {
|
2020-04-28 19:13:37 +00:00
|
|
|
for (prefix, k) in COMMENT_PREFIX_TO_KIND.iter() {
|
|
|
|
if *k == self.kind() && self.text().starts_with(prefix) {
|
|
|
|
return prefix;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unreachable!()
|
2019-04-02 07:23:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 09:18:52 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
|
|
|
pub struct CommentKind {
|
|
|
|
pub shape: CommentShape,
|
|
|
|
pub doc: Option<CommentPlacement>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
|
|
|
pub enum CommentShape {
|
2019-04-02 07:23:18 +00:00
|
|
|
Line,
|
2019-04-02 09:18:52 +00:00
|
|
|
Block,
|
2019-04-02 07:23:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:18:52 +00:00
|
|
|
impl CommentShape {
|
|
|
|
pub fn is_line(self) -> bool {
|
|
|
|
self == CommentShape::Line
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_block(self) -> bool {
|
|
|
|
self == CommentShape::Block
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
|
|
|
pub enum CommentPlacement {
|
|
|
|
Inner,
|
|
|
|
Outer,
|
|
|
|
}
|
|
|
|
|
|
|
|
const COMMENT_PREFIX_TO_KIND: &[(&str, CommentKind)] = {
|
2019-07-04 20:05:17 +00:00
|
|
|
use {CommentPlacement::*, CommentShape::*};
|
2019-04-02 09:18:52 +00:00
|
|
|
&[
|
2020-04-28 08:23:45 +00:00
|
|
|
("////", CommentKind { shape: Line, doc: None }),
|
2019-04-02 09:18:52 +00:00
|
|
|
("///", CommentKind { shape: Line, doc: Some(Outer) }),
|
|
|
|
("//!", CommentKind { shape: Line, doc: Some(Inner) }),
|
|
|
|
("/**", CommentKind { shape: Block, doc: Some(Outer) }),
|
2019-04-02 11:42:47 +00:00
|
|
|
("/*!", CommentKind { shape: Block, doc: Some(Inner) }),
|
2019-04-02 09:18:52 +00:00
|
|
|
("//", CommentKind { shape: Line, doc: None }),
|
|
|
|
("/*", CommentKind { shape: Block, doc: None }),
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
fn kind_by_prefix(text: &str) -> CommentKind {
|
2020-04-25 09:37:34 +00:00
|
|
|
if text == "/**/" {
|
|
|
|
return CommentKind { shape: CommentShape::Block, doc: None };
|
|
|
|
}
|
2019-04-02 09:18:52 +00:00
|
|
|
for (prefix, kind) in COMMENT_PREFIX_TO_KIND.iter() {
|
|
|
|
if text.starts_with(prefix) {
|
|
|
|
return *kind;
|
2019-04-02 07:23:18 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-02 09:18:52 +00:00
|
|
|
panic!("bad comment text: {:?}", text)
|
|
|
|
}
|
2019-04-02 07:23:18 +00:00
|
|
|
|
2020-11-06 17:39:09 +00:00
|
|
|
impl ast::Whitespace {
|
2019-04-02 07:23:18 +00:00
|
|
|
pub fn spans_multiple_lines(&self) -> bool {
|
|
|
|
let text = self.text();
|
|
|
|
text.find('\n').map_or(false, |idx| text[idx + 1..].contains('\n'))
|
|
|
|
}
|
|
|
|
}
|
2019-11-16 19:50:41 +00:00
|
|
|
|
2020-02-27 16:19:53 +00:00
|
|
|
pub struct QuoteOffsets {
|
2020-06-11 23:06:28 +00:00
|
|
|
pub quotes: (TextRange, TextRange),
|
2020-02-27 16:19:53 +00:00
|
|
|
pub contents: TextRange,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl QuoteOffsets {
|
|
|
|
fn new(literal: &str) -> Option<QuoteOffsets> {
|
|
|
|
let left_quote = literal.find('"')?;
|
|
|
|
let right_quote = literal.rfind('"')?;
|
|
|
|
if left_quote == right_quote {
|
|
|
|
// `literal` only contains one quote
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-04-24 21:40:41 +00:00
|
|
|
let start = TextSize::from(0);
|
2020-04-24 22:57:47 +00:00
|
|
|
let left_quote = TextSize::try_from(left_quote).unwrap() + TextSize::of('"');
|
|
|
|
let right_quote = TextSize::try_from(right_quote).unwrap();
|
2020-04-24 21:40:41 +00:00
|
|
|
let end = TextSize::of(literal);
|
2020-02-27 16:19:53 +00:00
|
|
|
|
|
|
|
let res = QuoteOffsets {
|
2020-06-11 23:06:28 +00:00
|
|
|
quotes: (TextRange::new(start, left_quote), TextRange::new(right_quote, end)),
|
2020-04-24 21:40:41 +00:00
|
|
|
contents: TextRange::new(left_quote, right_quote),
|
2020-02-27 16:19:53 +00:00
|
|
|
};
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait HasQuotes: AstToken {
|
|
|
|
fn quote_offsets(&self) -> Option<QuoteOffsets> {
|
|
|
|
let text = self.text().as_str();
|
|
|
|
let offsets = QuoteOffsets::new(text)?;
|
|
|
|
let o = self.syntax().text_range().start();
|
|
|
|
let offsets = QuoteOffsets {
|
2020-06-11 23:06:28 +00:00
|
|
|
quotes: (offsets.quotes.0 + o, offsets.quotes.1 + o),
|
2020-02-27 16:19:53 +00:00
|
|
|
contents: offsets.contents + o,
|
|
|
|
};
|
|
|
|
Some(offsets)
|
|
|
|
}
|
|
|
|
fn open_quote_text_range(&self) -> Option<TextRange> {
|
2020-06-11 23:06:28 +00:00
|
|
|
self.quote_offsets().map(|it| it.quotes.0)
|
2020-02-27 16:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn close_quote_text_range(&self) -> Option<TextRange> {
|
2020-06-11 23:06:28 +00:00
|
|
|
self.quote_offsets().map(|it| it.quotes.1)
|
2020-02-27 16:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn text_range_between_quotes(&self) -> Option<TextRange> {
|
|
|
|
self.quote_offsets().map(|it| it.contents)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 17:39:09 +00:00
|
|
|
impl HasQuotes for ast::String {}
|
2020-02-27 16:19:53 +00:00
|
|
|
|
|
|
|
pub trait HasStringValue: HasQuotes {
|
2020-07-09 17:21:41 +00:00
|
|
|
fn value(&self) -> Option<Cow<'_, str>>;
|
2020-02-27 16:19:53 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 21:21:56 +00:00
|
|
|
impl ast::String {
|
|
|
|
pub fn is_raw(&self) -> bool {
|
|
|
|
self.text().starts_with('r')
|
|
|
|
}
|
|
|
|
pub fn map_range_up(&self, range: TextRange) -> Option<TextRange> {
|
|
|
|
let contents_range = self.text_range_between_quotes()?;
|
|
|
|
assert!(TextRange::up_to(contents_range.len()).contains_range(range));
|
|
|
|
Some(range + contents_range.start())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 17:39:09 +00:00
|
|
|
impl HasStringValue for ast::String {
|
2020-07-09 17:21:41 +00:00
|
|
|
fn value(&self) -> Option<Cow<'_, str>> {
|
2020-11-06 21:21:56 +00:00
|
|
|
if self.is_raw() {
|
|
|
|
let text = self.text().as_str();
|
|
|
|
let text =
|
|
|
|
&text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
|
|
|
|
return Some(Cow::Borrowed(text));
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:50:41 +00:00
|
|
|
let text = self.text().as_str();
|
2020-02-27 16:19:53 +00:00
|
|
|
let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
|
2019-11-16 19:50:41 +00:00
|
|
|
|
2020-11-06 17:39:09 +00:00
|
|
|
let mut buf = String::with_capacity(text.len());
|
2019-11-16 19:50:41 +00:00
|
|
|
let mut has_error = false;
|
2020-05-24 11:12:16 +00:00
|
|
|
unescape_literal(text, Mode::Str, &mut |_, unescaped_char| match unescaped_char {
|
2020-02-27 16:19:53 +00:00
|
|
|
Ok(c) => buf.push(c),
|
|
|
|
Err(_) => has_error = true,
|
2019-11-16 19:50:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if has_error {
|
|
|
|
return None;
|
|
|
|
}
|
2020-07-09 17:21:41 +00:00
|
|
|
// FIXME: don't actually allocate for borrowed case
|
|
|
|
let res = if buf == text { Cow::Borrowed(text) } else { Cow::Owned(buf) };
|
|
|
|
Some(res)
|
2019-11-16 19:50:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 21:21:56 +00:00
|
|
|
impl ast::ByteString {
|
|
|
|
pub fn is_raw(&self) -> bool {
|
|
|
|
self.text().starts_with("br")
|
2019-11-16 19:50:41 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-17 07:37:18 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum FormatSpecifier {
|
|
|
|
Open,
|
|
|
|
Close,
|
|
|
|
Integer,
|
|
|
|
Identifier,
|
|
|
|
Colon,
|
|
|
|
Fill,
|
|
|
|
Align,
|
|
|
|
Sign,
|
|
|
|
NumberSign,
|
|
|
|
Zero,
|
|
|
|
DollarSign,
|
|
|
|
Dot,
|
|
|
|
Asterisk,
|
|
|
|
QuestionMark,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait HasFormatSpecifier: AstToken {
|
2020-04-22 13:28:35 +00:00
|
|
|
fn char_ranges(
|
|
|
|
&self,
|
|
|
|
) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>>;
|
|
|
|
|
2020-04-22 08:08:46 +00:00
|
|
|
fn lex_format_specifier<F>(&self, mut callback: F)
|
2020-04-17 07:37:18 +00:00
|
|
|
where
|
|
|
|
F: FnMut(TextRange, FormatSpecifier),
|
|
|
|
{
|
2020-04-22 13:28:35 +00:00
|
|
|
let char_ranges = if let Some(char_ranges) = self.char_ranges() {
|
|
|
|
char_ranges
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
let mut chars = char_ranges.iter().peekable();
|
2020-04-17 07:37:18 +00:00
|
|
|
|
2020-04-22 13:28:35 +00:00
|
|
|
while let Some((range, first_char)) = chars.next() {
|
2020-04-17 07:37:18 +00:00
|
|
|
match first_char {
|
2020-04-22 13:28:35 +00:00
|
|
|
Ok('{') => {
|
2020-04-17 07:37:18 +00:00
|
|
|
// Format specifier, see syntax at https://doc.rust-lang.org/std/fmt/index.html#syntax
|
2020-04-22 13:28:35 +00:00
|
|
|
if let Some((_, Ok('{'))) = chars.peek() {
|
2020-04-17 07:37:18 +00:00
|
|
|
// Escaped format specifier, `{{`
|
|
|
|
chars.next();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-04-22 13:28:35 +00:00
|
|
|
callback(*range, FormatSpecifier::Open);
|
2020-04-17 07:37:18 +00:00
|
|
|
|
|
|
|
// check for integer/identifier
|
2020-04-22 13:28:35 +00:00
|
|
|
match chars
|
|
|
|
.peek()
|
|
|
|
.and_then(|next| next.1.as_ref().ok())
|
|
|
|
.copied()
|
|
|
|
.unwrap_or_default()
|
|
|
|
{
|
2020-04-17 07:37:18 +00:00
|
|
|
'0'..='9' => {
|
|
|
|
// integer
|
2020-04-22 13:28:35 +00:00
|
|
|
read_integer(&mut chars, &mut callback);
|
2020-04-17 07:37:18 +00:00
|
|
|
}
|
2020-04-22 13:28:35 +00:00
|
|
|
c if c == '_' || c.is_alphabetic() => {
|
2020-04-17 07:37:18 +00:00
|
|
|
// identifier
|
2020-04-22 13:28:35 +00:00
|
|
|
read_identifier(&mut chars, &mut callback);
|
2020-04-17 07:37:18 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2020-04-22 13:28:35 +00:00
|
|
|
if let Some((_, Ok(':'))) = chars.peek() {
|
|
|
|
skip_char_and_emit(&mut chars, FormatSpecifier::Colon, &mut callback);
|
2020-04-17 07:37:18 +00:00
|
|
|
|
|
|
|
// check for fill/align
|
|
|
|
let mut cloned = chars.clone().take(2);
|
2020-04-22 13:28:35 +00:00
|
|
|
let first = cloned
|
|
|
|
.next()
|
|
|
|
.and_then(|next| next.1.as_ref().ok())
|
|
|
|
.copied()
|
|
|
|
.unwrap_or_default();
|
|
|
|
let second = cloned
|
|
|
|
.next()
|
|
|
|
.and_then(|next| next.1.as_ref().ok())
|
|
|
|
.copied()
|
|
|
|
.unwrap_or_default();
|
2020-04-17 07:37:18 +00:00
|
|
|
match second {
|
|
|
|
'<' | '^' | '>' => {
|
|
|
|
// alignment specifier, first char specifies fillment
|
|
|
|
skip_char_and_emit(
|
|
|
|
&mut chars,
|
|
|
|
FormatSpecifier::Fill,
|
2020-04-22 08:08:46 +00:00
|
|
|
&mut callback,
|
2020-04-17 07:37:18 +00:00
|
|
|
);
|
|
|
|
skip_char_and_emit(
|
|
|
|
&mut chars,
|
|
|
|
FormatSpecifier::Align,
|
2020-04-22 08:08:46 +00:00
|
|
|
&mut callback,
|
2020-04-17 07:37:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => match first {
|
|
|
|
'<' | '^' | '>' => {
|
|
|
|
skip_char_and_emit(
|
|
|
|
&mut chars,
|
|
|
|
FormatSpecifier::Align,
|
2020-04-22 08:08:46 +00:00
|
|
|
&mut callback,
|
2020-04-17 07:37:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for sign
|
2020-04-22 13:28:35 +00:00
|
|
|
match chars
|
|
|
|
.peek()
|
|
|
|
.and_then(|next| next.1.as_ref().ok())
|
|
|
|
.copied()
|
|
|
|
.unwrap_or_default()
|
|
|
|
{
|
2020-04-17 07:37:18 +00:00
|
|
|
'+' | '-' => {
|
|
|
|
skip_char_and_emit(
|
|
|
|
&mut chars,
|
|
|
|
FormatSpecifier::Sign,
|
2020-04-22 08:08:46 +00:00
|
|
|
&mut callback,
|
2020-04-17 07:37:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for `#`
|
2020-04-22 13:28:35 +00:00
|
|
|
if let Some((_, Ok('#'))) = chars.peek() {
|
2020-04-17 07:37:18 +00:00
|
|
|
skip_char_and_emit(
|
|
|
|
&mut chars,
|
|
|
|
FormatSpecifier::NumberSign,
|
2020-04-22 08:08:46 +00:00
|
|
|
&mut callback,
|
2020-04-17 07:37:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for `0`
|
|
|
|
let mut cloned = chars.clone().take(2);
|
2020-04-22 13:28:35 +00:00
|
|
|
let first = cloned.next().and_then(|next| next.1.as_ref().ok()).copied();
|
|
|
|
let second = cloned.next().and_then(|next| next.1.as_ref().ok()).copied();
|
2020-04-17 07:37:18 +00:00
|
|
|
|
|
|
|
if first == Some('0') && second != Some('$') {
|
2020-04-22 13:28:35 +00:00
|
|
|
skip_char_and_emit(&mut chars, FormatSpecifier::Zero, &mut callback);
|
2020-04-17 07:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// width
|
2020-04-22 13:28:35 +00:00
|
|
|
match chars
|
|
|
|
.peek()
|
|
|
|
.and_then(|next| next.1.as_ref().ok())
|
|
|
|
.copied()
|
|
|
|
.unwrap_or_default()
|
|
|
|
{
|
2020-04-17 07:37:18 +00:00
|
|
|
'0'..='9' => {
|
2020-04-22 13:28:35 +00:00
|
|
|
read_integer(&mut chars, &mut callback);
|
|
|
|
if let Some((_, Ok('$'))) = chars.peek() {
|
2020-04-17 07:37:18 +00:00
|
|
|
skip_char_and_emit(
|
|
|
|
&mut chars,
|
|
|
|
FormatSpecifier::DollarSign,
|
2020-04-22 08:08:46 +00:00
|
|
|
&mut callback,
|
2020-04-17 07:37:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-04-22 13:28:35 +00:00
|
|
|
c if c == '_' || c.is_alphabetic() => {
|
|
|
|
read_identifier(&mut chars, &mut callback);
|
2020-06-07 20:57:24 +00:00
|
|
|
// can be either width (indicated by dollar sign, or type in which case
|
|
|
|
// the next sign has to be `}`)
|
|
|
|
let next =
|
|
|
|
chars.peek().and_then(|next| next.1.as_ref().ok()).copied();
|
|
|
|
match next {
|
|
|
|
Some('$') => skip_char_and_emit(
|
|
|
|
&mut chars,
|
|
|
|
FormatSpecifier::DollarSign,
|
|
|
|
&mut callback,
|
|
|
|
),
|
|
|
|
Some('}') => {
|
|
|
|
skip_char_and_emit(
|
|
|
|
&mut chars,
|
|
|
|
FormatSpecifier::Close,
|
|
|
|
&mut callback,
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
_ => continue,
|
|
|
|
};
|
2020-04-17 07:37:18 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// precision
|
2020-04-22 13:28:35 +00:00
|
|
|
if let Some((_, Ok('.'))) = chars.peek() {
|
|
|
|
skip_char_and_emit(&mut chars, FormatSpecifier::Dot, &mut callback);
|
|
|
|
|
|
|
|
match chars
|
|
|
|
.peek()
|
|
|
|
.and_then(|next| next.1.as_ref().ok())
|
|
|
|
.copied()
|
|
|
|
.unwrap_or_default()
|
|
|
|
{
|
2020-04-17 07:37:18 +00:00
|
|
|
'*' => {
|
|
|
|
skip_char_and_emit(
|
|
|
|
&mut chars,
|
|
|
|
FormatSpecifier::Asterisk,
|
2020-04-22 08:08:46 +00:00
|
|
|
&mut callback,
|
2020-04-17 07:37:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
'0'..='9' => {
|
2020-04-22 13:28:35 +00:00
|
|
|
read_integer(&mut chars, &mut callback);
|
|
|
|
if let Some((_, Ok('$'))) = chars.peek() {
|
2020-04-17 07:37:18 +00:00
|
|
|
skip_char_and_emit(
|
|
|
|
&mut chars,
|
|
|
|
FormatSpecifier::DollarSign,
|
2020-04-22 08:08:46 +00:00
|
|
|
&mut callback,
|
2020-04-17 07:37:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-04-22 13:28:35 +00:00
|
|
|
c if c == '_' || c.is_alphabetic() => {
|
|
|
|
read_identifier(&mut chars, &mut callback);
|
|
|
|
if chars.peek().and_then(|next| next.1.as_ref().ok()).copied()
|
|
|
|
!= Some('$')
|
|
|
|
{
|
2020-04-17 07:37:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
skip_char_and_emit(
|
|
|
|
&mut chars,
|
|
|
|
FormatSpecifier::DollarSign,
|
2020-04-22 08:08:46 +00:00
|
|
|
&mut callback,
|
2020-04-17 07:37:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// type
|
2020-04-22 13:28:35 +00:00
|
|
|
match chars
|
|
|
|
.peek()
|
|
|
|
.and_then(|next| next.1.as_ref().ok())
|
|
|
|
.copied()
|
|
|
|
.unwrap_or_default()
|
|
|
|
{
|
2020-04-17 07:37:18 +00:00
|
|
|
'?' => {
|
|
|
|
skip_char_and_emit(
|
|
|
|
&mut chars,
|
|
|
|
FormatSpecifier::QuestionMark,
|
2020-04-22 08:08:46 +00:00
|
|
|
&mut callback,
|
2020-04-17 07:37:18 +00:00
|
|
|
);
|
|
|
|
}
|
2020-04-22 13:28:35 +00:00
|
|
|
c if c == '_' || c.is_alphabetic() => {
|
|
|
|
read_identifier(&mut chars, &mut callback);
|
2020-04-17 07:37:18 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-07 20:57:24 +00:00
|
|
|
if let Some((_, Ok('}'))) = chars.peek() {
|
|
|
|
skip_char_and_emit(&mut chars, FormatSpecifier::Close, &mut callback);
|
|
|
|
} else {
|
2020-04-17 07:37:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2020-04-22 13:28:35 +00:00
|
|
|
while let Some((_, Ok(next_char))) = chars.peek() {
|
2020-04-17 07:37:18 +00:00
|
|
|
match next_char {
|
|
|
|
'{' => break,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
chars.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-22 13:28:35 +00:00
|
|
|
fn skip_char_and_emit<'a, I, F>(
|
|
|
|
chars: &mut std::iter::Peekable<I>,
|
2020-04-17 07:37:18 +00:00
|
|
|
emit: FormatSpecifier,
|
|
|
|
callback: &mut F,
|
|
|
|
) where
|
2020-04-22 13:28:35 +00:00
|
|
|
I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>,
|
2020-04-17 07:37:18 +00:00
|
|
|
F: FnMut(TextRange, FormatSpecifier),
|
|
|
|
{
|
2020-04-22 13:28:35 +00:00
|
|
|
let (range, _) = chars.next().unwrap();
|
|
|
|
callback(*range, emit);
|
2020-04-17 07:37:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 13:28:35 +00:00
|
|
|
fn read_integer<'a, I, F>(chars: &mut std::iter::Peekable<I>, callback: &mut F)
|
2020-04-17 07:37:18 +00:00
|
|
|
where
|
2020-04-22 13:28:35 +00:00
|
|
|
I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>,
|
2020-04-17 07:37:18 +00:00
|
|
|
F: FnMut(TextRange, FormatSpecifier),
|
|
|
|
{
|
2020-04-22 13:28:35 +00:00
|
|
|
let (mut range, c) = chars.next().unwrap();
|
|
|
|
assert!(c.as_ref().unwrap().is_ascii_digit());
|
|
|
|
while let Some((r, Ok(next_char))) = chars.peek() {
|
|
|
|
if next_char.is_ascii_digit() {
|
|
|
|
chars.next();
|
2020-04-24 21:40:41 +00:00
|
|
|
range = range.cover(*r);
|
2020-04-22 13:28:35 +00:00
|
|
|
} else {
|
|
|
|
break;
|
2020-04-17 07:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-22 13:28:35 +00:00
|
|
|
callback(range, FormatSpecifier::Integer);
|
2020-04-17 07:37:18 +00:00
|
|
|
}
|
2020-04-22 13:28:35 +00:00
|
|
|
|
|
|
|
fn read_identifier<'a, I, F>(chars: &mut std::iter::Peekable<I>, callback: &mut F)
|
2020-04-17 07:37:18 +00:00
|
|
|
where
|
2020-04-22 13:28:35 +00:00
|
|
|
I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>,
|
2020-04-17 07:37:18 +00:00
|
|
|
F: FnMut(TextRange, FormatSpecifier),
|
|
|
|
{
|
2020-04-22 13:28:35 +00:00
|
|
|
let (mut range, c) = chars.next().unwrap();
|
|
|
|
assert!(c.as_ref().unwrap().is_alphabetic() || *c.as_ref().unwrap() == '_');
|
|
|
|
while let Some((r, Ok(next_char))) = chars.peek() {
|
|
|
|
if *next_char == '_' || next_char.is_ascii_digit() || next_char.is_alphabetic() {
|
|
|
|
chars.next();
|
2020-04-24 21:40:41 +00:00
|
|
|
range = range.cover(*r);
|
2020-04-22 13:28:35 +00:00
|
|
|
} else {
|
|
|
|
break;
|
2020-04-17 07:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-22 13:28:35 +00:00
|
|
|
callback(range, FormatSpecifier::Identifier);
|
2020-04-17 07:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 17:39:09 +00:00
|
|
|
impl HasFormatSpecifier for ast::String {
|
2020-04-22 13:28:35 +00:00
|
|
|
fn char_ranges(
|
|
|
|
&self,
|
|
|
|
) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>> {
|
|
|
|
let text = self.text().as_str();
|
|
|
|
let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
|
|
|
|
let offset = self.text_range_between_quotes()?.start() - self.syntax().text_range().start();
|
|
|
|
|
|
|
|
let mut res = Vec::with_capacity(text.len());
|
2020-05-24 11:12:16 +00:00
|
|
|
unescape_literal(text, Mode::Str, &mut |range, unescaped_char| {
|
2020-04-22 13:28:35 +00:00
|
|
|
res.push((
|
2020-04-24 22:57:47 +00:00
|
|
|
TextRange::new(range.start.try_into().unwrap(), range.end.try_into().unwrap())
|
2020-04-24 21:40:41 +00:00
|
|
|
+ offset,
|
2020-04-22 13:28:35 +00:00
|
|
|
unescaped_char,
|
|
|
|
))
|
|
|
|
});
|
|
|
|
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 17:54:01 +00:00
|
|
|
impl ast::IntNumber {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
pub(crate) const SUFFIXES: &'static [&'static str] = &[
|
|
|
|
"u8", "u16", "u32", "u64", "u128", "usize",
|
|
|
|
"i8", "i16", "i32", "i64", "i128", "isize",
|
|
|
|
];
|
|
|
|
|
2020-11-06 18:01:25 +00:00
|
|
|
pub fn radix(&self) -> Radix {
|
|
|
|
match self.text().get(..2).unwrap_or_default() {
|
|
|
|
"0b" => Radix::Binary,
|
|
|
|
"0o" => Radix::Octal,
|
|
|
|
"0x" => Radix::Hexadecimal,
|
|
|
|
_ => Radix::Decimal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn value(&self) -> Option<u128> {
|
2020-11-06 17:54:01 +00:00
|
|
|
let token = self.syntax();
|
|
|
|
|
|
|
|
let mut text = token.text().as_str();
|
2020-11-06 18:01:25 +00:00
|
|
|
if let Some(suffix) = self.suffix() {
|
|
|
|
text = &text[..text.len() - suffix.len()]
|
2020-11-06 17:54:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 18:01:25 +00:00
|
|
|
let radix = self.radix();
|
|
|
|
text = &text[radix.prefix_len()..];
|
|
|
|
|
2020-11-06 17:54:01 +00:00
|
|
|
let buf;
|
|
|
|
if text.contains("_") {
|
|
|
|
buf = text.replace('_', "");
|
|
|
|
text = buf.as_str();
|
|
|
|
};
|
|
|
|
|
2020-11-06 18:01:25 +00:00
|
|
|
let value = u128::from_str_radix(text, radix as u32).ok()?;
|
|
|
|
Some(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn suffix(&self) -> Option<&str> {
|
|
|
|
let text = self.text();
|
|
|
|
// FIXME: don't check a fixed set of suffixes, `1_0_1___lol` is valid
|
|
|
|
// syntax, suffix is `lol`.
|
|
|
|
ast::IntNumber::SUFFIXES.iter().find_map(|suffix| {
|
|
|
|
if text.ends_with(suffix) {
|
|
|
|
return Some(&text[text.len() - suffix.len()..]);
|
|
|
|
}
|
|
|
|
None
|
|
|
|
})
|
2020-11-06 17:54:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ast::FloatNumber {
|
|
|
|
pub(crate) const SUFFIXES: &'static [&'static str] = &["f32", "f64"];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
|
|
|
pub enum Radix {
|
|
|
|
Binary = 2,
|
|
|
|
Octal = 8,
|
|
|
|
Decimal = 10,
|
|
|
|
Hexadecimal = 16,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Radix {
|
|
|
|
pub const ALL: &'static [Radix] =
|
|
|
|
&[Radix::Binary, Radix::Octal, Radix::Decimal, Radix::Hexadecimal];
|
|
|
|
|
|
|
|
const fn prefix_len(&self) -> usize {
|
|
|
|
match self {
|
|
|
|
Self::Decimal => 0,
|
|
|
|
_ => 2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|