From bd8f493406bd2210c1763f21cf2909ef4cc58a75 Mon Sep 17 00:00:00 2001 From: Alex Habich Date: Tue, 16 Jul 2024 15:32:57 -0500 Subject: [PATCH] Implement rust-style #import --- src/imports.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/imports.rs b/src/imports.rs index fdc3412..c70c6c4 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -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. @@ -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; } @@ -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) });