Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement rust-style #import #14

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ use crate::{
};

lazy_static::lazy_static! {
static ref IMPORT_CUSTOM_PATH_REGEX: Regex = Regex::new(r"(?:^|\n)\s*#\s*import\s+([^\s]+)").unwrap();
static ref IMPORT_CUSTOM_PATH_AS_REGEX: Regex = Regex::new(r"(?:^|\n)\s*#\s*import\s+([^\s]+)\s+as\s+([^\s]+)").unwrap();
static ref IMPORT_ITEMS_REGEX: Regex = Regex::new(r"(?:^|\n)\s*#\s*import\s+([^\s]+)\s+((?:[\w|\d|_]+)(?:\s*,\s*[\w|\d|_]+)*)").unwrap();
static ref IMPORT_CUSTOM_PATH_REGEX: Regex = Regex::new(r"(?:^|\n)\s*#\s*import\s+([^\s]+\.wgsl)").unwrap();
static ref IMPORT_CUSTOM_PATH_AS_REGEX: Regex = Regex::new(r"(?:^|\n)\s*#\s*import\s+([^\s]+\.wgsl)\s+as\s+([^\s]+)").unwrap();
static ref IMPORT_ITEMS_REGEX: Regex = Regex::new(r"(?:^|\n)\s*#\s*import\s+([^\s]+\.wgsl)\s+([^\s]+(?:\s*,\s*[^\s]+)*)").unwrap();
static ref IMPORT_SINGLE_ITEM_REGEX: Regex = Regex::new(r"(?:^|\n)\s*#\s*import\s+([^\s]+\.wgsl)\s*::\s*([^\s]+)").unwrap();
static ref IMPORT_ITEMS_BRACKETS_REGEX: Regex = Regex::new(r"(?:^|\n)\s*#\s*import\s+([^\s]+\.wgsl)\s*::\s*\{\s*([^\s]+(?:\s*,\s*[^\s]+)*)\s*\}").unwrap();
}

/// Finds an arbitrary path between two nodes in a dag.
Expand All @@ -41,6 +43,12 @@ fn all_imports_in_source<'a>(source: &'a str) -> HashSet<&'a str> {
for import in IMPORT_ITEMS_REGEX.captures_iter(&source) {
requirements.insert(import.get(1).unwrap().as_str());
}
for import in IMPORT_SINGLE_ITEM_REGEX.captures_iter(&source) {
requirements.insert(import.get(1).unwrap().as_str());
}
for import in IMPORT_ITEMS_BRACKETS_REGEX.captures_iter(&source) {
requirements.insert(import.get(1).unwrap().as_str());
}
return requirements;
}

Expand All @@ -58,7 +66,10 @@ fn replace_import_names_in_source<'a>(
None => return full.to_owned(),
};

let sub = format!("{:^len$}", sub, len = name.len());
// Right alignment is needed for naga_oil to correctly parse rust-style imports:
// `#import foo.wgsl::bar` will become `#import foo::bar`
// naga_oil does not support spaces between import items
let sub = format!("{:>len$}", sub, len = name.len());

capture.get(0).unwrap().as_str().replace(name, &sub)
});
Expand Down
Loading