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
77
78
79
80
81
82
83
84
use crate::tracking::{ SourceReference, SourceRegion, SourceAttribution };


pub struct LexerIterator<'a> {
  pub source_ref: SourceReference<'a>,
  pub body: Vec<char>,
  pub index: usize,
  pub length: usize,
  pub curr: char
}

impl<'a> LexerIterator<'a> {
  pub fn new (source_ref: SourceReference<'a>) -> Self {
    let index = 0;

    let body = source_ref.get_source().unwrap().body.clone();
    let length = body.len();
    let curr = if index < length { body[index] } else { '\0' };

    Self {
      source_ref,
      body,
      index,
      length,
      curr,
    }
  }


  pub fn advance_n (&mut self, n: usize) -> char {
    self.index = (self.index + n).min(self.length);
    self.curr = if self.valid() { self.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_ref.valid()
  }


  pub fn peek_prev (&self) -> char {
    if self.valid() { self.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_ref.valid() { self.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 { SourceRegion { start: self.length - 1, end: self.length } }
  }

  pub fn get_attribution (&self, region: SourceRegion) -> SourceAttribution {
    self.source_ref.get_attribution(region)
  }

  pub fn warning (&mut self, region: SourceRegion, msg: String) {
    self.source_ref.warning(region, msg);
  }

  pub fn error (&mut self, region: SourceRegion, msg: String) {
    self.source_ref.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);
  }
}