Files

113 lines
4.7 KiB
Plaintext
Raw Permalink Normal View History

2026-10-26 10:14:11 -05:00
#! @file note.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains `note`, a diagnostic the lexer, parser, or checker makes of what is
#! wrong with a file, the message it reads as, and `note_sink`, where each of
#! them puts the notes it makes.
module sakura::analyzer;
#! A diagnostic reported while including or checking source files.
#!
#! It holds its text, so it is good for as long as it is kept, whatever made
#! it, and it has one owner: it is moved, as from where it was made to where it
#! is kept, rather than copied. This information can be used to present
#! warnings and errors for source code that failed stages of compilation.
pub struct note {
pub var file: u32; #! The index of the file it is about.
pub var offset: u32; #! Where what it is about begins in the source text.
pub var length: u32; #! How many bytes of the source text it is about, for underlining them.
pub var code: ec; #! What kind of mistake it is about.
pub var warning: bool; #! Whether it is a warning, which never stops a program, rather than an error.
var detail: string; #! What it says.
var found: bool; #! Whether it says what was expected, so the text it is about was found instead.
var lexed: bool; #! Whether the lexer made it, rather than the parser.
# The instance of a generic the checker found it in, as "In `max<u8>`", which
# is empty where it was found in what was written.
pub var instance: string; #! The instance of a generic it was found in, as "In `max<u8>`", or empty.
# A place the note refers to besides its own, as where a name was first
# declared, and what it says of it. Its detail is empty where there is none.
pub var related_file: u32; #! The file of a place it refers to besides its own.
pub var related_offset: u32; #! Where that place begins.
pub var related_length: u32; #! How many bytes it covers.
pub var related: string; #! What it says of that place, or empty where there is none.
# The declaration the checker found it checking, whose notes are made again
# where it is checked again, or 0.
var origin: u32; #! The declaration being checked where it was made, or 0.
}
#! What the note says, with what was found where it expected something else, as
#! "Expected `;` after the statement. Found `}`."
#! @param this The note.
#! @param source The source the note is about.
#! @returns The message.
pub def note::message(&this, source: &source): string {
var made := string::copy_of(&this.detail);
return made if !this.found;
if this.length == 0 {
made.append(" Found the end of the file.");
return made;
}
made.append(" Found `");
made.append(source.make_lexeme(this.offset, this.length));
made.append("`.");
return made;
}
#! Where the lexer, the parser, or the checker puts the notes it makes: kept in
#! a vector of notes, or handed to what hears them as they are made, up to the
#! most notes it makes.
#!
#! A note is admitted before it is made, which counts it, so one past the limit
#! is never made at all, nor its text copied; one admitted is then given, to
#! what hears notes where something does, or kept.
pub struct note_sink {
var notes: &mut vector<note>; #! Where notes are kept, where nothing hears them.
pub var heard: maybe<def(&mut note): void>; #! What hears each note as it is made, which may move it, as where another thread reports them, or nothing.
pub var count: u32; #! How many notes were made, which `limit` bounds.
val limit: u32; #! The most notes made.
}
#! Makes a sink keeping notes in `notes`, which nothing hears yet.
#! @param notes Where the notes are kept.
#! @param limit The most notes made.
#! @returns The sink.
pub def note_sink::new(notes: &mut vector<note>, limit: u32): note_sink {
return note_sink {
notes = notes,
limit = limit
};
}
#! Whether as many notes were made as the limit allows.
#! @param this The sink.
#! @returns Whether they were.
pub val note_sink::full: bool { get => this.count >= this.limit; }
#! Counts a note about to be made, unless as many were made as the limit allows.
#! @param this The sink.
#! @returns Whether the note is to be made.
pub def note_sink::admits(&mut this): bool {
return false if this.count >= this.limit;
this.count++;
return true;
}
#! Gives a note admitted to what hears notes, or keeps it where nothing does.
#! @param this The sink.
#! @param made The note, which is moved from.
pub def note_sink::give(&mut this, made: &mut note): void {
if this.heard is some(hear) {
hear(made);
return;
}
this.notes.append(move(made));
}