Parse sub section

This commit is contained in:
Jaby 2023-06-29 22:18:31 +02:00
parent 024e4145e0
commit 394dbb1e85
1 changed files with 50 additions and 10 deletions

View File

@ -11,27 +11,67 @@ pub fn scan(input: Input) -> Result<Vec<Section>, Error> {
} }
fn process<F: std::iter::Iterator<Item=String>>(mut line_iter:F) -> Result<(), Error> { fn process<F: std::iter::Iterator<Item=String>>(mut line_iter:F) -> Result<(), Error> {
while let Some(line) = line_iter.next() { while let Some(mut line) = line_iter.next() {
if line.starts_with(".") { loop {
process_section(line, &mut line_iter)?; if line.starts_with(".") {
line = process_section(line, &mut line_iter)?;
}
else {
break;
}
} }
} }
Ok(()) Ok(())
} }
fn process_section<F: std::iter::Iterator<Item=String>>(line: String, line_iter:&mut F) -> Result<(), Error> { fn process_section<F: std::iter::Iterator<Item=String>>(line: String, line_iter:&mut F) -> Result<String, Error> {
let (section, _line) = read_section(line, || { let mut next_line_closure = || {
line_iter.next().ok_or(Error::from_str("")) line_iter.next().ok_or(Error::from_str("Unexpected end of file"))
})?; };
let (mut section, line) = read_section(line, &mut next_line_closure)?;
println!("Section: {} @{:?} {:?}", section.name, section.adr, section.size); println!("Section: {} @{:?} {:?}", section.name, section.adr, section.size);
Ok(()) process_subsection(&mut section, line, &mut next_line_closure)
} }
fn read_section<F:FnMut()-> Result<String, Error>>(mut line: String, mut next_line: F) -> Result<(Section, Option<String>), Error> { fn process_subsection<F:FnMut()-> Result<String, Error>>(_section: &mut Section, line: Option<String>, next_line: &mut F) -> Result<String, Error> {
let mut line = {
if let Some(line) = line {
line
}
else {
next_line()?
}
};
loop {
if line.starts_with(" .") {
println!(">>> SubSection: {}", line);
}
else if line.is_empty() {
return Ok(line);
}
line = {
if let Ok(line) = next_line() {
line
}
else {
// EOF
return Ok(String::from(""));
}
};
}
}
fn read_section<F:FnMut()-> Result<String, Error>>(mut line: String, next_line: &mut F) -> Result<(Section, Option<String>), Error> {
let mut split_line = line.split_whitespace(); let mut split_line = line.split_whitespace();
let name = split_line.next().ok_or(Error::from_str(""))?.to_string(); let name = split_line.next().ok_or(Error::from_str("Couldn't locate section name"))?.to_string();
let need_new_line = name.chars().count() > 16; let need_new_line = name.chars().count() > 16;
if need_new_line { if need_new_line {