Skip to content

Commit 6271fba

Browse files
authored
[ruff] Auto generate ast Pattern nodes (#21024)
1 parent a51a0f1 commit 6271fba

File tree

54 files changed

+928
-890
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+928
-890
lines changed

crates/ruff_python_ast/ast.toml

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -559,15 +559,48 @@ InterpolatedStringLiteralElement = { variant = "Literal" }
559559
[Pattern]
560560
doc = "See also [pattern](https://docs.python.org/3/library/ast.html#ast.pattern)"
561561

562-
[Pattern.nodes]
563-
PatternMatchValue = {}
564-
PatternMatchSingleton = {}
565-
PatternMatchSequence = {}
566-
PatternMatchMapping = {}
567-
PatternMatchClass = {}
568-
PatternMatchStar = {}
569-
PatternMatchAs = {}
570-
PatternMatchOr = {}
562+
[Pattern.nodes.PatternMatchValue]
563+
doc = "See also [MatchValue](https://docs.python.org/3/library/ast.html#ast.MatchValue)"
564+
fields = [{ name = "value", type = "Box<Expr>" }]
565+
566+
[Pattern.nodes.PatternMatchSingleton]
567+
doc = "See also [MatchSingleton](https://docs.python.org/3/library/ast.html#ast.MatchSingleton)"
568+
fields = [{ name = "value", type = "Singleton" }]
569+
570+
[Pattern.nodes.PatternMatchSequence]
571+
doc = "See also [MatchSequence](https://docs.python.org/3/library/ast.html#ast.MatchSequence)"
572+
fields = [{ name = "patterns", type = "Pattern*" }]
573+
574+
[Pattern.nodes.PatternMatchMapping]
575+
doc = "See also [MatchMapping](https://docs.python.org/3/library/ast.html#ast.MatchMapping)"
576+
fields = [
577+
{ name = "keys", type = "Expr*" },
578+
{ name = "patterns", type = "Pattern*" },
579+
{ name = "rest", type = "Identifier?" },
580+
]
581+
custom_source_order = true
582+
583+
[Pattern.nodes.PatternMatchClass]
584+
doc = "See also [MatchClass](https://docs.python.org/3/library/ast.html#ast.MatchClass)"
585+
fields = [
586+
{ name = "cls", type = "Box<Expr>" },
587+
{ name = "arguments", type = "PatternArguments" },
588+
]
589+
590+
[Pattern.nodes.PatternMatchStar]
591+
doc = "See also [MatchStar](https://docs.python.org/3/library/ast.html#ast.MatchStar)"
592+
fields = [{ name = "name", type = "Identifier?" }]
593+
594+
[Pattern.nodes.PatternMatchAs]
595+
doc = "See also [MatchAs](https://docs.python.org/3/library/ast.html#ast.MatchAs)"
596+
fields = [
597+
{ name = "pattern", type = "Box<Pattern>?" },
598+
{ name = "name", type = "Identifier?" },
599+
]
600+
601+
[Pattern.nodes.PatternMatchOr]
602+
doc = "See also [MatchOr](https://docs.python.org/3/library/ast.html#ast.MatchOr)"
603+
fields = [{ name = "patterns", type = "Pattern*" }]
571604

572605
[TypeParam]
573606
doc = "See also [type_param](https://docs.python.org/3/library/ast.html#ast.type_param)"

crates/ruff_python_ast/generate.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
"WithItem",
3939
"MatchCase",
4040
"Alias",
41+
"Singleton",
42+
"PatternArguments",
4143
}
4244

4345

crates/ruff_python_ast/src/generated.rs

Lines changed: 193 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ruff_python_ast/src/node.rs

Lines changed: 0 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -235,50 +235,6 @@ impl ast::ExceptHandlerExceptHandler {
235235
}
236236
}
237237

238-
impl ast::PatternMatchValue {
239-
pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V)
240-
where
241-
V: SourceOrderVisitor<'a> + ?Sized,
242-
{
243-
let ast::PatternMatchValue {
244-
value,
245-
range: _,
246-
node_index: _,
247-
} = self;
248-
visitor.visit_expr(value);
249-
}
250-
}
251-
252-
impl ast::PatternMatchSingleton {
253-
pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V)
254-
where
255-
V: SourceOrderVisitor<'a> + ?Sized,
256-
{
257-
let ast::PatternMatchSingleton {
258-
value,
259-
range: _,
260-
node_index: _,
261-
} = self;
262-
visitor.visit_singleton(value);
263-
}
264-
}
265-
266-
impl ast::PatternMatchSequence {
267-
pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V)
268-
where
269-
V: SourceOrderVisitor<'a> + ?Sized,
270-
{
271-
let ast::PatternMatchSequence {
272-
patterns,
273-
range: _,
274-
node_index: _,
275-
} = self;
276-
for pattern in patterns {
277-
visitor.visit_pattern(pattern);
278-
}
279-
}
280-
}
281-
282238
impl ast::PatternMatchMapping {
283239
pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V)
284240
where
@@ -311,76 +267,6 @@ impl ast::PatternMatchMapping {
311267
}
312268
}
313269

314-
impl ast::PatternMatchClass {
315-
pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V)
316-
where
317-
V: SourceOrderVisitor<'a> + ?Sized,
318-
{
319-
let ast::PatternMatchClass {
320-
cls,
321-
arguments: parameters,
322-
range: _,
323-
node_index: _,
324-
} = self;
325-
visitor.visit_expr(cls);
326-
visitor.visit_pattern_arguments(parameters);
327-
}
328-
}
329-
330-
impl ast::PatternMatchStar {
331-
pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V)
332-
where
333-
V: SourceOrderVisitor<'a> + ?Sized,
334-
{
335-
let ast::PatternMatchStar {
336-
range: _,
337-
node_index: _,
338-
name,
339-
} = self;
340-
341-
if let Some(name) = name {
342-
visitor.visit_identifier(name);
343-
}
344-
}
345-
}
346-
347-
impl ast::PatternMatchAs {
348-
pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V)
349-
where
350-
V: SourceOrderVisitor<'a> + ?Sized,
351-
{
352-
let ast::PatternMatchAs {
353-
pattern,
354-
range: _,
355-
node_index: _,
356-
name,
357-
} = self;
358-
if let Some(pattern) = pattern {
359-
visitor.visit_pattern(pattern);
360-
}
361-
362-
if let Some(name) = name {
363-
visitor.visit_identifier(name);
364-
}
365-
}
366-
}
367-
368-
impl ast::PatternMatchOr {
369-
pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V)
370-
where
371-
V: SourceOrderVisitor<'a> + ?Sized,
372-
{
373-
let ast::PatternMatchOr {
374-
patterns,
375-
range: _,
376-
node_index: _,
377-
} = self;
378-
for pattern in patterns {
379-
visitor.visit_pattern(pattern);
380-
}
381-
}
382-
}
383-
384270
impl ast::PatternArguments {
385271
pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V)
386272
where

0 commit comments

Comments
 (0)