243 lines
8.9 KiB
Plaintext
243 lines
8.9 KiB
Plaintext
#! @file notes.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains how the checker reports what is wrong: at a node of the file being
|
|
#! checked, or of another, with the instance of a generic it was found in and
|
|
#! a related place where there is one. A note holds a copy of its text, made
|
|
#! once the note is admitted, so text made for a note that is dropped, as one
|
|
#! made while an argument is tried quietly, is let go of with what made it.
|
|
|
|
module sakura::analyzer;
|
|
|
|
|
|
#! Reports what is wrong at the node of the file being checked.
|
|
#! @param this The checker.
|
|
#! @param node The node.
|
|
#! @param code What kind of mistake it is.
|
|
#! @param detail What the note says.
|
|
pub def checker::report(&mut this, node: u32, code: ec, detail: strview): void {
|
|
this.report_in(this.at.file, node, code, detail);
|
|
}
|
|
|
|
#! Reports what is wrong at the node, with text made for it.
|
|
#! @param this The checker.
|
|
#! @param node The node.
|
|
#! @param code What kind of mistake it is.
|
|
#! @param text What the note says, which the note copies.
|
|
pub def checker::report_text(&mut this, node: u32, code: ec, text: &string): void {
|
|
this.report_in(this.at.file, node, code, text.view());
|
|
}
|
|
|
|
#! Reports what is wrong at the node of the file.
|
|
#! @param this The checker.
|
|
#! @param file The file.
|
|
#! @param node The node.
|
|
#! @param code What kind of mistake it is.
|
|
#! @param detail What the note says.
|
|
pub def checker::report_in(&mut this, file: u32, node: u32, code: ec, detail: strview): void {
|
|
this.report_place(file, node, code, detail, 0, 0, 0, "");
|
|
}
|
|
|
|
#! Reports what is wrong at the declaration, in its own file, wherever checking
|
|
#! is.
|
|
#! @param this The checker.
|
|
#! @param id The declaration.
|
|
#! @param code What kind of mistake it is.
|
|
#! @param detail What the note says.
|
|
pub def checker::report_decl(&mut this, id: u32, code: ec, detail: strview): void {
|
|
this.report_in(this.decls.files.p[id], this.decls.nodes.p[id], code, detail);
|
|
}
|
|
|
|
#! Reports what is wrong at the node of the file, and a place it relates to,
|
|
#! where it says something of one: to whatever hears the checker's notes, or in
|
|
#! its notes.
|
|
#! @param this The checker.
|
|
#! @param file The file.
|
|
#! @param node The node.
|
|
#! @param code What kind of mistake it is.
|
|
#! @param detail What the note says.
|
|
#! @param related_file The file of the place it relates to.
|
|
#! @param related_offset Where that place begins.
|
|
#! @param related_length How many bytes it covers.
|
|
#! @param related What the note says of it, or empty where it relates
|
|
#! to none.
|
|
def checker::report_place(
|
|
&mut this,
|
|
file: u32,
|
|
node: u32,
|
|
code: ec,
|
|
detail: strview,
|
|
related_file: u32,
|
|
related_offset: u32,
|
|
related_length: u32,
|
|
related: strview
|
|
): void {
|
|
if this.mute != 0 {
|
|
this.dropped++;
|
|
if code == ec::generic && detail.starts_with("Nothing gives") {
|
|
this.uninferred++;
|
|
}
|
|
return;
|
|
}
|
|
this.failed = true;
|
|
this.errors++;
|
|
return if !this.sink.admits();
|
|
|
|
val syntax := &this.units[file].syntax;
|
|
val token := syntax.node_tokens[node];
|
|
var made := note {
|
|
file = file,
|
|
offset = syntax.token_offsets[token],
|
|
length = syntax.token_length(token, this.names),
|
|
code = code,
|
|
detail = string::from(detail, nothing {}),
|
|
instance = this.instance_text(),
|
|
related_file = related_file,
|
|
related_offset = related_offset,
|
|
related_length = related_length,
|
|
related = string::from(related, nothing {}),
|
|
origin = this.at.decl
|
|
};
|
|
this.sink.give(&mut made);
|
|
}
|
|
|
|
#! Warns of what is likely wrong at the node of the file being checked, which
|
|
#! never stops a program, nor marks what is checked as failed.
|
|
#! @param this The checker.
|
|
#! @param node The node.
|
|
#! @param code What kind of mistake it is about.
|
|
#! @param detail What the note says.
|
|
pub def checker::warn(&mut this, node: u32, code: ec, detail: strview): void {
|
|
this.warn_place(node, code, detail, 0);
|
|
}
|
|
|
|
#! Warns of what is likely wrong at the node, with text made for it, and the
|
|
#! node declaring what it is about, where one is given.
|
|
#! @param this The checker.
|
|
#! @param node The node.
|
|
#! @param code What kind of mistake it is about.
|
|
#! @param text What the note says, which the note copies.
|
|
#! @param related The node declaring what it is about, in the same file, or 0.
|
|
pub def checker::warn_text(&mut this, node: u32, code: ec, text: &string, related: u32): void {
|
|
this.warn_place(node, code, text.view(), related);
|
|
}
|
|
|
|
#! Warns of what is likely wrong at the node: to whatever hears the checker's
|
|
#! notes, or in its notes.
|
|
#! @param this The checker.
|
|
#! @param node The node.
|
|
#! @param code What kind of mistake it is about.
|
|
#! @param detail What the note says.
|
|
#! @param related The node declaring what it is about, in the same file, or 0.
|
|
def checker::warn_place(&mut this, node: u32, code: ec, detail: strview, related: u32): void {
|
|
return if this.mute != 0 || !this.sink.admits();
|
|
val file := this.at.file;
|
|
val syntax := &this.units[file].syntax;
|
|
val token := syntax.node_tokens[node];
|
|
var made := note {
|
|
file = file,
|
|
offset = syntax.token_offsets[token],
|
|
length = syntax.token_length(token, this.names),
|
|
code = code,
|
|
detail = string::from(detail, nothing {}),
|
|
warning = true,
|
|
instance = this.instance_text(),
|
|
origin = this.at.decl
|
|
};
|
|
if related != 0 {
|
|
val other := syntax.node_tokens[related];
|
|
made.related_file = file;
|
|
made.related_offset = syntax.token_offsets[other];
|
|
made.related_length = syntax.token_length(other, this.names);
|
|
made.related = string::from("It is declared here.", nothing {});
|
|
}
|
|
this.sink.give(&mut made);
|
|
}
|
|
|
|
#! Reports what is wrong at the node, and the place of another it relates to,
|
|
#! as where a name was first declared.
|
|
#! @param this The checker.
|
|
#! @param node The node.
|
|
#! @param code What kind of mistake it is.
|
|
#! @param detail What the note says.
|
|
#! @param file The file of the other node.
|
|
#! @param other The other node.
|
|
#! @param related What the note says of it.
|
|
pub def checker::report_related(
|
|
&mut this,
|
|
node: u32,
|
|
code: ec,
|
|
detail: strview,
|
|
file: u32,
|
|
other: u32,
|
|
related: strview
|
|
): void {
|
|
this.report_related_in(this.at.file, node, code, detail, file, other, related);
|
|
}
|
|
|
|
#! Reports what is wrong at the declaration, in its own file, wherever checking
|
|
#! is, and the place of another declaration it relates to, as where the name
|
|
#! was first declared.
|
|
#! @param this The checker.
|
|
#! @param id The declaration.
|
|
#! @param code What kind of mistake it is.
|
|
#! @param detail What the note says.
|
|
#! @param other The other declaration.
|
|
#! @param related What the note says of it.
|
|
pub def checker::report_decl_related(&mut this, id: u32, code: ec, detail: strview, other: u32, related: strview): void {
|
|
this.report_related_in(this.decls.files.p[id], this.decls.nodes.p[id], code, detail,
|
|
this.decls.files.p[other], this.decls.nodes.p[other], related);
|
|
}
|
|
|
|
#! Reports what is wrong at the node of the file, and the place of another node
|
|
#! it relates to.
|
|
#! @param this The checker.
|
|
#! @param file The file.
|
|
#! @param node The node.
|
|
#! @param code What kind of mistake it is.
|
|
#! @param detail What the note says.
|
|
#! @param other_file The file of the other node.
|
|
#! @param other The other node.
|
|
#! @param related What the note says of it.
|
|
def checker::report_related_in(
|
|
&mut this,
|
|
file: u32,
|
|
node: u32,
|
|
code: ec,
|
|
detail: strview,
|
|
other_file: u32,
|
|
other: u32,
|
|
related: strview
|
|
): void {
|
|
val syntax := &this.units[other_file].syntax;
|
|
val token := syntax.node_tokens[other];
|
|
this.report_place(file, node, code, detail, other_file, syntax.token_offsets[token], syntax.token_length(token, this.names), related);
|
|
}
|
|
|
|
#! What notes made in the instance being checked say of it, as "In `max<u8>`",
|
|
#! or nothing outside an instance.
|
|
#! @param this The checker.
|
|
#! @returns The text, or empty text.
|
|
def checker::instance_text(&mut this): string {
|
|
return string::new(nothing {}) if this.at.instance == 0;
|
|
|
|
val held: instance = this.instances[this.at.instance - 1];
|
|
var text := string::from("In `", nothing {});
|
|
text.append(this.names.text(this.decls.names.p[held.decl]));
|
|
text.append("<");
|
|
# A pack's types are written as the arguments they are, as `show_all<i64, bool>`.
|
|
this.describe_args(this.types.parts_view(held.args), &mut text);
|
|
text.append(">`");
|
|
return text;
|
|
}
|
|
|
|
#! Counts what the checker met and does not check yet, which it lets through
|
|
#! rather than reporting, so a program using it is not wrongly refused.
|
|
#! @param this The checker.
|
|
#! @param node The node.
|
|
pub def checker::unsupported(&mut this, node: u32): void {
|
|
this.skipped++;
|
|
}
|