MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / parse

Function parse

codegraph-kernel/src/parse_budget.rs:42–58  ·  view source on GitHub ↗

`parser.parse(source, None)`, cancelled once the extraction's deadline passes: `Err("timeout: …")` then, and the old `Err("parser returned null tree")` for any other null tree.

(parser: &mut Parser, source: &str)

Source from the content-addressed store, hash-verified

40/// passes: `Err("timeout: …")` then, and the old `Err("parser returned null
41/// tree")` for any other null tree.
42pub fn parse(parser: &mut Parser, source: &str) -> Result<Tree, String> {
43 let Some((deadline, ms)) = DEADLINE.with(Cell::get) else {
44 return parser.parse(source, None).ok_or_else(|| "parser returned null tree".to_string());
45 };
46 let bytes = source.as_bytes();
47 let len = bytes.len();
48 let mut read = |i: usize, _: Point| (i < len).then(|| &bytes[i..]).unwrap_or_default();
49 let mut cancel = |_: &ParseState| Instant::now() >= deadline;
50 let tree = parser.parse_with_options(&mut read, None, Some(ParseOptions::new().progress_callback(&mut cancel)));
51 tree.ok_or_else(|| {
52 if Instant::now() >= deadline {
53 format!("timeout: the parse ran past its {ms}ms budget")
54 } else {
55 "parser returned null tree".to_string()
56 }
57 })
58}
59
60#[cfg(test)]
61mod tests {

Callers 15

extractFunction · 0.70
extractFunction · 0.70
extractFunction · 0.70
extractFunction · 0.70
extractFunction · 0.70
extractFunction · 0.70
extractFunction · 0.70
extractFunction · 0.70
extractFunction · 0.70
extractFunction · 0.70

Calls 3

withMethod · 0.80
parseMethod · 0.80
lenMethod · 0.80