From db9d767841f8e5a1ec49275e6731d7c21777c256 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Wed, 18 Oct 2023 18:53:39 +0530 Subject: [PATCH] New `Singleton` enum for `PatternMatchSingleton` node --- crates/ruff_python_ast/src/comparable.rs | 23 ++++++++++++++++--- crates/ruff_python_ast/src/node.rs | 2 +- crates/ruff_python_ast/src/nodes.rs | 19 ++++++++++++++- .../ruff_python_ast/src/visitor/preorder.rs | 7 ++++-- crates/ruff_python_ast/tests/preorder.rs | 6 ++++- crates/ruff_python_codegen/src/generator.rs | 14 ++++++++--- .../src/pattern/pattern_match_singleton.rs | 9 ++++---- crates/ruff_python_parser/src/python.lalrpop | 2 +- crates/ruff_python_parser/src/python.rs | 4 ++-- ...f_python_parser__parser__tests__patma.snap | 12 +++------- 10 files changed, 70 insertions(+), 28 deletions(-) diff --git a/crates/ruff_python_ast/src/comparable.rs b/crates/ruff_python_ast/src/comparable.rs index bcf097924f8ac9..295ab5b576d936 100644 --- a/crates/ruff_python_ast/src/comparable.rs +++ b/crates/ruff_python_ast/src/comparable.rs @@ -185,8 +185,8 @@ pub struct PatternMatchValue<'a> { } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct PatternMatchSingleton<'a> { - value: ComparableConstant<'a>, +pub struct PatternMatchSingleton { + value: ComparableSingleton, } #[derive(Debug, PartialEq, Eq, Hash)] @@ -227,7 +227,7 @@ pub struct PatternMatchOr<'a> { #[derive(Debug, PartialEq, Eq, Hash)] pub enum ComparablePattern<'a> { MatchValue(PatternMatchValue<'a>), - MatchSingleton(PatternMatchSingleton<'a>), + MatchSingleton(PatternMatchSingleton), MatchSequence(PatternMatchSequence<'a>), MatchMapping(PatternMatchMapping<'a>), MatchClass(PatternMatchClass<'a>), @@ -326,6 +326,23 @@ impl<'a> From<&'a ast::Decorator> for ComparableDecorator<'a> { } } +#[derive(Debug, PartialEq, Eq, Hash)] +pub enum ComparableSingleton { + None, + True, + False, +} + +impl From<&ast::Singleton> for ComparableSingleton { + fn from(singleton: &ast::Singleton) -> Self { + match singleton { + ast::Singleton::None => Self::None, + ast::Singleton::True => Self::True, + ast::Singleton::False => Self::False, + } + } +} + #[derive(Debug, PartialEq, Eq, Hash)] pub enum ComparableConstant<'a> { None, diff --git a/crates/ruff_python_ast/src/node.rs b/crates/ruff_python_ast/src/node.rs index 17725fe0d65ae1..4a13ea63d63e59 100644 --- a/crates/ruff_python_ast/src/node.rs +++ b/crates/ruff_python_ast/src/node.rs @@ -3139,7 +3139,7 @@ impl AstNode for ast::PatternMatchSingleton { V: PreorderVisitor<'a> + ?Sized, { let ast::PatternMatchSingleton { value, range: _ } = self; - visitor.visit_constant(value); + visitor.visit_singleton(value); } } impl AstNode for ast::PatternMatchSequence { diff --git a/crates/ruff_python_ast/src/nodes.rs b/crates/ruff_python_ast/src/nodes.rs index 172ba5a93b90f6..bb7d9dcd7c6675 100644 --- a/crates/ruff_python_ast/src/nodes.rs +++ b/crates/ruff_python_ast/src/nodes.rs @@ -1923,7 +1923,7 @@ impl From for Pattern { #[derive(Clone, Debug, PartialEq)] pub struct PatternMatchSingleton { pub range: TextRange, - pub value: Constant, + pub value: Singleton, } impl From for Pattern { @@ -2578,6 +2578,23 @@ impl Ranged for Identifier { } } +#[derive(Clone, Debug, PartialEq)] +pub enum Singleton { + None, + True, + False, +} + +impl From for Singleton { + fn from(value: bool) -> Self { + if value { + Singleton::True + } else { + Singleton::False + } + } +} + #[derive(Clone, Debug, PartialEq, is_macro::Is)] pub enum Constant { None, diff --git a/crates/ruff_python_ast/src/visitor/preorder.rs b/crates/ruff_python_ast/src/visitor/preorder.rs index 9f69e219ed376d..94d957c61736ef 100644 --- a/crates/ruff_python_ast/src/visitor/preorder.rs +++ b/crates/ruff_python_ast/src/visitor/preorder.rs @@ -1,8 +1,8 @@ use crate::{ Alias, Arguments, BoolOp, CmpOp, Comprehension, Constant, Decorator, ElifElseClause, ExceptHandler, Expr, Keyword, MatchCase, Mod, Operator, Parameter, ParameterWithDefault, - Parameters, Pattern, PatternArguments, PatternKeyword, Stmt, TypeParam, TypeParams, UnaryOp, - WithItem, + Parameters, Pattern, PatternArguments, PatternKeyword, Singleton, Stmt, TypeParam, TypeParams, + UnaryOp, WithItem, }; use crate::{AnyNodeRef, AstNode}; @@ -44,6 +44,9 @@ pub trait PreorderVisitor<'a> { #[inline] fn visit_constant(&mut self, _constant: &'a Constant) {} + #[inline] + fn visit_singleton(&mut self, _singleton: &'a Singleton) {} + #[inline] fn visit_bool_op(&mut self, bool_op: &'a BoolOp) { walk_bool_op(self, bool_op); diff --git a/crates/ruff_python_ast/tests/preorder.rs b/crates/ruff_python_ast/tests/preorder.rs index 0f41fa1e279942..9017b016821933 100644 --- a/crates/ruff_python_ast/tests/preorder.rs +++ b/crates/ruff_python_ast/tests/preorder.rs @@ -10,7 +10,7 @@ use ruff_python_ast::visitor::preorder::{ use ruff_python_ast::AnyNodeRef; use ruff_python_ast::{ Alias, BoolOp, CmpOp, Comprehension, Constant, ExceptHandler, Expr, Keyword, MatchCase, Mod, - Operator, Parameter, Parameters, Pattern, Stmt, TypeParam, UnaryOp, WithItem, + Operator, Parameter, Parameters, Pattern, Singleton, Stmt, TypeParam, UnaryOp, WithItem, }; use ruff_python_parser::lexer::lex; use ruff_python_parser::{parse_tokens, Mode}; @@ -197,6 +197,10 @@ impl PreorderVisitor<'_> for RecordVisitor { self.emit(&constant); } + fn visit_singleton(&mut self, singleton: &Singleton) { + self.emit(&singleton); + } + fn visit_bool_op(&mut self, bool_op: &BoolOp) { self.emit(&bool_op); } diff --git a/crates/ruff_python_codegen/src/generator.rs b/crates/ruff_python_codegen/src/generator.rs index 0bd53c94db3eeb..29db51c5d76884 100644 --- a/crates/ruff_python_codegen/src/generator.rs +++ b/crates/ruff_python_codegen/src/generator.rs @@ -5,8 +5,8 @@ use std::ops::Deref; use ruff_python_ast::{ self as ast, Alias, ArgOrKeyword, BoolOp, CmpOp, Comprehension, Constant, ConversionFlag, DebugText, ExceptHandler, Expr, Identifier, MatchCase, Operator, Parameter, Parameters, - Pattern, Stmt, Suite, TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple, - WithItem, + Pattern, Singleton, Stmt, Suite, TypeParam, TypeParamParamSpec, TypeParamTypeVar, + TypeParamTypeVarTuple, WithItem, }; use ruff_python_ast::{ParameterWithDefault, TypeParams}; use ruff_python_literal::escape::{AsciiEscape, Escape, UnicodeEscape}; @@ -672,7 +672,7 @@ impl<'a> Generator<'a> { self.unparse_expr(value, precedence::MAX); } Pattern::MatchSingleton(ast::PatternMatchSingleton { value, range: _ }) => { - self.unparse_constant(value); + self.unparse_singleton(value); } Pattern::MatchSequence(ast::PatternMatchSequence { patterns, range: _ }) => { self.p("["); @@ -1166,6 +1166,14 @@ impl<'a> Generator<'a> { } } + pub(crate) fn unparse_singleton(&mut self, singleton: &Singleton) { + match singleton { + Singleton::None => self.p("None"), + Singleton::True => self.p("True"), + Singleton::False => self.p("False"), + } + } + pub(crate) fn unparse_constant(&mut self, constant: &Constant) { assert_eq!(f64::MAX_10_EXP, 308); let inf_str = "1e309"; diff --git a/crates/ruff_python_formatter/src/pattern/pattern_match_singleton.rs b/crates/ruff_python_formatter/src/pattern/pattern_match_singleton.rs index 0fa9a8af6800a0..649b0ec79f040a 100644 --- a/crates/ruff_python_formatter/src/pattern/pattern_match_singleton.rs +++ b/crates/ruff_python_formatter/src/pattern/pattern_match_singleton.rs @@ -1,5 +1,5 @@ use ruff_python_ast::AnyNodeRef; -use ruff_python_ast::{Constant, PatternMatchSingleton}; +use ruff_python_ast::{PatternMatchSingleton, Singleton}; use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses}; use crate::prelude::*; @@ -10,10 +10,9 @@ pub struct FormatPatternMatchSingleton; impl FormatNodeRule for FormatPatternMatchSingleton { fn fmt_fields(&self, item: &PatternMatchSingleton, f: &mut PyFormatter) -> FormatResult<()> { match item.value { - Constant::None => token("None").fmt(f), - Constant::Bool(true) => token("True").fmt(f), - Constant::Bool(false) => token("False").fmt(f), - _ => unreachable!(), + Singleton::None => token("None").fmt(f), + Singleton::True => token("True").fmt(f), + Singleton::False => token("False").fmt(f), } } } diff --git a/crates/ruff_python_parser/src/python.lalrpop b/crates/ruff_python_parser/src/python.lalrpop index efd05d7072836d..8d3aacdfd80cda 100644 --- a/crates/ruff_python_parser/src/python.lalrpop +++ b/crates/ruff_python_parser/src/python.lalrpop @@ -649,7 +649,7 @@ AddOpExpr: ast::ParenthesizedExpr = { LiteralPattern: ast::Pattern = { "None" => ast::PatternMatchSingleton { - value: ast::Constant::None, + value: ast::Singleton::None, range: (location..end_location).into() }.into(), "True" => ast::PatternMatchSingleton { diff --git a/crates/ruff_python_parser/src/python.rs b/crates/ruff_python_parser/src/python.rs index fab9983acd411e..db1de2e1b353db 100644 --- a/crates/ruff_python_parser/src/python.rs +++ b/crates/ruff_python_parser/src/python.rs @@ -1,5 +1,5 @@ // auto-generated: "lalrpop 0.20.0" -// sha3: 4615d1de345ecffb47457b6c6e5acd3859c2cc0dffa0187ff99d65d0ce1c6b80 +// sha3: 659bb984f00c336cf30c371b9f53bf4487db048c52419c0000f09a9c049551d4 use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; use ruff_python_ast::{self as ast, Int, IpyEscapeKind}; use crate::{ @@ -34336,7 +34336,7 @@ fn __action115< ) -> ast::Pattern { ast::PatternMatchSingleton { - value: ast::Constant::None, + value: ast::Singleton::None, range: (location..end_location).into() }.into() } diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__patma.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__patma.snap index 622287a6becde4..d88e875fd88b65 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__patma.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__patma.snap @@ -719,9 +719,7 @@ expression: parse_ast MatchSingleton( PatternMatchSingleton { range: 621..625, - value: Bool( - true, - ), + value: True, }, ), ], @@ -2402,9 +2400,7 @@ expression: parse_ast pattern: MatchSingleton( PatternMatchSingleton { range: 1947..1952, - value: Bool( - false, - ), + value: False, }, ), guard: None, @@ -3051,9 +3047,7 @@ expression: parse_ast MatchSingleton( PatternMatchSingleton { range: 2405..2410, - value: Bool( - false, - ), + value: False, }, ), ],