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

Method parse_list

crates/ruff_python_parser/src/parser/mod.rs:699–751  ·  view source on GitHub ↗

Parses a list of elements where each element is parsed using the given `parse_element` function.

(
        &mut self,
        recovery_context_kind: RecoveryContextKind,
        mut parse_element: impl FnMut(&mut Parser<'src>),
    )

Source from the content-addressed store, hash-verified

697 /// Parses a list of elements where each element is parsed using the given
698 /// `parse_element` function.
699 fn parse_list(
700 &mut self,
701 recovery_context_kind: RecoveryContextKind,
702 mut parse_element: impl FnMut(&mut Parser<'src>),
703 ) {
704 let mut progress = ParserProgress::default();
705 let mut unexpected_indents = 0;
706
707 let saved_context = self.recovery_context;
708 self.recovery_context = self
709 .recovery_context
710 .union(RecoveryContext::from_kind(recovery_context_kind));
711
712 loop {
713 progress.assert_progressing(self);
714
715 if 0 < unexpected_indents && self.at(TokenKind::Dedent) {
716 // Ignore this `Dedent` like we ignored the `Indent`, avoiding extra errors from
717 // being imbalanced
718 unexpected_indents -= 1;
719 self.bump(TokenKind::Dedent);
720 } else if recovery_context_kind.is_list_element(self) {
721 parse_element(self);
722 } else if recovery_context_kind.is_regular_list_terminator(self) {
723 break;
724 } else {
725 // Run the error recovery: If the token is recognised as an element or terminator
726 // of an enclosing list, then we try to re-lex in the context of a logical line and
727 // break out of list parsing.
728 if self.is_enclosing_list_element_or_terminator() {
729 self.tokens.re_lex_logical_token();
730 break;
731 }
732
733 self.add_error(
734 recovery_context_kind.create_error(self),
735 self.current_token_range(),
736 );
737
738 if matches!(
739 recovery_context_kind,
740 RecoveryContextKind::ModuleStatements | RecoveryContextKind::BlockStatements
741 ) && self.at(TokenKind::Indent)
742 {
743 // For this invalid `Indent`, ensure the matching `Dedent` gets consumed as well
744 unexpected_indents += 1;
745 }
746 self.bump_any();
747 }
748 }
749
750 self.recovery_context = saved_context;
751 }
752
753 /// Parses a comma separated list of elements into a vector where each element
754 /// is parsed using the given `parse_element` function.

Callers 4

parse_blockMethod · 0.80

Calls 13

assert_progressingMethod · 0.80
is_list_elementMethod · 0.80
create_errorMethod · 0.80
current_token_rangeMethod · 0.80
bump_anyMethod · 0.80
defaultFunction · 0.50
unionMethod · 0.45
atMethod · 0.45
bumpMethod · 0.45
re_lex_logical_tokenMethod · 0.45

Tested by

no test coverage detected