Skip to content

Commit

Permalink
Continued nom parser implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Lucas committed Apr 11, 2024
1 parent bcb6d99 commit 394bd93
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions src/root/nom_parser/parse_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,27 @@ use crate::root::nom_parser::parse_util::discard_ignored;

// ! BROKEN

const DEFAULT_TERMINATORS: [(char, char); 2] = [
('{', '}'),
('(', ')')
const DEFAULT_TERMINATORS: [(char, char, bool); 3] = [
('{', '}', true),
('(', ')', true),
('"', '"', false)
];

pub fn default_section(s: Span, section_char: char) -> ParseResult {
let x = section(
s,
DEFAULT_TERMINATORS.iter().find_position(|(c, _)| *c == section_char).unwrap().0,
DEFAULT_TERMINATORS.iter().find_position(|(c, _, _)| *c == section_char).unwrap().0,
&DEFAULT_TERMINATORS
);
// println!("----------\n{:?}\n**{}\n\n**{}\n\n**{}", section_char, s.fragment(), x.as_ref().unwrap().0.fragment(), x.as_ref().unwrap().1.fragment());
x
}

pub fn section<'a>(s: Span<'a>, terminator: usize, all_terminators: &[(char, char)]) -> ParseResult<'a> {
pub fn section<'a>(s: Span<'a>, terminator: usize, all_terminators: &[(char, char, bool)]) -> ParseResult<'a> {
let (initial_span, _) = nchar(all_terminators[terminator].0)(s)?;

let allow_subsections = all_terminators[terminator].2;

let mut depth = 0;

let mut s = initial_span;
Expand All @@ -44,8 +47,7 @@ pub fn section<'a>(s: Span<'a>, terminator: usize, all_terminators: &[(char, cha
if let Ok((ns, _)) = nchar::<_, ErrorTree>(all_terminators[terminator].1)(s) {
if depth == 0 {
return Ok((ns, initial_span.take_split(initial_span.offset(&s)).1));
}
else {
} else {
s = ns;
depth -= 1;
continue;
Expand All @@ -58,21 +60,23 @@ pub fn section<'a>(s: Span<'a>, terminator: usize, all_terminators: &[(char, cha
continue;
}

for (pos, t) in all_terminators.iter().enumerate() {
if pos == terminator { continue; }
if allow_subsections {
for (pos, t) in all_terminators.iter().enumerate() {
if pos == terminator { continue; }

if let Ok(_) = nchar::<_, ErrorTree>(t.0)(s) {
s = section(s, pos, all_terminators)?.0;
continue 'main;
}
if let Ok(_) = nchar::<_, ErrorTree>(t.0)(s) {
s = section(s, pos, all_terminators)?.0;
continue 'main;
}

if let Ok(_) = nchar::<_, ErrorTree>(t.1)(s) {
// Unopened section closed
todo!()
if let Ok(_) = nchar::<_, ErrorTree>(t.1)(s) {
// Unopened section closed
todo!()
}
}
}

s = anychar(s)?.0;
s = anychar(s)?.0;
}
}

Err(Error(ErrorTree::from_char(s, all_terminators[terminator].1)))
Expand Down

0 comments on commit 394bd93

Please sign in to comment.