oxilean_parse/source_map/
types.rs1use std::fmt;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub struct SourceId(pub u32);
8
9impl fmt::Display for SourceId {
10 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11 write!(f, "SourceId({})", self.0)
12 }
13}
14
15#[derive(Debug, Clone, PartialEq, Eq, Hash)]
17pub struct Span {
18 pub source: SourceId,
20 pub start: usize,
22 pub end: usize,
24}
25
26impl Span {
27 pub fn new(source: SourceId, start: usize, end: usize) -> Self {
29 Self { source, start, end }
30 }
31
32 pub fn len(&self) -> usize {
34 self.end.saturating_sub(self.start)
35 }
36
37 pub fn is_empty(&self) -> bool {
39 self.start >= self.end
40 }
41}
42
43impl fmt::Display for Span {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 write!(f, "{}:{}..{}", self.source, self.start, self.end)
46 }
47}
48
49#[derive(Debug, Clone, PartialEq, Eq)]
51pub enum SourceKind {
52 File,
54 MacroExpansion {
56 macro_name: String,
58 expansion_site: Span,
60 },
61 Generated {
63 generator: String,
65 },
66 Repl,
68}
69
70impl fmt::Display for SourceKind {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 match self {
73 SourceKind::File => write!(f, "file"),
74 SourceKind::MacroExpansion { macro_name, .. } => {
75 write!(f, "macro expansion of `{}`", macro_name)
76 }
77 SourceKind::Generated { generator } => write!(f, "generated by `{}`", generator),
78 SourceKind::Repl => write!(f, "repl"),
79 }
80 }
81}
82
83#[derive(Debug, Clone)]
85pub struct SourceInfo {
86 pub id: SourceId,
88 pub name: String,
90 pub content: String,
92 pub kind: SourceKind,
94}
95
96#[derive(Debug, Clone, Default)]
98pub struct SpanChain {
99 pub spans: Vec<Span>,
101}
102
103impl SpanChain {
104 pub fn new() -> Self {
106 Self { spans: Vec::new() }
107 }
108
109 pub fn push(&mut self, span: Span) {
111 self.spans.push(span);
112 }
113
114 pub fn innermost(&self) -> Option<&Span> {
116 self.spans.last()
117 }
118
119 pub fn outermost(&self) -> Option<&Span> {
121 self.spans.first()
122 }
123
124 pub fn depth(&self) -> usize {
126 self.spans.len()
127 }
128}
129
130#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
132pub struct Position {
133 pub line: u32,
135 pub col: u32,
137}
138
139impl Position {
140 pub fn new(line: u32, col: u32) -> Self {
142 Self { line, col }
143 }
144}
145
146impl fmt::Display for Position {
147 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
148 write!(f, "{}:{}", self.line + 1, self.col + 1)
149 }
150}
151
152#[derive(Debug, Clone, PartialEq, Eq, Hash)]
154pub struct LineColumn {
155 pub source: SourceId,
157 pub position: Position,
159}
160
161impl LineColumn {
162 pub fn new(source: SourceId, line: u32, col: u32) -> Self {
164 Self {
165 source,
166 position: Position::new(line, col),
167 }
168 }
169}
170
171impl fmt::Display for LineColumn {
172 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
173 write!(f, "{}:{}", self.source, self.position)
174 }
175}
176
177#[derive(Debug, Default)]
183pub struct SourceMap {
184 pub sources: Vec<SourceInfo>,
186 pub next_id: u32,
188}