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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use crate::source::buffer::Input;
use std::convert::TryInto;
use std::rc::Rc;

#[derive(Clone)]
pub struct Range {
    pub begin_pos: usize,
    pub end_pos: usize,
    pub(crate) input: Rc<Input>,
}

impl PartialEq for Range {
    fn eq(&self, other: &Self) -> bool {
        self.begin_pos == other.begin_pos && self.end_pos == other.end_pos
    }
}

impl Range {
    pub fn new(begin_pos: usize, end_pos: usize, input: Rc<Input>) -> Self {
        debug_assert!(
            (begin_pos < end_pos) || (begin_pos == end_pos && end_pos == input.len()),
            "begin_pos = {}, end_pos = {}, input.len() = {}",
            begin_pos,
            end_pos,
            input.len()
        );
        debug_assert!(
            end_pos <= input.len(),
            "end_pos = {}, len = {}",
            end_pos,
            input.len()
        );
        Self {
            begin_pos,
            end_pos,
            input,
        }
    }

    pub fn begin(&self) -> Self {
        self.with_begin(self.begin_pos).with_end(self.begin_pos)
    }

    pub fn end(&self) -> Self {
        self.with_begin(self.end_pos).with_end(self.end_pos)
    }

    pub fn size(&self) -> usize {
        self.end_pos - self.begin_pos
    }

    pub fn to_range(&self) -> std::ops::Range<usize> {
        self.begin_pos..self.end_pos
    }

    pub fn with_begin(&self, begin_pos: usize) -> Self {
        Self::new(begin_pos, self.end_pos, Rc::clone(&self.input))
    }

    pub fn with_end(&self, end_pos: usize) -> Self {
        Self::new(self.begin_pos, end_pos, Rc::clone(&self.input))
    }

    pub fn with(&self, begin_pos: usize, end_pos: usize) -> Self {
        Self::new(begin_pos, end_pos, Rc::clone(&self.input))
    }

    pub fn adjust_begin(&self, d: i32) -> Self {
        let begin_pos: i32 = self
            .begin_pos
            .try_into()
            .expect("failed to convert location to i32 (is it too big?)");
        let begin_pos: usize = (begin_pos + d)
            .try_into()
            .expect("failed to convert location to usize (is it negative?)");
        Self::new(begin_pos, self.end_pos, Rc::clone(&self.input))
    }

    pub fn adjust_end(&self, d: i32) -> Self {
        let end_pos: i32 = self
            .end_pos
            .try_into()
            .expect("failed to convert location to i32 (is it too big?)");
        let end_pos: usize = (end_pos + d)
            .try_into()
            .expect("failed to convert location to usize (is it negative?)");
        Self::new(self.begin_pos, end_pos, Rc::clone(&self.input))
    }

    pub fn resize(&self, new_size: usize) -> Self {
        self.with_end(self.begin_pos + new_size)
    }

    pub fn join(&self, other: &Self) -> Self {
        Self::new(
            std::cmp::min(self.begin_pos, other.begin_pos),
            std::cmp::max(self.end_pos, other.end_pos),
            Rc::clone(&self.input),
        )
    }

    pub(crate) fn maybe_join(self, other: &Option<Self>) -> Self {
        match other {
            Some(other) => self.join(other),
            None => self,
        }
    }

    pub fn is_empty(&self) -> bool {
        self.begin_pos == self.end_pos
    }

    pub fn begin_line_col(&self) -> Option<(usize, usize)> {
        self.input.line_col_for_pos(self.begin_pos)
    }

    pub fn end_line_col(&self) -> Option<(usize, usize)> {
        self.input.line_col_for_pos(self.end_pos)
    }

    pub fn expand_to_line(&self) -> Option<(usize, Self)> {
        let (begin_line, _) = self.begin_line_col()?;
        let line_no = begin_line;
        let line = &self.input.lines[line_no];
        Some((line_no, self.with(line.start, line.line_end())))
    }

    pub fn source(&self) -> Option<String> {
        let bytes = self.input.substr_at(self.begin_pos, self.end_pos)?;
        Some(String::from_utf8_lossy(bytes).into_owned())
    }

    pub(crate) fn print(&self, name: &str) {
        println!(
            "{}{} {}",
            " ".repeat(self.begin_pos),
            "~".repeat(self.size()),
            name
        )
    }
}

impl std::fmt::Debug for Range {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&format!("{}...{}", self.begin_pos, self.end_pos))
    }
}