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:668–786  ·  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

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