neo_solidity/
sourcemap.rs

1//! Source Map Module
2//!
3//! Maps bytecode positions to source code locations.
4
5use crate::error::SourceLocation;
6
7/// Source map entry
8#[derive(Debug, Clone)]
9pub struct SourceMapEntry {
10    pub bytecode_offset: usize,
11    pub source_location: SourceLocation,
12}
13
14/// Source map builder
15#[derive(Default)]
16pub struct SourceMapBuilder {
17    entries: Vec<SourceMapEntry>,
18}
19
20impl SourceMapBuilder {
21    pub fn new() -> Self {
22        Self::default()
23    }
24
25    pub fn add(&mut self, offset: usize, loc: SourceLocation) {
26        self.entries.push(SourceMapEntry {
27            bytecode_offset: offset,
28            source_location: loc,
29        });
30    }
31
32    pub fn build(self) -> SourceMap {
33        SourceMap {
34            entries: self.entries,
35        }
36    }
37}
38
39/// Compiled source map
40pub struct SourceMap {
41    entries: Vec<SourceMapEntry>,
42}
43
44impl SourceMap {
45    pub fn lookup(&self, offset: usize) -> Option<&SourceLocation> {
46        self.entries
47            .iter()
48            .rev()
49            .find(|e| e.bytecode_offset <= offset)
50            .map(|e| &e.source_location)
51    }
52}