pddl_ish_parser/parser/
utils.rs1use crate::models::parser_error::ParserError;
2
3use super::error_context::get_error_context;
4
5pub fn extract_balanced(input: &str, open: char, close: char) -> Result<String, ParserError> {
6 let mut balance = 0;
7 let mut start = 0;
8 let mut end = 0;
9
10 for (i, c) in input.chars().enumerate() {
11 if c == open {
12 if balance == 0 {
13 start = i;
14 }
15 balance += 1;
16 } else if c == close {
17 balance -= 1;
18 if balance == 0 {
19 end = i;
20 break;
21 }
22 }
23 }
24
25 if balance != 0 {
26 return Err(ParserError::new("Unbalanced parentheses".to_string(), get_error_context(input)));
27 }
28
29 Ok(input[start..=end].to_string())
30}