-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource_map.rs
More file actions
28 lines (24 loc) · 796 Bytes
/
Copy pathsource_map.rs
File metadata and controls
28 lines (24 loc) · 796 Bytes
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
use serde::{Deserialize, Serialize};
use techscript_common::Span;
/// Source code mappings resolving runtime offsets back to code spans.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct SourceMap {
pub mappings: Vec<(u32, Span)>,
}
impl SourceMap {
/// Creates a new empty SourceMap.
pub fn new() -> Self {
Self::default()
}
/// Registers a span location for the bytecode instruction offset.
pub fn add(&mut self, offset: u32, span: Span) {
self.mappings.push((offset, span));
}
/// Resolves instruction offset back to span.
pub fn resolve(&self, offset: u32) -> Option<Span> {
self.mappings
.iter()
.find(|&&(off, _)| off == offset)
.map(|&(_, span)| span)
}
}