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

Method parse_list

crates/ruff_python_parser/src/parser/mod.rs:733–785  ·  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

731 /// Parses a list of elements where each element is parsed using the given
732 /// `parse_element` function.
733 fn parse_list(
734 &mut self,
735 recovery_context_kind: RecoveryContextKind,
736 mut parse_element: impl FnMut(&mut Parser<'src>),
737 ) {
738 let mut progress = ParserProgress::default();
739 let mut unexpected_indents = 0;
740
741 let saved_context = self.recovery_context;
742 self.recovery_context = self
743 .recovery_context
744 .union(RecoveryContext::from_kind(recovery_context_kind));
745
746 loop {
747 progress.assert_progressing(self);
748
749 if 0 < unexpected_indents && self.at(TokenKind::Dedent) {
750 // Ignore this `Dedent` like we ignored the `Indent`, avoiding extra errors from
751 // being imbalanced
752 unexpected_indents -= 1;
753 self.bump(TokenKind::Dedent);
754 } else if recovery_context_kind.is_list_element(self) {
755 parse_element(self);
756 } else if recovery_context_kind.is_regular_list_terminator(self) {
757 break;
758 } else {
759 // Run the error recovery: If the token is recognised as an element or terminator
760 // of an enclosing list, then we try to re-lex in the context of a logical line and
761 // break out of list parsing.
762 if self.is_enclosing_list_element_or_terminator() {
763 self.tokens.re_lex_logical_token();
764 break;
765 }
766
767 self.add_error(
768 recovery_context_kind.create_error(self),
769 self.current_token_range(),
770 );
771
772 if matches!(
773 recovery_context_kind,
774 RecoveryContextKind::ModuleStatements | RecoveryContextKind::BlockStatements
775 ) && self.at(TokenKind::Indent)
776 {
777 // For this invalid `Indent`, ensure the matching `Dedent` gets consumed as well
778 unexpected_indents += 1;
779 }
780 self.bump_any();
781 }
782 }
783
784 self.recovery_context = saved_context;
785 }
786
787 /// Parses a comma separated list of elements into a vector where each element
788 /// 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