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

Method parse_list

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

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