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

Method parse

crates/ruff_python_ast/src/script.rs:65–74  ·  view source on GitHub ↗

Given the contents of a Python file, extract the `script` metadata block with leading comment hashes removed and map its offsets to the original Python script. Given the following input string representing the contents of a Python script: ```python #!/usr/bin/env python3 # /// script # requires-python = '>=3.11' # dependencies = [ # 'requests<3', # 'rich', # ] # /// import requests print("

(contents: &str)

Source from the content-addressed store, hash-verified

63 ///
64 /// See: <https://peps.python.org/pep-0723/>
65 pub fn parse(contents: &str) -> Option<Self> {
66 let mut offset = 0;
67 while let Some(index) = FINDER.find(&contents.as_bytes()[offset..]) {
68 match Self::parse_at(contents, offset + index) {
69 Ok(tag) => return Some(tag),
70 Err(end) => offset = end,
71 }
72 }
73 None
74 }
75
76 /// Returns the offset at which to resume searching if the block is invalid. An opening
77 /// marker inside an unclosed comment block would scan the same suffix and fail too.

Callers 2

try_fromMethod · 0.45
deserializeMethod · 0.45

Calls 2

findMethod · 0.45
as_bytesMethod · 0.45

Tested by

no test coverage detected