use microcad_lang_base::{ComputedHash, Hashed, LineIndex, Span, SrcRef, Url};
#[derive(Debug)]
pub enum ParseContext<'source> {
Element(Hashed<&'source str>),
Source {
url: Url,
line_index: LineIndex,
line_offset: u32,
code: Hashed<&'source str>,
},
}
impl<'source> ParseContext<'source> {
pub fn new(source: &'source str) -> Self {
Self::Element(Hashed::new(source))
}
pub fn with_url(self, url: Url) -> Self {
match self {
Self::Source {
line_index,
line_offset,
code,
..
} => Self::Source {
url,
code,
line_index,
line_offset,
},
Self::Element(code) => Self::Source {
line_index: LineIndex::new(&code),
url,
code,
line_offset: 0,
},
}
}
pub fn with_line_offset(self, line_offset: u32) -> Self {
match self {
Self::Source {
url,
line_index,
code: source,
..
} => Self::Source {
url,
code: source,
line_index,
line_offset,
},
Self::Element(source) => Self::Source {
line_index: LineIndex::new(&source),
url: microcad_lang_base::virtual_url(&format!("source_{}", source.computed_hash())),
code: source,
line_offset,
},
}
}
pub fn src_ref(&self, span: &Span) -> SrcRef {
match self {
Self::Source {
line_index,
line_offset,
code: source,
..
} => line_index
.src_ref(source.value(), span, source.computed_hash())
.with_line_offset(*line_offset),
Self::Element(source) => {
SrcRef::new(span.clone(), Default::default(), source.computed_hash())
}
}
}
}
impl<'source> From<&'source microcad_lang_base::Source> for ParseContext<'source> {
fn from(source: &'source microcad_lang_base::Source) -> Self {
Self::Source {
url: source.url.clone(),
line_index: LineIndex::new(&source.code),
line_offset: source.line_offset,
code: Hashed::new(source.code.value()),
}
}
}