Skip to content

Commit

Permalink
Passing import data around
Browse files Browse the repository at this point in the history
  • Loading branch information
neunenak committed Jun 21, 2023
1 parent 47e23da commit da34121
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@ const VALID_ALIAS_ATTRIBUTES: [Attribute; 1] = [Attribute::Private];

#[derive(Debug)]
pub(crate) struct Import {
/// Path of the import as literally given within the `!include` directive
path: PathBuf,

// Canonicalized version of the above path
canonical_path: Option<PathBuf>,

/// The line on which the `!include` directive appears in the original file
line: usize,
}

impl Import {
pub(crate) fn path(&self) -> &Path {
self.path.as_ref()
}

pub(crate) fn add_canonical_path(&mut self, canonical_path: PathBuf) {
self.canonical_path = Some(canonical_path);
}
}

#[derive(Default)]
Expand All @@ -31,6 +41,7 @@ impl<'src> Analyzer<'src> {
if let Item::Include { name, path } = item {
Some(Import {
path: Path::new(path).to_owned(),
canonical_path: None,
line: name.line,
})
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ impl Compiler {
#[derive(Debug)]
pub(crate) struct AstImport<'src> {
pub(crate) ast: Ast<'src>,
canonical_path: PathBuf,
import: Import,
}

impl<'src> AstImport<'src> {
pub(crate) fn new(ast: Ast<'src>, canonical_path: PathBuf) -> Self {
pub(crate) fn new(ast: Ast<'src>, import: Import) -> Self {
Self {
ast,
canonical_path,
import,
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Loader {
None => break,
};

for import in imports {
for mut import in imports.into_iter() {
let given_path = import.path();
let canonical_path = Self::canonicalize_path(&given_path, &cur_path)?;

Expand All @@ -73,7 +73,8 @@ impl Loader {
let src = self.load_and_alloc(&canonical_path)?;
let ast = Compiler::parse(src)?;
queue.push_back((canonical_path.clone(), Analyzer::get_imports(&ast)));
let ast_meta = AstImport::new(ast, canonical_path);
import.add_canonical_path(canonical_path);
let ast_meta = AstImport::new(ast, import);
imported_asts.push(ast_meta);
}
}
Expand Down

0 comments on commit da34121

Please sign in to comment.