Summary -- This PR detects the relaxed grammar for decorators proposed in [PEP 614](https://peps.python.org/pep-0614/) on Python 3.8 and lower. The 3.8 grammar for decorators is [here](https://docs.python.org/3.8/reference/compound_stmts.html#grammar-token-decorators): ``` decorators ::= decorator+ decorator ::= "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE dotted_name ::= identifier ("." identifier)* ``` in contrast to the current grammar [here](https://docs.python.org/3/reference/compound_stmts.html#grammar-token-python-grammar-decorators) ``` decorators ::= decorator+ decorator ::= "@" assignment_expression NEWLINE assignment_expression ::= [identifier ":="] expression ``` Test Plan -- New inline parser tests.
58 lines
2.2 KiB
Rust
58 lines
2.2 KiB
Rust
use ruff_python_ast::{self as ast, CmpOp, Expr, ExprContext};
|
|
|
|
use crate::TokenKind;
|
|
|
|
/// Set the `ctx` for `Expr::Id`, `Expr::Attribute`, `Expr::Subscript`, `Expr::Starred`,
|
|
/// `Expr::Tuple` and `Expr::List`. If `expr` is either `Expr::Tuple` or `Expr::List`,
|
|
/// recursively sets the `ctx` for their elements.
|
|
pub(super) fn set_expr_ctx(expr: &mut Expr, new_ctx: ExprContext) {
|
|
match expr {
|
|
Expr::Name(ast::ExprName { ctx, .. })
|
|
| Expr::Attribute(ast::ExprAttribute { ctx, .. })
|
|
| Expr::Subscript(ast::ExprSubscript { ctx, .. }) => *ctx = new_ctx,
|
|
Expr::Starred(ast::ExprStarred { value, ctx, .. }) => {
|
|
*ctx = new_ctx;
|
|
set_expr_ctx(value, new_ctx);
|
|
}
|
|
Expr::UnaryOp(ast::ExprUnaryOp { operand, .. }) => {
|
|
set_expr_ctx(operand, new_ctx);
|
|
}
|
|
Expr::List(ast::ExprList { elts, ctx, .. })
|
|
| Expr::Tuple(ast::ExprTuple { elts, ctx, .. }) => {
|
|
*ctx = new_ctx;
|
|
elts.iter_mut()
|
|
.for_each(|element| set_expr_ctx(element, new_ctx));
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
/// Converts a [`TokenKind`] array of size 2 to its correspondent [`CmpOp`].
|
|
pub(super) const fn token_kind_to_cmp_op(tokens: [TokenKind; 2]) -> Option<CmpOp> {
|
|
Some(match tokens {
|
|
[TokenKind::Is, TokenKind::Not] => CmpOp::IsNot,
|
|
[TokenKind::Is, _] => CmpOp::Is,
|
|
[TokenKind::Not, TokenKind::In] => CmpOp::NotIn,
|
|
[TokenKind::In, _] => CmpOp::In,
|
|
[TokenKind::EqEqual, _] => CmpOp::Eq,
|
|
[TokenKind::NotEqual, _] => CmpOp::NotEq,
|
|
[TokenKind::Less, _] => CmpOp::Lt,
|
|
[TokenKind::LessEqual, _] => CmpOp::LtE,
|
|
[TokenKind::Greater, _] => CmpOp::Gt,
|
|
[TokenKind::GreaterEqual, _] => CmpOp::GtE,
|
|
_ => return None,
|
|
})
|
|
}
|
|
|
|
/// Helper for `parse_decorators` to determine if `expr` is a [`dotted_name`] from the decorator
|
|
/// grammar before Python 3.9.
|
|
///
|
|
/// [`dotted_name`]: https://docs.python.org/3.8/reference/compound_stmts.html#grammar-token-dotted-name
|
|
pub(super) fn is_name_or_attribute_expression(expr: &Expr) -> bool {
|
|
match expr {
|
|
Expr::Attribute(attr) => is_name_or_attribute_expression(&attr.value),
|
|
Expr::Name(_) => true,
|
|
_ => false,
|
|
}
|
|
}
|