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(Copy, Clone, Debug, Eq, PartialEq)]
22pub enum ParseMode {
23 Format,
25 InlineAsm,
27 Diagnostic,
32}
33
34#[derive(Clone, Debug, PartialEq)]
37pub enum Piece<'input> {
38 Lit(&'input str),
40 NextArgument(Box<Argument<'input>>),
43}
44
45#[derive(Clone, Debug, 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 matches!(self.position, Position::ArgumentNamed(_)) && self.format == FormatSpec::default()
60 }
61}
62
63#[derive(Clone, Debug, PartialEq, 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(Clone, Debug, 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(Copy, Clone, Debug, PartialEq, Default)]
118pub enum Alignment {
119 AlignLeft,
121 AlignRight,
123 AlignCenter,
125 #[default]
127 AlignUnknown,
128}
129
130#[derive(Copy, Clone, Debug, PartialEq)]
132pub enum Sign {
133 Plus,
135 Minus,
137}
138
139#[derive(Copy, Clone, Debug, PartialEq)]
141pub enum DebugHex {
142 Lower,
144 Upper,
146}
147
148#[derive(Clone, Debug, PartialEq, 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, vec![])
313 } else {
314 if snippet.starts_with('"') {
316 let without_quotes = &snippet[1..snippet.len() - 1];
319 let (mut ok, mut vec) = (true, 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 = 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(), vec![])
338 }
339 } else if chars.as_str() == "" {
340 (true, 1 + end, vec)
341 } else {
342 (false, snippet.len(), vec![])
343 }
344 } else {
345 (false, snippet.len(), vec![])
346 }
347 } else {
348 (false, snippet.len(), vec![])
350 }
351 }
352 } else {
353 (false, input.len() - if appended_newline { 1 } else { 0 }, 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: vec![],
382 curarg: 0,
383 arg_places: vec![],
384 last_open_brace: None,
385 is_source_literal,
386 end_of_snippet,
387 cur_line_start: quote_offset,
388 line_spans: 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, 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_positional_arg_instead_of_captured_arg(arg),
465 }
466 }
467 }
468
469 fn ws(&mut self) {
471 let rest = &self.input_vec[self.input_vec_index..];
472 let step = rest.iter().position(|&(_, _, c)| !c.is_whitespace()).unwrap_or(rest.len());
473 self.input_vec_index += step;
474 }
475
476 fn string(&mut self, start: usize) -> &'input str {
479 while let Some((r, i, c)) = self.peek() {
480 match c {
481 '{' | '}' => {
482 return &self.input[start..i];
483 }
484 '\n' if self.is_source_literal => {
485 self.input_vec_index += 1;
486 self.line_spans.push(self.cur_line_start..r.start);
487 self.cur_line_start = r.end;
488 }
489 _ => {
490 self.input_vec_index += 1;
491 if self.is_source_literal && r.start == self.cur_line_start && c.is_whitespace()
492 {
493 self.cur_line_start = r.end;
494 }
495 }
496 }
497 }
498 &self.input[start..]
499 }
500
501 fn argument(&mut self) -> Argument<'input> {
503 let start_idx = self.input_vec_index;
504
505 let position = self.position();
506 self.ws();
507
508 let end_idx = self.input_vec_index;
509
510 let format = match self.mode {
511 ParseMode::Format => self.format(),
512 ParseMode::InlineAsm => self.inline_asm(),
513 ParseMode::Diagnostic => self.diagnostic(),
514 };
515
516 let position = position.unwrap_or_else(|| {
518 let i = self.curarg;
519 self.curarg += 1;
520 ArgumentImplicitlyIs(i)
521 });
522
523 let position_span =
524 self.input_vec_index2range(start_idx).start..self.input_vec_index2range(end_idx).start;
525 Argument { position, position_span, format }
526 }
527
528 fn position(&mut self) -> Option<Position<'input>> {
533 if let Some(i) = self.integer() {
534 Some(ArgumentIs(i.into()))
535 } else {
536 match self.peek() {
537 Some((range, _, c)) if rustc_lexer::is_id_start(c) => {
538 let start = range.start;
539 let word = self.word();
540
541 if word == "r"
543 && let Some((r, _, '#')) = self.peek()
544 && self.peek_ahead().is_some_and(|(_, _, c)| rustc_lexer::is_id_start(c))
545 {
546 self.input_vec_index += 1;
547 let prefix_end = r.end;
548 let word = self.word();
549 let prefix_span = start..prefix_end;
550 let full_span =
551 start..self.input_vec_index2range(self.input_vec_index).start;
552 self.errors.insert(0, ParseError {
553 description: "raw identifiers are not supported".to_owned(),
554 note: Some("identifiers in format strings can be keywords and don't need to be prefixed with `r#`".to_string()),
555 label: "raw identifier used here".to_owned(),
556 span: full_span,
557 secondary_label: None,
558 suggestion: Suggestion::RemoveRawIdent(prefix_span),
559 });
560 return Some(ArgumentNamed(word));
561 }
562
563 Some(ArgumentNamed(word))
564 }
565 _ => None,
569 }
570 }
571 }
572
573 fn input_vec_index2pos(&self, index: usize) -> usize {
574 if let Some((_, pos, _)) = self.input_vec.get(index) { *pos } else { self.input.len() }
575 }
576
577 fn input_vec_index2range(&self, index: usize) -> Range<usize> {
578 if let Some((r, _, _)) = self.input_vec.get(index) {
579 r.clone()
580 } else {
581 self.end_of_snippet..self.end_of_snippet
582 }
583 }
584
585 fn format(&mut self) -> FormatSpec<'input> {
588 let mut spec = FormatSpec::default();
589
590 if !self.consume(':') {
591 return spec;
592 }
593
594 if let (Some((r, _, c)), Some((_, _, '>' | '<' | '^'))) = (self.peek(), self.peek_ahead()) {
596 self.input_vec_index += 1;
597 spec.fill = Some(c);
598 spec.fill_span = Some(r);
599 }
600 if self.consume('<') {
602 spec.align = AlignLeft;
603 } else if self.consume('>') {
604 spec.align = AlignRight;
605 } else if self.consume('^') {
606 spec.align = AlignCenter;
607 }
608 if self.consume('+') {
610 spec.sign = Some(Sign::Plus);
611 } else if self.consume('-') {
612 spec.sign = Some(Sign::Minus);
613 }
614 if self.consume('#') {
616 spec.alternate = true;
617 }
618 let mut havewidth = false;
620
621 if let Some((range, _)) = self.consume_pos('0') {
622 if let Some((r, _)) = self.consume_pos('$') {
627 spec.width = CountIsParam(0);
628 spec.width_span = Some(range.start..r.end);
629 havewidth = true;
630 } else {
631 spec.zero_pad = true;
632 }
633 }
634
635 if !havewidth {
636 let start_idx = self.input_vec_index;
637 spec.width = self.count();
638 if spec.width != CountImplied {
639 let end = self.input_vec_index2range(self.input_vec_index).start;
640 spec.width_span = Some(self.input_vec_index2range(start_idx).start..end);
641 }
642 }
643
644 if let Some((range, _)) = self.consume_pos('.') {
645 if self.consume('*') {
646 let i = self.curarg;
649 self.curarg += 1;
650 spec.precision = CountIsStar(i);
651 } else {
652 spec.precision = self.count();
653 }
654 spec.precision_span =
655 Some(range.start..self.input_vec_index2range(self.input_vec_index).start);
656 }
657
658 let start_idx = self.input_vec_index;
659 if self.consume('x') {
661 if self.consume('?') {
662 spec.debug_hex = Some(DebugHex::Lower);
663 spec.ty = "?";
664 } else {
665 spec.ty = "x";
666 }
667 } else if self.consume('X') {
668 if self.consume('?') {
669 spec.debug_hex = Some(DebugHex::Upper);
670 spec.ty = "?";
671 } else {
672 spec.ty = "X";
673 }
674 } else if let Some((range, _)) = self.consume_pos('?') {
675 spec.ty = "?";
676 if let Some((r, _, c @ ('#' | 'x' | 'X'))) = self.peek() {
677 self.errors.insert(
678 0,
679 ParseError {
680 description: format!("expected `}}`, found `{c}`"),
681 note: None,
682 label: "expected `'}'`".into(),
683 span: r.clone(),
684 secondary_label: None,
685 suggestion: Suggestion::ReorderFormatParameter(
686 range.start..r.end,
687 format!("{c}?"),
688 ),
689 },
690 );
691 }
692 } else {
693 spec.ty = self.word();
694 if !spec.ty.is_empty() {
695 let start = self.input_vec_index2range(start_idx).start;
696 let end = self.input_vec_index2range(self.input_vec_index).start;
697 spec.ty_span = Some(start..end);
698 }
699 }
700 spec
701 }
702
703 fn inline_asm(&mut self) -> FormatSpec<'input> {
706 let mut spec = FormatSpec::default();
707
708 if !self.consume(':') {
709 return spec;
710 }
711
712 let start_idx = self.input_vec_index;
713 spec.ty = self.word();
714 if !spec.ty.is_empty() {
715 let start = self.input_vec_index2range(start_idx).start;
716 let end = self.input_vec_index2range(self.input_vec_index).start;
717 spec.ty_span = Some(start..end);
718 }
719
720 spec
721 }
722
723 fn diagnostic(&mut self) -> FormatSpec<'input> {
725 let mut spec = FormatSpec::default();
726
727 let Some((Range { start, .. }, start_idx)) = self.consume_pos(':') else {
728 return spec;
729 };
730
731 spec.ty = self.string(start_idx);
732 spec.ty_span = {
733 let end = self.input_vec_index2range(self.input_vec_index).start;
734 Some(start..end)
735 };
736 spec
737 }
738
739 fn count(&mut self) -> Count<'input> {
743 if let Some(i) = self.integer() {
744 if self.consume('$') { CountIsParam(i.into()) } else { CountIs(i) }
745 } else {
746 let start_idx = self.input_vec_index;
747 let word = self.word();
748 if word.is_empty() {
749 CountImplied
750 } else if let Some((r, _)) = self.consume_pos('$') {
751 CountIsName(word, self.input_vec_index2range(start_idx).start..r.start)
752 } else {
753 self.input_vec_index = start_idx;
754 CountImplied
755 }
756 }
757 }
758
759 fn word(&mut self) -> &'input str {
762 let index = self.input_vec_index;
763 match self.peek() {
764 Some((ref r, i, c)) if rustc_lexer::is_id_start(c) => {
765 self.input_vec_index += 1;
766 (r.start, i)
767 }
768 _ => {
769 return "";
770 }
771 };
772 let (err_end, end): (usize, usize) = loop {
773 if let Some((ref r, i, c)) = self.peek() {
774 if rustc_lexer::is_id_continue(c) {
775 self.input_vec_index += 1;
776 } else {
777 break (r.start, i);
778 }
779 } else {
780 break (self.end_of_snippet, self.input.len());
781 }
782 };
783
784 let word = &self.input[self.input_vec_index2pos(index)..end];
785 if word == "_" {
786 self.errors.push(ParseError {
787 description: "invalid argument name `_`".into(),
788 note: Some("argument name cannot be a single underscore".into()),
789 label: "invalid argument name".into(),
790 span: self.input_vec_index2range(index).start..err_end,
791 secondary_label: None,
792 suggestion: Suggestion::None,
793 });
794 }
795 word
796 }
797
798 fn integer(&mut self) -> Option<u16> {
799 let mut cur: u16 = 0;
800 let mut found = false;
801 let mut overflow = false;
802 let start_index = self.input_vec_index;
803 while let Some((_, _, c)) = self.peek() {
804 if let Some(i) = c.to_digit(10) {
805 self.input_vec_index += 1;
806 let (tmp, mul_overflow) = cur.overflowing_mul(10);
807 let (tmp, add_overflow) = tmp.overflowing_add(i as u16);
808 if mul_overflow || add_overflow {
809 overflow = true;
810 }
811 cur = tmp;
812 found = true;
813 } else {
814 break;
815 }
816 }
817
818 if overflow {
819 let overflowed_int = &self.input[self.input_vec_index2pos(start_index)
820 ..self.input_vec_index2pos(self.input_vec_index)];
821 self.errors.push(ParseError {
822 description: format!(
823 "integer `{}` does not fit into the type `u16` whose range is `0..={}`",
824 overflowed_int,
825 u16::MAX
826 ),
827 note: None,
828 label: "integer out of range for `u16`".into(),
829 span: self.input_vec_index2range(start_index).start
830 ..self.input_vec_index2range(self.input_vec_index).end,
831 secondary_label: None,
832 suggestion: Suggestion::None,
833 });
834 }
835
836 found.then_some(cur)
837 }
838
839 fn suggest_format_debug(&mut self) {
840 if let (Some((range, _)), Some(_)) = (self.consume_pos('?'), self.consume_pos(':')) {
841 let word = self.word();
842 self.errors.insert(
843 0,
844 ParseError {
845 description: "expected format parameter to occur after `:`".to_owned(),
846 note: Some(format!("`?` comes after `:`, try `{}:{}` instead", word, "?")),
847 label: "expected `?` to occur after `:`".to_owned(),
848 span: range,
849 secondary_label: None,
850 suggestion: Suggestion::None,
851 },
852 );
853 }
854 }
855
856 fn missing_colon_before_debug_formatter(&mut self) {
857 if let Some((range, _)) = self.consume_pos('?') {
858 let span = range.clone();
859 self.errors.insert(
860 0,
861 ParseError {
862 description: "expected `}`, found `?`".to_owned(),
863 note: Some(format!("to print `{{`, you can escape it using `{{{{`",)),
864 label: "expected `:` before `?` to format with `Debug`".to_owned(),
865 span: range,
866 secondary_label: None,
867 suggestion: Suggestion::AddMissingColon(span),
868 },
869 );
870 }
871 }
872
873 fn suggest_format_align(&mut self, alignment: char) {
874 if let Some((range, _)) = self.consume_pos(alignment) {
875 self.errors.insert(
876 0,
877 ParseError {
878 description:
879 "expected alignment specifier after `:` in format string; example: `{:>?}`"
880 .to_owned(),
881 note: None,
882 label: format!("expected `{}` to occur after `:`", alignment),
883 span: range,
884 secondary_label: None,
885 suggestion: Suggestion::None,
886 },
887 );
888 }
889 }
890
891 fn suggest_positional_arg_instead_of_captured_arg(&mut self, arg: &Argument<'_>) {
892 if !arg.is_identifier() {
894 return;
895 }
896
897 if let Some((_range, _pos)) = self.consume_pos('.') {
898 let field = self.argument();
899 if !self.consume('}') {
902 return;
903 }
904 if let ArgumentNamed(_) = arg.position {
905 match field.position {
906 ArgumentNamed(_) => {
907 self.errors.insert(
908 0,
909 ParseError {
910 description: "field access isn't supported".to_string(),
911 note: None,
912 label: "not supported".to_string(),
913 span: arg.position_span.start..field.position_span.end,
914 secondary_label: None,
915 suggestion: Suggestion::UsePositional,
916 },
917 );
918 }
919 ArgumentIs(_) => {
920 self.errors.insert(
921 0,
922 ParseError {
923 description: "tuple index access isn't supported".to_string(),
924 note: None,
925 label: "not supported".to_string(),
926 span: arg.position_span.start..field.position_span.end,
927 secondary_label: None,
928 suggestion: Suggestion::UsePositional,
929 },
930 );
931 }
932 _ => {}
933 };
934 }
935 }
936 }
937}
938
939#[cfg(all(test, target_pointer_width = "64"))]
941rustc_index::static_assert_size!(Piece<'_>, 16);
942
943#[cfg(test)]
944mod tests;