#![deny(unstable_features)]
#![doc(
html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/",
html_playground_url = "https://play.rust-lang.org/",
test(attr(deny(warnings)))
)]
#![warn(unreachable_pub)]
use std::{iter, str, string};
pub use Alignment::*;
pub use Count::*;
pub use Piece::*;
pub use Position::*;
use rustc_lexer::unescape;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct InnerSpan {
pub start: usize,
pub end: usize,
}
impl InnerSpan {
pub fn new(start: usize, end: usize) -> InnerSpan {
InnerSpan { start, end }
}
}
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct InnerWidthMapping {
pub position: usize,
pub before: usize,
pub after: usize,
}
impl InnerWidthMapping {
pub fn new(position: usize, before: usize, after: usize) -> InnerWidthMapping {
InnerWidthMapping { position, before, after }
}
}
#[derive(Clone, PartialEq, Eq)]
enum InputStringKind {
NotALiteral,
Literal { width_mappings: Vec<InnerWidthMapping> },
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ParseMode {
Format,
InlineAsm,
}
#[derive(Copy, Clone)]
struct InnerOffset(usize);
impl InnerOffset {
fn to(self, end: InnerOffset) -> InnerSpan {
InnerSpan::new(self.0, end.0)
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Piece<'a> {
String(&'a str),
NextArgument(Box<Argument<'a>>),
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Argument<'a> {
pub position: Position<'a>,
pub position_span: InnerSpan,
pub format: FormatSpec<'a>,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct FormatSpec<'a> {
pub fill: Option<char>,
pub fill_span: Option<InnerSpan>,
pub align: Alignment,
pub sign: Option<Sign>,
pub alternate: bool,
pub zero_pad: bool,
pub debug_hex: Option<DebugHex>,
pub precision: Count<'a>,
pub precision_span: Option<InnerSpan>,
pub width: Count<'a>,
pub width_span: Option<InnerSpan>,
pub ty: &'a str,
pub ty_span: Option<InnerSpan>,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Position<'a> {
ArgumentImplicitlyIs(usize),
ArgumentIs(usize),
ArgumentNamed(&'a str),
}
impl Position<'_> {
pub fn index(&self) -> Option<usize> {
match self {
ArgumentIs(i, ..) | ArgumentImplicitlyIs(i) => Some(*i),
_ => None,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Alignment {
AlignLeft,
AlignRight,
AlignCenter,
AlignUnknown,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Sign {
Plus,
Minus,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum DebugHex {
Lower,
Upper,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Count<'a> {
CountIs(usize),
CountIsName(&'a str, InnerSpan),
CountIsParam(usize),
CountIsStar(usize),
CountImplied,
}
pub struct ParseError {
pub description: string::String,
pub note: Option<string::String>,
pub label: string::String,
pub span: InnerSpan,
pub secondary_label: Option<(string::String, InnerSpan)>,
pub suggestion: Suggestion,
}
pub enum Suggestion {
None,
UsePositional,
RemoveRawIdent(InnerSpan),
}
pub struct Parser<'a> {
mode: ParseMode,
input: &'a str,
cur: iter::Peekable<str::CharIndices<'a>>,
pub errors: Vec<ParseError>,
pub curarg: usize,
style: Option<usize>,
pub arg_places: Vec<InnerSpan>,
width_map: Vec<InnerWidthMapping>,
last_opening_brace: Option<InnerSpan>,
append_newline: bool,
pub is_source_literal: bool,
cur_line_start: usize,
pub line_spans: Vec<InnerSpan>,
}
impl<'a> Iterator for Parser<'a> {
type Item = Piece<'a>;
fn next(&mut self) -> Option<Piece<'a>> {
if let Some(&(pos, c)) = self.cur.peek() {
match c {
'{' => {
let curr_last_brace = self.last_opening_brace;
let byte_pos = self.to_span_index(pos);
let lbrace_end = InnerOffset(byte_pos.0 + self.to_span_width(pos));
self.last_opening_brace = Some(byte_pos.to(lbrace_end));
self.cur.next();
if self.consume('{') {
self.last_opening_brace = curr_last_brace;
Some(String(self.string(pos + 1)))
} else {
let arg = self.argument(lbrace_end);
if let Some(rbrace_pos) = self.consume_closing_brace(&arg) {
if self.is_source_literal {
let lbrace_byte_pos = self.to_span_index(pos);
let rbrace_byte_pos = self.to_span_index(rbrace_pos);
let width = self.to_span_width(rbrace_pos);
self.arg_places.push(
lbrace_byte_pos.to(InnerOffset(rbrace_byte_pos.0 + width)),
);
}
} else if let Some(&(_, maybe)) = self.cur.peek() {
match maybe {
'?' => self.suggest_format_debug(),
'<' | '^' | '>' => self.suggest_format_align(maybe),
_ => self.suggest_positional_arg_instead_of_captured_arg(arg),
}
}
Some(NextArgument(Box::new(arg)))
}
}
'}' => {
self.cur.next();
if self.consume('}') {
Some(String(self.string(pos + 1)))
} else {
let err_pos = self.to_span_index(pos);
self.err_with_note(
"unmatched `}` found",
"unmatched `}`",
"if you intended to print `}`, you can escape it using `}}`",
err_pos.to(err_pos),
);
None
}
}
_ => Some(String(self.string(pos))),
}
} else {
if self.is_source_literal {
let span = self.span(self.cur_line_start, self.input.len());
if self.line_spans.last() != Some(&span) {
self.line_spans.push(span);
}
}
None
}
}
}
impl<'a> Parser<'a> {
pub fn new(
s: &'a str,
style: Option<usize>,
snippet: Option<string::String>,
append_newline: bool,
mode: ParseMode,
) -> Parser<'a> {
let input_string_kind = find_width_map_from_snippet(s, snippet, style);
let (width_map, is_source_literal) = match input_string_kind {
InputStringKind::Literal { width_mappings } => (width_mappings, true),
InputStringKind::NotALiteral => (Vec::new(), false),
};
Parser {
mode,
input: s,
cur: s.char_indices().peekable(),
errors: vec![],
curarg: 0,
style,
arg_places: vec![],
width_map,
last_opening_brace: None,
append_newline,
is_source_literal,
cur_line_start: 0,
line_spans: vec![],
}
}
fn err<S1: Into<string::String>, S2: Into<string::String>>(
&mut self,
description: S1,
label: S2,
span: InnerSpan,
) {
self.errors.push(ParseError {
description: description.into(),
note: None,
label: label.into(),
span,
secondary_label: None,
suggestion: Suggestion::None,
});
}
fn err_with_note<
S1: Into<string::String>,
S2: Into<string::String>,
S3: Into<string::String>,
>(
&mut self,
description: S1,
label: S2,
note: S3,
span: InnerSpan,
) {
self.errors.push(ParseError {
description: description.into(),
note: Some(note.into()),
label: label.into(),
span,
secondary_label: None,
suggestion: Suggestion::None,
});
}
fn consume(&mut self, c: char) -> bool {
self.consume_pos(c).is_some()
}
fn consume_pos(&mut self, c: char) -> Option<usize> {
if let Some(&(pos, maybe)) = self.cur.peek() {
if c == maybe {
self.cur.next();
return Some(pos);
}
}
None
}
fn remap_pos(&self, mut pos: usize) -> InnerOffset {
for width in &self.width_map {
if pos > width.position {
pos += width.before - width.after;
} else if pos == width.position && width.after == 0 {
pos += width.before;
} else {
break;
}
}
InnerOffset(pos)
}
fn to_span_index(&self, pos: usize) -> InnerOffset {
let raw = self.style.map_or(0, |raw| raw + 1);
let pos = self.remap_pos(pos);
InnerOffset(raw + pos.0 + 1)
}
fn to_span_width(&self, pos: usize) -> usize {
let pos = self.remap_pos(pos);
match self.width_map.iter().find(|w| w.position == pos.0) {
Some(w) => w.before,
None => 1,
}
}
fn span(&self, start_pos: usize, end_pos: usize) -> InnerSpan {
let start = self.to_span_index(start_pos);
let end = self.to_span_index(end_pos);
start.to(end)
}
fn consume_closing_brace(&mut self, arg: &Argument<'_>) -> Option<usize> {
self.ws();
let pos;
let description;
if let Some(&(peek_pos, maybe)) = self.cur.peek() {
if maybe == '}' {
self.cur.next();
return Some(peek_pos);
}
pos = peek_pos;
description = format!("expected `}}`, found `{}`", maybe.escape_debug());
} else {
description = "expected `}` but string was terminated".to_owned();
pos = self.input.len() - if self.append_newline { 1 } else { 0 };
}
let pos = self.to_span_index(pos);
let label = "expected `}`".to_owned();
let (note, secondary_label) = if arg.format.fill == Some('}') {
(
Some("the character `}` is interpreted as a fill character because of the `:` that precedes it".to_owned()),
arg.format.fill_span.map(|sp| ("this is not interpreted as a formatting closing brace".to_owned(), sp)),
)
} else {
(
Some("if you intended to print `{`, you can escape it using `{{`".to_owned()),
self.last_opening_brace.map(|sp| ("because of this opening brace".to_owned(), sp)),
)
};
self.errors.push(ParseError {
description,
note,
label,
span: pos.to(pos),
secondary_label,
suggestion: Suggestion::None,
});
None
}
fn ws(&mut self) {
while let Some(&(_, c)) = self.cur.peek() {
if c.is_whitespace() {
self.cur.next();
} else {
break;
}
}
}
fn string(&mut self, start: usize) -> &'a str {
while let Some(&(pos, c)) = self.cur.peek() {
match c {
'{' | '}' => {
return &self.input[start..pos];
}
'\n' if self.is_source_literal => {
self.line_spans.push(self.span(self.cur_line_start, pos));
self.cur_line_start = pos + 1;
self.cur.next();
}
_ => {
if self.is_source_literal && pos == self.cur_line_start && c.is_whitespace() {
self.cur_line_start = pos + c.len_utf8();
}
self.cur.next();
}
}
}
&self.input[start..self.input.len()]
}
fn argument(&mut self, start: InnerOffset) -> Argument<'a> {
let pos = self.position();
let end = self
.cur
.clone()
.find(|(_, ch)| !ch.is_whitespace())
.map_or(start, |(end, _)| self.to_span_index(end));
let position_span = start.to(end);
let format = match self.mode {
ParseMode::Format => self.format(),
ParseMode::InlineAsm => self.inline_asm(),
};
let pos = match pos {
Some(position) => position,
None => {
let i = self.curarg;
self.curarg += 1;
ArgumentImplicitlyIs(i)
}
};
Argument { position: pos, position_span, format }
}
fn position(&mut self) -> Option<Position<'a>> {
if let Some(i) = self.integer() {
Some(ArgumentIs(i))
} else {
match self.cur.peek() {
Some(&(lo, c)) if rustc_lexer::is_id_start(c) => {
let word = self.word();
if word == "r" {
if let Some((pos, '#')) = self.cur.peek() {
if self.input[pos + 1..]
.chars()
.next()
.is_some_and(rustc_lexer::is_id_start)
{
self.cur.next();
let word = self.word();
let prefix_span = self.span(lo, lo + 2);
let full_span = self.span(lo, lo + 2 + word.len());
self.errors.insert(0, ParseError {
description: "raw identifiers are not supported".to_owned(),
note: Some("identifiers in format strings can be keywords and don't need to be prefixed with `r#`".to_string()),
label: "raw identifier used here".to_owned(),
span: full_span,
secondary_label: None,
suggestion: Suggestion::RemoveRawIdent(prefix_span),
});
return Some(ArgumentNamed(word));
}
}
}
Some(ArgumentNamed(word))
}
_ => None,
}
}
}
fn current_pos(&mut self) -> usize {
if let Some(&(pos, _)) = self.cur.peek() { pos } else { self.input.len() }
}
fn format(&mut self) -> FormatSpec<'a> {
let mut spec = FormatSpec {
fill: None,
fill_span: None,
align: AlignUnknown,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountImplied,
precision_span: None,
width: CountImplied,
width_span: None,
ty: &self.input[..0],
ty_span: None,
};
if !self.consume(':') {
return spec;
}
if let Some(&(idx, c)) = self.cur.peek() {
if let Some((_, '>' | '<' | '^')) = self.cur.clone().nth(1) {
spec.fill = Some(c);
spec.fill_span = Some(self.span(idx, idx + 1));
self.cur.next();
}
}
if self.consume('<') {
spec.align = AlignLeft;
} else if self.consume('>') {
spec.align = AlignRight;
} else if self.consume('^') {
spec.align = AlignCenter;
}
if self.consume('+') {
spec.sign = Some(Sign::Plus);
} else if self.consume('-') {
spec.sign = Some(Sign::Minus);
}
if self.consume('#') {
spec.alternate = true;
}
let mut havewidth = false;
if self.consume('0') {
if let Some(end) = self.consume_pos('$') {
spec.width = CountIsParam(0);
spec.width_span = Some(self.span(end - 1, end + 1));
havewidth = true;
} else {
spec.zero_pad = true;
}
}
if !havewidth {
let start = self.current_pos();
spec.width = self.count(start);
if spec.width != CountImplied {
let end = self.current_pos();
spec.width_span = Some(self.span(start, end));
}
}
if let Some(start) = self.consume_pos('.') {
if self.consume('*') {
let i = self.curarg;
self.curarg += 1;
spec.precision = CountIsStar(i);
} else {
spec.precision = self.count(start + 1);
}
let end = self.current_pos();
spec.precision_span = Some(self.span(start, end));
}
let ty_span_start = self.current_pos();
if self.consume('x') {
if self.consume('?') {
spec.debug_hex = Some(DebugHex::Lower);
spec.ty = "?";
} else {
spec.ty = "x";
}
} else if self.consume('X') {
if self.consume('?') {
spec.debug_hex = Some(DebugHex::Upper);
spec.ty = "?";
} else {
spec.ty = "X";
}
} else if self.consume('?') {
spec.ty = "?";
} else {
spec.ty = self.word();
if !spec.ty.is_empty() {
let ty_span_end = self.current_pos();
spec.ty_span = Some(self.span(ty_span_start, ty_span_end));
}
}
spec
}
fn inline_asm(&mut self) -> FormatSpec<'a> {
let mut spec = FormatSpec {
fill: None,
fill_span: None,
align: AlignUnknown,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountImplied,
precision_span: None,
width: CountImplied,
width_span: None,
ty: &self.input[..0],
ty_span: None,
};
if !self.consume(':') {
return spec;
}
let ty_span_start = self.current_pos();
spec.ty = self.word();
if !spec.ty.is_empty() {
let ty_span_end = self.current_pos();
spec.ty_span = Some(self.span(ty_span_start, ty_span_end));
}
spec
}
fn count(&mut self, start: usize) -> Count<'a> {
if let Some(i) = self.integer() {
if self.consume('$') { CountIsParam(i) } else { CountIs(i) }
} else {
let tmp = self.cur.clone();
let word = self.word();
if word.is_empty() {
self.cur = tmp;
CountImplied
} else if let Some(end) = self.consume_pos('$') {
let name_span = self.span(start, end);
CountIsName(word, name_span)
} else {
self.cur = tmp;
CountImplied
}
}
}
fn word(&mut self) -> &'a str {
let start = match self.cur.peek() {
Some(&(pos, c)) if rustc_lexer::is_id_start(c) => {
self.cur.next();
pos
}
_ => {
return "";
}
};
let mut end = None;
while let Some(&(pos, c)) = self.cur.peek() {
if rustc_lexer::is_id_continue(c) {
self.cur.next();
} else {
end = Some(pos);
break;
}
}
let end = end.unwrap_or(self.input.len());
let word = &self.input[start..end];
if word == "_" {
self.err_with_note(
"invalid argument name `_`",
"invalid argument name",
"argument name cannot be a single underscore",
self.span(start, end),
);
}
word
}
fn integer(&mut self) -> Option<usize> {
let mut cur: usize = 0;
let mut found = false;
let mut overflow = false;
let start = self.current_pos();
while let Some(&(_, c)) = self.cur.peek() {
if let Some(i) = c.to_digit(10) {
let (tmp, mul_overflow) = cur.overflowing_mul(10);
let (tmp, add_overflow) = tmp.overflowing_add(i as usize);
if mul_overflow || add_overflow {
overflow = true;
}
cur = tmp;
found = true;
self.cur.next();
} else {
break;
}
}
if overflow {
let end = self.current_pos();
let overflowed_int = &self.input[start..end];
self.err(
format!(
"integer `{}` does not fit into the type `usize` whose range is `0..={}`",
overflowed_int,
usize::MAX
),
"integer out of range for `usize`",
self.span(start, end),
);
}
found.then_some(cur)
}
fn suggest_format_debug(&mut self) {
if let (Some(pos), Some(_)) = (self.consume_pos('?'), self.consume_pos(':')) {
let word = self.word();
let pos = self.to_span_index(pos);
self.errors.insert(0, ParseError {
description: "expected format parameter to occur after `:`".to_owned(),
note: Some(format!("`?` comes after `:`, try `{}:{}` instead", word, "?")),
label: "expected `?` to occur after `:`".to_owned(),
span: pos.to(pos),
secondary_label: None,
suggestion: Suggestion::None,
});
}
}
fn suggest_format_align(&mut self, alignment: char) {
if let Some(pos) = self.consume_pos(alignment) {
let pos = self.to_span_index(pos);
self.errors.insert(0, ParseError {
description: "expected format parameter to occur after `:`".to_owned(),
note: None,
label: format!("expected `{}` to occur after `:`", alignment),
span: pos.to(pos),
secondary_label: None,
suggestion: Suggestion::None,
});
}
}
fn suggest_positional_arg_instead_of_captured_arg(&mut self, arg: Argument<'a>) {
if let Some(end) = self.consume_pos('.') {
let byte_pos = self.to_span_index(end);
let start = InnerOffset(byte_pos.0 + 1);
let field = self.argument(start);
if !self.consume('}') {
return;
}
if let ArgumentNamed(_) = arg.position {
match field.position {
ArgumentNamed(_) => {
self.errors.insert(0, ParseError {
description: "field access isn't supported".to_string(),
note: None,
label: "not supported".to_string(),
span: InnerSpan::new(arg.position_span.start, field.position_span.end),
secondary_label: None,
suggestion: Suggestion::UsePositional,
});
}
ArgumentIs(_) => {
self.errors.insert(0, ParseError {
description: "tuple index access isn't supported".to_string(),
note: None,
label: "not supported".to_string(),
span: InnerSpan::new(arg.position_span.start, field.position_span.end),
secondary_label: None,
suggestion: Suggestion::UsePositional,
});
}
_ => {}
};
}
}
}
}
fn find_width_map_from_snippet(
input: &str,
snippet: Option<string::String>,
str_style: Option<usize>,
) -> InputStringKind {
let snippet = match snippet {
Some(ref s) if s.starts_with('"') || s.starts_with("r\"") || s.starts_with("r#") => s,
_ => return InputStringKind::NotALiteral,
};
if str_style.is_some() {
return InputStringKind::Literal { width_mappings: Vec::new() };
}
let snippet = &snippet[1..snippet.len() - 1];
let input_no_nl = input.trim_end_matches('\n');
let Some(unescaped) = unescape_string(snippet) else {
return InputStringKind::NotALiteral;
};
let unescaped_no_nl = unescaped.trim_end_matches('\n');
if unescaped_no_nl != input_no_nl {
return InputStringKind::NotALiteral;
}
let mut s = snippet.char_indices();
let mut width_mappings = vec![];
while let Some((pos, c)) = s.next() {
match (c, s.clone().next()) {
('\\', Some((_, '\n'))) => {
let _ = s.next();
let mut width = 2;
while let Some((_, c)) = s.clone().next() {
if matches!(c, ' ' | '\n' | '\t') {
width += 1;
let _ = s.next();
} else {
break;
}
}
width_mappings.push(InnerWidthMapping::new(pos, width, 0));
}
('\\', Some((_, 'n' | 't' | 'r' | '0' | '\\' | '\'' | '\"'))) => {
width_mappings.push(InnerWidthMapping::new(pos, 2, 1));
let _ = s.next();
}
('\\', Some((_, 'x'))) => {
s.nth(2);
width_mappings.push(InnerWidthMapping::new(pos, 4, 1));
}
('\\', Some((_, 'u'))) => {
let mut width = 2;
let _ = s.next();
if let Some((_, next_c)) = s.next() {
if next_c == '{' {
let digits_len =
s.clone().take(6).take_while(|(_, c)| c.is_ascii_hexdigit()).count();
let len_utf8 = s
.as_str()
.get(..digits_len)
.and_then(|digits| u32::from_str_radix(digits, 16).ok())
.and_then(char::from_u32)
.map_or(1, char::len_utf8);
let required_skips = digits_len.saturating_sub(len_utf8.saturating_sub(1));
width += required_skips + 2;
s.nth(digits_len);
} else if next_c.is_ascii_hexdigit() {
width += 1;
let mut i = 0; while let (Some((_, c)), _) = (s.next(), i < 6) {
if c.is_ascii_hexdigit() {
width += 1;
} else {
break;
}
i += 1;
}
}
}
width_mappings.push(InnerWidthMapping::new(pos, width, 1));
}
_ => {}
}
}
InputStringKind::Literal { width_mappings }
}
fn unescape_string(string: &str) -> Option<string::String> {
let mut buf = string::String::new();
let mut ok = true;
unescape::unescape_unicode(string, unescape::Mode::Str, &mut |_, unescaped_char| {
match unescaped_char {
Ok(c) => buf.push(c),
Err(_) => ok = false,
}
});
ok.then_some(buf)
}
#[cfg(target_pointer_width = "64")]
rustc_index::static_assert_size!(Piece<'_>, 16);
#[cfg(test)]
mod tests;