MCPcopy Create free account
hub / github.com/astral-sh/ruff / parse_match_pattern_class

Method parse_match_pattern_class

crates/ruff_python_parser/src/parser/pattern.rs:650–768  ·  view source on GitHub ↗

Parses the [pattern arguments] in a class pattern. # Panics If the parser isn't positioned at a `(` token. See: [pattern arguments]: https://docs.python.org/3/reference/compound_stmts.html#grammar-token-python-grammar-pattern_arguments

(
        &mut self,
        cls: Pattern,
        start: TextSize,
    )

Source from the content-addressed store, hash-verified

648 ///
649 /// [pattern arguments]: https://docs.python.org/3/reference/compound_stmts.html#grammar-token-python-grammar-pattern_arguments
650 fn parse_match_pattern_class(
651 &mut self,
652 cls: Pattern,
653 start: TextSize,
654 ) -> ast::PatternMatchClass {
655 let arguments_start = self.node_start();
656
657 let cls = match cls {
658 Pattern::MatchAs(ast::PatternMatchAs {
659 pattern: None,
660 name: Some(ident),
661 ..
662 }) => {
663 if ident.is_valid() {
664 Box::new(Expr::Name(ast::ExprName {
665 range: ident.range(),
666 id: ident.id,
667 ctx: ExprContext::Load,
668 node_index: AtomicNodeIndex::NONE,
669 }))
670 } else {
671 Box::new(Expr::Name(ast::ExprName {
672 range: ident.range(),
673 id: Name::empty(),
674 ctx: ExprContext::Invalid,
675 node_index: AtomicNodeIndex::NONE,
676 }))
677 }
678 }
679 Pattern::MatchValue(ast::PatternMatchValue { value, .. })
680 if matches!(&*value, Expr::Attribute(_)) =>
681 {
682 value
683 }
684 pattern => {
685 self.add_error(
686 ParseErrorType::OtherError("Invalid value for a class pattern".to_string()),
687 &pattern,
688 );
689 Box::new(recovery::pattern_to_expr(pattern))
690 }
691 };
692
693 self.bump(TokenKind::Lpar);
694
695 let mut patterns = Patterns::new();
696 let mut keywords = vec![];
697 let mut has_seen_pattern = false;
698 let mut has_seen_keyword_pattern = false;
699
700 self.parse_comma_separated_list(
701 RecoveryContextKind::MatchPatternClassArguments,
702 |parser| {
703 let pattern_start = parser.node_start();
704 let pattern = parser.parse_match_pattern(AllowStarPattern::No);
705
706 if parser.eat(TokenKind::Equal) {
707 has_seen_pattern = false;

Callers 1

Calls 15

NameClass · 0.85
pattern_to_exprFunction · 0.85
node_startMethod · 0.80
is_validMethod · 0.80
parse_match_patternMethod · 0.80
missing_node_rangeMethod · 0.80
node_rangeMethod · 0.80
expectMethod · 0.80
emptyFunction · 0.50
rangeMethod · 0.45
add_errorMethod · 0.45

Tested by

no test coverage detected