mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-21 12:05:27 +00:00
Refactor parse_formatted_value
This commit is contained in:
parent
c21d0d9283
commit
9aed9143fe
1 changed files with 10 additions and 13 deletions
|
@ -26,7 +26,7 @@ impl FStringParser {
|
||||||
mut chars: iter::Peekable<str::Chars<'a>>,
|
mut chars: iter::Peekable<str::Chars<'a>>,
|
||||||
nested: u8,
|
nested: u8,
|
||||||
) -> Result<(Vec<Expr>, iter::Peekable<str::Chars<'a>>), FStringErrorType> {
|
) -> Result<(Vec<Expr>, iter::Peekable<str::Chars<'a>>), FStringErrorType> {
|
||||||
let mut expression = String::from("{");
|
let mut expression = String::new();
|
||||||
let mut spec = None;
|
let mut spec = None;
|
||||||
let mut delims = Vec::new();
|
let mut delims = Vec::new();
|
||||||
let mut conversion = ConversionFlag::None;
|
let mut conversion = ConversionFlag::None;
|
||||||
|
@ -67,14 +67,14 @@ impl FStringParser {
|
||||||
Some('a') => ConversionFlag::Ascii,
|
Some('a') => ConversionFlag::Ascii,
|
||||||
Some('r') => ConversionFlag::Repr,
|
Some('r') => ConversionFlag::Repr,
|
||||||
Some(_) => {
|
Some(_) => {
|
||||||
return Err(if expression[1..].trim().is_empty() {
|
return Err(if expression.trim().is_empty() {
|
||||||
EmptyExpression
|
EmptyExpression
|
||||||
} else {
|
} else {
|
||||||
InvalidConversionFlag
|
InvalidConversionFlag
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
return Err(if expression[1..].trim().is_empty() {
|
return Err(if expression.trim().is_empty() {
|
||||||
EmptyExpression
|
EmptyExpression
|
||||||
} else {
|
} else {
|
||||||
UnclosedLbrace
|
UnclosedLbrace
|
||||||
|
@ -84,14 +84,14 @@ impl FStringParser {
|
||||||
|
|
||||||
if let Some(&peek) = chars.peek() {
|
if let Some(&peek) = chars.peek() {
|
||||||
if peek != '}' && peek != ':' {
|
if peek != '}' && peek != ':' {
|
||||||
return Err(if expression[1..].trim().is_empty() {
|
return Err(if expression.trim().is_empty() {
|
||||||
EmptyExpression
|
EmptyExpression
|
||||||
} else {
|
} else {
|
||||||
UnclosedLbrace
|
UnclosedLbrace
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return Err(if expression[1..].trim().is_empty() {
|
return Err(if expression.trim().is_empty() {
|
||||||
EmptyExpression
|
EmptyExpression
|
||||||
} else {
|
} else {
|
||||||
UnclosedLbrace
|
UnclosedLbrace
|
||||||
|
@ -156,15 +156,14 @@ impl FStringParser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
'}' => {
|
'}' => {
|
||||||
if expression[1..].trim().is_empty() {
|
if expression.trim().is_empty() {
|
||||||
return Err(EmptyExpression);
|
return Err(EmptyExpression);
|
||||||
}
|
}
|
||||||
expression.push(ch);
|
|
||||||
|
|
||||||
let ret = if !self_documenting {
|
let ret = if !self_documenting {
|
||||||
vec![self.expr(ExprKind::FormattedValue {
|
vec![self.expr(ExprKind::FormattedValue {
|
||||||
value: Box::new(
|
value: Box::new(
|
||||||
parse_fstring_expr(&expression[1..expression.len() - 1])
|
parse_fstring_expr(&expression)
|
||||||
.map_err(|e| InvalidExpression(Box::new(e.error)))?,
|
.map_err(|e| InvalidExpression(Box::new(e.error)))?,
|
||||||
),
|
),
|
||||||
conversion: conversion as _,
|
conversion: conversion as _,
|
||||||
|
@ -173,9 +172,7 @@ impl FStringParser {
|
||||||
} else {
|
} else {
|
||||||
vec![
|
vec![
|
||||||
self.expr(ExprKind::Constant {
|
self.expr(ExprKind::Constant {
|
||||||
value: Constant::Str(
|
value: Constant::Str(expression.to_owned() + "="),
|
||||||
expression[1..expression.len() - 1].to_owned() + "=",
|
|
||||||
),
|
|
||||||
kind: None,
|
kind: None,
|
||||||
}),
|
}),
|
||||||
self.expr(ExprKind::Constant {
|
self.expr(ExprKind::Constant {
|
||||||
|
@ -184,7 +181,7 @@ impl FStringParser {
|
||||||
}),
|
}),
|
||||||
self.expr(ExprKind::FormattedValue {
|
self.expr(ExprKind::FormattedValue {
|
||||||
value: Box::new(
|
value: Box::new(
|
||||||
parse_fstring_expr(&expression[1..expression.len() - 1])
|
parse_fstring_expr(&expression)
|
||||||
.map_err(|e| InvalidExpression(Box::new(e.error)))?,
|
.map_err(|e| InvalidExpression(Box::new(e.error)))?,
|
||||||
),
|
),
|
||||||
conversion: (if conversion == ConversionFlag::None && spec.is_none()
|
conversion: (if conversion == ConversionFlag::None && spec.is_none()
|
||||||
|
@ -226,7 +223,7 @@ impl FStringParser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(if expression[1..].trim().is_empty() {
|
Err(if expression.trim().is_empty() {
|
||||||
EmptyExpression
|
EmptyExpression
|
||||||
} else {
|
} else {
|
||||||
UnclosedLbrace
|
UnclosedLbrace
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue