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

Function parse_expression_range

crates/ruff_python_parser/src/lib.rs:159–169  ·  view source on GitHub ↗

Parses a Python expression for the given range in the source. This function allows to specify the range of the expression in the source code, other than that, it behaves exactly like [`parse_expression`]. # Example Parsing one of the numeric literal which is part of an addition expression: ``` use ruff_python_parser::parse_expression_range; # use ruff_text_size::{TextRange, TextSize}; let par

(
    source: &str,
    range: TextRange,
)

Source from the content-addressed store, hash-verified

157/// assert!(parsed.is_ok());
158/// ```
159pub fn parse_expression_range(
160 source: &str,
161 range: TextRange,
162) -> Result<Parsed<ModExpression>, ParseError> {
163 let source = &source[..range.end().to_usize()];
164 Parser::new_starts_at(source, range.start(), ParseOptions::from(Mode::Expression))
165 .parse()
166 .try_into_expression()
167 .unwrap()
168 .into_result()
169}
170
171/// Parses a Python expression as if it is parenthesized.
172///

Callers 1

parse_string_annotationFunction · 0.85

Calls 6

to_usizeMethod · 0.80
into_resultMethod · 0.80
try_into_expressionMethod · 0.80
endMethod · 0.45
parseMethod · 0.45
startMethod · 0.45

Tested by

no test coverage detected