1#![deny(unstable_features)]
11#![doc(test(attr(deny(warnings), allow(internal_features))))]
12use std::ops::Range;
15
16pub use Alignment::*;
17pub use Count::*;
18pub use Position::*;
19
20#[derive(#[automatically_derived]
impl ::core::marker::Copy for ParseMode { }Copy, #[automatically_derived]
impl ::core::clone::Clone for ParseMode {
#[inline]
fn clone(&self) -> ParseMode { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for ParseMode {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
ParseMode::Format => "Format",
ParseMode::InlineAsm => "InlineAsm",
ParseMode::Diagnostic => "Diagnostic",
})
}
}Debug, #[automatically_derived]
impl ::core::cmp::Eq for ParseMode {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::cmp::PartialEq for ParseMode {
#[inline]
fn eq(&self, other: &ParseMode) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq)]
22pub enum ParseMode {
23 Format,
25 InlineAsm,
27 Diagnostic,
32}
33
34#[derive(#[automatically_derived]
impl<'input> ::core::clone::Clone for Piece<'input> {
#[inline]
fn clone(&self) -> Piece<'input> {
match self {
Piece::Lit(__self_0) =>
Piece::Lit(::core::clone::Clone::clone(__self_0)),
Piece::NextArgument(__self_0) =>
Piece::NextArgument(::core::clone::Clone::clone(__self_0)),
}
}
}Clone, #[automatically_derived]
impl<'input> ::core::fmt::Debug for Piece<'input> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Piece::Lit(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Lit",
&__self_0),
Piece::NextArgument(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"NextArgument", &__self_0),
}
}
}Debug, #[automatically_derived]
impl<'input> ::core::cmp::PartialEq for Piece<'input> {
#[inline]
fn eq(&self, other: &Piece<'input>) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(Piece::Lit(__self_0), Piece::Lit(__arg1_0)) =>
__self_0 == __arg1_0,
(Piece::NextArgument(__self_0), Piece::NextArgument(__arg1_0))
=> __self_0 == __arg1_0,
_ => unsafe { ::core::intrinsics::unreachable() }
}
}
}PartialEq)]
37pub enum Piece<'input> {
38 Lit(&'input str),
40 NextArgument(Box<Argument<'input>>),
43}
44
45#[derive(#[automatically_derived]
impl<'input> ::core::clone::Clone for Argument<'input> {
#[inline]
fn clone(&self) -> Argument<'input> {
Argument {
position: ::core::clone::Clone::clone(&self.position),
position_span: ::core::clone::Clone::clone(&self.position_span),
format: ::core::clone::Clone::clone(&self.format),
}
}
}Clone, #[automatically_derived]
impl<'input> ::core::fmt::Debug for Argument<'input> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f, "Argument",
"position", &self.position, "position_span", &self.position_span,
"format", &&self.format)
}
}Debug, #[automatically_derived]
impl<'input> ::core::cmp::PartialEq for Argument<'input> {
#[inline]
fn eq(&self, other: &Argument<'input>) -> bool {
self.position == other.position &&
self.position_span == other.position_span &&
self.format == other.format
}
}PartialEq)]
47pub struct Argument<'input> {
48 pub position: Position<'input>,
50 pub position_span: Range<usize>,
53 pub format: FormatSpec<'input>,
55}
56
57impl<'input> Argument<'input> {
58 pub fn is_identifier(&self) -> bool {
59 #[allow(non_exhaustive_omitted_patterns)] match self.position {
Position::ArgumentNamed(_) => true,
_ => false,
}matches!(self.position, Position::ArgumentNamed(_)) && self.format == FormatSpec::default()
60 }
61}
62
63#[derive(#[automatically_derived]
impl<'input> ::core::clone::Clone for FormatSpec<'input> {
#[inline]
fn clone(&self) -> FormatSpec<'input> {
FormatSpec {
fill: ::core::clone::Clone::clone(&self.fill),
fill_span: ::core::clone::Clone::clone(&self.fill_span),
align: ::core::clone::Clone::clone(&self.align),
sign: ::core::clone::Clone::clone(&self.sign),
alternate: ::core::clone::Clone::clone(&self.alternate),
zero_pad: ::core::clone::Clone::clone(&self.zero_pad),
debug_hex: ::core::clone::Clone::clone(&self.debug_hex),
precision: ::core::clone::Clone::clone(&self.precision),
precision_span: ::core::clone::Clone::clone(&self.precision_span),
width: ::core::clone::Clone::clone(&self.width),
width_span: ::core::clone::Clone::clone(&self.width_span),
ty: ::core::clone::Clone::clone(&self.ty),
ty_span: ::core::clone::Clone::clone(&self.ty_span),
}
}
}Clone, #[automatically_derived]
impl<'input> ::core::fmt::Debug for FormatSpec<'input> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let names: &'static _ =
&["fill", "fill_span", "align", "sign", "alternate", "zero_pad",
"debug_hex", "precision", "precision_span", "width",
"width_span", "ty", "ty_span"];
let values: &[&dyn ::core::fmt::Debug] =
&[&self.fill, &self.fill_span, &self.align, &self.sign,
&self.alternate, &self.zero_pad, &self.debug_hex,
&self.precision, &self.precision_span, &self.width,
&self.width_span, &self.ty, &&self.ty_span];
::core::fmt::Formatter::debug_struct_fields_finish(f, "FormatSpec",
names, values)
}
}Debug, #[automatically_derived]
impl<'input> ::core::cmp::PartialEq for FormatSpec<'input> {
#[inline]
fn eq(&self, other: &FormatSpec<'input>) -> bool {
self.alternate == other.alternate && self.zero_pad == other.zero_pad
&& self.fill == other.fill &&
self.fill_span == other.fill_span &&
self.align == other.align && self.sign == other.sign &&
self.debug_hex == other.debug_hex &&
self.precision == other.precision &&
self.precision_span == other.precision_span &&
self.width == other.width &&
self.width_span == other.width_span && self.ty == other.ty
&& self.ty_span == other.ty_span
}
}PartialEq, #[automatically_derived]
impl<'input> ::core::default::Default for FormatSpec<'input> {
#[inline]
fn default() -> FormatSpec<'input> {
FormatSpec {
fill: ::core::default::Default::default(),
fill_span: ::core::default::Default::default(),
align: ::core::default::Default::default(),
sign: ::core::default::Default::default(),
alternate: ::core::default::Default::default(),
zero_pad: ::core::default::Default::default(),
debug_hex: ::core::default::Default::default(),
precision: ::core::default::Default::default(),
precision_span: ::core::default::Default::default(),
width: ::core::default::Default::default(),
width_span: ::core::default::Default::default(),
ty: ::core::default::Default::default(),
ty_span: ::core::default::Default::default(),
}
}
}Default)]
65pub struct FormatSpec<'input> {
66 pub fill: Option<char>,
68 pub fill_span: Option<Range<usize>>,
70 pub align: Alignment,
72 pub sign: Option<Sign>,
74 pub alternate: bool,
76 pub zero_pad: bool,
78 pub debug_hex: Option<DebugHex>,
80 pub precision: Count<'input>,
82 pub precision_span: Option<Range<usize>>,
84 pub width: Count<'input>,
86 pub width_span: Option<Range<usize>>,
88 pub ty: &'input str,
92 pub ty_span: Option<Range<usize>>,
94}
95
96#[derive(#[automatically_derived]
impl<'input> ::core::clone::Clone for Position<'input> {
#[inline]
fn clone(&self) -> Position<'input> {
match self {
Position::ArgumentImplicitlyIs(__self_0) =>
Position::ArgumentImplicitlyIs(::core::clone::Clone::clone(__self_0)),
Position::ArgumentIs(__self_0) =>
Position::ArgumentIs(::core::clone::Clone::clone(__self_0)),
Position::ArgumentNamed(__self_0) =>
Position::ArgumentNamed(::core::clone::Clone::clone(__self_0)),
}
}
}Clone, #[automatically_derived]
impl<'input> ::core::fmt::Debug for Position<'input> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Position::ArgumentImplicitlyIs(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"ArgumentImplicitlyIs", &__self_0),
Position::ArgumentIs(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"ArgumentIs", &__self_0),
Position::ArgumentNamed(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"ArgumentNamed", &__self_0),
}
}
}Debug, #[automatically_derived]
impl<'input> ::core::cmp::PartialEq for Position<'input> {
#[inline]
fn eq(&self, other: &Position<'input>) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(Position::ArgumentImplicitlyIs(__self_0),
Position::ArgumentImplicitlyIs(__arg1_0)) =>
__self_0 == __arg1_0,
(Position::ArgumentIs(__self_0),
Position::ArgumentIs(__arg1_0)) => __self_0 == __arg1_0,
(Position::ArgumentNamed(__self_0),
Position::ArgumentNamed(__arg1_0)) => __self_0 == __arg1_0,
_ => unsafe { ::core::intrinsics::unreachable() }
}
}
}PartialEq)]
98pub enum Position<'input> {
99 ArgumentImplicitlyIs(usize),
101 ArgumentIs(usize),
103 ArgumentNamed(&'input str),
105}
106
107impl Position<'_> {
108 pub fn index(&self) -> Option<usize> {
109 match self {
110 ArgumentIs(i, ..) | ArgumentImplicitlyIs(i) => Some(*i),
111 _ => None,
112 }
113 }
114}
115
116#[derive(#[automatically_derived]
impl ::core::marker::Copy for Alignment { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Alignment {
#[inline]
fn clone(&self) -> Alignment { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for Alignment {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
Alignment::AlignLeft => "AlignLeft",
Alignment::AlignRight => "AlignRight",
Alignment::AlignCenter => "AlignCenter",
Alignment::AlignUnknown => "AlignUnknown",
})
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for Alignment {
#[inline]
fn eq(&self, other: &Alignment) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::default::Default for Alignment {
#[inline]
fn default() -> Alignment { Self::AlignUnknown }
}Default)]
118pub enum Alignment {
119 AlignLeft,
121 AlignRight,
123 AlignCenter,
125 #[default]
127 AlignUnknown,
128}
129
130#[derive(#[automatically_derived]
impl ::core::marker::Copy for Sign { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Sign {
#[inline]
fn clone(&self) -> Sign { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for Sign {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self { Sign::Plus => "Plus", Sign::Minus => "Minus", })
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for Sign {
#[inline]
fn eq(&self, other: &Sign) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq)]
132pub enum Sign {
133 Plus,
135 Minus,
137}
138
139#[derive(#[automatically_derived]
impl ::core::marker::Copy for DebugHex { }Copy, #[automatically_derived]
impl ::core::clone::Clone for DebugHex {
#[inline]
fn clone(&self) -> DebugHex { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for DebugHex {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
DebugHex::Lower => "Lower",
DebugHex::Upper => "Upper",
})
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for DebugHex {
#[inline]
fn eq(&self, other: &DebugHex) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq)]
141pub enum DebugHex {
142 Lower,
144 Upper,
146}
147
148#[derive(#[automatically_derived]
impl<'input> ::core::clone::Clone for Count<'input> {
#[inline]
fn clone(&self) -> Count<'input> {
match self {
Count::CountIs(__self_0) =>
Count::CountIs(::core::clone::Clone::clone(__self_0)),
Count::CountIsName(__self_0, __self_1) =>
Count::CountIsName(::core::clone::Clone::clone(__self_0),
::core::clone::Clone::clone(__self_1)),
Count::CountIsParam(__self_0) =>
Count::CountIsParam(::core::clone::Clone::clone(__self_0)),
Count::CountIsStar(__self_0) =>
Count::CountIsStar(::core::clone::Clone::clone(__self_0)),
Count::CountImplied => Count::CountImplied,
}
}
}Clone, #[automatically_derived]
impl<'input> ::core::fmt::Debug for Count<'input> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Count::CountIs(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"CountIs", &__self_0),
Count::CountIsName(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"CountIsName", __self_0, &__self_1),
Count::CountIsParam(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"CountIsParam", &__self_0),
Count::CountIsStar(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"CountIsStar", &__self_0),
Count::CountImplied =>
::core::fmt::Formatter::write_str(f, "CountImplied"),
}
}
}Debug, #[automatically_derived]
impl<'input> ::core::cmp::PartialEq for Count<'input> {
#[inline]
fn eq(&self, other: &Count<'input>) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(Count::CountIs(__self_0), Count::CountIs(__arg1_0)) =>
__self_0 == __arg1_0,
(Count::CountIsName(__self_0, __self_1),
Count::CountIsName(__arg1_0, __arg1_1)) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1,
(Count::CountIsParam(__self_0), Count::CountIsParam(__arg1_0))
=> __self_0 == __arg1_0,
(Count::CountIsStar(__self_0), Count::CountIsStar(__arg1_0))
=> __self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl<'input> ::core::default::Default for Count<'input> {
#[inline]
fn default() -> Count<'input> { Self::CountImplied }
}Default)]
151pub enum Count<'input> {
152 CountIs(u16),
154 CountIsName(&'input str, Range<usize>),
156 CountIsParam(usize),
158 CountIsStar(usize),
160 #[default]
162 CountImplied,
163}
164
165pub struct ParseError {
166 pub description: String,
167 pub note: Option<String>,
168 pub label: String,
169 pub span: Range<usize>,
170 pub secondary_label: Option<(String, Range<usize>)>,
171 pub suggestion: Suggestion,
172}
173
174pub enum Suggestion {
175 None,
176 UsePositional,
179 RemoveRawIdent(Range<usize>),
182 ReorderFormatParameter(Range<usize>, String),
187 AddMissingColon(Range<usize>),
190}
191
192pub struct Parser<'input> {
199 mode: ParseMode,
200 input: &'input str,
202 input_vec: Vec<(Range<usize>, usize, char)>,
204 input_vec_index: usize,
206 pub errors: Vec<ParseError>,
208 pub curarg: usize,
210 pub arg_places: Vec<Range<usize>>,
212 last_open_brace: Option<Range<usize>>,
214 pub is_source_literal: bool,
218 end_of_snippet: usize,
220 cur_line_start: usize,
222 pub line_spans: Vec<Range<usize>>,
225}
226
227impl<'input> Iterator for Parser<'input> {
228 type Item = Piece<'input>;
229
230 fn next(&mut self) -> Option<Piece<'input>> {
231 if let Some((Range { start, end }, idx, ch)) = self.peek() {
232 match ch {
233 '{' => {
234 self.input_vec_index += 1;
235 if let Some((_, i, '{')) = self.peek() {
236 self.input_vec_index += 1;
237 Some(Piece::Lit(self.string(i)))
240 } else {
241 self.last_open_brace = Some(start..end);
243 let arg = self.argument();
244 self.ws();
245 if let Some((close_brace_range, _)) = self.consume_pos('}') {
246 if self.is_source_literal {
247 self.arg_places.push(start..close_brace_range.end);
248 }
249 } else {
250 self.missing_closing_brace(&arg);
251 }
252
253 Some(Piece::NextArgument(Box::new(arg)))
254 }
255 }
256 '}' => {
257 self.input_vec_index += 1;
258 if let Some((_, i, '}')) = self.peek() {
259 self.input_vec_index += 1;
260 Some(Piece::Lit(self.string(i)))
263 } else {
264 self.errors.push(ParseError {
266 description: "unmatched `}` found".into(),
267 note: Some(
268 "if you intended to print `}`, you can escape it using `}}`".into(),
269 ),
270 label: "unmatched `}`".into(),
271 span: start..end,
272 secondary_label: None,
273 suggestion: Suggestion::None,
274 });
275 None
276 }
277 }
278 _ => Some(Piece::Lit(self.string(idx))),
279 }
280 } else {
281 if self.is_source_literal {
283 let span = self.cur_line_start..self.end_of_snippet;
284 if self.line_spans.last() != Some(&span) {
285 self.line_spans.push(span);
286 }
287 }
288 None
289 }
290 }
291}
292
293impl<'input> Parser<'input> {
294 pub fn new(
300 input: &'input str,
301 style: Option<usize>,
302 snippet: Option<String>,
303 appended_newline: bool,
304 mode: ParseMode,
305 ) -> Self {
306 let quote_offset = style.map_or(1, |nr_hashes| nr_hashes + 2);
307
308 let (is_source_literal, end_of_snippet, pre_input_vec) = if let Some(snippet) = snippet {
309 if let Some(nr_hashes) = style {
310 (true, snippet.len() - nr_hashes - 1, ::alloc::vec::Vec::new()vec![])
313 } else {
314 if snippet.starts_with('"') {
316 let without_quotes = &snippet[1..snippet.len() - 1];
319 let (mut ok, mut vec) = (true, ::alloc::vec::Vec::new()vec![]);
320 let mut chars = input.chars();
321 rustc_literal_escaper::unescape_str(without_quotes, |range, res| match res {
322 Ok(ch) if ok && chars.next().is_some_and(|c| ch == c) => {
323 vec.push((range, ch));
324 }
325 _ => {
326 ok = false;
327 vec = ::alloc::vec::Vec::new()vec![];
328 }
329 });
330 let end = vec.last().map(|(r, _)| r.end).unwrap_or(0);
331 if ok {
332 if appended_newline {
333 if chars.as_str() == "\n" {
334 vec.push((end..end + 1, '\n'));
335 (true, 1 + end, vec)
336 } else {
337 (false, snippet.len(), ::alloc::vec::Vec::new()vec![])
338 }
339 } else if chars.as_str() == "" {
340 (true, 1 + end, vec)
341 } else {
342 (false, snippet.len(), ::alloc::vec::Vec::new()vec![])
343 }
344 } else {
345 (false, snippet.len(), ::alloc::vec::Vec::new()vec![])
346 }
347 } else {
348 (false, snippet.len(), ::alloc::vec::Vec::new()vec![])
350 }
351 }
352 } else {
353 (false, input.len() - if appended_newline { 1 } else { 0 }, ::alloc::vec::Vec::new()vec![])
355 };
356
357 let input_vec: Vec<(Range<usize>, usize, char)> = if pre_input_vec.is_empty() {
358 input
361 .char_indices()
362 .map(|(idx, c)| {
363 let i = idx + quote_offset;
364 (i..i + c.len_utf8(), idx, c)
365 })
366 .collect()
367 } else {
368 input
370 .char_indices()
371 .zip(pre_input_vec)
372 .map(|((i, c), (r, _))| (r.start + quote_offset..r.end + quote_offset, i, c))
373 .collect()
374 };
375
376 Parser {
377 mode,
378 input,
379 input_vec,
380 input_vec_index: 0,
381 errors: ::alloc::vec::Vec::new()vec![],
382 curarg: 0,
383 arg_places: ::alloc::vec::Vec::new()vec![],
384 last_open_brace: None,
385 is_source_literal,
386 end_of_snippet,
387 cur_line_start: quote_offset,
388 line_spans: ::alloc::vec::Vec::new()vec![],
389 }
390 }
391
392 pub fn peek(&self) -> Option<(Range<usize>, usize, char)> {
394 self.input_vec.get(self.input_vec_index).cloned()
395 }
396
397 pub fn peek_ahead(&self) -> Option<(Range<usize>, usize, char)> {
399 self.input_vec.get(self.input_vec_index + 1).cloned()
400 }
401
402 fn consume(&mut self, c: char) -> bool {
406 self.consume_pos(c).is_some()
407 }
408
409 fn consume_pos(&mut self, ch: char) -> Option<(Range<usize>, usize)> {
414 if let Some((r, i, c)) = self.peek()
415 && ch == c
416 {
417 self.input_vec_index += 1;
418 return Some((r, i));
419 }
420
421 None
422 }
423
424 fn missing_closing_brace(&mut self, arg: &Argument<'_>) {
426 let (range, description) = if let Some((r, _, c)) = self.peek() {
427 (r.start..r.start, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected `}}`, found `{0}`",
c.escape_debug()))
})format!("expected `}}`, found `{}`", c.escape_debug()))
428 } else {
429 (
430 self.end_of_snippet..self.end_of_snippet,
432 "expected `}` but string was terminated".to_owned(),
433 )
434 };
435
436 let (note, secondary_label) = if arg.format.fill == Some('}') {
437 (
438 Some("the character `}` is interpreted as a fill character because of the `:` that precedes it".to_owned()),
439 arg.format.fill_span.clone().map(|sp| ("this is not interpreted as a formatting closing brace".to_owned(), sp)),
440 )
441 } else {
442 (
443 Some("if you intended to print `{`, you can escape it using `{{`".to_owned()),
444 self.last_open_brace
445 .clone()
446 .map(|sp| ("because of this opening brace".to_owned(), sp)),
447 )
448 };
449
450 self.errors.push(ParseError {
451 description,
452 note,
453 label: "expected `}`".to_owned(),
454 span: range.start..range.start,
455 secondary_label,
456 suggestion: Suggestion::None,
457 });
458
459 if let (Some((_, _, c)), Some((_, _, nc))) = (self.peek(), self.peek_ahead()) {
460 match (c, nc) {
461 ('?', '}') => self.missing_colon_before_debug_formatter(),
462 ('?', _) => self.suggest_format_debug(),
463 ('<' | '^' | '>', _) => self.suggest_format_align(c),
464 (',', _) => self.suggest_unsupported_python_numeric_grouping(),
465 _ => self.suggest_positional_arg_instead_of_captured_arg(arg),
466 }
467 }
468 }
469
470 fn ws(&mut self) {
472 let rest = &self.input_vec[self.input_vec_index..];
473 let step = rest.iter().position(|&(_, _, c)| !c.is_whitespace()).unwrap_or(rest.len());
474 self.input_vec_index += step;
475 }
476
477 fn string(&mut self, start: usize) -> &'input str {
480 while let Some((r, i, c)) = self.peek() {
481 match c {
482 '{' | '}' => {
483 return &self.input[start..i];
484 }
485 '\n' if self.is_source_literal => {
486 self.input_vec_index += 1;
487 self.line_spans.push(self.cur_line_start..r.start);
488 self.cur_line_start = r.end;
489 }
490 _ => {
491 self.input_vec_index += 1;
492 if self.is_source_literal && r.start == self.cur_line_start && c.is_whitespace()
493 {
494 self.cur_line_start = r.end;
495 }
496 }
497 }
498 }
499 &self.input[start..]
500 }
501
502 fn argument(&mut self) -> Argument<'input> {
504 let start_idx = self.input_vec_index;
505
506 let position = self.position();
507 self.ws();
508
509 let end_idx = self.input_vec_index;
510
511 let format = match self.mode {
512 ParseMode::Format => self.format(),
513 ParseMode::InlineAsm => self.inline_asm(),
514 ParseMode::Diagnostic => self.diagnostic(),
515 };
516
517 let position = position.unwrap_or_else(|| {
519 let i = self.curarg;
520 self.curarg += 1;
521 ArgumentImplicitlyIs(i)
522 });
523
524 let position_span =
525 self.input_vec_index2range(start_idx).start..self.input_vec_index2range(end_idx).start;
526 Argument { position, position_span, format }
527 }
528
529 fn position(&mut self) -> Option<Position<'input>> {
534 if let Some(i) = self.integer() {
535 Some(ArgumentIs(i.into()))
536 } else {
537 match self.peek() {
538 Some((range, _, c)) if rustc_lexer::is_id_start(c) => {
539 let start = range.start;
540 let word = self.word();
541
542 if word == "r"
544 && let Some((r, _, '#')) = self.peek()
545 && self.peek_ahead().is_some_and(|(_, _, c)| rustc_lexer::is_id_start(c))
546 {
547 self.input_vec_index += 1;
548 let prefix_end = r.end;
549 let word = self.word();
550 let prefix_span = start..prefix_end;
551 let full_span =
552 start..self.input_vec_index2range(self.input_vec_index).start;
553 self.errors.insert(0, ParseError {
554 description: "raw identifiers are not supported".to_owned(),
555 note: Some("identifiers in format strings can be keywords and don't need to be prefixed with `r#`".to_string()),
556 label: "raw identifier used here".to_owned(),
557 span: full_span,
558 secondary_label: None,
559 suggestion: Suggestion::RemoveRawIdent(prefix_span),
560 });
561 return Some(ArgumentNamed(word));
562 }
563
564 Some(ArgumentNamed(word))
565 }
566 _ => None,
570 }
571 }
572 }
573
574 fn input_vec_index2pos(&self, index: usize) -> usize {
575 if let Some((_, pos, _)) = self.input_vec.get(index) { *pos } else { self.input.len() }
576 }
577
578 fn input_vec_index2range(&self, index: usize) -> Range<usize> {
579 if let Some((r, _, _)) = self.input_vec.get(index) {
580 r.clone()
581 } else {
582 self.end_of_snippet..self.end_of_snippet
583 }
584 }
585
586 fn format(&mut self) -> FormatSpec<'input> {
589 let mut spec = FormatSpec::default();
590
591 if !self.consume(':') {
592 return spec;
593 }
594
595 if let (Some((r, _, c)), Some((_, _, '>' | '<' | '^'))) = (self.peek(), self.peek_ahead()) {
597 self.input_vec_index += 1;
598 spec.fill = Some(c);
599 spec.fill_span = Some(r);
600 }
601 if self.consume('<') {
603 spec.align = AlignLeft;
604 } else if self.consume('>') {
605 spec.align = AlignRight;
606 } else if self.consume('^') {
607 spec.align = AlignCenter;
608 }
609 if self.consume('+') {
611 spec.sign = Some(Sign::Plus);
612 } else if self.consume('-') {
613 spec.sign = Some(Sign::Minus);
614 }
615 if self.consume('#') {
617 spec.alternate = true;
618 }
619 let mut havewidth = false;
621
622 if let Some((range, _)) = self.consume_pos('0') {
623 if let Some((r, _)) = self.consume_pos('$') {
628 spec.width = CountIsParam(0);
629 spec.width_span = Some(range.start..r.end);
630 havewidth = true;
631 } else {
632 spec.zero_pad = true;
633 }
634 }
635
636 if !havewidth {
637 let start_idx = self.input_vec_index;
638 spec.width = self.count();
639 if spec.width != CountImplied {
640 let end = self.input_vec_index2range(self.input_vec_index).start;
641 spec.width_span = Some(self.input_vec_index2range(start_idx).start..end);
642 }
643 }
644
645 if let Some((range, _)) = self.consume_pos('.') {
646 if self.consume('*') {
647 let i = self.curarg;
650 self.curarg += 1;
651 spec.precision = CountIsStar(i);
652 } else {
653 spec.precision = self.count();
654 }
655 spec.precision_span =
656 Some(range.start..self.input_vec_index2range(self.input_vec_index).start);
657 }
658
659 let start_idx = self.input_vec_index;
660 if self.consume('x') {
662 if self.consume('?') {
663 spec.debug_hex = Some(DebugHex::Lower);
664 spec.ty = "?";
665 } else {
666 spec.ty = "x";
667 }
668 } else if self.consume('X') {
669 if self.consume('?') {
670 spec.debug_hex = Some(DebugHex::Upper);
671 spec.ty = "?";
672 } else {
673 spec.ty = "X";
674 }
675 } else if let Some((range, _)) = self.consume_pos('?') {
676 spec.ty = "?";
677 if let Some((r, _, c @ ('#' | 'x' | 'X'))) = self.peek() {
678 self.errors.insert(
679 0,
680 ParseError {
681 description: ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected `}}`, found `{0}`", c))
})format!("expected `}}`, found `{c}`"),
682 note: None,
683 label: "expected `'}'`".into(),
684 span: r.clone(),
685 secondary_label: None,
686 suggestion: Suggestion::ReorderFormatParameter(
687 range.start..r.end,
688 ::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("{0}?", c)) })format!("{c}?"),
689 ),
690 },
691 );
692 }
693 } else {
694 spec.ty = self.word();
695 if !spec.ty.is_empty() {
696 let start = self.input_vec_index2range(start_idx).start;
697 let end = self.input_vec_index2range(self.input_vec_index).start;
698 spec.ty_span = Some(start..end);
699 }
700 }
701 spec
702 }
703
704 fn inline_asm(&mut self) -> FormatSpec<'input> {
707 let mut spec = FormatSpec::default();
708
709 if !self.consume(':') {
710 return spec;
711 }
712
713 let start_idx = self.input_vec_index;
714 spec.ty = self.word();
715 if !spec.ty.is_empty() {
716 let start = self.input_vec_index2range(start_idx).start;
717 let end = self.input_vec_index2range(self.input_vec_index).start;
718 spec.ty_span = Some(start..end);
719 }
720
721 spec
722 }
723
724 fn diagnostic(&mut self) -> FormatSpec<'input> {
726 let mut spec = FormatSpec::default();
727
728 let Some((Range { start, .. }, start_idx)) = self.consume_pos(':') else {
729 return spec;
730 };
731
732 spec.ty = self.string(start_idx);
733 spec.ty_span = {
734 let end = self.input_vec_index2range(self.input_vec_index).start;
735 Some(start..end)
736 };
737 spec
738 }
739
740 fn count(&mut self) -> Count<'input> {
744 if let Some(i) = self.integer() {
745 if self.consume('$') { CountIsParam(i.into()) } else { CountIs(i) }
746 } else {
747 let start_idx = self.input_vec_index;
748 let word = self.word();
749 if word.is_empty() {
750 CountImplied
751 } else if let Some((r, _)) = self.consume_pos('$') {
752 CountIsName(word, self.input_vec_index2range(start_idx).start..r.start)
753 } else {
754 self.input_vec_index = start_idx;
755 CountImplied
756 }
757 }
758 }
759
760 fn word(&mut self) -> &'input str {
763 let index = self.input_vec_index;
764 match self.peek() {
765 Some((ref r, i, c)) if rustc_lexer::is_id_start(c) => {
766 self.input_vec_index += 1;
767 (r.start, i)
768 }
769 _ => {
770 return "";
771 }
772 };
773 let (err_end, end): (usize, usize) = loop {
774 if let Some((ref r, i, c)) = self.peek() {
775 if rustc_lexer::is_id_continue(c) {
776 self.input_vec_index += 1;
777 } else {
778 break (r.start, i);
779 }
780 } else {
781 break (self.end_of_snippet, self.input.len());
782 }
783 };
784
785 let word = &self.input[self.input_vec_index2pos(index)..end];
786 if word == "_" {
787 self.errors.push(ParseError {
788 description: "invalid argument name `_`".into(),
789 note: Some("argument name cannot be a single underscore".into()),
790 label: "invalid argument name".into(),
791 span: self.input_vec_index2range(index).start..err_end,
792 secondary_label: None,
793 suggestion: Suggestion::None,
794 });
795 }
796 word
797 }
798
799 fn integer(&mut self) -> Option<u16> {
800 let mut cur: u16 = 0;
801 let mut found = false;
802 let mut overflow = false;
803 let start_index = self.input_vec_index;
804 while let Some((_, _, c)) = self.peek() {
805 if let Some(i) = c.to_digit(10) {
806 self.input_vec_index += 1;
807 let (tmp, mul_overflow) = cur.overflowing_mul(10);
808 let (tmp, add_overflow) = tmp.overflowing_add(i as u16);
809 if mul_overflow || add_overflow {
810 overflow = true;
811 }
812 cur = tmp;
813 found = true;
814 } else {
815 break;
816 }
817 }
818
819 if overflow {
820 let overflowed_int = &self.input[self.input_vec_index2pos(start_index)
821 ..self.input_vec_index2pos(self.input_vec_index)];
822 self.errors.push(ParseError {
823 description: ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("integer `{0}` does not fit into the type `u16` whose range is `0..={1}`",
overflowed_int, u16::MAX))
})format!(
824 "integer `{}` does not fit into the type `u16` whose range is `0..={}`",
825 overflowed_int,
826 u16::MAX
827 ),
828 note: None,
829 label: "integer out of range for `u16`".into(),
830 span: self.input_vec_index2range(start_index).start
831 ..self.input_vec_index2range(self.input_vec_index).end,
832 secondary_label: None,
833 suggestion: Suggestion::None,
834 });
835 }
836
837 found.then_some(cur)
838 }
839
840 fn suggest_format_debug(&mut self) {
841 if let (Some((range, _)), Some(_)) = (self.consume_pos('?'), self.consume_pos(':')) {
842 let word = self.word();
843 self.errors.insert(
844 0,
845 ParseError {
846 description: "expected format parameter to occur after `:`".to_owned(),
847 note: Some(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`?` comes after `:`, try `{0}:{1}` instead",
word, "?"))
})format!("`?` comes after `:`, try `{}:{}` instead", word, "?")),
848 label: "expected `?` to occur after `:`".to_owned(),
849 span: range,
850 secondary_label: None,
851 suggestion: Suggestion::None,
852 },
853 );
854 }
855 }
856
857 fn missing_colon_before_debug_formatter(&mut self) {
858 if let Some((range, _)) = self.consume_pos('?') {
859 let span = range.clone();
860 self.errors.insert(
861 0,
862 ParseError {
863 description: "expected `}`, found `?`".to_owned(),
864 note: Some(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("to print `{{`, you can escape it using `{{{{`"))
})format!("to print `{{`, you can escape it using `{{{{`",)),
865 label: "expected `:` before `?` to format with `Debug`".to_owned(),
866 span: range,
867 secondary_label: None,
868 suggestion: Suggestion::AddMissingColon(span),
869 },
870 );
871 }
872 }
873
874 fn suggest_format_align(&mut self, alignment: char) {
875 if let Some((range, _)) = self.consume_pos(alignment) {
876 self.errors.insert(
877 0,
878 ParseError {
879 description:
880 "expected alignment specifier after `:` in format string; example: `{:>?}`"
881 .to_owned(),
882 note: None,
883 label: ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected `{0}` to occur after `:`",
alignment))
})format!("expected `{}` to occur after `:`", alignment),
884 span: range,
885 secondary_label: None,
886 suggestion: Suggestion::None,
887 },
888 );
889 }
890 }
891
892 fn suggest_positional_arg_instead_of_captured_arg(&mut self, arg: &Argument<'_>) {
893 if !arg.is_identifier() {
895 return;
896 }
897
898 if let Some((_range, _pos)) = self.consume_pos('.') {
899 let field = self.argument();
900 if !self.consume('}') {
903 return;
904 }
905 if let ArgumentNamed(_) = arg.position {
906 match field.position {
907 ArgumentNamed(_) => {
908 self.errors.insert(
909 0,
910 ParseError {
911 description: "field access isn't supported".to_string(),
912 note: None,
913 label: "not supported".to_string(),
914 span: arg.position_span.start..field.position_span.end,
915 secondary_label: None,
916 suggestion: Suggestion::UsePositional,
917 },
918 );
919 }
920 ArgumentIs(_) => {
921 self.errors.insert(
922 0,
923 ParseError {
924 description: "tuple index access isn't supported".to_string(),
925 note: None,
926 label: "not supported".to_string(),
927 span: arg.position_span.start..field.position_span.end,
928 secondary_label: None,
929 suggestion: Suggestion::UsePositional,
930 },
931 );
932 }
933 _ => {}
934 };
935 }
936 }
937 }
938
939 fn suggest_unsupported_python_numeric_grouping(&mut self) {
940 if let Some((range, _)) = self.consume_pos(',') {
941 self.errors.insert(
942 0,
943 ParseError {
944 description:
945 "python's numeric grouping `,` is not supported in rust format strings"
946 .to_owned(),
947 note: Some(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("to print `{{`, you can escape it using `{{{{`"))
})format!("to print `{{`, you can escape it using `{{{{`",)),
948 label: "expected `}`".to_owned(),
949 span: range,
950 secondary_label: self
951 .last_open_brace
952 .clone()
953 .map(|sp| ("because of this opening brace".to_owned(), sp)),
954 suggestion: Suggestion::None,
955 },
956 );
957 }
958 }
959}
960
961#[cfg(all(test, target_pointer_width = "64"))]
963rustc_index::static_assert_size!(Piece<'_>, 16);
964
965#[cfg(test)]
966mod tests;