Initial Commit
This commit is contained in:
782 files changed
+145156
No files matched your search
@@ -0,0 +1,81 @@
|
||||
#! @file highlights.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the names of a file as what the checker made of each, for an editor
|
||||
#! to colour them by that rather than by how they are written.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! A name of a file, as what the checker made of it.
|
||||
pub struct highlight {
|
||||
pub var offset: u32; #! Where the name begins in the file's text.
|
||||
pub var length: u32; #! How many bytes it covers.
|
||||
pub var kind: symbol; #! What it is.
|
||||
pub var declared: bool; #! Whether this is where it is declared.
|
||||
pub var readonly: bool; #! Whether what it names is never written, as a `val` or a `const`.
|
||||
}
|
||||
|
||||
#! The names of the file the checker resolved, in the order they are written,
|
||||
#! each with what it is, so an editor colours the rest as it otherwise would. A
|
||||
#! file whose syntax has mistakes gives the names of what parsed.
|
||||
#! @param this The checker.
|
||||
#! @param file The file.
|
||||
#! @returns The names.
|
||||
pub def checker::highlights(&mut this, file: u32): vector<highlight> {
|
||||
var made := vector::<highlight>::new();
|
||||
val syntax := &this.units[file].syntax;
|
||||
val asked := this.begin_query(file);
|
||||
val walked := &asked.walked;
|
||||
for &node in walked.order {
|
||||
val token: u32 = syntax.node_tokens[node];
|
||||
# A contract's word, `old` within an `ensures`, and `undefined` as a
|
||||
# variable's value are the language's own words there, though names
|
||||
# everywhere else.
|
||||
val held := syntax.node_kinds[node];
|
||||
if held == nk::contract || held == nk::old_value || held == nk::undefined {
|
||||
made.append(highlight {
|
||||
offset = syntax.token_offsets[token], length = syntax.token_length(token, this.names), kind = symbol::keyword
|
||||
});
|
||||
continue;
|
||||
}
|
||||
continue if syntax.token_kinds[token] != tk::name;
|
||||
val meant := this.meaning_of(node, walked, &asked.decls);
|
||||
continue if !meant.found();
|
||||
var declared := meant.local == node && meant.file == file;
|
||||
if meant.decl != 0 {
|
||||
declared = this.decls.nodes.p[meant.decl] == node && this.decls.files.p[meant.decl] == file;
|
||||
}
|
||||
if meant.is_module {
|
||||
val outer: u32 = walked.parents[walked.parents[node]];
|
||||
declared = outer != 0 && (syntax.node_kinds[outer] == nk::module_decl || syntax.node_kinds[outer] == nk::extern_module)
|
||||
|| (walked.parents[node] != 0 && (syntax.node_kinds[walked.parents[node]] == nk::module_decl
|
||||
|| syntax.node_kinds[walked.parents[node]] == nk::extern_module));
|
||||
}
|
||||
# A type named where it is used is never written there.
|
||||
val kind := this.symbol_meant(&meant);
|
||||
val names_type := kind == symbol::struct_type || kind == symbol::trait_type || kind == symbol::enum_type
|
||||
|| kind == symbol::type_name || kind == symbol::type_param;
|
||||
made.append(highlight {
|
||||
offset = syntax.token_offsets[token],
|
||||
length = syntax.token_length(token, this.names),
|
||||
kind = kind,
|
||||
declared = declared,
|
||||
readonly = this.readonly_meant(&meant) || (names_type && !declared)
|
||||
});
|
||||
this.query_file(file);
|
||||
}
|
||||
|
||||
# A receiver is written before the name of what it declares, as `rect` is in
|
||||
# `def rect::area`, so the names are put in the order they are written, and a
|
||||
# name the walk reached twice is one.
|
||||
made.sort_by(def(a: &highlight, b: &highlight): bool { return a.offset < b.offset; });
|
||||
var kept := vector::<highlight>::new();
|
||||
for &each in made {
|
||||
continue if kept.length != 0 && kept.last.offset == each.offset;
|
||||
kept.append(each);
|
||||
}
|
||||
return kept;
|
||||
}
|
||||
Reference in new issue
Block a user