plissken-core::parser::python
Rust
Python source code parser using tree-sitter
Structs
Section titled “Structs”struct PythonParser
Section titled “struct PythonParser”pub
Methods
Section titled “Methods”pub
fn new() -> SelfSource
pub fn new() -> Self { let mut parser = Parser::new(); parser .set_language(&tree_sitter_python::LANGUAGE.into()) .expect("Failed to load Python grammar"); Self { parser } }parse_file
Section titled “parse_file”pub
fn parse_file( &mut self, path: &Path,) -> crate::error::Result<PythonModule>Parse a Python source file.
Errors:
Returns PlisskenError::FileRead if the file cannot be read,
PlisskenError::Parse if the Python syntax is invalid.
Source
pub fn parse_file(&mut self, path: &Path) -> crate::error::Result<PythonModule> { use crate::error::PlisskenError;
let content = std::fs::read_to_string(path).map_err(|e| PlisskenError::file_read(path, e))?; self.parse_str(&content, path) }parse_str
Section titled “parse_str”pub
fn parse_str( &mut self, content: &str, path: &Path,) -> crate::error::Result<PythonModule>Parse Python source from a string.
Errors:
Returns PlisskenError::Parse if the Python syntax is invalid.
Source
pub fn parse_str(&mut self, content: &str, path: &Path) -> crate::error::Result<PythonModule> { use crate::error::PlisskenError;
let tree = self .parser .parse(content, None) .ok_or_else(|| PlisskenError::python_parse(path, "failed to parse Python source"))?;
let root = tree.root_node();
// Extract module docstring let docstring = extract_module_docstring(&root, content); // Parse docstring into structured form let parsed_doc = docstring.as_ref().map(|d| parse_docstring(d));
// Extract items let items = extract_module_items(&root, content, path);
Ok(PythonModule { path: path.display().to_string(), docstring, parsed_doc, items, source_type: SourceType::Python, source: SourceSpan::new( path.to_path_buf(), 1, content.lines().count().max(1), content, ), }) }