`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)
| 40 | /// passes: `Err("timeout: …")` then, and the old `Err("parser returned null |
| 41 | /// tree")` for any other null tree. |
| 42 | pub 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)] |
| 61 | mod tests { |