language_reporting/
simple.rs

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
#[derive(Debug, Clone)]
pub struct SimpleFile {
    name: String,
    contents: String,
}

#[derive(Debug, Clone, Default)]
pub struct SimpleReportingFiles {
    files: Vec<SimpleFile>,
}

impl SimpleReportingFiles {
    pub fn add(&mut self, name: impl Into<String>, value: impl Into<String>) -> usize {
        self.files.push(SimpleFile {
            name: name.into(),
            contents: value.into(),
        });

        self.files.len() - 1
    }
}

impl crate::ReportingFiles for SimpleReportingFiles {
    type Span = SimpleSpan;
    type FileId = usize;

    fn file_id(&self, span: SimpleSpan) -> usize {
        span.file_id
    }

    fn file_name(&self, id: usize) -> crate::FileName {
        crate::FileName::Verbatim(self.files[id].name.clone())
    }

    fn byte_span(&self, _file: usize, _from_index: usize, _to_index: usize) -> Option<Self::Span> {
        unimplemented!()
    }

    fn byte_index(&self, file: usize, line: usize, column: usize) -> Option<usize> {
        let source = &self.files[file].contents;
        let mut seen_lines = 0;
        let mut seen_bytes = 0;

        for (pos, _) in source.match_indices('\n') {
            if seen_lines == line {
                return Some(seen_bytes + column);
            } else {
                seen_lines += 1;
                seen_bytes = pos + 1;
            }
        }

        None
    }

    fn location(&self, file: usize, index: usize) -> Option<crate::Location> {
        let source = &self.files[file].contents;
        let mut seen_lines = 0;
        let mut seen_bytes = 0;

        for (pos, _) in source.match_indices('\n') {
            if pos > index {
                return Some(crate::Location::new(seen_lines, index - seen_bytes));
            } else {
                seen_lines += 1;
                seen_bytes = pos;
            }
        }

        None
    }

    fn line_span(&self, file: usize, line: usize) -> Option<Self::Span> {
        let source = &self.files[file].contents;
        let mut seen_lines = 0;
        let mut seen_bytes = 0;

        for (pos, _) in source.match_indices('\n') {
            if seen_lines == line {
                return Some(SimpleSpan::new(file, seen_bytes, pos));
            } else {
                seen_lines += 1;
                seen_bytes = pos + 1;
            }
        }

        None
    }

    fn source(&self, span: SimpleSpan) -> Option<String> {
        let source = &self.files[span.file_id].contents;

        Some(source[span.start..span.end].to_string())
    }
}

#[derive(Debug, Copy, Clone)]
pub struct SimpleSpan {
    file_id: usize,
    start: usize,
    end: usize,
}

impl SimpleSpan {
    pub fn new(file_id: usize, start: usize, end: usize) -> SimpleSpan {
        assert!(
            end >= start,
            "SimpleSpan {} must be bigger than {}",
            end,
            start
        );

        SimpleSpan {
            file_id,
            start,
            end,
        }
    }
}

impl crate::ReportingSpan for SimpleSpan {
    fn with_start(&self, start: usize) -> Self {
        SimpleSpan::new(self.file_id, start, self.end)
    }

    fn with_end(&self, end: usize) -> Self {
        SimpleSpan::new(self.file_id, self.start, end)
    }

    fn start(&self) -> usize {
        self.start
    }

    fn end(&self) -> usize {
        self.end
    }
}