Skip to content

Commit

Permalink
New Singleton enum for PatternMatchSingleton node
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Oct 19, 2023
1 parent 71c6d5a commit 2b94722
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 26 deletions.
23 changes: 20 additions & 3 deletions crates/ruff_python_ast/src/comparable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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>),
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_ast/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 18 additions & 1 deletion crates/ruff_python_ast/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1923,7 +1923,7 @@ impl From<PatternMatchValue> for Pattern {
#[derive(Clone, Debug, PartialEq)]
pub struct PatternMatchSingleton {
pub range: TextRange,
pub value: Constant,
pub value: Singleton,
}

impl From<PatternMatchSingleton> for Pattern {
Expand Down Expand Up @@ -2578,6 +2578,23 @@ impl Ranged for Identifier {
}
}

#[derive(Clone, Debug, PartialEq)]
pub enum Singleton {
None,
True,
False,
}

impl From<bool> 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,
Expand Down
7 changes: 5 additions & 2 deletions crates/ruff_python_ast/src/visitor/preorder.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion crates/ruff_python_ast/tests/preorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 11 additions & 1 deletion crates/ruff_python_codegen/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::ops::Deref;

use ast::Singleton;
use ruff_python_ast::{
self as ast, Alias, ArgOrKeyword, BoolOp, CmpOp, Comprehension, Constant, ConversionFlag,
DebugText, ExceptHandler, Expr, Identifier, MatchCase, Operator, Parameter, Parameters,
Expand Down Expand Up @@ -672,7 +673,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("[");
Expand Down Expand Up @@ -1166,6 +1167,15 @@ 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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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::*;
Expand All @@ -10,10 +10,9 @@ pub struct FormatPatternMatchSingleton;
impl FormatNodeRule<PatternMatchSingleton> 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),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_parser/src/python.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ AddOpExpr: ast::ParenthesizedExpr = {

LiteralPattern: ast::Pattern = {
<location:@L> "None" <end_location:@R> => ast::PatternMatchSingleton {
value: ast::Constant::None,
value: ast::Singleton::None,
range: (location..end_location).into()
}.into(),
<location:@L> "True" <end_location:@R> => ast::PatternMatchSingleton {
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_python_parser/src/python.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -34336,7 +34336,7 @@ fn __action115<
) -> ast::Pattern
{
ast::PatternMatchSingleton {
value: ast::Constant::None,
value: ast::Singleton::None,
range: (location..end_location).into()
}.into()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -719,9 +719,7 @@ expression: parse_ast
MatchSingleton(
PatternMatchSingleton {
range: 621..625,
value: Bool(
true,
),
value: True,
},
),
],
Expand Down Expand Up @@ -2402,9 +2400,7 @@ expression: parse_ast
pattern: MatchSingleton(
PatternMatchSingleton {
range: 1947..1952,
value: Bool(
false,
),
value: False,
},
),
guard: None,
Expand Down Expand Up @@ -3051,9 +3047,7 @@ expression: parse_ast
MatchSingleton(
PatternMatchSingleton {
range: 2405..2410,
value: Bool(
false,
),
value: False,
},
),
],
Expand Down

0 comments on commit 2b94722

Please sign in to comment.