1use std::fmt::{self, Debug, Display};
2use std::sync::Arc;
3
4#[derive(Clone, PartialEq, Eq, Hash)]
5pub enum Span {
6 Panic,
7 Egglog(Arc<EgglogSpan>),
8 Rust(Arc<RustSpan>),
9}
10
11#[derive(Debug, Clone, PartialEq, Eq, Hash)]
12pub struct EgglogSpan {
13 pub file: Arc<SrcFile>,
14 pub i: usize,
15 pub j: usize,
16}
17
18#[derive(Debug, Clone, PartialEq, Eq, Hash)]
19pub struct RustSpan {
20 pub file: &'static str,
21 pub line: u32,
22 pub column: u32,
23}
24
25#[derive(Debug, PartialEq, Eq, Hash, Clone)]
26pub struct SrcFile {
27 pub name: Option<String>,
28 pub contents: String,
29}
30
31impl SrcFile {
32 pub fn get_location(&self, offset: usize) -> (usize, usize) {
33 let mut line = 1;
34 let mut col = 1;
35 for (i, c) in self.contents.char_indices() {
36 if i == offset {
37 break;
38 }
39 if c == '\n' {
40 line += 1;
41 col = 1;
42 } else {
43 col += 1;
44 }
45 }
46 (line, col)
47 }
48}
49
50impl Span {
51 pub fn string(&self) -> &str {
52 match self {
53 Span::Panic => panic!("Span::Panic in Span::string"),
54 Span::Rust(_) => panic!("Span::Rust cannot track end position"),
55 Span::Egglog(span) => &span.file.contents[span.i..span.j],
56 }
57 }
58}
59
60impl Debug for Span {
61 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62 write!(f, "{}", self)
63 }
64}
65
66impl Display for Span {
67 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68 match self {
69 Span::Panic => panic!("Span::Panic in impl Display"),
70 Span::Rust(span) => write!(f, "At {}:{} of {}", span.line, span.column, span.file),
71 Span::Egglog(span) => {
72 let (start_line, start_col) = span.file.get_location(span.i);
73 let (end_line, end_col) = span
74 .file
75 .get_location((span.j.saturating_sub(1)).max(span.i));
76 let quote = self.string();
77 match (&span.file.name, start_line == end_line) {
78 (Some(filename), true) => write!(
79 f,
80 "In {}:{}-{} of {filename}: {quote}",
81 start_line, start_col, end_col
82 ),
83 (Some(filename), false) => write!(
84 f,
85 "In {}:{}-{}:{} of {filename}: {quote}",
86 start_line, start_col, end_line, end_col
87 ),
88 (None, false) => write!(
89 f,
90 "In {}:{}-{}:{}: {quote}",
91 start_line, start_col, end_line, end_col
92 ),
93 (None, true) => {
94 write!(f, "In {}:{}-{}: {quote}", start_line, start_col, end_col)
95 }
96 }
97 }
98 }
99 }
100}