Skip to content

Commit

Permalink
Add a helper for parsing string literals
Browse files Browse the repository at this point in the history
Summary: To get better than we do currently, we probably have to do a little smarter with what goes where. Easiest to have a helper that gets the whole string literal, not us doing some preprocessing outside.

Reviewed By: stroxler

Differential Revision: D68324304

fbshipit-source-id: 62ef30a7e7b11a840be4903d2b57c9bb1a0e6533
  • Loading branch information
ndmitchell authored and facebook-github-bot committed Jan 17, 2025
1 parent 540c3e8 commit 6d4576a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
13 changes: 13 additions & 0 deletions pyre2/pyre2/bin/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::slice;

use ruff_python_ast::Expr;
use ruff_python_ast::ExprName;
use ruff_python_ast::ExprStringLiteral;
use ruff_python_ast::Identifier;
use ruff_python_ast::ModModule;
use ruff_python_ast::Parameter;
Expand All @@ -18,6 +19,7 @@ use ruff_python_ast::Pattern;
use ruff_python_ast::PySourceType;
use ruff_python_ast::Stmt;
use ruff_python_ast::StmtIf;
use ruff_python_ast::StringFlags;
use ruff_python_parser::parse_expression_range;
use ruff_python_parser::parse_unchecked_source;
use ruff_python_parser::ParseError;
Expand Down Expand Up @@ -52,6 +54,17 @@ impl Ast {
.body)
}

pub fn parse_type_literal(x: &ExprStringLiteral) -> anyhow::Result<Expr> {
let mut s = x.value.to_str().to_owned();
if x.value.iter().any(|x| x.flags.is_triple_quoted()) {
// Implicitly bracketed, so add them explicitly
s = format!("({s})");
}
// These positions are adequate (they are at least inside the literal) but might not be precise
// given string gaps.
Ast::parse_expr(&s, x.range.start())
}

pub fn unpack_slice(x: &Expr) -> &[Expr] {
match x {
Expr::Tuple(x) => &x.elts,
Expand Down
16 changes: 5 additions & 11 deletions pyre2/pyre2/bin/binding/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ use ruff_python_ast::StmtClassDef;
use ruff_python_ast::StmtFunctionDef;
use ruff_python_ast::StmtImportFrom;
use ruff_python_ast::StmtReturn;
use ruff_python_ast::StringFlags;
use ruff_python_ast::TypeParam;
use ruff_python_ast::TypeParams;
use ruff_text_size::Ranged;
Expand Down Expand Up @@ -759,15 +758,7 @@ impl<'a> BindingsBuilder<'a> {
}
}
Expr::StringLiteral(literal) => {
let mut s = literal.value.to_str().to_owned();
if literal.value.iter().any(|x| x.flags.is_triple_quoted()) {
// Implicitly bracketed, so add them explicitly
s = format!("({s})");
}
// We use position information to uniquely key names, so make sure we find fresh positions.
// Because of string escapes and splits, these might not be perfect, but they are definitely fresh
// as they point inside the string we got rid of.
match Ast::parse_expr(&s, literal.range.start()) {
match Ast::parse_type_literal(literal) {
Ok(expr) => {
*x = expr;
// You are not allowed to nest type strings in type strings,
Expand All @@ -776,7 +767,10 @@ impl<'a> BindingsBuilder<'a> {
Err(e) => {
self.error(
literal.range,
format!("Could not parse type string: {s}, got {e}"),
format!(
"Could not parse type string: {}, got {e}",
literal.value.to_str()
),
);
}
}
Expand Down

0 comments on commit 6d4576a

Please sign in to comment.