Skip to content

Commit

Permalink
refactor all tokens to have an index field
Browse files Browse the repository at this point in the history
  • Loading branch information
misson20000 committed Oct 28, 2024
1 parent 94b2128 commit deed567
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 224 deletions.
24 changes: 12 additions & 12 deletions src/model/listing/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,9 +654,9 @@ mod tests {
node: document.root.clone(),
node_path: structure::Path::default(),
node_addr: addr::unit::NULL,
node_child_index: 0,
depth: 1,
},
index: 0,
extent: addr::Extent::sized(addr::unit::NULL, 16.into()),
line: addr::Extent::sized(addr::unit::NULL, 16.into()),
}).as_token_ref());
Expand Down Expand Up @@ -689,9 +689,9 @@ mod tests {
node: document.root.clone(),
node_path: structure::Path::default(),
node_addr: addr::unit::NULL,
node_child_index: 1,
depth: 1,
},
index: 1,
extent: addr::Extent::sized(4.into(), 12.into()),
line: addr::Extent::sized(0.into(), 16.into()),
}).as_token_ref());
Expand Down Expand Up @@ -724,9 +724,9 @@ mod tests {
node: document.root.clone(),
node_path: structure::Path::default(),
node_addr: addr::unit::NULL,
node_child_index: 2,
depth: 1,
},
index: 2,
extent: addr::Extent::sized(8.into(), 8.into()),
line: addr::Extent::sized(0.into(), 16.into()),
}).as_token_ref());
Expand All @@ -745,9 +745,9 @@ mod tests {
node: document.root.clone(),
node_path: structure::Path::default(),
node_addr: addr::unit::NULL,
node_child_index: 0,
depth: 1,
},
index: 0,
extent: addr::Extent::sized(addr::unit::NULL, 16.into()),
line: addr::Extent::sized(addr::unit::NULL, 16.into()),
}).as_token_ref());
Expand All @@ -764,9 +764,9 @@ mod tests {
node: document.root.clone(),
node_path: structure::Path::default(),
node_addr: addr::unit::NULL,
node_child_index: 0,
depth: 1,
},
index: 0,
extent: addr::Extent::sized(addr::unit::NULL, 16.into()),
line: addr::Extent::sized(addr::unit::NULL, 16.into()),
}).as_token_ref());
Expand Down Expand Up @@ -799,9 +799,9 @@ mod tests {
node: document.root.clone(),
node_path: structure::Path::default(),
node_addr: addr::unit::NULL,
node_child_index: 1,
depth: 1,
},
index: 1,
extent: addr::Extent::sized(8.into(), 8.into()),
line: addr::Extent::sized(addr::unit::NULL, 16.into()),
}).as_token_ref());
Expand All @@ -819,9 +819,9 @@ mod tests {
node: document.root.clone(),
node_path: structure::Path::default(),
node_addr: addr::unit::NULL,
node_child_index: 0,
depth: 1,
},
index: 0,
extent: addr::Extent::sized(0x20.into(), 0x10.into()),
line: addr::Extent::sized(0x20.into(), 0x10.into()),
}).as_token_ref());
Expand Down Expand Up @@ -857,9 +857,9 @@ mod tests {
node: node.clone(),
node_path: vec![0],
node_addr: 0x12.into(),
node_child_index: 0,
depth: 2,
},
index: 0,
extent: addr::Extent::sized(0x10.into(), 0x10.into()),
line: addr::Extent::sized(0x10.into(), 0x10.into()),
}).as_token_ref());
Expand All @@ -877,9 +877,9 @@ mod tests {
node: document.root.clone(),
node_path: structure::Path::default(),
node_addr: addr::unit::NULL,
node_child_index: 0,
depth: 1,
},
index: 0,
extent: addr::Extent::sized(0x0.into(), 0x10.into()),
line: addr::Extent::sized(0x0.into(), 0x10.into()),
}).as_token_ref());
Expand Down Expand Up @@ -915,9 +915,9 @@ mod tests {
node: document.root.clone(),
node_path: vec![],
node_addr: 0x0.into(),
node_child_index: 0,
depth: 1,
},
index: 0,
extent: addr::Extent::sized(0x0.into(), 0x10.into()),
line: addr::Extent::sized(0x0.into(), 0x10.into()),
}).as_token_ref());
Expand Down Expand Up @@ -976,9 +976,9 @@ mod tests {
node: document.root.clone(),
node_path: structure::Path::default(),
node_addr: addr::unit::NULL,
node_child_index: 0,
depth: 1,
},
index: 0,
extent: addr::Extent::sized(addr::unit::NULL, 16.into()),
line: addr::Extent::sized(addr::unit::NULL, 16.into()),
}).as_token_ref());
Expand All @@ -997,10 +997,10 @@ mod tests {
node: document.root.clone(),
node_path: structure::Path::default(),
node_addr: addr::unit::NULL,
node_child_index: 0,
depth: 0,
},
kind: token::PunctuationKind::CloseBracket,
index: 0,
}).as_token_ref());
}
}
96 changes: 96 additions & 0 deletions src/model/listing/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,8 @@ impl fmt::Debug for Line {
mod tests {
use super::*;

use crate::model::listing::token::AsTokenRef;

#[test]
fn test_hexstring_backwards_construction() {
let node = structure::Node::builder()
Expand All @@ -556,6 +558,7 @@ mod tests {
node: node.clone(),
node_path: vec![],
node_addr: addr::unit::NULL,
node_child_index: 0,
depth: 0,
},
extent: addr::Extent::sized_u64(0, 8),
Expand All @@ -567,8 +570,101 @@ mod tests {
node: node.clone(),
node_path: vec![],
node_addr: addr::unit::NULL,
node_child_index: 0,
depth: 0,
},
})), LinePushResult::Accepted);
}

#[test]
fn containing_position() {
let root = structure::Node::builder()
.name("root")
.size(0x40)
.child(0x10, |b| b
.name("child0")
.size(0x20))
.child(0x14, |b| b
.name("child1")
.size(0x50)
.children_display(structure::ChildrenDisplay::Summary)
.child(0x0, |b| b
.name("child1.0")
.size(0x18))
.child(0x20, |b| b
.name("child1.1")
.size(0x18))
.child(0x34, |b| b
.name("child1.2")
.size(0x18))
.child(0x48, |b| b
.name("child1.3")
.size(0x1c)))
.child(0x60, |b| b
.name("child2")
.size(0x4))
.build();

/* Pregenerate all the lines from a simple forward walk through the whole document. */
let mut position = stream::Position::at_beginning(root.clone());
let mut lines = vec![];
loop {
let mut begin = position.clone();
begin.canonicalize_next();

let line = Line::next_from_position(&mut position);
if line.is_empty() {
break;
}

let mut end = position.clone();
end.canonicalize_next();

lines.push((begin, line, end));
}

let mut position = stream::Position::at_beginning(root.clone());
let mut i = 0;
loop {
/* For every token in the stream, */
let token = match position.gen_token() {
stream::TokenGenerationResult::Ok(token) => token,
stream::TokenGenerationResult::Skip => if position.move_next() { continue } else { break },
stream::TokenGenerationResult::Boundary => break,
};

/* Find it in the array of lines we pregenerated. */
let expected_index_in_line = loop {
if let Some(index) = lines[i].1.iter_tokens().position(|t| t == token.as_token_ref()) {
break index;
} else {
i+= 1;
}
};

/* Make a new line containing just this token. */
let mut line_end = position.clone();
let (line, mut line_begin, index_in_line) = Line::containing_position(&mut line_end);
line_begin.canonicalize_next();
line_end.canonicalize_next();

/* Check that the line matches the one from the pregenerated array. */
if line != lines[i].1 || index_in_line != expected_index_in_line || line_begin != lines[i].0 || line_end != lines[i].2 {
println!("seeked to {:?}", token);
println!("line from forward walk : {}", lines[i].1);
println!("line from containing_position: {}", line);
println!("expected index {}, got index {}", expected_index_in_line, index_in_line);

println!("begin position [actual] : {:#?}", line_begin);
println!("begin position [expected]: {:#?}", lines[i].0);

println!("end position [actual] : {:#?}", line_end);
println!("end position [expected]: {:#?}", lines[i].2);

panic!("mismatched");
}

position.move_next();
}
}
}
Loading

0 comments on commit deed567

Please sign in to comment.