#! @file support.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains what the tests share: lexing, parsing, and checking text, printing #! trees, and asking what notes were made. module sakura::analyzer; #! Text that was lexed, with what the lexer reported. struct lexed { var source: source; var names: names; var syntax: syntax; var notes: vector; } #! Text that was lexed and parsed, with what both reported. struct parsed { var source: source; var names: names; var syntax: syntax; var notes: vector; var root: u32; } #! Lexes the text through its `eof`. #! @param text The text. #! @returns Its source, names, tokens, and notes. def lex(text: strview): lexed { var made := lexed { source = source::from("test.sakura", text), names = names::new(), syntax = syntax::new(), notes = vector::::new() }; var lexing := lexer::new(&mut made.source, &mut made.names, &mut made.notes, 0); lexing.tokenize_all(&mut made.syntax); return made; } #! Lexes and parses the text as a file. #! @param text The text. #! @returns Its source, names, syntax, notes, and `document` node. def parse(text: strview): parsed { var made := parsed { source = source::from("test.sakura", text), names = names::new(), syntax = syntax::new(), notes = vector::::new() }; var lexing := lexer::new(&mut made.source, &mut made.names, &mut made.notes, 0); lexing.tokenize_all(&mut made.syntax); var parsing := parser::new(&mut made.syntax, &made.names, &mut made.notes, 0); made.root = parsing.parse(); return made; } #! The kinds of the tokens lexed, without the `eof` ending them. #! @param text The text. #! @returns The kinds. def kinds_of(text: strview): vector { val lexing := lex(text); var kinds := vector::::new(); for &each in lexing.syntax.token_kinds { if each != tk::eof { kinds.append(each); } } return kinds; } #! Whether the tokens lexed are exactly those kinds. #! @param text The text. #! @param kinds The kinds expected, without the `eof`. #! @returns Whether they are. def lexes_as(text: strview, kinds: tk...): bool { val got := kinds_of(text); return false if got.length != kinds.length; for i in 0..got.length { return false if got.at(i) != kinds.at(i); } return true; } #! The tree of a whole file, printed. #! @param text The text. #! @returns The tree, as `printer` writes it. def tree(text: strview): string { val made := parse(text); return made.syntax.print(made.root, &made.source, &made.names); } #! The statements of a function's body, each printed, separated by spaces. #! @param text A file holding the function first. #! @returns The statements. def statements(text: strview): string { var wrapped := string::from("def test(): void {\n", nothing {}); wrapped.append(text); wrapped.append("\n}\n"); val made := parse(wrapped.view()); val function := made.syntax.run_at(made.syntax.node_lefts.at(made.root), 0); val record := made.syntax.node_lefts.at(function); val body := made.syntax.extras.at(record as u64 + 5); val run := made.syntax.node_lefts.at(body); var out := string::new(nothing {}); for i in 0..made.syntax.run_length(run) { if i != 0 { out.append(" "); } out.append(made.syntax.print(made.syntax.run_at(run, i as u32), &made.source, &made.names).view()); } return out; } #! An expression statement's expression, printed. #! @param text A file holding a function whose body is the expression #! statement. #! @returns The expression. def expression(text: strview): string { var wrapped := string::from(text, nothing {}); wrapped.append(";"); val printed := statements(wrapped.view()); val inner := printed.view(); # `(expr_stmt:x EXPR)` without its own head and closing parenthesis. var at := 0u; var spaces := 0u; while at < inner.length && spaces == 0 { if inner.at(at) == ' ' { spaces++; } at++; } return string::from(inner.substr(at, inner.length - at - 1), nothing {}); } #! Whether a note of the code was made at the line and column. #! @param made What was parsed. #! @param line The note's line, counted from 1. #! @param column Its column, counted from 1. #! @param code What kind of note it is. #! @returns Whether it was. def noted(made: &parsed, line: u32, column: u32, code: ec): bool { for &each in made.notes { val at := made.source.lines.place_of(each.offset); return true if at.line == line && at.column == column && each.code == code; } return false; } #! Whether a note of the code was made at the line and column, lexing only. #! @param made What was lexed. #! @param line The note's line, counted from 1. #! @param column Its column, counted from 1. #! @param code What kind of note it is. #! @returns Whether it was. def lex_noted(made: &lexed, line: u32, column: u32, code: ec): bool { for &each in made.notes { val at := made.source.lines.place_of(each.offset); return true if at.line == line && at.column == column && each.code == code; } return false; } #! Parses `before`, then reparses it as edited into `after`, and says whether #! the tree and notes are those a whole parse of `after` makes. #! @param before The text first parsed. #! @param after The text it is edited into. #! @returns Whether they are. def reparses_as_whole(before: strview, after: strview): bool { var made := parse(before); var fresh := lex(after); made.root = made.syntax.reparse(&mut fresh.syntax, &fresh.names, made.root, &mut made.notes, &mut fresh.notes, 0); val whole := parse(after); return false if made.syntax.print(made.root, &fresh.source, &fresh.names).view() != whole.syntax.print(whole.root, &whole.source, &whole.names).view(); return false if made.notes.length != whole.notes.length; for i in 0..whole.notes.length { val each := &whole.notes.at(i); val other := &made.notes.at(i); return false if other.offset != each.offset || other.code != each.code || other.lexed != each.lexed; } return true; } #! A note the checker made, where it is, and what it says. struct seen { var line: u32; var column: u32; var code: ec; var detail: string; var instance: string; #! The instance of a generic it was found in, or empty. var related: u32; #! The line of the place it relates to, or 0. } #! What checking a program found: its notes, and the type of each local named #! in `asked`, as notes spell it. struct checking { var notes: vector; var asked: string; var skipped: u32; } #! Checks the first text, then each after it in turn as an edit of the one #! before, checking again only what each edit affects, and whether hovering #! each name of the last text shows what it does once that text is checked #! whole. #! @param texts The text before every edit, then the text after each. #! @returns The names whose hovers differ, each with both, or empty text. def hovers_after_edits(texts: strview...): string { return hovers_after_edits_with(span::::empty(), ...texts); } #! Checks the files, the edited one last, then each text after the first in turn #! as an edit of the edited file, as `hovers_after_edits` does. #! @param others The files checked with it, which are not edited. #! @param texts The text before every edit, then the text after each. #! @returns The names whose hovers differ, each with both, or empty text. def hovers_after_edits_with(others: span, texts: strview...): string { var names := names::new(); var notes := vector::::new(); var parse_notes := vector::>::new(); var sources := vector::::new(); var syntaxes := vector::::new(); var roots := vector::::new(); val edited := others.length; sources.reserve(others.length + texts.length); syntaxes.reserve(others.length + 1); parse_notes.reserve(others.length + 1); for i in 0..(others.length + 1) { sources.append(source::from(i == edited ? "test.sakura" : "other.sakura", i == edited ? texts[0] : others[i])); syntaxes.append(syntax::new()); parse_notes.append(vector::::new()); } for i in 0..(others.length + 1) { var lexing := lexer::new(&mut sources[i], &mut names, &mut parse_notes[i], i as u32); lexing.tokenize_all(&mut syntaxes[i]); var parsing := parser::new(&mut syntaxes[i], &names, &mut parse_notes[i], i as u32); roots.append(parsing.parse()); } var checker := checker::new(&mut names, &mut notes); for i in 0..(others.length + 1) { checker.add(&sources[i], &syntaxes[i], roots[i]); } checker.check(); for i in 1..texts.length { sources.append(source::from("test.sakura", texts[i])); val at := sources.length - 1; var fresh := syntax::new(); var fresh_notes := vector::::new(); var again := lexer::new(&mut sources[at], &mut names, &mut fresh_notes, edited as u32); again.tokenize_all(&mut fresh); roots[edited] = syntaxes[edited].reparse(&mut fresh, &names, roots[edited], &mut parse_notes[edited], &mut fresh_notes, edited as u32); checker.update(edited as u32, &sources[at], roots[edited]); } val last := texts[texts.length - 1]; var files := vector::::new(); for &each in others { files.append(each); } files.append(last); return asked(def(whole: &mut checker): string { var made := string::new(nothing {}); var at: u64 = 0; while at < last.length { if !is_word_byte(last[at]) || (at > 0 && is_word_byte(last[at - 1])) { at++; continue; } var one := checker.hover(edited as u32, at as u32); var two := whole.hover(edited as u32, at as u32); var a := string::new(nothing {}); var b := string::new(nothing {}); if one is some(&found) { a = string::copy_of(&found.signature); } if two is some(&found) { b = string::copy_of(&found.signature); } if a.view() != b.view() { val line := $"{at}: {a} | {b}\n"; made.append(line.view()); } at++; } return made; }, ...files.view()); } #! The top-level nodes of a file once parsed, each with the first node of what #! it declares. #! @param text The file's text. #! @returns The pairs, as `top << 32 | first`. def top_nodes(text: strview): vector { val made := parse(text); var found := vector::::new(); val decls := made.syntax.node_lefts[made.root]; for i in 0..made.syntax.run_length(decls) { val top := made.syntax.run_at(decls, i as u32); var lowest := top; var todo := vector::::new(); todo.append(top); while todo.length != 0 { val at: u32 = todo[todo.length - 1]; todo.remove_at(todo.length - 1); lowest = at < lowest ? at : lowest; made.syntax.children(at, &mut todo); } found.append((top as u64 << 32) | lowest as u64); } return found; } #! Lexes, parses, and checks the files as one program, the first named #! `test.sakura`, and the type of the local named `asked` in the first file. #! @param asked The name of a local whose type is asked for, or empty text. #! @param texts The files' texts. #! @returns The notes made, the local's type as notes spell it, and how much was #! let through unchecked. def check_program(asked: strview, texts: strview...): checking { var names := names::new(); var notes := vector::::new(); var sources := vector::::new(); var syntaxes := vector::::new(); var roots := vector::::new(); # Each file's source and syntax stay where they are once the checker refers # to them, so there is room for all of them before any is made. sources.reserve(texts.length); syntaxes.reserve(texts.length); for i in 0..texts.length { sources.append(source::from(i == 0 ? "test.sakura" : "other.sakura", texts.at(i))); syntaxes.append(syntax::new()); } for i in 0..texts.length { var lexing := lexer::new(sources.at(i), &mut names, &mut notes, i as u32); lexing.tokenize_all(syntaxes.at(i)); var parsing := parser::new(syntaxes.at(i), &names, &mut notes, i as u32); roots.append(parsing.parse()); } var checker := checker::new(&mut names, &mut notes); for i in 0..texts.length { checker.add(sources.at(i), syntaxes.at(i), roots.at(i)); } checker.check(); var made := checking { notes = vector::::new(), asked = string::new(nothing {}), skipped = checker.skipped }; for &each in notes { val at := sources.at(each.file).lines.place_of(each.offset); val related := each.related.length == 0 ? 0 : sources.at(each.related_file).lines.place_of(each.related_offset).line; made.notes.append(seen { line = at.line, column = at.column, code = each.code, detail = string::from(each.detail, nothing {}), instance = string::from(each.instance, nothing {}), related = related }); } if asked.length != 0 { checker.use_table(checker.units.at(0).table); val syntax := &syntaxes.at(0); for at in 1..syntax.node_count { val node := at as u32; continue if syntax.node_kinds.at(node) != nk::variable; continue if names.text(syntax.token_names.at(syntax.node_tokens.at(node))) != asked; var described := checker.describe(checker.type_at(node)); made.asked = move(&mut described); } } return made; } #! Whether the program checks without a note. #! @param texts The files' texts. #! @returns Whether it does. def checks(texts: strview...): bool { val made := check_program("", ...texts); return made.notes.length == 0; } #! Whether checking the program makes a note of the code at the line and #! column, among any others. #! @param line The note's line, counted from 1. #! @param column Its column, counted from 1. #! @param code What kind of note it is. #! @param texts The files' texts. #! @returns Whether it does. def check_noted(line: u32, column: u32, code: ec, texts: strview...): bool { val made := check_program("", ...texts); for &each in made.notes { return true if each.line == line && each.column == column && each.code == code; } return false; } #! How many notes checking the program makes. #! @param texts The files' texts. #! @returns How many. def note_count(texts: strview...): u64 { val made := check_program("", ...texts); return made.notes.length; } #! The type of the local of the name in the program, as notes spell it. #! @param name The local's name. #! @param text The program's text. #! @returns Its type. def local_type(name: strview, text: strview): string { var made := check_program(name, text); return move(&mut made.asked); } #! A small standard library for checking programs that use what the checker #! knows of std, as the real one declares it. const mini_std: strview = """ module std; module core; pub struct nothing {} pub trait equatable { def equals(&this, other: &self): bool; } pub trait ordered : equatable { def compare(&this, other: &self): i64; } pub trait addable { def add(&this, other: &self): self; } pub trait subtractable { def sub(&this, other: &self): self; } pub trait multipliable { def mul(&this, other: &self): self; } pub trait divisible { def div(&this, other: &self): self; } pub trait modular { def rem(&this, other: &self): self; } pub trait negatable { def negate(&this): self; } pub trait additive : equatable, addable, subtractable, negatable {} pub trait multiplicative : equatable, multipliable {} pub trait numeric : additive, multiplicative, divisible, modular {} pub trait integer {} pub trait ordinal {} pub trait zeroable {} pub trait lockfree {} pub trait owning { def close(&mut this): void; } pub union maybe { pub var none: nothing; pub var some: T; } pub union result { pub var value: T; pub var error: E; } pub trait iterator { def next(&mut this): maybe; } pub trait iterable { type cursor: iterator<&T>; def iter(&this): cursor; } pub struct span { pub var data: *T; pub var count: u64; } pub val span::length: u64 { get => this.count; } pub def span::at(&this, index: u64): &T { return &this.data[index]; } pub struct span::cursor : iterator<&T> { var at: *T; var left: u64; } pub def span::cursor::next(&mut this): maybe<&T> { if this.left == 0 { return nothing {}; } this.left--; return &this.at[0]; } pub def span::iter(&this): span::cursor { return span::::cursor { at = this.data, left = this.count }; } """; #! What `mini_std` lacks for checking anonymous functions given for bounds and #! ranges as values: `invokable`, `range`, which `a..b` makes, and `strview`. const ranges_std: strview = """ module std; module core; pub type strview = span; pub trait invokable { def invoke(&mut this, args: Ts...): R; } pub struct range : iterator where T: ordinal { pub var start: T; pub var end: T; var _last: bool; } pub def range::next(&mut this): maybe { return nothing {}; } """; #! What checking a file again after an edit found, against checking it whole. struct rechecked { var same: bool; #! Whether its notes are those checking the edited text whole makes. var checked: u32; #! How many top-level declarations were checked again. var notes: u64; #! How many notes it made. } #! Checks `before`, with `others` after it, then edits it into `after`, parses #! it again, and checks it again, and says whether its notes are those of #! checking the edited program whole, and how much was checked again. #! @param before The text of the file first checked. #! @param after The text it is edited into. #! @param others The texts of the program's other files. #! @returns Whether its notes are the same, and how much was checked again. def check_edit(before: strview, after: strview, others: strview...): rechecked { var names := names::new(); var notes := vector::::new(); var sources := vector::::new(); var syntaxes := vector::::new(); var roots := vector::::new(); sources.reserve(others.length + 2); syntaxes.reserve(others.length + 1); sources.append(source::from("test.sakura", before)); syntaxes.append(syntax::new()); for &each in others { sources.append(source::from("other.sakura", each)); syntaxes.append(syntax::new()); } for i in 0..syntaxes.length { var lexing := lexer::new(sources.at(i), &mut names, &mut notes, i as u32); lexing.tokenize_all(syntaxes.at(i)); var parsing := parser::new(syntaxes.at(i), &names, &mut notes, i as u32); roots.append(parsing.parse()); } var checker := checker::new(&mut names, &mut notes); for i in 0..syntaxes.length { checker.add(sources.at(i), syntaxes.at(i), roots.at(i)); } checker.check(); # The edit: the text lexed whole, then parsed again where it changed. sources.append(source::from("test.sakura", after)); val edited := sources.length - 1; var fresh := syntax::new(); var fresh_notes := vector::::new(); var lexing := lexer::new(sources.at(edited), &mut names, &mut fresh_notes, 0); lexing.tokenize_all(&mut fresh); val root := syntaxes.at(0).reparse(&mut fresh, &names, roots.at(0), &mut notes, &mut fresh_notes, 0); val count := checker.update(0, sources.at(edited), root); var texts := vector::::new(); texts.append(after); for &each in others { texts.append(each); } val whole := check_program("", ...texts.view()); var same := whole.notes.length == notes.length; for &want in whole.notes { var found := false; for &each in notes { val at := sources.at(each.file == 0 ? edited : each.file as u64).lines.place_of(each.offset); found = found || (at.line == want.line && at.column == want.column && each.code == want.code); } same = same && found; } return rechecked { same = same, checked = count, notes = notes.length }; } #! Lexes, parses, and checks the files as one program, the first named #! `test.sakura` and the rest `other.sakura`, then asks it a question. #! @param question What is asked of the checked program. #! @param texts The files' texts. #! @returns What the question gives. def asked(question: def(&mut checker): string, texts: strview...): string { var names := names::new(); var notes := vector::::new(); var sources := vector::::new(); var syntaxes := vector::::new(); var roots := vector::::new(); sources.reserve(texts.length); syntaxes.reserve(texts.length); for i in 0..texts.length { sources.append(source::from(i == 0 ? "test.sakura" : "other.sakura", texts[i])); syntaxes.append(syntax::new()); } for i in 0..texts.length { var lexing := lexer::new(sources[i], &mut names, &mut notes, i as u32); lexing.tokenize_all(syntaxes[i]); var parsing := parser::new(syntaxes[i], &names, &mut notes, i as u32); roots.append(parsing.parse()); } var checker := checker::new(&mut names, &mut notes); for i in 0..texts.length { checker.add(sources[i], syntaxes[i], roots[i]); } checker.check(); return question(&mut checker); } #! Where text first holds `needle`, and `skip` bytes into it. #! @param text The text. #! @param needle What to find. #! @param skip How far into it the place is. #! @returns The offset, or the text's length where it does not hold it. def place_in(text: strview, needle: strview, skip: u32 = 0): u32 { var at := 0u; while at + needle.length <= text.length && text.substr(at, needle.length) != needle { at++; } return at as u32 + skip; } #! What hovering the first place the text holds `needle` shows, once checked: #! the signature, and after a blank line the documentation, where there is #! some. #! @param text The program's text. #! @param needle The text to hover. #! @param skip How far into the needle the place hovered is. #! @returns What hovering shows, or empty text. def hover_of(text: strview, needle: strview, skip: u32 = 0): string { return asked(def(c: &mut checker): string { var shown := c.hover(0, place_in(text, needle, skip)) else { return string::new(nothing {}); }; var made := move(&mut shown.signature); if shown.documentation.length != 0 { made.append("\n\n"); made.append(shown.documentation.view()); } return made; }, text); }