Remove Parse trait (#6235)

This commit is contained in:
Micha Reiser 2023-08-01 18:35:03 +02:00 committed by GitHub
parent 83fe103d6e
commit debfca3a11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 235 additions and 1420 deletions

View file

@ -49,132 +49,131 @@ pub(crate) fn set_context(expr: Expr, ctx: ExprContext) -> Expr {
#[cfg(test)]
mod tests {
use crate::Parse;
use ruff_python_ast as ast;
use crate::parser::parse_suite;
#[test]
fn test_assign_name() {
let source = "x = (1, 2, 3)";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_assign_tuple() {
let source = "(x, y) = (1, 2, 3)";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_assign_list() {
let source = "[x, y] = (1, 2, 3)";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_assign_attribute() {
let source = "x.y = (1, 2, 3)";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_assign_subscript() {
let source = "x[y] = (1, 2, 3)";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_assign_starred() {
let source = "(x, *y) = (1, 2, 3)";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_assign_for() {
let source = "for x in (1, 2, 3): pass";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_assign_list_comp() {
let source = "x = [y for y in (1, 2, 3)]";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_assign_set_comp() {
let source = "x = {y for y in (1, 2, 3)}";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_assign_with() {
let source = "with 1 as x: pass";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_assign_named_expr() {
let source = "if x:= 1: pass";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_ann_assign_name() {
let source = "x: int = 1";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_aug_assign_name() {
let source = "x += 1";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_aug_assign_attribute() {
let source = "x.y += (1, 2, 3)";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_aug_assign_subscript() {
let source = "x[y] += (1, 2, 3)";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_del_name() {
let source = "del x";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_del_attribute() {
let source = "del x.y";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_del_subscript() {
let source = "del x[y]";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
}

View file

@ -133,15 +133,15 @@ const fn is_starred(exp: &ast::Expr) -> bool {
#[cfg(test)]
mod tests {
use super::*;
use crate::{Parse, ParseErrorType};
use ruff_python_ast::{self as ast};
use crate::parser::parse_suite;
use crate::ParseErrorType;
macro_rules! function_and_lambda {
($($name:ident: $code:expr,)*) => {
$(
#[test]
fn $name() {
let parse_ast = ast::Suite::parse($code, "<test>");
let parse_ast = crate::parser::parse_suite($code, "<test>");
insta::assert_debug_snapshot!(parse_ast);
}
)*
@ -172,7 +172,7 @@ mod tests {
}
fn function_parse_error(src: &str) -> LexicalErrorType {
let parse_ast = ast::Suite::parse(src, "<test>");
let parse_ast = parse_suite(src, "<test>");
parse_ast
.map_err(|e| match e.error {
ParseErrorType::Lexical(e) => e,

View file

@ -94,14 +94,13 @@
//! mode or tokenizing the source beforehand:
//!
//! ```
//! use ruff_python_parser::{Parse};
//! use ruff_python_ast as ast;
//! use ruff_python_parser::parse_suite;
//!
//! let python_source = r#"
//! def is_odd(i):
//! return bool(i & 1)
//! "#;
//! let ast = ast::Suite::parse(python_source, "<embedded>");
//! let ast = parse_suite(python_source, "<embedded>");
//!
//! assert!(ast.is_ok());
//! ```
@ -111,11 +110,11 @@
//! [lexer]: crate::lexer
use crate::lexer::LexResult;
pub use parse::Parse;
pub use parser::{parse, parse_starts_at, parse_tokens, ParseError, ParseErrorType};
#[allow(deprecated)]
pub use parser::{parse_expression, parse_expression_starts_at, parse_program};
use ruff_python_ast::{CmpOp, Expr, ModModule, Ranged, Suite};
pub use parser::{
parse, parse_expression, parse_expression_starts_at, parse_program, parse_starts_at,
parse_suite, parse_tokens, ParseError, ParseErrorType,
};
use ruff_python_ast::{CmpOp, Expr, Mod, Ranged, Suite};
use ruff_text_size::{TextRange, TextSize};
pub use string::FStringErrorType;
pub use token::{StringKind, Tok, TokenKind};
@ -124,7 +123,6 @@ mod function;
// Skip flattening lexer to distinguish from full ruff_python_parser
mod context;
pub mod lexer;
mod parse;
mod parser;
mod soft_keywords;
mod string;
@ -149,7 +147,10 @@ pub fn parse_program_tokens(
lxr: Vec<LexResult>,
source_path: &str,
) -> anyhow::Result<Suite, ParseError> {
ModModule::parse_tokens(lxr, source_path).map(|module| module.body)
match parse_tokens(lxr, Mode::Module, source_path)? {
Mod::Module(m) => Ok(m.body),
Mod::Expression(_) => unreachable!("Mode::Module doesn't return other variant"),
}
}
/// Return the `Range` of the first `Tok::Colon` token in a `Range`.
@ -344,11 +345,10 @@ mod python {
#[cfg(test)]
mod tests {
use crate::Parse;
use crate::{first_colon_range, locate_cmp_ops, LocatedCmpOp};
use crate::{first_colon_range, locate_cmp_ops, parse_expression, LocatedCmpOp};
use anyhow::Result;
use ruff_python_ast::CmpOp;
use ruff_python_ast::Expr;
use ruff_text_size::{TextLen, TextRange, TextSize};
#[test]
@ -366,7 +366,7 @@ mod tests {
#[test]
fn extract_cmp_op_location() -> Result<()> {
let contents = "x == 1";
let expr = Expr::parse(contents, "<filename>")?;
let expr = parse_expression(contents, "<filename>")?;
assert_eq!(
locate_cmp_ops(&expr, contents),
vec![LocatedCmpOp::new(
@ -376,7 +376,7 @@ mod tests {
);
let contents = "x != 1";
let expr = Expr::parse(contents, "<filename>")?;
let expr = parse_expression(contents, "<filename>")?;
assert_eq!(
locate_cmp_ops(&expr, contents),
vec![LocatedCmpOp::new(
@ -386,7 +386,7 @@ mod tests {
);
let contents = "x is 1";
let expr = Expr::parse(contents, "<filename>")?;
let expr = parse_expression(contents, "<filename>")?;
assert_eq!(
locate_cmp_ops(&expr, contents),
vec![LocatedCmpOp::new(
@ -396,7 +396,7 @@ mod tests {
);
let contents = "x is not 1";
let expr = Expr::parse(contents, "<filename>")?;
let expr = parse_expression(contents, "<filename>")?;
assert_eq!(
locate_cmp_ops(&expr, contents),
vec![LocatedCmpOp::new(
@ -406,7 +406,7 @@ mod tests {
);
let contents = "x in 1";
let expr = Expr::parse(contents, "<filename>")?;
let expr = parse_expression(contents, "<filename>")?;
assert_eq!(
locate_cmp_ops(&expr, contents),
vec![LocatedCmpOp::new(
@ -416,7 +416,7 @@ mod tests {
);
let contents = "x not in 1";
let expr = Expr::parse(contents, "<filename>")?;
let expr = parse_expression(contents, "<filename>")?;
assert_eq!(
locate_cmp_ops(&expr, contents),
vec![LocatedCmpOp::new(
@ -426,7 +426,7 @@ mod tests {
);
let contents = "x != (1 is not 2)";
let expr = Expr::parse(contents, "<filename>")?;
let expr = parse_expression(contents, "<filename>")?;
assert_eq!(
locate_cmp_ops(&expr, contents),
vec![LocatedCmpOp::new(

File diff suppressed because it is too large Load diff

View file

@ -18,14 +18,15 @@ use itertools::Itertools;
pub(super) use lalrpop_util::ParseError as LalrpopError;
use ruff_text_size::{TextRange, TextSize};
use crate::lexer::{lex, lex_starts_at};
use crate::{
lexer::{self, LexResult, LexicalError, LexicalErrorType},
python,
token::Tok,
Mode, Parse,
Mode,
};
use ruff_python_ast as ast;
use ruff_python_ast::ModModule;
use ruff_python_ast::{Mod, ModModule, Suite};
/// Parse a full Python program usually consisting of multiple lines.
///
@ -47,9 +48,16 @@ use ruff_python_ast::ModModule;
/// let program = parser::parse_program(source, "<embedded>");
/// assert!(program.is_ok());
/// ```
#[deprecated = "Use ruff_python_ast::Suite::parse from ruff_python_parser::Parse trait."]
pub fn parse_program(source: &str, source_path: &str) -> Result<ast::Suite, ParseError> {
ModModule::parse(source, source_path).map(|module| module.body)
pub fn parse_program(source: &str, source_path: &str) -> Result<ModModule, ParseError> {
let lexer = lex(source, Mode::Module);
match parse_tokens(lexer, Mode::Module, source_path)? {
Mod::Module(m) => Ok(m),
Mod::Expression(_) => unreachable!("Mode::Module doesn't return other variant"),
}
}
pub fn parse_suite(source: &str, source_path: &str) -> Result<Suite, ParseError> {
parse_program(source, source_path).map(|m| m.body)
}
/// Parses a single Python expression.
@ -68,9 +76,12 @@ pub fn parse_program(source: &str, source_path: &str) -> Result<ast::Suite, Pars
/// assert!(expr.is_ok());
///
/// ```
#[deprecated = "Use ruff_python_ast::Expr::parse from ruff_python_parser::Parse trait."]
pub fn parse_expression(source: &str, path: &str) -> Result<ast::Expr, ParseError> {
ast::Expr::parse(source, path)
pub fn parse_expression(source: &str, source_path: &str) -> Result<ast::Expr, ParseError> {
let lexer = lex(source, Mode::Expression);
match parse_tokens(lexer, Mode::Expression, source_path)? {
Mod::Expression(expression) => Ok(*expression.body),
Mod::Module(_m) => unreachable!("Mode::Expression doesn't return other variant"),
}
}
/// Parses a Python expression from a given location.
@ -90,13 +101,16 @@ pub fn parse_expression(source: &str, path: &str) -> Result<ast::Expr, ParseErro
/// let expr = parse_expression_starts_at("1 + 2", "<embedded>", TextSize::from(400));
/// assert!(expr.is_ok());
/// ```
#[deprecated = "Use ruff_python_ast::Expr::parse_starts_at from ruff_python_parser::Parse trait."]
pub fn parse_expression_starts_at(
source: &str,
path: &str,
source_path: &str,
offset: TextSize,
) -> Result<ast::Expr, ParseError> {
ast::Expr::parse_starts_at(source, path, offset)
let lexer = lex_starts_at(source, Mode::Module, offset);
match parse_tokens(lexer, Mode::Expression, source_path)? {
Mod::Expression(expression) => Ok(*expression.body),
Mod::Module(_m) => unreachable!("Mode::Expression doesn't return other variant"),
}
}
/// Parse the given Python source code using the specified [`Mode`].
@ -384,71 +398,69 @@ impl ParseErrorType {
#[cfg(test)]
mod tests {
use crate::Parse;
use insta::assert_debug_snapshot;
use ruff_python_ast as ast;
use super::*;
#[test]
fn test_parse_empty() {
let parse_ast = ast::Suite::parse("", "<test>").unwrap();
let parse_ast = parse_suite("", "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_string() {
let source = "'Hello world'";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_f_string() {
let source = "f'Hello world'";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_print_hello() {
let source = "print('Hello world')";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_print_2() {
let source = "print('Hello world', 2)";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_kwargs() {
let source = "my_func('positional', keyword=2)";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_if_elif_else() {
let source = "if 1: 10\nelif 2: 20\nelse: 30";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_lambda() {
let source = "lambda x, y: x * y"; // lambda(x, y): x * y";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_lambda_no_args() {
let source = "lambda: 1";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
@ -456,7 +468,7 @@ mod tests {
fn test_parse_tuples() {
let source = "a, b = 4, 5";
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
insta::assert_debug_snapshot!(parse_suite(source, "<test>").unwrap());
}
#[test]
@ -468,7 +480,7 @@ class Foo(A, B):
def method_with_default(self, arg='default'):
pass
";
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
insta::assert_debug_snapshot!(parse_suite(source, "<test>").unwrap());
}
#[test]
@ -499,7 +511,7 @@ class Foo[**P](): ...
class Foo[X, Y: str, *U, **P]():
pass
";
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
insta::assert_debug_snapshot!(parse_suite(source, "<test>").unwrap());
}
#[test]
fn test_parse_function_definition() {
@ -525,69 +537,69 @@ def func[**P](*args: P.args, **kwargs: P.kwargs):
def func[T, U: str, *Ts, **P]():
pass
";
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
insta::assert_debug_snapshot!(parse_suite(source, "<test>").unwrap());
}
#[test]
fn test_parse_dict_comprehension() {
let source = "{x1: x2 for y in z}";
let parse_ast = ast::Expr::parse(source, "<test>").unwrap();
let parse_ast = parse_expression(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_list_comprehension() {
let source = "[x for y in z]";
let parse_ast = ast::Expr::parse(source, "<test>").unwrap();
let parse_ast = parse_expression(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_double_list_comprehension() {
let source = "[x for y, y2 in z for a in b if a < 5 if a > 10]";
let parse_ast = ast::Expr::parse(source, "<test>").unwrap();
let parse_ast = parse_expression(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_generator_comprehension() {
let source = "(x for y in z)";
let parse_ast = ast::Expr::parse(source, "<test>").unwrap();
let parse_ast = parse_expression(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_named_expression_generator_comprehension() {
let source = "(x := y + 1 for y in z)";
let parse_ast = ast::Expr::parse(source, "<test>").unwrap();
let parse_ast = parse_expression(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_if_else_generator_comprehension() {
let source = "(x if y else y for y in z)";
let parse_ast = ast::Expr::parse(source, "<test>").unwrap();
let parse_ast = parse_expression(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_bool_op_or() {
let source = "x or y";
let parse_ast = ast::Expr::parse(source, "<test>").unwrap();
let parse_ast = parse_expression(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_bool_op_and() {
let source = "x and y";
let parse_ast = ast::Expr::parse(source, "<test>").unwrap();
let parse_ast = parse_expression(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_slice() {
let source = "x[1:2:3]";
let parse_ast = ast::Expr::parse(source, "<test>").unwrap();
let parse_ast = parse_expression(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
@ -621,7 +633,7 @@ with (0 as a,): pass
with (0 as a, 1 as b): pass
with (0 as a, 1 as b,): pass
";
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
insta::assert_debug_snapshot!(parse_suite(source, "<test>").unwrap());
}
#[test]
@ -644,7 +656,7 @@ with (0 as a, 1 as b,): pass
"with a := 0 as x: pass",
"with (a := 0 as x): pass",
] {
assert!(ast::Suite::parse(source, "<test>").is_err());
assert!(parse_suite(source, "<test>").is_err());
}
}
@ -656,7 +668,7 @@ array[0, *indexes, -1] = array_slice
array[*indexes_to_select, *indexes_to_select]
array[3:5, *indexes_to_select]
";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
@ -669,13 +681,13 @@ array[3:5, *indexes_to_select]
("OFFSET %d" % offset) if offset else None,
)
)"#;
let parse_ast = ast::Expr::parse(source, "<test>").unwrap();
let parse_ast = parse_expression(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_try() {
let parse_ast = ast::Suite::parse(
let parse_ast = parse_suite(
r#"try:
raise ValueError(1)
except TypeError as e:
@ -690,7 +702,7 @@ except OSError as e:
#[test]
fn test_try_star() {
let parse_ast = ast::Suite::parse(
let parse_ast = parse_suite(
r#"try:
raise ExceptionGroup("eg",
[ValueError(1), TypeError(2), OSError(3), OSError(4)])
@ -706,7 +718,7 @@ except* OSError as e:
#[test]
fn test_dict_unpacking() {
let parse_ast = ast::Expr::parse(r#"{"a": "b", **c, "d": "e"}"#, "<test>").unwrap();
let parse_ast = parse_expression(r#"{"a": "b", **c, "d": "e"}"#, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
@ -758,7 +770,7 @@ type X \
type X[T] \
= T
"#;
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
insta::assert_debug_snapshot!(parse_suite(source, "<test>").unwrap());
}
#[test]
@ -795,7 +807,7 @@ type = 1
type = x = 1
x = type = 1
"#;
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
insta::assert_debug_snapshot!(parse_suite(source, "<test>").unwrap());
}
#[test]
@ -820,7 +832,7 @@ x = 10000
x = 133333
"#;
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
insta::assert_debug_snapshot!(parse_suite(source, "<test>").unwrap());
}
#[test]
@ -846,7 +858,7 @@ if 10 .real:
y = 100[no]
y = 100(no)
"#;
assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
assert_debug_snapshot!(parse_suite(source, "<test>").unwrap());
}
#[test]
@ -874,7 +886,7 @@ match match:
match = lambda query: query == event
print(match(12))
"#;
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
insta::assert_debug_snapshot!(parse_suite(source, "<test>").unwrap());
}
#[test]
@ -1044,13 +1056,13 @@ match w := x,:
case y as v,:
z = 0
"#;
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_match() {
let parse_ast = ast::Suite::parse(
let parse_ast = parse_suite(
r#"
match {"test": 1}:
case {
@ -1080,7 +1092,7 @@ match x:
#[test]
fn test_variadic_generics() {
let parse_ast = ast::Suite::parse(
let parse_ast = parse_suite(
r#"
def args_to_tuple(*args: *Ts) -> Tuple[*Ts]: ...
"#,
@ -1090,26 +1102,9 @@ def args_to_tuple(*args: *Ts) -> Tuple[*Ts]: ...
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_constant() {
use num_traits::ToPrimitive;
let c = ast::Constant::parse_without_path("'string'").unwrap();
assert_eq!(c.str().unwrap(), "string");
let c = ast::Constant::parse_without_path("10").unwrap();
assert_eq!(c.int().unwrap().to_i32().unwrap(), 10);
}
#[test]
fn test_parse_identifier() {
let i = ast::Identifier::parse_without_path("test").unwrap();
assert_eq!(i.as_str(), "test");
}
#[test]
fn decorator_ranges() {
let parse_ast = ast::Suite::parse(
let parse_ast = parse_suite(
r#"
@my_decorator
def test():

View file

@ -11,9 +11,9 @@ use ruff_text_size::{TextLen, TextRange, TextSize};
// we have to do the parsing here, manually.
use crate::{
lexer::{LexicalError, LexicalErrorType},
parse_expression_starts_at,
parser::{ParseError, ParseErrorType},
token::{StringKind, Tok},
Parse,
};
// unicode_name2 does not expose `MAX_NAME_LENGTH`, so we replicate that constant here, fix #3798
@ -550,7 +550,7 @@ impl<'a> StringParser<'a> {
fn parse_fstring_expr(source: &str, location: TextSize) -> Result<Expr, ParseError> {
let fstring_body = format!("({source})");
ast::Expr::parse_starts_at(&fstring_body, "<fstring>", location)
parse_expression_starts_at(&fstring_body, "<fstring>", location)
}
fn parse_string(
@ -785,10 +785,8 @@ impl From<FStringError> for crate::parser::LalrpopError<TextSize, Tok, LexicalEr
#[cfg(test)]
mod tests {
use crate::Parse;
use ruff_python_ast as ast;
use super::*;
use crate::parser::parse_suite;
fn parse_fstring(source: &str) -> Result<Vec<Expr>, LexicalError> {
StringParser::new(source, StringKind::FString, false, TextSize::default()).parse()
@ -926,63 +924,63 @@ mod tests {
#[test]
fn test_parse_string_concat() {
let source = "'Hello ' 'world'";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_u_string_concat_1() {
let source = "'Hello ' u'world'";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_u_string_concat_2() {
let source = "u'Hello ' 'world'";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_f_string_concat_1() {
let source = "'Hello ' f'world'";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_f_string_concat_2() {
let source = "'Hello ' f'world'";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_f_string_concat_3() {
let source = "'Hello ' f'world{\"!\"}'";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_u_f_string_concat_1() {
let source = "u'Hello ' f'world'";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_u_f_string_concat_2() {
let source = "u'Hello ' f'world' '!'";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_string_triple_quotes_with_kind() {
let source = "u'''Hello, world!'''";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
@ -990,7 +988,7 @@ mod tests {
fn test_single_quoted_byte() {
// single quote
let source = r##"b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'"##;
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
@ -998,7 +996,7 @@ mod tests {
fn test_double_quoted_byte() {
// double quote
let source = r##"b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff""##;
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
@ -1006,42 +1004,42 @@ mod tests {
fn test_escape_char_in_byte_literal() {
// backslash does not escape
let source = r##"b"omkmok\Xaa""##; // spell-checker:ignore omkmok
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_raw_byte_literal_1() {
let source = r"rb'\x1z'";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_raw_byte_literal_2() {
let source = r"rb'\\'";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_escape_octet() {
let source = r##"b'\43a\4\1234'"##;
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_fstring_escaped_newline() {
let source = r#"f"\n{x}""#;
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_fstring_constant_range() {
let source = r#"f"aaa{bbb}ccc{ddd}eee""#;
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
@ -1049,28 +1047,28 @@ mod tests {
fn test_fstring_unescaped_newline() {
let source = r#"f"""
{x}""""#;
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_fstring_escaped_character() {
let source = r#"f"\\{x}""#;
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_raw_fstring() {
let source = r#"rf"{x}""#;
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_triple_quoted_raw_fstring() {
let source = r#"rf"""{x}""""#;
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
@ -1078,7 +1076,7 @@ mod tests {
fn test_fstring_line_continuation() {
let source = r#"rf"\
{x}""#;
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
@ -1088,7 +1086,7 @@ mod tests {
#[test]
fn $name() {
let source = format!(r#""\N{{{0}}}""#, $alias);
let parse_ast = ast::Suite::parse(&source, "<test>").unwrap();
let parse_ast = parse_suite(&source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
)*

View file

@ -1,4 +1,4 @@
use crate::Parse;
use crate::{parse_expression, parse_expression_starts_at};
use anyhow::Result;
use ruff_python_ast::relocate::relocate_expr;
use ruff_python_ast::str;
@ -33,7 +33,7 @@ pub fn parse_type_annotation(
// isn't the case, e.g., for implicit concatenations, or for annotations that contain
// escaped quotes.
let leading_quote = str::leading_quote(expression).unwrap();
let expr = Expr::parse_starts_at(
let expr = parse_expression_starts_at(
value,
"<filename>",
range.start() + leading_quote.text_len(),
@ -41,7 +41,7 @@ pub fn parse_type_annotation(
Ok((expr, AnnotationKind::Simple))
} else {
// Otherwise, consider this a "complex" annotation.
let mut expr = Expr::parse(value, "<filename>")?;
let mut expr = parse_expression(value, "<filename>")?;
relocate_expr(&mut expr, range);
Ok((expr, AnnotationKind::Complex))
}