Skip to content

Commit

Permalink
Code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Sep 8, 2023
1 parent c986c60 commit ad56d52
Show file tree
Hide file tree
Showing 15 changed files with 9,393 additions and 9,432 deletions.
3 changes: 1 addition & 2 deletions crates/ruff_python_ast/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2552,8 +2552,7 @@ impl Constant {
}
}

/// Returns `true` if the constant is a string constant that's a unicode
/// string (i.e., `u"..."`).
/// Returns `true` if the constant is a string constant that is a unicode string (i.e., `u"..."`).
pub fn is_unicode_string(&self) -> bool {
match self {
Constant::Str(value) => value.unicode,
Expand Down
8 changes: 7 additions & 1 deletion crates/ruff_python_parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,7 @@ a = 1
let parse_ast = parse_suite(
r#"
f"{" f"}"
f"{foo!s}"
f"{3,}"
f"{3!=4:}"
f'{3:{"}"}>10}'
Expand All @@ -1264,6 +1265,11 @@ f"{ foo = :.3f }"
f"{ foo = !s }"
f"{ 1, 2 = }"
f'{f"{3.1415=:.1f}":*^20}'
{"foo " f"bar {x + y} " "baz": 10}
match foo:
case "foo " f"bar {x + y} " "baz":
pass
"#
.trim(),
"<test>",
Expand All @@ -1279,7 +1285,7 @@ f'{f"{3.1415=:.1f}":*^20}'
u"foo" f"{bar}" "baz" " some"
"foo" f"{bar}" u"baz" " some"
"foo" f"{bar}" "baz" u" some"
u"foo" f"bar {baz} realy" u"bar" "no"
u"foo" f"bar {baz} really" u"bar" "no"
"#
.trim(),
"<test>",
Expand Down
68 changes: 40 additions & 28 deletions crates/ruff_python_parser/src/python.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// See also: https://greentreesnakes.readthedocs.io/en/latest/nodes.html#keyword

use num_bigint::BigInt;
use ruff_text_size::{Ranged, TextRange, TextSize};
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
use ruff_python_ast::{self as ast, IpyEscapeKind};
use crate::{
FStringErrorType,
Expand Down Expand Up @@ -1580,7 +1580,10 @@ StringLiteralOrFString: ast::ParenthesizedExpr = {
};

StringLiteral: ast::ParenthesizedExpr = {
<location:@L> <s:string> =>? Ok(parse_string_literal(<>)?.into()),
<start_location:@L> <string:string> =>? {
let (source, kind, triple_quoted) = string;
Ok(parse_string_literal(&source, kind, triple_quoted, start_location)?.into())
}
};

FStringExpr: ast::ParenthesizedExpr = {
Expand All @@ -1595,41 +1598,50 @@ FStringExpr: ast::ParenthesizedExpr = {

FStringMiddlePattern: Option<ast::Expr> = {
FStringReplacementField,
<location:@L> <s:fstring_middle> =>? Ok(parse_fstring_middle(<>)?),
<start_location:@L> <fstring_middle:fstring_middle> =>? {
let (source, is_raw) = fstring_middle;
Ok(parse_fstring_middle(&source, is_raw, start_location)?)
}
};

FStringReplacementField: Option<ast::Expr> = {
<location:@L> "{" <value:TestListOrYieldExpr> <debug:"="?> <c:FStringConversion?> <format_spec:FStringFormatSpec?> "}" <end_location:@R> => {
let mut conversion = ast::ConversionFlag::None;
let debug_text = if debug.is_some() {
let start_offset = location + TextSize::from(1); // +1 to skip the opening brace
let format_spec_ref = format_spec.as_ref();
let end_offset = c.map_or_else(|| {
// Subtract 1 to account for the closing brace or the `:` in the format spec
format_spec_ref.map_or(end_location, |f| f.range().start()) - TextSize::from(1)
}, |(offset, flag)| {
conversion = flag;
offset
});
Some(ast::DebugText {
<location:@L> "{" <value:TestListOrYieldExpr> <debug:"="?> <conversion:FStringConversion?> <format_spec:FStringFormatSpecSuffix?> "}" <end_location:@R> => {
let debug_text = debug.map(|_| {
let start_offset = location + "{".text_len();
let end_offset = if let Some((conversion_start, _)) = conversion {
conversion_start
} else {
format_spec.as_ref().map_or_else(
|| end_location - "}".text_len(),
|spec| spec.range().start() - ":".text_len(),
)
};
ast::DebugText {
leading: source_code[TextRange::new(start_offset, value.range().start())].to_string(),
trailing: source_code[TextRange::new(value.range().end(), end_offset)].to_string(),
})
} else {
None
};
Some(ast::ExprFormattedValue {
value: Box::new(value.into()),
debug_text,
conversion,
format_spec: format_spec.map(Box::new),
range: (location..end_location).into(),
}.into())
}
});
Some(
ast::ExprFormattedValue {
value: Box::new(value.into()),
debug_text,
conversion: conversion.map_or(ast::ConversionFlag::None, |(_, conversion_flag)| {
conversion_flag
}),
format_spec: format_spec.map(Box::new),
range: (location..end_location).into(),
}
.into()
)
}
};

FStringFormatSpecSuffix: ast::Expr = {
":" <format_spec:FStringFormatSpec> => format_spec
};

FStringFormatSpec: ast::Expr = {
":" <location:@L> <values:FStringMiddlePattern*> <end_location:@R> => {
<location:@L> <values:FStringMiddlePattern*> <end_location:@R> => {
ast::ExprFString {
values: values.into_iter().flatten().collect(),
implicit_concatenated: false,
Expand Down
Loading

0 comments on commit ad56d52

Please sign in to comment.