pub struct SourceFile {
pub name: String,
pub text: String,
/* private fields */
}Expand description
One source file: its name and text.
Fields§
§name: StringThe name the file was opened with, for diagnostics.
text: StringThe full text.
Implementations§
Source§impl SourceFile
impl SourceFile
Sourcepub fn line_column(&self, offset: u32) -> (usize, usize)
pub fn line_column(&self, offset: u32) -> (usize, usize)
The 1-based line and column of a byte offset, with the line numbers that
#line gives.
Examples found in repository?
examples/milkdrop_corpus.rs (line 54)
15fn main() {
16 let mut args = std::env::args().skip(1);
17 let Some(root) = args.next() else {
18 eprintln!("usage: milkdrop_corpus <preset folder> [examples per error]");
19 std::process::exit(2);
20 };
21 let examples: usize = args.next().and_then(|n| n.parse().ok()).unwrap_or(1);
22 let mut presets = Vec::new();
23 collect(Path::new(&root), &mut presets);
24 presets.sort();
25
26 let started = Instant::now();
27 let (mut shaders, mut failed) = (0usize, 0usize);
28 let (mut max_parens, mut max_braces) = (0usize, 0usize);
29 let mut groups: BTreeMap<String, Vec<String>> = BTreeMap::new();
30 for path in &presets {
31 let Ok(bytes) = std::fs::read(path) else {
32 continue;
33 };
34 let text = String::from_utf8_lossy(&bytes);
35 for (kind, shader) in shaders_of(&text) {
36 shaders += 1;
37 let source =
38 until_last_brace(&shader).replace("shader_body", "void shader_body_main()");
39 let (parens, braces) = nesting(&source);
40 max_parens = max_parens.max(parens);
41 max_braces = max_braces.max(braces);
42 let name = format!("{} [{kind}]", path.display());
43 let parsed = parse(&name, &source, Options::default());
44 let Some(first) = parsed.diagnostics.first() else {
45 continue;
46 };
47 failed += 1;
48 let group = groups.entry(group_of(&first.message)).or_default();
49 if group.len() < examples {
50 let line = parsed
51 .files
52 .get(first.span.file)
53 .map(|f| {
54 let (line, _) = f.line_column(first.span.start);
55 f.text
56 .lines()
57 .nth(line - 1)
58 .unwrap_or("")
59 .trim()
60 .to_string()
61 })
62 .unwrap_or_default();
63 group.push(format!("{}\n {line}", first.display(&parsed.files)));
64 } else {
65 group.push(String::new());
66 }
67 }
68 }
69
70 let mut ranked: Vec<_> = groups.into_iter().collect();
71 ranked.sort_by_key(|(_, v)| std::cmp::Reverse(v.len()));
72 for (message, hits) in &ranked {
73 println!("{:>6} {message}", hits.len());
74 for example in hits.iter().filter(|e| !e.is_empty()) {
75 println!(" {example}");
76 }
77 }
78 println!(
79 "{} presets, {shaders} shaders, {} parsed, {failed} failed ({:.2}% parsed) in {:.2?}",
80 presets.len(),
81 shaders - failed,
82 100.0 * (shaders - failed) as f64 / shaders.max(1) as f64,
83 started.elapsed()
84 );
85 println!("deepest nesting: {max_parens} parentheses, {max_braces} braces");
86}Sourcepub fn included_from(&self) -> Option<Span>
pub fn included_from(&self) -> Option<Span>
The #include directive that opened the file, if one did.
Trait Implementations§
Source§impl Clone for SourceFile
impl Clone for SourceFile
Source§impl Debug for SourceFile
impl Debug for SourceFile
impl Eq for SourceFile
Source§impl PartialEq for SourceFile
impl PartialEq for SourceFile
impl StructuralPartialEq for SourceFile
Auto Trait Implementations§
impl Forget for SourceFile
impl Freeze for SourceFilewhere
*const u8 is !null: Freeze,
PhantomData<u8>: Freeze,
PhantomData<u32>: Freeze,
PhantomData<LineMark>: Freeze,
impl Move for SourceFile
impl RefUnwindSafe for SourceFile
impl Send for SourceFilewhere
RawVecInner: Send,
impl Sync for SourceFilewhere
RawVecInner: Sync,
impl Unpin for SourceFile
impl UnsafeUnpin for SourceFilewhere
*const u8 is !null: UnsafeUnpin,
PhantomData<u8>: UnsafeUnpin,
PhantomData<u32>: UnsafeUnpin,
PhantomData<LineMark>: UnsafeUnpin,
impl UnwindSafe for SourceFilewhere
RawVecInner: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more