Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
default = ["constant-optimization", "fold"]
constant-optimization = ["fold"]
fold = []
implicit-concat = []
unparse = ["rustpython-common"]

[dependencies]
Expand Down
8 changes: 8 additions & 0 deletions compiler/ast/src/ast_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ pub enum ExprKind<U = ()> {
JoinedStr {
values: Vec<Expr<U>>,
},
#[cfg(feature = "implicit-concat")]
ImplicitConcat {
values: Vec<Expr<U>>,
},
Constant {
value: Constant,
kind: Option<String>,
Expand Down Expand Up @@ -863,6 +867,10 @@ pub mod fold {
ExprKind::JoinedStr { values } => Ok(ExprKind::JoinedStr {
values: Foldable::fold(values, folder)?,
}),
#[cfg(feature = "implicit-concat")]
ExprKind::ImplicitConcat { values } => Ok(ExprKind::JoinedStr {
values: Foldable::fold(values, folder)?,
}),
ExprKind::Constant { value, kind } => Ok(ExprKind::Constant {
value: Foldable::fold(value, folder)?,
kind: Foldable::fold(kind, folder)?,
Expand Down
2 changes: 2 additions & 0 deletions compiler/ast/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ impl<U> ExprKind<U> {
"literal"
}
}
#[cfg(feature = "implicit-concat")]
ExprKind::ImplicitConcat { .. } => "implicit concatenation",
ExprKind::FormattedValue { .. } => "f-string expression",
ExprKind::Name { .. } => "name",
ExprKind::Lambda { .. } => "lambda",
Expand Down
6 changes: 6 additions & 0 deletions compiler/ast/src/unparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ impl<'a> Unparser<'a> {
format_spec,
} => self.unparse_formatted(value, *conversion, format_spec.as_deref())?,
ExprKind::JoinedStr { values } => self.unparse_joinedstr(values, false)?,
#[cfg(feature = "implicit-concat")]
ExprKind::ImplicitConcat { values } => {
for value in values {
self.unparse_expr(value, precedence::ATOM)?;
}
}
ExprKind::Constant { value, kind } => {
if let Some(kind) = kind {
self.p(kind)?;
Expand Down
1 change: 1 addition & 0 deletions compiler/parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ edition = "2021"

[features]
default = ["lalrpop"] # removing this causes potential build failure
implicit-concat = ["rustpython-ast/implicit-concat"]

[build-dependencies]
anyhow = "1.0.45"
Expand Down
78 changes: 51 additions & 27 deletions compiler/parser/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub fn parse_strings(
values: Vec<(Location, (String, StringKind, bool), Location)>,
) -> Result<Expr, LexicalError> {
// Preserve the initial location and kind.
let is_implicit_concat = cfg!(feature = "implicit-concat") && values.len() > 1;
let initial_start = values[0].0;
let last_end = values.last().unwrap().2;
let initial_kind = (values[0].1 .1 == StringKind::Unicode).then(|| "u".to_owned());
Expand All @@ -29,17 +30,30 @@ pub fn parse_strings(
});
}

#[cfg(feature = "implicit-concat")]
if is_implicit_concat {
let mut exprs: Vec<Expr> = vec![];
for (start, (source, kind, triple_quoted), end) in values {
let expr = parse_string(&source, kind, triple_quoted, start, end)?;
exprs.push(expr);
}
return Ok(Expr::new(
initial_start,
last_end,
ExprKind::ImplicitConcat { values: exprs },
));
}

if has_bytes {
let mut content: Vec<u8> = vec![];
for (start, (source, kind, triple_quoted), end) in values {
for value in parse_string(&source, kind, triple_quoted, start, end)? {
match value.node {
ExprKind::Constant {
value: Constant::Bytes(value),
..
} => content.extend(value),
_ => unreachable!("Unexpected non-bytes expression."),
}
let expr = parse_string(&source, kind, triple_quoted, start, end)?;
match expr.node {
ExprKind::Constant {
value: Constant::Bytes(value),
..
} => content.extend(value),
_ => unreachable!("Unexpected non-bytes expression."),
}
}
return Ok(Expr::new(
Expand All @@ -55,14 +69,13 @@ pub fn parse_strings(
if !has_fstring {
let mut content: Vec<String> = vec![];
for (start, (source, kind, triple_quoted), end) in values {
for value in parse_string(&source, kind, triple_quoted, start, end)? {
match value.node {
ExprKind::Constant {
value: Constant::Str(value),
..
} => content.push(value),
_ => unreachable!("Unexpected non-string expression."),
}
let expr = parse_string(&source, kind, triple_quoted, start, end)?;
match expr.node {
ExprKind::Constant {
value: Constant::Str(value),
..
} => content.push(value),
_ => unreachable!("Unexpected non-string expression."),
}
}
return Ok(Expr::new(
Expand Down Expand Up @@ -91,20 +104,31 @@ pub fn parse_strings(
};

for (start, (source, kind, triple_quoted), end) in values {
for value in parse_string(&source, kind, triple_quoted, start, end)? {
match value.node {
ExprKind::FormattedValue { .. } => {
if !current.is_empty() {
deduped.push(take_current(&mut current));
let expr = parse_string(&source, kind, triple_quoted, start, end)?;

match expr.node {
ExprKind::Constant {
value: Constant::Str(s),
..
} => current.push(s),
ExprKind::JoinedStr { values } => {
for value in values {
match &value.node {
ExprKind::FormattedValue { .. } => {
if !current.is_empty() {
deduped.push(take_current(&mut current));
}
deduped.push(value)
}
ExprKind::Constant {
value: Constant::Str(s),
..
} => current.push(s.to_owned()),
_ => unreachable!("Unexpected expression for JoinedStr value"),
}
deduped.push(value)
}
ExprKind::Constant {
value: Constant::Str(value),
..
} => current.push(value),
_ => unreachable!("Unexpected non-string expression."),
}
_ => unreachable!("Unexpected non-string expression."),
}
}
if !current.is_empty() {
Expand Down
11 changes: 6 additions & 5 deletions compiler/parser/src/string_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,13 +502,14 @@ impl<'a> StringParser<'a> {
}))
}

pub fn parse(&mut self) -> Result<Vec<Expr>, LexicalError> {
pub fn parse(&mut self) -> Result<Expr, LexicalError> {
if self.kind.is_fstring() {
self.parse_fstring(0)
.map(|values| self.expr(ExprKind::JoinedStr { values }))
} else if self.kind.is_bytes() {
self.parse_bytes().map(|expr| vec![expr])
self.parse_bytes()
} else {
self.parse_string().map(|expr| vec![expr])
self.parse_string()
}
}
}
Expand All @@ -528,15 +529,15 @@ pub fn parse_string(
triple_quoted: bool,
start: Location,
end: Location,
) -> Result<Vec<Expr>, LexicalError> {
) -> Result<Expr, LexicalError> {
StringParser::new(source, kind, triple_quoted, start, end).parse()
}

#[cfg(test)]
mod tests {
use super::*;

fn parse_fstring(source: &str) -> Result<Vec<Expr>, FStringErrorType> {
fn parse_fstring(source: &str) -> Result<Expr, FStringErrorType> {
StringParser::new(
source,
StringKind::FString,
Expand Down
3 changes: 2 additions & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
stable
[toolchain]
channel = "nightly"