From 1553b785cbc75bfc47066096b81d0066017a91ad Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 30 Jun 2026 13:21:38 -0700 Subject: Added LookAhead, fixed up the proc-macro. LookAhead should make the rest of the parser and lexer stuff way easier, probably spin that out into it's own lib later on. --- lookahead/Cargo.toml | 6 ++++ lookahead/src/lib.rs | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 lookahead/Cargo.toml create mode 100644 lookahead/src/lib.rs (limited to 'lookahead') diff --git a/lookahead/Cargo.toml b/lookahead/Cargo.toml new file mode 100644 index 0000000..d79f913 --- /dev/null +++ b/lookahead/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "lookahead" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/lookahead/src/lib.rs b/lookahead/src/lib.rs new file mode 100644 index 0000000..08bf20e --- /dev/null +++ b/lookahead/src/lib.rs @@ -0,0 +1,84 @@ +pub struct LookAhead +where + U: Iterator + Sized +{ + iter: U, + data: [Option;C], + cur: usize, + fill: usize, +} + +impl LookAhead +where + U: Iterator + Sized +{ + pub fn new(iter: U) -> Self { + Self { + iter, + data: [const {None};C], + cur: 0, + fill: 0, + } + } + + pub fn peek(&mut self, offset: usize) -> Option<&T> { + assert!(offset std::iter::Iterator for LookAhead +where + U: Iterator + Sized +{ + type Item=T; + + fn next(&mut self) -> Option { + if self.fill == 0 { + self.iter.next() + } else { + let old = self.cur; + self.cur = (self.cur+1)%C; + self.fill -= 1; + self.data[old].take() + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn basic() { + let data = [1, 2, 3, 4, 5, 6, 7, 8]; + let mut i = LookAhead::<3,_,_>::new(data.iter()); + assert_eq!(i.next(), Some(&1)); + assert_eq!(i.peek(1), Some(&&3)); + assert_eq!(i.peek(0), Some(&&2)); + assert_eq!(i.next(), Some(&2)); + assert_eq!(i.peek(0), Some(&&3)); + assert_eq!(i.next(), Some(&3)); + assert_eq!(i.next(), Some(&4)); + assert_eq!(i.next(), Some(&5)); + assert_eq!(i.peek(0), Some(&&6)); + assert_eq!(i.peek(1), Some(&&7)); + assert_eq!(i.peek(2), Some(&&8)); + assert_eq!(i.next(), Some(&6)); + assert_eq!(i.peek(2), None); + assert_eq!(i.next(), Some(&7)); + assert_eq!(i.peek(2), None); + assert_eq!(i.next(), Some(&8)); + assert_eq!(i.next(), None); + } +} -- cgit v1.2.3