Skip to main content

microcad_lang_parse/ast/
source.rs

1// Copyright © 2026 The µcad authors <info@microcad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! µcad source API
5
6use microcad_lang_base::{
7    ComputedHash, GetSourceLocInfoByHash, Hashed, Refer, SourceLocInfo, SrcRef, SrcReferrer, Url,
8};
9
10use crate::ast;
11
12/// A µcad source with a parse syntax tree with a line offset and the hashed original source code.
13pub struct Source {
14    /// The source url
15    pub url: Url,
16    /// Line offset
17    pub line_offset: u32,
18    /// The original code
19    pub code: Hashed<String>,
20    /// The µcad program
21    pub ast: Refer<ast::Program>,
22}
23
24impl SrcReferrer for Source {
25    fn src_ref(&self) -> SrcRef {
26        self.ast.src_ref()
27    }
28}
29
30impl GetSourceLocInfoByHash for Source {
31    fn get_source_loc_info_by_hash(
32        &'_ self,
33        hash: microcad_lang_base::HashId,
34    ) -> Option<microcad_lang_base::SourceLocInfo<'_>> {
35        if hash == self.code.computed_hash() {
36            Some(SourceLocInfo {
37                code: &self.code,
38                url: self.url.clone(),
39                line_offset: self.line_offset,
40            })
41        } else {
42            None
43        }
44    }
45}