Compare commits

...

6 Commits

Author SHA1 Message Date
Zanie
d2b0b1ca26 Simplify comment 2023-08-22 10:23:31 -05:00
Zanie
6d88ac4ca2 Restore unrelated snapshot 2023-08-22 10:22:34 -05:00
Zanie
268be0cf8e Add more test cases; check format types that occur after placeholders 2023-08-22 10:08:08 -05:00
Zanie
5df9ba716f Consume all remaining placeholders at end of format spec parsing 2023-08-22 09:57:13 -05:00
Zanie
0e3284244b Add failing test case for bad-string-format-character 2023-08-22 09:37:26 -05:00
Zanie
d272874dfd Clean up implementation 2023-08-22 09:36:48 -05:00
3 changed files with 45 additions and 11 deletions

View File

@@ -16,8 +16,10 @@
"{:*^30s}".format("centered") # OK
"{:{s}}".format("hello", s="s") # OK (nested replacement value not checked)
"{:{s:y}}".format("hello", s="s") # [bad-format-character] (nested replacement format spec checked)
"{0:.{prec}g}".format(1.23, prec=15) # OK
"{0:.{foo}x{bar}y{foobar}g}".format(...) # OK (all nested replacements are consumed without considering in between chars)
"{0:.{foo}{bar}{foobar}y}".format(...) # [bad-format-character] (check value after replacements)
## f-strings

View File

@@ -51,14 +51,24 @@ bad_string_format_character.py:15:1: PLE1300 Unsupported format character 'y'
17 | "{:*^30s}".format("centered") # OK
|
bad_string_format_character.py:20:1: PLE1300 Unsupported format character 'y'
bad_string_format_character.py:19:1: PLE1300 Unsupported format character 'y'
|
17 | "{:*^30s}".format("centered") # OK
18 | "{:{s}}".format("hello", s="s") # OK (nested replacement value not checked)
19 |
20 | "{:{s:y}}".format("hello", s="s") # [bad-format-character] (nested replacement format spec checked)
19 | "{:{s:y}}".format("hello", s="s") # [bad-format-character] (nested replacement format spec checked)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PLE1300
21 |
22 | ## f-strings
20 | "{0:.{prec}g}".format(1.23, prec=15) # OK
21 | "{0:.{foo}x{bar}y{foobar}g}".format(...) # OK (all nested replacements are consumed without considering in between chars)
|
bad_string_format_character.py:22:1: PLE1300 Unsupported format character 'y'
|
20 | "{0:.{prec}g}".format(1.23, prec=15) # OK
21 | "{0:.{foo}x{bar}y{foobar}g}".format(...) # OK (all nested replacements are consumed without considering in between chars)
22 | "{0:.{foo}{bar}{foobar}y}".format(...) # [bad-format-character] (check value after replacements)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PLE1300
23 |
24 | ## f-strings
|

View File

@@ -318,9 +318,9 @@ fn parse_precision(text: &str) -> Result<(Option<usize>, &str), FormatSpecError>
})
}
/// Parses a format part within a format spec
/// Parses a placeholder within a format spec
fn parse_nested_placeholder<'a>(
parts: &mut Vec<FormatPart>,
placeholders: &mut Vec<FormatPart>,
text: &'a str,
) -> Result<&'a str, FormatSpecError> {
match FormatString::parse_spec(text, AllowPlaceholderNesting::No) {
@@ -328,16 +328,38 @@ fn parse_nested_placeholder<'a>(
Err(FormatParseError::MissingStartBracket) => Ok(text),
Err(err) => Err(FormatSpecError::InvalidPlaceholder(err)),
Ok((format_part, text)) => {
parts.push(format_part);
placeholders.push(format_part);
Ok(text)
}
}
}
/// Parse and consume all placeholders in a format spec
/// This will also consume any intermediate characters such as `x` and `y` in `"x{foo}y{bar}z"`
fn consume_all_placeholders<'a>(
placeholders: &mut Vec<FormatPart>,
text: &'a str,
) -> Result<&'a str, FormatSpecError> {
let mut chars = text.chars();
let mut text = text;
let mut placeholder_count = placeholders.len();
while chars.clone().contains(&'{') {
text = parse_nested_placeholder(placeholders, chars.as_str())?;
chars = text.chars();
// If we did not parse a placeholder, consume a character
if placeholder_count == placeholders.len() {
chars.next();
} else {
placeholder_count = placeholders.len();
}
}
Ok(text)
}
impl FormatSpec {
pub fn parse(text: &str) -> Result<Self, FormatSpecError> {
let mut replacements = vec![];
// get_integer in CPython
let text = parse_nested_placeholder(&mut replacements, text)?;
let (conversion, text) = FormatConversion::parse(text);
let text = parse_nested_placeholder(&mut replacements, text)?;
@@ -354,7 +376,7 @@ impl FormatSpec {
let (grouping_option, text) = FormatGrouping::parse(text);
let text = parse_nested_placeholder(&mut replacements, text)?;
let (precision, text) = parse_precision(text)?;
let text = parse_nested_placeholder(&mut replacements, text)?;
let text = consume_all_placeholders(&mut replacements, text)?;
let (format_type, _text) = if text.is_empty() {
(None, text)