#![allow(clippy::useless_conversion)]
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyString};
use std::sync::atomic::{AtomicU64, Ordering};
use tree_sitter::Parser;
use crate::calls;
use crate::calls::CallEdge;
use crate::cfg;
use crate::cfg::{BasicBlock, CfgEdge, FunctionCfg};
use crate::classify::{self as classify_mod, BinaryKind, ClassifyLimits, FileClass, TextEncoding};
use crate::fingerprint;
use crate::fingerprint::Fingerprint;
use crate::flat::{self, FlatTree};
use crate::imports;
#[cfg(feature = "lang-markdown")]
use crate::markdown::{self, FrontMatter, FrontMatterKind, Section};
use crate::path_ignore;
use crate::regions;
use crate::regions::IgnoredRegion;
use crate::registry;
use crate::registry::LanguageInfo;
use crate::suppressions as suppressions_mod;
use crate::suppressions::Suppression;
use crate::tsquery::{self, Capture};
fn parse(language_key: &str, source: &[u8]) -> PyResult<tree_sitter::Tree> {
let language = registry::language_for_key(language_key)
.ok_or_else(|| PyValueError::new_err(format!("unknown language key: {language_key}")))?;
let mut parser = Parser::new();
parser
.set_language(&language)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
parser
.parse(source, None)
.ok_or_else(|| PyValueError::new_err("tree-sitter failed to parse source"))
}
fn sloc_mode_for_key(language_key: &str) -> PyResult<registry::SlocMode> {
let info_key = match language_key {
"tsx" => "typescript",
"markdown_inline" => "markdown",
_ => language_key,
};
registry::languages()
.iter()
.find(|l| l.key == info_key)
.map(|l| l.sloc_mode)
.ok_or_else(|| PyValueError::new_err(format!("unknown language key: {language_key}")))
}
#[pyclass(name = "LanguageInfo")]
#[derive(Clone)]
struct PyLanguageInfo {
#[pyo3(get)]
name: String,
#[pyo3(get)]
key: String,
#[pyo3(get)]
extensions: Vec<String>,
#[pyo3(get)]
explicit_only: Vec<String>,
#[pyo3(get)]
filenames: Vec<String>,
}
impl From<&LanguageInfo> for PyLanguageInfo {
fn from(info: &LanguageInfo) -> Self {
Self {
name: info.name.to_string(),
key: info.key.to_string(),
extensions: info.extensions.iter().map(|s| s.to_string()).collect(),
explicit_only: info.explicit_only.iter().map(|s| s.to_string()).collect(),
filenames: info.filenames.iter().map(|s| s.to_string()).collect(),
}
}
}
#[pyclass(name = "CallEdge")]
#[derive(Clone)]
struct PyCallEdge {
#[pyo3(get)]
caller: String,
#[pyo3(get)]
callee: String,
#[pyo3(get)]
is_external: bool,
}
impl From<CallEdge> for PyCallEdge {
fn from(e: CallEdge) -> Self {
Self {
caller: e.caller,
callee: e.callee,
is_external: e.is_external,
}
}
}
#[pyclass(name = "BasicBlock")]
#[derive(Clone)]
struct PyBasicBlock {
#[pyo3(get)]
id: usize,
#[pyo3(get)]
statements: Vec<(usize, usize)>,
#[pyo3(get)]
byte_range: (usize, usize),
#[pyo3(get)]
condition_range: Option<(usize, usize)>,
}
impl From<&BasicBlock> for PyBasicBlock {
fn from(b: &BasicBlock) -> Self {
Self {
id: b.id,
statements: b.statements.clone(),
byte_range: b.byte_range,
condition_range: b.condition_range,
}
}
}
fn cfg_edge_name(edge: CfgEdge) -> &'static str {
match edge {
CfgEdge::Fallthrough => "fallthrough",
CfgEdge::TrueBranch => "true_branch",
CfgEdge::FalseBranch => "false_branch",
CfgEdge::BackEdge => "back_edge",
CfgEdge::Return => "return",
CfgEdge::Break => "break",
CfgEdge::Continue => "continue",
}
}
#[pyclass(name = "FunctionCfg")]
#[derive(Clone)]
struct PyFunctionCfg {
#[pyo3(get)]
blocks: Vec<PyBasicBlock>,
#[pyo3(get)]
edges: Vec<(usize, usize, String)>,
#[pyo3(get)]
entry: usize,
#[pyo3(get)]
exits: Vec<usize>,
}
impl From<FunctionCfg> for PyFunctionCfg {
fn from(cfg: FunctionCfg) -> Self {
Self {
blocks: cfg.blocks.iter().map(PyBasicBlock::from).collect(),
edges: cfg
.edges
.iter()
.map(|(from, to, e)| (*from, *to, cfg_edge_name(*e).to_string()))
.collect(),
entry: cfg.entry,
exits: cfg.exits,
}
}
}
#[pyclass(name = "Fingerprint")]
#[derive(Clone)]
struct PyFingerprint {
#[pyo3(get)]
name: Option<String>,
#[pyo3(get)]
kind: String,
#[pyo3(get)]
hash: u64,
#[pyo3(get)]
node_count: usize,
#[pyo3(get)]
start_byte: usize,
#[pyo3(get)]
end_byte: usize,
#[pyo3(get)]
start_line: usize,
#[pyo3(get)]
end_line: usize,
}
impl From<Fingerprint> for PyFingerprint {
fn from(f: Fingerprint) -> Self {
Self {
name: f.name,
kind: f.kind.to_string(),
hash: f.hash,
node_count: f.node_count,
start_byte: f.start_byte,
end_byte: f.end_byte,
start_line: f.start_line,
end_line: f.end_line,
}
}
}
#[pyclass(name = "Suppression")]
#[derive(Clone)]
struct PySuppression {
#[pyo3(get)]
comment_line: usize,
#[pyo3(get)]
target_line: Option<usize>,
#[pyo3(get)]
tool: String,
#[pyo3(get)]
rule: String,
}
impl From<Suppression> for PySuppression {
fn from(s: Suppression) -> Self {
Self {
comment_line: s.comment_line,
target_line: s.target_line,
tool: s.tool,
rule: s.rule,
}
}
}
#[pyclass(name = "IgnoredRegion")]
#[derive(Clone)]
struct PyIgnoredRegion {
#[pyo3(get)]
byte_range: (usize, usize),
#[pyo3(get)]
line_range: (usize, usize),
#[pyo3(get)]
tools: Option<Vec<String>>,
}
impl From<IgnoredRegion> for PyIgnoredRegion {
fn from(r: IgnoredRegion) -> Self {
Self {
byte_range: (r.byte_range.start, r.byte_range.end),
line_range: (r.line_range.start, r.line_range.end),
tools: r.tools,
}
}
}
#[cfg(feature = "lang-markdown")]
#[pyclass(name = "MarkdownSection")]
#[derive(Clone)]
struct PyMarkdownSection {
#[pyo3(get)]
level: u8,
#[pyo3(get)]
heading_text: String,
#[pyo3(get)]
start_line: usize,
#[pyo3(get)]
end_line: usize,
#[pyo3(get)]
start_byte: usize,
#[pyo3(get)]
end_byte: usize,
#[pyo3(get)]
heading_end_byte: usize,
}
#[cfg(feature = "lang-markdown")]
impl From<Section> for PyMarkdownSection {
fn from(s: Section) -> Self {
Self {
level: s.level,
start_line: s.start_line,
end_line: s.end_line,
start_byte: s.heading_range.start,
end_byte: s.body_range.end,
heading_end_byte: s.heading_range.end,
heading_text: s.heading_text,
}
}
}
#[cfg(feature = "lang-markdown")]
#[pyclass(name = "FrontMatter")]
#[derive(Clone)]
struct PyFrontMatter {
#[pyo3(get)]
kind: &'static str,
#[pyo3(get)]
start_line: usize,
#[pyo3(get)]
end_line: usize,
#[pyo3(get)]
start_byte: usize,
#[pyo3(get)]
end_byte: usize,
#[pyo3(get)]
body_start_byte: usize,
#[pyo3(get)]
body_end_byte: usize,
}
#[cfg(feature = "lang-markdown")]
impl From<FrontMatter> for PyFrontMatter {
fn from(f: FrontMatter) -> Self {
Self {
kind: match f.kind {
FrontMatterKind::Yaml => "yaml",
FrontMatterKind::Toml => "toml",
},
start_line: f.start_line,
end_line: f.end_line,
start_byte: f.byte_range.start,
end_byte: f.byte_range.end,
body_start_byte: f.body_range.start,
body_end_byte: f.body_range.end,
}
}
}
#[pyclass(name = "FlatTree", frozen)]
struct PyFlatTree {
inner: FlatTree,
source: Py<PyBytes>,
kind_strs: Vec<Py<PyString>>,
serial: u64,
}
static TREE_SERIAL: AtomicU64 = AtomicU64::new(1);
#[pyclass(name = "Node", frozen)]
struct PyNode {
tree: Py<PyFlatTree>,
row: u32,
}
impl PyNode {
fn t(&self) -> &PyFlatTree {
self.tree.get()
}
fn at(&self, py: Python<'_>, row: u32) -> Option<PyNode> {
(row != flat::NONE).then(|| PyNode {
tree: self.tree.clone_ref(py),
row,
})
}
fn i(&self) -> usize {
self.row as usize
}
fn child_rows(&self) -> &[u32] {
let f = &self.t().inner;
let (lo, hi) = (f.child_offset[self.i()], f.child_offset[self.i() + 1]);
&f.child_list[lo as usize..hi as usize]
}
fn flag(&self, bit: u32) -> bool {
self.t().inner.flags[self.i()] & bit != 0
}
fn field_id(&self, name: &str) -> Option<u32> {
let fields = &self.t().inner.fields;
fields
.iter()
.skip(1)
.position(|f| f == name)
.map(|p| (p + 1) as u32)
}
fn step_named(&self, links: &[u32]) -> u32 {
let flags = &self.t().inner.flags;
let mut row = links[self.i()];
while row != flat::NONE && flags[row as usize] & flat::FLAG_NAMED == 0 {
row = links[row as usize];
}
row
}
}
#[pymethods]
impl PyNode {
#[getter(r#type)]
fn kind(&self, py: Python<'_>) -> Py<PyString> {
self.t().kind_strs[self.t().inner.kind[self.i()] as usize].clone_ref(py)
}
#[getter]
fn id(&self) -> u64 {
self.t().serial << 32 | u64::from(self.row)
}
#[getter]
fn start_byte(&self) -> u32 {
self.t().inner.start_byte[self.i()]
}
#[getter]
fn end_byte(&self) -> u32 {
self.t().inner.end_byte[self.i()]
}
#[getter]
fn start_point(&self) -> (u32, u32) {
let f = &self.t().inner;
(f.start_row[self.i()], f.start_col[self.i()])
}
#[getter]
fn end_point(&self) -> (u32, u32) {
let f = &self.t().inner;
(f.end_row[self.i()], f.end_col[self.i()])
}
#[getter]
fn text<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
let source = self.t().source.as_bytes(py);
let (s, e) = (self.start_byte() as usize, self.end_byte() as usize);
PyBytes::new_bound(py, &source[s.min(source.len())..e.min(source.len())])
}
#[getter]
fn is_named(&self) -> bool {
self.flag(flat::FLAG_NAMED)
}
#[getter]
fn is_error(&self) -> bool {
self.flag(flat::FLAG_ERROR)
}
#[getter]
fn is_missing(&self) -> bool {
self.flag(flat::FLAG_MISSING)
}
#[getter]
fn is_extra(&self) -> bool {
self.flag(flat::FLAG_EXTRA)
}
#[getter]
fn has_error(&self) -> bool {
self.flag(flat::FLAG_HAS_ERROR)
}
#[getter]
fn parent(&self, py: Python<'_>) -> Option<PyNode> {
self.at(py, self.t().inner.parent[self.i()])
}
#[getter]
fn prev_sibling(&self, py: Python<'_>) -> Option<PyNode> {
self.at(py, self.t().inner.prev_sibling[self.i()])
}
#[getter]
fn next_sibling(&self, py: Python<'_>) -> Option<PyNode> {
self.at(py, self.t().inner.next_sibling[self.i()])
}
#[getter]
fn prev_named_sibling(&self, py: Python<'_>) -> Option<PyNode> {
self.at(py, self.step_named(&self.t().inner.prev_sibling))
}
#[getter]
fn next_named_sibling(&self, py: Python<'_>) -> Option<PyNode> {
self.at(py, self.step_named(&self.t().inner.next_sibling))
}
#[getter]
fn children(&self, py: Python<'_>) -> Vec<PyNode> {
self.child_rows()
.iter()
.filter_map(|&r| self.at(py, r))
.collect()
}
#[getter]
fn named_children(&self, py: Python<'_>) -> Vec<PyNode> {
let flags = &self.t().inner.flags;
self.child_rows()
.iter()
.filter(|&&r| flags[r as usize] & flat::FLAG_NAMED != 0)
.filter_map(|&r| self.at(py, r))
.collect()
}
#[getter]
fn child_count(&self) -> usize {
self.child_rows().len()
}
#[getter]
fn named_child_count(&self) -> usize {
let flags = &self.t().inner.flags;
self.child_rows()
.iter()
.filter(|&&r| flags[r as usize] & flat::FLAG_NAMED != 0)
.count()
}
fn child_by_field_name(&self, py: Python<'_>, name: &str) -> Option<PyNode> {
let id = self.field_id(name)?;
let field = &self.t().inner.field;
let row = *self
.child_rows()
.iter()
.find(|&&r| field[r as usize] == id)?;
self.at(py, row)
}
fn children_by_field_name(&self, py: Python<'_>, name: &str) -> Vec<PyNode> {
let Some(id) = self.field_id(name) else {
return Vec::new();
};
let field = &self.t().inner.field;
self.child_rows()
.iter()
.filter(|&&r| field[r as usize] == id)
.filter_map(|&r| self.at(py, r))
.collect()
}
fn __eq__(&self, other: &Bound<'_, PyAny>) -> bool {
other
.downcast::<PyNode>()
.is_ok_and(|o| o.get().row == self.row && o.get().tree.is(&self.tree))
}
fn __hash__(&self) -> u64 {
self.id()
}
fn __repr__(&self, py: Python<'_>) -> String {
let (s, e) = (self.start_point(), self.end_point());
format!(
"<Node type={}, start_point=({}, {}), end_point=({}, {})>",
self.kind(py),
s.0,
s.1,
e.0,
e.1
)
}
}
fn column<'py>(py: Python<'py>, values: &[u32]) -> Bound<'py, PyBytes> {
let mut bytes = Vec::with_capacity(values.len() * 4);
for v in values {
bytes.extend_from_slice(&v.to_ne_bytes());
}
PyBytes::new_bound(py, &bytes)
}
#[pymethods]
impl PyFlatTree {
fn __len__(&self) -> usize {
self.inner.len()
}
#[getter]
fn root_node(slf: &Bound<'_, Self>) -> PyNode {
PyNode {
tree: slf.clone().unbind(),
row: 0,
}
}
#[getter]
fn source(&self, py: Python<'_>) -> Py<PyBytes> {
self.source.clone_ref(py)
}
#[getter]
fn kinds(&self) -> Vec<String> {
self.inner.kinds.clone()
}
#[getter]
fn fields(&self) -> Vec<String> {
self.inner.fields.clone()
}
#[getter]
fn kind<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
column(py, &self.inner.kind)
}
#[getter]
fn field<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
column(py, &self.inner.field)
}
#[getter]
fn flags<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
column(py, &self.inner.flags)
}
#[getter]
fn parent<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
column(py, &self.inner.parent)
}
#[getter]
fn first_child<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
column(py, &self.inner.first_child)
}
#[getter]
fn next_sibling<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
column(py, &self.inner.next_sibling)
}
#[getter]
fn prev_sibling<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
column(py, &self.inner.prev_sibling)
}
#[getter]
fn start_byte<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
column(py, &self.inner.start_byte)
}
#[getter]
fn end_byte<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
column(py, &self.inner.end_byte)
}
#[getter]
fn start_row<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
column(py, &self.inner.start_row)
}
#[getter]
fn start_col<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
column(py, &self.inner.start_col)
}
#[getter]
fn end_row<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
column(py, &self.inner.end_row)
}
#[getter]
fn end_col<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
column(py, &self.inner.end_col)
}
#[getter]
fn child_offset<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
column(py, &self.inner.child_offset)
}
#[getter]
fn child_list<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
column(py, &self.inner.child_list)
}
}
#[pyclass(name = "Capture")]
#[derive(Clone)]
struct PyCapture {
#[pyo3(get)]
pattern_index: usize,
#[pyo3(get)]
name: String,
#[pyo3(get)]
kind: &'static str,
#[pyo3(get)]
start_byte: usize,
#[pyo3(get)]
end_byte: usize,
#[pyo3(get)]
start_point: (usize, usize),
#[pyo3(get)]
end_point: (usize, usize),
}
impl From<Capture> for PyCapture {
fn from(c: Capture) -> Self {
Self {
pattern_index: c.pattern_index,
name: c.name,
kind: c.kind,
start_byte: c.start_byte,
end_byte: c.end_byte,
start_point: c.start_point,
end_point: c.end_point,
}
}
}
#[pyclass(name = "FileClass", frozen)]
#[derive(Clone)]
struct PyFileClass {
#[pyo3(get)]
kind: &'static str,
#[pyo3(get)]
size: Option<u64>,
#[pyo3(get)]
limit: Option<u64>,
#[pyo3(get)]
binary_kind: Option<&'static str>,
#[pyo3(get)]
mime: Option<&'static str>,
#[pyo3(get)]
encoding: Option<&'static str>,
#[pyo3(get)]
utf8_bom: bool,
#[pyo3(get)]
language_by_extension: Option<&'static str>,
#[pyo3(get)]
language_by_content: Option<&'static str>,
}
#[pymethods]
impl PyFileClass {
#[getter]
fn is_source_text(&self) -> bool {
self.kind == "source_text"
}
#[getter]
fn likely_language(&self) -> Option<&'static str> {
self.language_by_extension.or(self.language_by_content)
}
fn __repr__(&self) -> String {
format!("<FileClass kind={}>", self.kind)
}
}
fn binary_kind_name(kind: BinaryKind) -> &'static str {
match kind {
BinaryKind::Archive => "archive",
BinaryKind::Compressed => "compressed",
BinaryKind::Elf => "elf",
BinaryKind::Pe => "pe",
BinaryKind::MachO => "macho",
BinaryKind::Image => "image",
BinaryKind::Pdf => "pdf",
BinaryKind::Document => "document",
BinaryKind::Media => "media",
BinaryKind::Font => "font",
BinaryKind::Unknown => "unknown",
}
}
fn encoding_name(encoding: TextEncoding) -> &'static str {
match encoding {
TextEncoding::Utf8 => "utf8",
TextEncoding::Utf8Bom => "utf8_bom",
TextEncoding::Utf16Le => "utf16_le",
TextEncoding::Utf16Be => "utf16_be",
TextEncoding::Latin1 => "latin1",
}
}
impl From<FileClass> for PyFileClass {
fn from(class: FileClass) -> Self {
let mut out = PyFileClass {
kind: "binary",
size: None,
limit: None,
binary_kind: None,
mime: None,
encoding: None,
utf8_bom: false,
language_by_extension: None,
language_by_content: None,
};
match class {
FileClass::Empty => out.kind = "empty",
FileClass::Oversize { size, limit } => {
out.kind = "oversize";
out.size = Some(size);
out.limit = Some(limit);
}
FileClass::Binary { kind, mime } => {
out.binary_kind = Some(binary_kind_name(kind));
out.mime = mime;
}
FileClass::SourceText(text) => {
out.kind = "source_text";
out.encoding = Some(encoding_name(text.encoding));
out.utf8_bom = text.utf8_bom;
out.language_by_extension = text.by_extension.map(|l| l.key);
out.language_by_content = text.by_content.map(|l| l.key);
}
}
out
}
}
#[pyclass(name = "PathIgnore", frozen)]
struct PyPathIgnore {
inner: path_ignore::PathIgnore,
}
#[pymethods]
impl PyPathIgnore {
#[new]
#[pyo3(signature = (patterns, fold_extension_case=false))]
fn new(patterns: Vec<String>, fold_extension_case: bool) -> PyResult<Self> {
let compiled = if fold_extension_case {
path_ignore::PathIgnore::with_extension_case_folded(&patterns)
} else {
path_ignore::PathIgnore::new(&patterns)
};
compiled
.map(|inner| Self { inner })
.map_err(|e| PyValueError::new_err(e.to_string()))
}
fn is_ignored(&self, path: &str) -> bool {
self.inner.is_ignored(std::path::Path::new(path))
}
}
#[pyfunction]
fn languages() -> Vec<PyLanguageInfo> {
registry::languages()
.iter()
.map(PyLanguageInfo::from)
.collect()
}
#[pyfunction]
fn supported_languages_report() -> String {
registry::supported_languages_report()
}
#[pyfunction]
fn is_discoverable_file(path: &str) -> bool {
registry::is_discoverable_file(std::path::Path::new(path))
}
#[pyfunction]
fn call_edges(language_key: &str, source: &str) -> PyResult<Vec<PyCallEdge>> {
let tree = parse(language_key, source.as_bytes())?;
Ok(calls::call_edges(tree.root_node(), source)
.into_iter()
.map(PyCallEdge::from)
.collect())
}
#[pyfunction]
fn import_sources(language_key: &str, source: &[u8]) -> PyResult<Vec<String>> {
let tree = parse(language_key, source)?;
Ok(imports::import_sources(&tree, source, language_key))
}
#[pyfunction]
fn function_cfg(
language_key: &str,
source: &str,
function_name: &str,
) -> PyResult<Option<PyFunctionCfg>> {
let tree = parse(language_key, source.as_bytes())?;
let source_bytes = source.as_bytes();
let target = crate::query::find_descendants(tree.root_node(), |n| {
calls::is_function_kind(n.kind())
&& calls::get_function_name(n, source).as_deref() == Some(function_name)
});
Ok(target
.into_iter()
.find_map(|node| cfg::build_function_cfg(node, source_bytes, language_key))
.map(PyFunctionCfg::from))
}
#[pyfunction]
fn function_fingerprints(
language_key: &str,
source: &str,
min_nodes: usize,
) -> PyResult<Vec<PyFingerprint>> {
let tree = parse(language_key, source.as_bytes())?;
Ok(
fingerprint::function_fingerprints(tree.root_node(), source, min_nodes)
.into_iter()
.map(PyFingerprint::from)
.collect(),
)
}
#[pyfunction]
fn suppressions(language_key: &str, source: &str) -> PyResult<Vec<PySuppression>> {
let sloc_mode = sloc_mode_for_key(language_key)?;
Ok(suppressions_mod::suppressions(source, sloc_mode)
.into_iter()
.map(PySuppression::from)
.collect())
}
#[cfg(feature = "lang-markdown")]
#[pyfunction]
fn markdown_sections(source: &str) -> PyResult<Vec<PyMarkdownSection>> {
let tree = parse("markdown", source.as_bytes())?;
Ok(markdown::sections(tree.root_node(), source)
.into_iter()
.map(PyMarkdownSection::from)
.collect())
}
#[cfg(feature = "lang-markdown")]
#[pyfunction]
fn markdown_front_matter(source: &str) -> PyResult<Option<PyFrontMatter>> {
let tree = parse("markdown", source.as_bytes())?;
Ok(markdown::front_matter(tree.root_node(), source).map(PyFrontMatter::from))
}
#[pyfunction]
fn ignored_regions(language_key: &str, source: &str) -> PyResult<Vec<PyIgnoredRegion>> {
let sloc_mode = sloc_mode_for_key(language_key)?;
Ok(regions::ignored_regions(source, sloc_mode)
.into_iter()
.map(PyIgnoredRegion::from)
.collect())
}
#[pyfunction]
fn parse_tree(
py: Python<'_>,
language_key: &str,
source: Bound<'_, PyBytes>,
) -> PyResult<PyFlatTree> {
let tree = parse(language_key, source.as_bytes())?;
let inner = flat::flatten(&tree);
let kind_strs = inner
.kinds
.iter()
.map(|k| PyString::intern_bound(py, k).unbind())
.collect();
Ok(PyFlatTree {
inner,
source: source.unbind(),
kind_strs,
serial: TREE_SERIAL.fetch_add(1, Ordering::Relaxed),
})
}
#[pyfunction]
fn query(language_key: &str, source: &str, query: &str) -> PyResult<Vec<PyCapture>> {
let language = registry::language_for_key(language_key)
.ok_or_else(|| PyValueError::new_err(format!("unknown language key: {language_key}")))?;
let tree = parse(language_key, source.as_bytes())?;
tsquery::run_query(&language, tree.root_node(), source.as_bytes(), query)
.map(|caps| caps.into_iter().map(PyCapture::from).collect())
.map_err(|e| PyValueError::new_err(e.to_string()))
}
#[pyfunction]
fn tags_query(language_key: &str) -> Option<String> {
tsquery::tags_query(language_key)
}
#[pyfunction]
#[pyo3(signature = (path, max_size=None, prefix_len=classify_mod::DEFAULT_PREFIX_LEN))]
fn classify_file(path: &str, max_size: Option<u64>, prefix_len: usize) -> PyResult<PyFileClass> {
let limits = ClassifyLimits {
prefix_len,
max_size,
};
classify_mod::classify_file(std::path::Path::new(path), &limits)
.map(PyFileClass::from)
.map_err(PyErr::from)
}
#[pyfunction]
#[pyo3(signature = (path, prefix, file_size, max_size=None))]
fn classify(path: &str, prefix: &[u8], file_size: u64, max_size: Option<u64>) -> PyFileClass {
let limits = ClassifyLimits {
max_size,
..ClassifyLimits::default()
};
classify_mod::classify(std::path::Path::new(path), prefix, file_size, &limits).into()
}
#[pymodule]
fn lang_parsing_substrate(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<PyLanguageInfo>()?;
m.add_class::<PyCallEdge>()?;
m.add_class::<PyBasicBlock>()?;
m.add_class::<PyFunctionCfg>()?;
m.add_class::<PyFingerprint>()?;
m.add_class::<PySuppression>()?;
m.add_class::<PyIgnoredRegion>()?;
m.add_class::<PyFlatTree>()?;
m.add_class::<PyNode>()?;
m.add_class::<PyCapture>()?;
m.add_class::<PyFileClass>()?;
m.add_class::<PyPathIgnore>()?;
m.add("DEFAULT_PREFIX_LEN", classify_mod::DEFAULT_PREFIX_LEN)?;
m.add("NONE", flat::NONE)?;
m.add_function(wrap_pyfunction!(languages, m)?)?;
m.add_function(wrap_pyfunction!(supported_languages_report, m)?)?;
m.add_function(wrap_pyfunction!(is_discoverable_file, m)?)?;
#[cfg(feature = "lang-markdown")]
{
m.add_class::<PyMarkdownSection>()?;
m.add_class::<PyFrontMatter>()?;
m.add_function(wrap_pyfunction!(markdown_sections, m)?)?;
m.add_function(wrap_pyfunction!(markdown_front_matter, m)?)?;
}
m.add_function(wrap_pyfunction!(call_edges, m)?)?;
m.add_function(wrap_pyfunction!(import_sources, m)?)?;
m.add_function(wrap_pyfunction!(function_cfg, m)?)?;
m.add_function(wrap_pyfunction!(function_fingerprints, m)?)?;
m.add_function(wrap_pyfunction!(suppressions, m)?)?;
m.add_function(wrap_pyfunction!(ignored_regions, m)?)?;
m.add_function(wrap_pyfunction!(parse_tree, m)?)?;
m.add_function(wrap_pyfunction!(query, m)?)?;
m.add_function(wrap_pyfunction!(tags_query, m)?)?;
m.add_function(wrap_pyfunction!(classify_file, m)?)?;
m.add_function(wrap_pyfunction!(classify, m)?)?;
Ok(())
}