mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Simplify code, have errors match CPython implementation.
This commit is contained in:
1 parent
a21a2d4773
commit
e037e37018
1 file changed
+29
-36
+29
-36
@@ -29,39 +29,25 @@ impl<'a> FStringParser<'a> {
|
||||
|
||||
while let Some(ch) = self.chars.next() {
|
||||
match ch {
|
||||
'!' if delims.is_empty() => {
|
||||
let x = self.chars.next();
|
||||
if let Some('=') = x {
|
||||
expression.push(ch);
|
||||
expression.push('=');
|
||||
} else {
|
||||
if expression.is_empty() {
|
||||
return Err(EmptyExpression);
|
||||
}
|
||||
'!' if delims.is_empty() && self.chars.peek() != Some(&'=') => {
|
||||
if expression.trim().is_empty() {
|
||||
return Err(EmptyExpression);
|
||||
}
|
||||
|
||||
conversion = Some(match x {
|
||||
Some('s') => ConversionFlag::Str,
|
||||
Some('a') => ConversionFlag::Ascii,
|
||||
Some('r') => ConversionFlag::Repr,
|
||||
Some(_) => {
|
||||
return Err(InvalidConversionFlag);
|
||||
}
|
||||
None => {
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
match self.chars.peek() {
|
||||
Some('}') => {
|
||||
continue;
|
||||
}
|
||||
Some(_) => {
|
||||
return Err(ExpectedRbrace);
|
||||
}
|
||||
None => {
|
||||
break;
|
||||
}
|
||||
conversion = Some(match self.chars.next() {
|
||||
Some('s') => ConversionFlag::Str,
|
||||
Some('a') => ConversionFlag::Ascii,
|
||||
Some('r') => ConversionFlag::Repr,
|
||||
Some(_) => {
|
||||
return Err(InvalidConversionFlag);
|
||||
}
|
||||
None => {
|
||||
return Err(ExpectedRbrace);
|
||||
}
|
||||
});
|
||||
|
||||
if self.chars.peek() != Some(&'}') {
|
||||
return Err(ExpectedRbrace);
|
||||
}
|
||||
}
|
||||
':' if delims.is_empty() => {
|
||||
@@ -314,16 +300,23 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_parse_invalid_fstring() {
|
||||
assert_eq!(parse_fstring("{"), Err(UnclosedLbrace));
|
||||
assert_eq!(parse_fstring("}"), Err(UnopenedRbrace));
|
||||
assert_eq!(parse_fstring("{5!a"), Err(UnclosedLbrace));
|
||||
assert_eq!(parse_fstring("{5!a"), Err(ExpectedRbrace));
|
||||
assert_eq!(parse_fstring("{5!a1}"), Err(ExpectedRbrace));
|
||||
assert_eq!(parse_fstring("{5!"), Err(ExpectedRbrace));
|
||||
|
||||
assert_eq!(parse_fstring("abc{!a 'cat'}"), Err(EmptyExpression));
|
||||
assert_eq!(parse_fstring("{!x}"), Err(EmptyExpression));
|
||||
assert_eq!(parse_fstring("{!a"), Err(EmptyExpression));
|
||||
assert_eq!(parse_fstring("{ !a}"), Err(EmptyExpression));
|
||||
|
||||
assert_eq!(parse_fstring("{5!}"), Err(InvalidConversionFlag));
|
||||
assert_eq!(parse_fstring("{5!x}"), Err(InvalidConversionFlag));
|
||||
|
||||
assert_eq!(parse_fstring("{a:{a:{b}}"), Err(ExpressionNestedTooDeeply));
|
||||
|
||||
assert_eq!(parse_fstring("{a:b}}"), Err(UnopenedRbrace));
|
||||
assert_eq!(parse_fstring("}"), Err(UnopenedRbrace));
|
||||
assert_eq!(parse_fstring("{a:{b}"), Err(UnclosedLbrace));
|
||||
assert_eq!(parse_fstring("{"), Err(UnclosedLbrace));
|
||||
|
||||
// TODO: check for InvalidExpression enum?
|
||||
assert!(parse_fstring("{class}").is_err());
|
||||
@@ -333,6 +326,6 @@ mod tests {
|
||||
fn test_parse_fstring_not_equals() {
|
||||
let source = String::from("{1 != 2}");
|
||||
let parse_ast = parse_fstring(&source);
|
||||
assert_ne!(parse_ast, Err(InvalidConversionFlag));
|
||||
assert!(parse_ast.is_ok());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user