Add //#include macro
You can now include WGSL files in other WGSL files
This commit is contained in:
parent
5c208a73ec
commit
2ce14e25d8
|
|
@ -10,3 +10,4 @@ proc-macro = true
|
||||||
quote = "1.0"
|
quote = "1.0"
|
||||||
syn = "2.0.90"
|
syn = "2.0.90"
|
||||||
naga = { version = "23.0.0", features = ["wgsl-in"] }
|
naga = { version = "23.0.0", features = ["wgsl-in"] }
|
||||||
|
regex = "1.11.1"
|
||||||
|
|
@ -1,55 +1,78 @@
|
||||||
use {
|
use {
|
||||||
crate::Error,
|
crate::Error,
|
||||||
|
regex::Regex,
|
||||||
std::{
|
std::{
|
||||||
fs,
|
fs,
|
||||||
|
io::{BufRead, BufReader},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SourceMap {
|
pub struct SourceMap {
|
||||||
filename: PathBuf,
|
lines: Vec<String>,
|
||||||
offset: usize,
|
files: Vec<PathBuf>,
|
||||||
total_lines: usize,
|
line_map: Vec<(usize, usize)>,
|
||||||
insertions: Vec<SourceMap>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SourceFile {
|
impl SourceMap {
|
||||||
source_text: String,
|
|
||||||
source_map: SourceMap,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SourceFile {
|
|
||||||
pub fn root_filename(&self) -> &Path {
|
pub fn root_filename(&self) -> &Path {
|
||||||
&self.source_map.filename
|
&self.files[0]
|
||||||
}
|
}
|
||||||
pub fn full_text(&self) -> &str {
|
pub fn full_text(&self) -> String {
|
||||||
&self.source_text
|
self.lines.join("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn map_source_line_num(&self, line_num: usize) -> (&Path, usize) {
|
pub fn map_source_line_num(&self, line_num: usize) -> (&Path, usize) {
|
||||||
(&self.source_map.filename, line_num)
|
let (source_file_index, source_file_line_num) = self.line_map[line_num];
|
||||||
|
(&self.files[source_file_index], source_file_line_num)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn all_filenames(&self) -> Vec<&Path> {
|
pub fn all_filenames(&self) -> &Vec<PathBuf> {
|
||||||
vec![&self.source_map.filename]
|
&self.files
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_source_file(source_dir: &Path, filename: &Path) -> Result<SourceFile, Error> {
|
fn ingest_file(
|
||||||
let source_text = fs::read_to_string(&source_dir.join(filename)).map_err(|err| {
|
source_dir: &Path,
|
||||||
|
filename: &Path,
|
||||||
|
source_map: &mut SourceMap,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
let file = fs::File::open(source_dir.join(filename)).map_err(|err| {
|
||||||
Error::from_message(format!(
|
Error::from_message(format!(
|
||||||
"Could not open \"{}\": {}",
|
"Could not open \"{}\": {}",
|
||||||
&filename.as_os_str().to_string_lossy(),
|
&filename.as_os_str().to_string_lossy(),
|
||||||
err
|
err
|
||||||
))
|
))
|
||||||
})?;
|
})?;
|
||||||
Ok(SourceFile {
|
let file_num = source_map.files.len();
|
||||||
source_text,
|
source_map.files.push(filename.to_path_buf());
|
||||||
source_map: SourceMap {
|
let var_name = Regex::new(r"^//\#include\s+(.*)\s*$");
|
||||||
filename: filename.into(),
|
let include_regex = var_name.unwrap();
|
||||||
offset: 0,
|
for (line_num, line) in BufReader::new(file).lines().enumerate() {
|
||||||
total_lines: 0,
|
let line = line.map_err(|err| {
|
||||||
insertions: vec![],
|
Error::from_message(format!(
|
||||||
},
|
"Error reading \"{}\": {}",
|
||||||
})
|
&filename.as_os_str().to_string_lossy(),
|
||||||
|
err
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
if let Some(captures) = include_regex.captures(&line) {
|
||||||
|
let nested_filename = captures.get(1).unwrap().as_str();
|
||||||
|
ingest_file(source_dir, Path::new(nested_filename), source_map)?;
|
||||||
|
} else {
|
||||||
|
source_map.lines.push(line);
|
||||||
|
source_map.line_map.push((file_num, line_num));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_source_file(source_dir: &Path, filename: &Path) -> Result<SourceMap, Error> {
|
||||||
|
let mut source_map = SourceMap {
|
||||||
|
lines: vec![],
|
||||||
|
files: vec![],
|
||||||
|
line_map: vec![],
|
||||||
|
};
|
||||||
|
ingest_file(source_dir, filename, &mut source_map)?;
|
||||||
|
Ok(source_map)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue