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:680–798  ·  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

678 ///
679 /// [pattern arguments]: https://docs.python.org/3/reference/compound_stmts.html#grammar-token-python-grammar-pattern_arguments
680 fn parse_match_pattern_class(
681 &mut self,
682 cls: Pattern,
683 start: TextSize,
684 ) -> ast::PatternMatchClass {
685 let arguments_start = self.node_start();
686
687 let cls = match cls {
688 Pattern::MatchAs(ast::PatternMatchAs {
689 pattern: None,
690 name: Some(ident),
691 ..
692 }) => {
693 if ident.is_valid() {
694 Box::new(Expr::Name(ast::ExprName {
695 range: ident.range(),
696 id: ident.id,
697 ctx: ExprContext::Load,
698 node_index: AtomicNodeIndex::NONE,
699 }))
700 } else {
701 Box::new(Expr::Name(ast::ExprName {
702 range: ident.range(),
703 id: Name::empty(),
704 ctx: ExprContext::Invalid,
705 node_index: AtomicNodeIndex::NONE,
706 }))
707 }
708 }
709 Pattern::MatchValue(ast::PatternMatchValue { value, .. })
710 if matches!(&*value, Expr::Attribute(_)) =>
711 {
712 value
713 }
714 pattern => {
715 self.add_error(
716 ParseErrorType::OtherError("Invalid value for a class pattern".to_string()),
717 &pattern,
718 );
719 Box::new(recovery::pattern_to_expr(pattern))
720 }
721 };
722
723 self.bump(TokenKind::Lpar);
724
725 let mut patterns = Patterns::new();
726 let mut keywords = vec![];
727 let mut has_seen_pattern = false;
728 let mut has_seen_keyword_pattern = false;
729
730 self.parse_comma_separated_list(
731 RecoveryContextKind::MatchPatternClassArguments,
732 |parser| {
733 let pattern_start = parser.node_start();
734 let pattern = parser.parse_match_pattern(AllowStarPattern::No);
735
736 if parser.eat(TokenKind::Equal) {
737 has_seen_pattern = false;

Callers 1

Calls 15

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

Tested by

no test coverage detected