1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
use crate::tracking::*; pub struct LexerIterator<'a> { pub source: &'a mut Source, pub index: usize, pub length: usize, pub curr: char } impl<'a> LexerIterator<'a> { pub fn new (source: &'a mut Source) -> Self { let index = 0; let length = source.body.len(); let curr = if index < length { source.body[index] } else { '\0' }; Self { source, index, length, curr, } } pub fn advance_n (&mut self, n: usize) -> char { self.index = (self.index + n).min(self.length); self.curr = if self.index < self.length { self.source.body[self.index] } else { '\0' }; self.curr } pub fn advance (&mut self) -> char { self.advance_n(1) } pub fn valid (&self) -> bool { self.index < self.length && self.source.valid() } pub fn peek_prev (&self) -> char { if self.index > 0 { self.source.body[self.index - 1] } else { '\0' } } pub fn peek_next (&self) -> char { let next_index = self.index + 1; if next_index < self.length { self.source.body[next_index] } else { '\0' } } pub fn get_simple_err_region (&self) -> SourceRegion { if self.valid() { SourceRegion { start: self.index, end: self.index + 1 } } else { self.source.get_end_err_region() } } pub fn warning (&mut self, region: SourceRegion, msg: String) { self.source.warning(region, msg); } pub fn error (&mut self, region: SourceRegion, msg: String) { self.source.error(region, msg); } pub fn simple_warning (&mut self, msg: String) { self.warning(self.get_simple_err_region(), msg); } pub fn simple_error (&mut self, msg: String) { self.error(self.get_simple_err_region(), msg); } }