-
Notifications
You must be signed in to change notification settings - Fork 7
/
fmt.rs
379 lines (326 loc) · 12.6 KB
/
fmt.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
use proc_macro2::{
Span,
token_stream::IntoIter as TokenIterator,
};
use unicode_xid::UnicodeXID;
use std::str::Chars;
use crate::{
err::Error,
ir::{
ArgRefKind, ArgRef, FormatStr, Style,
FormatStrFragment, FormatSpec, Align, Sign, Width, Precision,
},
};
use super::{parse, expect_helper_group, lit::expect_str_literal};
impl FormatStr {
pub(crate) fn parse(it: &mut TokenIterator) -> Result<Self, Error> {
/// Searches for the next closing `}`. Returns a pair of strings, the
/// first starting like `s` and ending at the closing brace, the second
/// starting at the brace and ending like `s`. Both strings exclude the
/// brace itself. If a closing brace can't be found, an error is
/// returned.
fn split_at_closing_brace(s: &str, span: Span) -> Result<(&str, &str), Error> {
// I *think* there can't be escaped closing braces inside the fmt
// format, so we can simply search for a single closing one.
let end = s.find("}")
.ok_or(err!(span, "unclosed '{{' in format string"))?;
Ok((&s[..end], &s[end + 1..]))
}
let (inner, _) = expect_helper_group(it.next())?;
let (raw, span) = parse(inner, expect_str_literal)?;
// Scan the whole string
let mut fragments = Vec::new();
let mut s = &raw[..];
while !s.is_empty() {
fn string_without<'a>(a: &'a str, b: &'a str) -> &'a str {
let end = b.as_ptr() as usize - a.as_ptr() as usize;
&a[..end]
}
// let start_string = s;
let mut args = Vec::new();
let mut fmt_str_parts = Vec::new();
// Scan until we reach a style tag.
let mut scanner = s;
loop {
match scanner.find('{') {
Some(brace_pos) => scanner = &scanner[brace_pos..],
None => {
// EOF reached: stop searching
scanner = &scanner[scanner.len()..];
break;
}
}
match () {
// Escaped brace: skip.
() if scanner.starts_with("{{") => scanner = &scanner[2..],
// Found a style tag: stop searching!
() if scanner.starts_with("{$") => break,
() if scanner.starts_with("{/$") => break,
// Found a styled argument: stop searching!
() if scanner.starts_with("{[") => break,
// An formatting argument. Gather some information about it
// and remember it for later.
_ => {
let (inner, rest) = split_at_closing_brace(&scanner[1..], span)?;
args.push(ArgRef::parse(inner)?);
fmt_str_parts.push(string_without(s, scanner).to_owned());
s = rest;
scanner = rest;
}
}
}
// Add the last string part and then push this fragment, unless it
// is completely empty.
fmt_str_parts.push(string_without(s, scanner).to_owned());
s = scanner;
if !args.is_empty() || fmt_str_parts.iter().any(|s| !s.is_empty()) {
fragments.push(FormatStrFragment::Fmt { args, fmt_str_parts });
}
if s.is_empty() {
break;
}
// At this point, `s` starts with either a styled argument or a
// style tag.
match () {
// Closing style tag.
() if s.starts_with("{/$}") => {
fragments.push(FormatStrFragment::StyleEnd);
s = &s[4..];
}
// Opening style tag.
() if s.starts_with("{$") => {
let (inner, rest) = split_at_closing_brace(&s[2..], span)?;
let style = Style::parse(inner, span)?;
fragments.push(FormatStrFragment::StyleStart(style));
s = rest;
}
() if s.starts_with("{[") => {
let (inner, rest) = split_at_closing_brace(&s[1..], span)?;
// Parse style information
let style_end = inner.find(']')
.ok_or(err!(span, "unclosed '[' in format string argument"))?;
let style = Style::parse(&inner[1..style_end], span)?;
fragments.push(FormatStrFragment::StyleStart(style));
// Parse the standard part of this arg reference.
let standard_inner = inner[style_end + 1..].trim_start();
let arg = ArgRef::parse(standard_inner)?;
fragments.push(FormatStrFragment::Fmt {
args: vec![arg],
fmt_str_parts: vec!["".into(), "".into()],
});
fragments.push(FormatStrFragment::StyleEnd);
s = rest;
}
_ => panic!("bug: at this point, there should be a style tag or styled arg"),
}
}
Ok(Self { fragments })
}
}
impl ArgRef {
/// (Partially) parses the inside of an format arg (`{...}`). The given
/// string `s` must be the inside of the arg and must *not* contain the
/// outer braces.
pub(crate) fn parse(s: &str) -> Result<Self, Error> {
// Split argument reference and format specs.
let (arg_str, format_spec) = match s.find(':') {
None => (s, ""),
Some(colon_pos) => (&s[..colon_pos], &s[colon_pos + 1..]),
};
// Check kind of argument reference.
let kind = if arg_str.is_empty() {
ArgRefKind::Next
} else if let Ok(pos) = arg_str.parse::<usize>() {
ArgRefKind::Position(pos)
} else {
// TODO: make sure the string is a valid Rust identifier
ArgRefKind::Name(arg_str.into())
};
let format_spec = FormatSpec::parse(format_spec)?;
Ok(Self { kind, format_spec })
}
}
impl FormatSpec {
/// Parses the format specification that comes after the `:` inside an
/// `{...}` argument. The given string must not include the `:` but might be
/// empty.
pub(crate) fn parse(s: &str) -> Result<Self, Error> {
/// Helper iterator for scanning the input
struct Peek2<'a> {
peek: Option<char>,
peek2: Option<char>,
rest: Chars<'a>,
}
impl<'a> Peek2<'a> {
fn new(s: &'a str) -> Self {
let mut rest = s.chars();
let peek = rest.next();
let peek2 = rest.next();
Self { peek, peek2, rest }
}
fn next_if<O>(&mut self, f: impl FnOnce(char) -> Option<O>) -> Option<O> {
let out = self.peek.and_then(f);
if out.is_some() {
self.next();
}
out
}
fn next_if_eq(&mut self, expected: char) -> bool {
if self.peek == Some(expected) {
self.next();
true
} else {
false
}
}
/// Parses one decimal number and returns whether or not it was
/// terminated by `$`.
fn parse_num(&mut self) -> Result<(usize, bool), Error> {
let mut num: usize = 0;
while matches!(self.peek, Some('0'..='9')) {
num = num.checked_mul(10)
.and_then(|num| {
num.checked_add((self.next().unwrap() as u32 - '0' as u32) as usize)
})
.ok_or(err!("width parameter value overflowed `usize`"))?;
}
let arg = self.next_if_eq('$');
Ok((num, arg))
}
/// Parses a Rust identifier terminated by '$'. When calling this
/// method, `self.peek.map_or(false, |c| c.is_xid_start())` must be
/// true.
fn parse_ident(&mut self) -> Result<String, Error> {
let mut name = String::from(self.next().unwrap());
while self.peek.map_or(false, |c| c.is_xid_continue()) {
name.push(self.next().unwrap());
}
if !self.next_if_eq('$') {
return Err(err!(
"invalid format string specification: width/precision named parameter \
does not end with '$'. Note: try the `std` macros to get much better \
error reporting."
));
}
Ok(name)
}
}
impl Iterator for Peek2<'_> {
type Item = char;
fn next(&mut self) -> Option<Self::Item> {
let out = self.peek.take();
if out.is_none() {
return None;
}
self.peek = self.peek2;
self.peek2 = self.rest.next();
out
}
}
let mut it = Peek2::new(s);
// Fill and align. The former can only exist if the latter also exists.
let (fill, align) = if let Some(align) = it.next_if(Align::from_char) {
(None, Some(align))
} else if let Some(align) = it.peek2.and_then(Align::from_char) {
let fill = it.next().unwrap();
it.next().unwrap();
(Some(fill), Some(align))
} else {
(None, None)
};
// Simple flags.
let sign = it.next_if(Sign::from_char);
let alternate = it.next_if_eq('#');
let zero = if it.peek == Some('0') && it.peek2 != Some('$') {
it.next().unwrap();
true
} else {
false
};
// Width or early exit.
let width = match it.peek {
// Either a width constant (`8`) or referring to a positional
// parameter (`2$`).
Some('0'..='9') => {
let (num, dollar) = it.parse_num()?;
if dollar {
Some(Width::Position(num))
} else {
Some(Width::Constant(num))
}
}
// The "type" (e.g. `?`, `X`) determining the formatting trait. This
// means we are done here.
Some(c) if it.peek2.is_none() => {
return Ok(Self {
fill,
align,
sign,
alternate,
zero,
width: None,
precision: None,
ty: Some(c),
});
}
// The start of a `width` named parameter (`foo$`).
Some(c) if c.is_xid_start() => Some(Width::Name(it.parse_ident()?)),
_ => None,
};
// Precision starting with '.'.
let precision = if it.next_if_eq('.') {
if it.next_if_eq('*') {
Some(Precision::Bundled)
} else if matches!(it.peek, Some('0'..='9')) {
let (num, dollar) = it.parse_num()?;
if dollar {
Some(Precision::Position(num))
} else {
Some(Precision::Constant(num))
}
} else {
Some(Precision::Name(it.parse_ident()?))
}
} else {
None
};
// Parse type char and make sure nothing else is left.
let ty = it.next();
if let Some(c) = it.next() {
return Err(err!(
"expected end of format specification, but found '{}'. Note: use the std \
macros to get much better error reporting.",
c,
));
}
Ok(Self {
fill,
align,
sign,
alternate,
zero,
width,
precision,
ty,
})
}
}
impl Align {
fn from_char(c: char) -> Option<Self> {
match c {
'<' => Some(Self::Left),
'^' => Some(Self::Center),
'>' => Some(Self::Right),
_ => None,
}
}
}
impl Sign {
fn from_char(c: char) -> Option<Self> {
match c {
'+' => Some(Self::Plus),
'-' => Some(Self::Minus),
_ => None,
}
}
}