1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Macro support for format strings
13 //! These structures are used when parsing format strings for the compiler.
14 //! Parsing does not happen at runtime: structures of `std::fmt::rt` are
15 //! generated instead.
17 // Do not remove on snapshot creation. Needed for bootstrap. (Issue #22364)
18 #![cfg_attr(stage0, feature(custom_attribute))]
19 #![crate_name = "fmt_macros"]
20 #![unstable(feature = "rustc_private", issue = "27812")]
22 #![crate_type = "rlib"]
23 #![crate_type = "dylib"]
24 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
25 html_favicon_url
= "https://doc.rust-lang.org/favicon.ico",
26 html_root_url
= "https://doc.rust-lang.org/nightly/",
27 html_playground_url
= "https://play.rust-lang.org/")]
29 #![feature(staged_api)]
32 pub use self::Piece
::*;
33 pub use self::Position
::*;
34 pub use self::Alignment
::*;
35 pub use self::Flag
::*;
36 pub use self::Count
::*;
42 /// A piece is a portion of the format string which represents the next part
43 /// to emit. These are emitted as a stream by the `Parser` class.
44 #[derive(Copy, Clone, PartialEq)]
46 /// A literal string which should directly be emitted
48 /// This describes that formatting should process the next argument (as
49 /// specified inside) for emission.
50 NextArgument(Argument
<'a
>),
53 /// Representation of an argument specification.
54 #[derive(Copy, Clone, PartialEq)]
55 pub struct Argument
<'a
> {
56 /// Where to find this argument
57 pub position
: Position
<'a
>,
58 /// How to format the argument
59 pub format
: FormatSpec
<'a
>,
62 /// Specification for the formatting of an argument in the format string.
63 #[derive(Copy, Clone, PartialEq)]
64 pub struct FormatSpec
<'a
> {
65 /// Optionally specified character to fill alignment with
66 pub fill
: Option
<char>,
67 /// Optionally specified alignment
69 /// Packed version of various flags provided
71 /// The integer precision to use
72 pub precision
: Count
<'a
>,
73 /// The string width requested for the resulting format
75 /// The descriptor string representing the name of the format desired for
76 /// this argument, this can be empty or any number of characters, although
77 /// it is required to be one word.
81 /// Enum describing where an argument for a format can be located.
82 #[derive(Copy, Clone, PartialEq)]
83 pub enum Position
<'a
> {
84 /// The argument will be in the next position. This is the default.
86 /// The argument is located at a specific index.
88 /// The argument has a name.
89 ArgumentNamed(&'a
str),
92 /// Enum of alignments which are supported.
93 #[derive(Copy, Clone, PartialEq)]
95 /// The value will be aligned to the left.
97 /// The value will be aligned to the right.
99 /// The value will be aligned in the center.
101 /// The value will take on a default alignment.
105 /// Various flags which can be applied to format strings. The meaning of these
106 /// flags is defined by the formatters themselves.
107 #[derive(Copy, Clone, PartialEq)]
109 /// A `+` will be used to denote positive numbers.
111 /// A `-` will be used to denote negative numbers. This is the default.
113 /// An alternate form will be used for the value. In the case of numbers,
114 /// this means that the number will be prefixed with the supplied string.
116 /// For numbers, this means that the number will be padded with zeroes,
117 /// and the sign (`+` or `-`) will precede them.
118 FlagSignAwareZeroPad
,
121 /// A count is used for the precision and width parameters of an integer, and
122 /// can reference either an argument or a literal integer.
123 #[derive(Copy, Clone, PartialEq)]
125 /// The count is specified explicitly.
127 /// The count is specified by the argument with the given name.
128 CountIsName(&'a
str),
129 /// The count is specified by the argument at the given index.
131 /// The count is specified by the next parameter.
133 /// The count is implied and cannot be explicitly specified.
137 /// The parser structure for interpreting the input format string. This is
138 /// modelled as an iterator over `Piece` structures to form a stream of tokens
141 /// This is a recursive-descent parser for the sake of simplicity, and if
142 /// necessary there's probably lots of room for improvement performance-wise.
143 pub struct Parser
<'a
> {
145 cur
: iter
::Peekable
<str::CharIndices
<'a
>>,
146 /// Error messages accumulated during parsing
147 pub errors
: Vec
<string
::String
>,
150 impl<'a
> Iterator
for Parser
<'a
> {
151 type Item
= Piece
<'a
>;
153 fn next(&mut self) -> Option
<Piece
<'a
>> {
154 if let Some(&(pos
, c
)) = self.cur
.peek() {
158 if self.consume('
{'
) {
159 Some(String(self.string(pos
+ 1)))
161 let ret
= Some(NextArgument(self.argument()));
162 self.must_consume('
}'
);
168 if self.consume('
}'
) {
169 Some(String(self.string(pos
+ 1)))
171 self.err("unmatched `}` found");
175 _
=> Some(String(self.string(pos
))),
183 impl<'a
> Parser
<'a
> {
184 /// Creates a new parser for the given format string
185 pub fn new(s
: &'a
str) -> Parser
<'a
> {
188 cur
: s
.char_indices().peekable(),
193 /// Notifies of an error. The message doesn't actually need to be of type
194 /// String, but I think it does when this eventually uses conditions so it
195 /// might as well start using it now.
196 fn err(&mut self, msg
: &str) {
197 self.errors
.push(msg
.to_owned());
200 /// Optionally consumes the specified character. If the character is not at
201 /// the current position, then the current iterator isn't moved and false is
202 /// returned, otherwise the character is consumed and true is returned.
203 fn consume(&mut self, c
: char) -> bool
{
204 if let Some(&(_
, maybe
)) = self.cur
.peek() {
205 if c
== maybe { self.cur.next(); true }
else { false }
211 /// Forces consumption of the specified character. If the character is not
212 /// found, an error is emitted.
213 fn must_consume(&mut self, c
: char) {
215 if let Some(&(_
, maybe
)) = self.cur
.peek() {
219 self.err(&format
!("expected `{:?}`, found `{:?}`", c
, maybe
));
222 self.err(&format
!("expected `{:?}` but string was terminated", c
));
226 /// Consumes all whitespace characters until the first non-whitespace
229 while let Some(&(_
, c
)) = self.cur
.peek() {
230 if c
.is_whitespace() { self.cur.next(); }
else { break }
234 /// Parses all of a string which is to be considered a "raw literal" in a
235 /// format string. This is everything outside of the braces.
236 fn string(&mut self, start
: usize) -> &'a
str {
237 // we may not consume the character, peek the iterator
238 while let Some(&(pos
, c
)) = self.cur
.peek() {
240 '{' | '}'
=> { return &self.input[start..pos]; }
241 _
=> { self.cur.next(); }
244 &self.input
[start
..self.input
.len()]
247 /// Parses an Argument structure, or what's contained within braces inside
248 /// the format string
249 fn argument(&mut self) -> Argument
<'a
> {
251 position
: self.position(),
252 format
: self.format(),
256 /// Parses a positional argument for a format. This could either be an
257 /// integer index of an argument, a named argument, or a blank string.
258 fn position(&mut self) -> Position
<'a
> {
259 if let Some(i
) = self.integer() {
262 match self.cur
.peek() {
263 Some(&(_
, c
)) if c
.is_alphabetic() => {
264 ArgumentNamed(self.word())
271 /// Parses a format specifier at the current position, returning all of the
272 /// relevant information in the FormatSpec struct.
273 fn format(&mut self) -> FormatSpec
<'a
> {
274 let mut spec
= FormatSpec
{
278 precision
: CountImplied
,
280 ty
: &self.input
[..0],
282 if !self.consume('
:'
) { return spec }
285 if let Some(&(_
, c
)) = self.cur
.peek() {
286 match self.cur
.clone().skip(1).next() {
287 Some((_
, '
>'
)) | Some((_
, '
<'
)) | Some((_
, '
^')) => {
295 if self.consume('
<'
) {
296 spec
.align
= AlignLeft
;
297 } else if self.consume('
>'
) {
298 spec
.align
= AlignRight
;
299 } else if self.consume('
^') {
300 spec
.align
= AlignCenter
;
303 if self.consume('
+'
) {
304 spec
.flags
|= 1 << (FlagSignPlus
as u32);
305 } else if self.consume('
-'
) {
306 spec
.flags
|= 1 << (FlagSignMinus
as u32);
309 if self.consume('
#') {
310 spec
.flags
|= 1 << (FlagAlternate
as u32);
312 // Width and precision
313 let mut havewidth
= false;
314 if self.consume('
0'
) {
315 // small ambiguity with '0$' as a format string. In theory this is a
316 // '0' flag and then an ill-formatted format string with just a '$'
317 // and no count, but this is better if we instead interpret this as
318 // no '0' flag and '0$' as the width instead.
319 if self.consume('$'
) {
320 spec
.width
= CountIsParam(0);
323 spec
.flags
|= 1 << (FlagSignAwareZeroPad
as u32);
327 spec
.width
= self.count();
329 if self.consume('
.'
) {
330 if self.consume('
*'
) {
331 spec
.precision
= CountIsNextParam
;
333 spec
.precision
= self.count();
336 // Finally the actual format specifier
337 if self.consume('?'
) {
340 spec
.ty
= self.word();
345 /// Parses a Count parameter at the current position. This does not check
346 /// for 'CountIsNextParam' because that is only used in precision, not
348 fn count(&mut self) -> Count
<'a
> {
349 if let Some(i
) = self.integer() {
350 if self.consume('$'
) { CountIsParam(i) }
else { CountIs(i) }
352 let tmp
= self.cur
.clone();
353 let word
= self.word();
358 if self.consume('$'
) {
368 /// Parses a word starting at the current position. A word is considered to
369 /// be an alphabetic character followed by any number of alphanumeric
371 fn word(&mut self) -> &'a
str {
372 let start
= match self.cur
.peek() {
373 Some(&(pos
, c
)) if c
.is_xid_start() => { self.cur.next(); pos }
374 _
=> { return &self.input[..0]; }
376 while let Some(&(pos
, c
)) = self.cur
.peek() {
377 if c
.is_xid_continue() {
380 return &self.input
[start
..pos
];
383 &self.input
[start
..self.input
.len()]
386 /// Optionally parses an integer at the current position. This doesn't deal
387 /// with overflow at all, it's just accumulating digits.
388 fn integer(&mut self) -> Option
<usize> {
390 let mut found
= false;
391 while let Some(&(_
, c
)) = self.cur
.peek() {
392 if let Some(i
) = c
.to_digit(10) {
393 cur
= cur
* 10 + i
as usize;
400 if found { Some(cur) }
else { None }
408 fn same(fmt
: &'
static str, p
: &[Piece
<'
static>]) {
409 let parser
= Parser
::new(fmt
);
410 assert
!(parser
.collect
::<Vec
<Piece
<'
static>>>() == p
);
413 fn fmtdflt() -> FormatSpec
<'
static> {
418 precision
: CountImplied
,
424 fn musterr(s
: &str) {
425 let mut p
= Parser
::new(s
);
427 assert
!(!p
.errors
.is_empty());
432 same("asdf", &[String("asdf")]);
433 same("a{{b", &[String("a"), String("{b")]);
434 same("a}}b", &[String("a"), String("}b")]);
435 same("a}}", &[String("a"), String("}")]);
436 same("}}", &[String("}")]);
437 same("\\}}", &[String("\\"), String("}")]);
440 #[test] fn invalid01() { musterr("{") }
441 #[test] fn invalid02() { musterr("}") }
442 #[test] fn invalid04() { musterr("{3a}") }
443 #[test] fn invalid05() { musterr("{:|}") }
444 #[test] fn invalid06() { musterr("{:>>>}") }
447 fn format_nothing() {
448 same("{}", &[NextArgument(Argument
{
449 position
: ArgumentNext
,
454 fn format_position() {
455 same("{3}", &[NextArgument(Argument
{
456 position
: ArgumentIs(3),
461 fn format_position_nothing_else() {
462 same("{3:}", &[NextArgument(Argument
{
463 position
: ArgumentIs(3),
469 same("{3:a}", &[NextArgument(Argument
{
470 position
: ArgumentIs(3),
475 precision
: CountImplied
,
482 fn format_align_fill() {
483 same("{3:>}", &[NextArgument(Argument
{
484 position
: ArgumentIs(3),
489 precision
: CountImplied
,
494 same("{3:0<}", &[NextArgument(Argument
{
495 position
: ArgumentIs(3),
500 precision
: CountImplied
,
505 same("{3:*<abcd}", &[NextArgument(Argument
{
506 position
: ArgumentIs(3),
511 precision
: CountImplied
,
519 same("{:10s}", &[NextArgument(Argument
{
520 position
: ArgumentNext
,
525 precision
: CountImplied
,
530 same("{:10$.10s}", &[NextArgument(Argument
{
531 position
: ArgumentNext
,
536 precision
: CountIs(10),
537 width
: CountIsParam(10),
541 same("{:.*s}", &[NextArgument(Argument
{
542 position
: ArgumentNext
,
547 precision
: CountIsNextParam
,
552 same("{:.10$s}", &[NextArgument(Argument
{
553 position
: ArgumentNext
,
558 precision
: CountIsParam(10),
563 same("{:a$.b$s}", &[NextArgument(Argument
{
564 position
: ArgumentNext
,
569 precision
: CountIsName("b"),
570 width
: CountIsName("a"),
577 same("{:-}", &[NextArgument(Argument
{
578 position
: ArgumentNext
,
582 flags
: (1 << FlagSignMinus
as u32),
583 precision
: CountImplied
,
588 same("{:+#}", &[NextArgument(Argument
{
589 position
: ArgumentNext
,
593 flags
: (1 << FlagSignPlus
as u32) | (1 << FlagAlternate
as u32),
594 precision
: CountImplied
,
601 fn format_mixture() {
602 same("abcd {3:a} efg", &[String("abcd "), NextArgument(Argument
{
603 position
: ArgumentIs(3),
608 precision
: CountImplied
,
612 }), String(" efg")]);