229 lines
8.5 KiB
Plaintext
229 lines
8.5 KiB
Plaintext
#! @file reparse.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains `syntax::reparse`, which parses only the top-level declarations an
|
||
|
|
#! edit touched, and keeps the trees of the rest.
|
||
|
|
|
||
|
|
module sakura::analyzer;
|
||
|
|
|
||
|
|
|
||
|
|
#! Reparses a file after an edit, keeping the trees of the top-level
|
||
|
|
#! declarations the edit did not touch, and parsing again only those it did.
|
||
|
|
#!
|
||
|
|
#! The edited text is lexed whole into `fresh`, which is cheap, and its tokens
|
||
|
|
#! are compared with the old ones from each end to find what changed. The
|
||
|
|
#! declarations around the change are parsed again from the fresh tokens, and
|
||
|
|
#! those after it keep their nodes, whose tokens move by how many the edit added
|
||
|
|
#! or took away. Where a declaration parsed again runs into where the change
|
||
|
|
#! ends, as one that lost its closing `}` does, the file is parsed whole
|
||
|
|
#! instead, so the tree is always the one a whole parse makes.
|
||
|
|
#!
|
||
|
|
#! The nodes of the declarations parsed again are left behind, unreachable. Once
|
||
|
|
#! there are as many nodes or extras again as the last whole parse made, the
|
||
|
|
#! file is parsed whole, which lets go of them, so a file edited for a long
|
||
|
|
#! while does not keep growing. The indexes of every node change when it does.
|
||
|
|
#! @param this The syntax tree.
|
||
|
|
#! @param fresh The edited text, lexed: its tokens, documentation, and
|
||
|
|
#! comments.
|
||
|
|
#! @param names The names `fresh` was lexed with.
|
||
|
|
#! @param document The `document` node of this syntax.
|
||
|
|
#! @param notes The notes of this file, both the lexer's and the
|
||
|
|
#! parser's.
|
||
|
|
#! @param fresh_notes What the lexer noted of the edited text, which are moved
|
||
|
|
#! into `notes`.
|
||
|
|
#! @param file The index of the file.
|
||
|
|
#! @returns The `document` node.
|
||
|
|
pub def syntax::reparse(
|
||
|
|
&mut this,
|
||
|
|
fresh: &mut syntax,
|
||
|
|
names: &names,
|
||
|
|
document: u32,
|
||
|
|
notes: &mut vector<note>,
|
||
|
|
fresh_notes: &mut vector<note>,
|
||
|
|
file: u32
|
||
|
|
): u32 {
|
||
|
|
val old_count := this.token_count as u32;
|
||
|
|
val new_count := fresh.token_count as u32;
|
||
|
|
val bytes := fresh.token_offsets[new_count - 1] as i64 - this.token_offsets[old_count - 1] as i64;
|
||
|
|
val shift := new_count as i64 - old_count as i64;
|
||
|
|
|
||
|
|
# What is the same before the change, and after it, where it has moved.
|
||
|
|
var same_before: u32 = 0;
|
||
|
|
while same_before < old_count - 1 && same_before < new_count - 1
|
||
|
|
&& this.same_token(fresh, same_before, same_before, 0) {
|
||
|
|
same_before++;
|
||
|
|
}
|
||
|
|
|
||
|
|
var same_after: u32 = 0;
|
||
|
|
while same_after < old_count - 1 - same_before && same_after < new_count - 1 - same_before
|
||
|
|
&& this.same_token(fresh, old_count - 2 - same_after, new_count - 2 - same_after, bytes) {
|
||
|
|
same_after++;
|
||
|
|
}
|
||
|
|
|
||
|
|
val changed_end := old_count - 1 - same_after;
|
||
|
|
|
||
|
|
# The declarations the change touches, from `first` through `last`, and the
|
||
|
|
# tokens they and the change span.
|
||
|
|
val decls: u32 = this.node_lefts[document];
|
||
|
|
val count := this.run_length(decls);
|
||
|
|
var first := count;
|
||
|
|
for i in 0..count {
|
||
|
|
if this.node_end(this.run_at(decls, i as u32)) >= same_before {
|
||
|
|
first = i as u32;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
var last := first;
|
||
|
|
for i in first..count {
|
||
|
|
break if this.start_of(this.run_at(decls, i as u32)) >= changed_end && i > first;
|
||
|
|
last = i as u32;
|
||
|
|
}
|
||
|
|
|
||
|
|
var from := same_before;
|
||
|
|
var to := changed_end;
|
||
|
|
if first < count {
|
||
|
|
val start := this.start_of(this.run_at(decls, first));
|
||
|
|
val end := this.node_end(this.run_at(decls, last)) + 1;
|
||
|
|
from = start < from ? start : from;
|
||
|
|
to = end > to ? end : to;
|
||
|
|
}
|
||
|
|
|
||
|
|
# The notes of what is parsed again, and all of the lexer's, are made again.
|
||
|
|
# They are put back in the order a whole parse makes them: the lexer's, then
|
||
|
|
# the parser's along the text, those after the change moving with it.
|
||
|
|
val from_byte := this.token_offsets[from];
|
||
|
|
val to_byte := this.token_offsets[to < old_count - 1 ? to : old_count - 1];
|
||
|
|
var before := vector::<note>::new();
|
||
|
|
var after := vector::<note>::new();
|
||
|
|
for &mut each in notes {
|
||
|
|
continue if each.lexed;
|
||
|
|
if each.offset < from_byte {
|
||
|
|
before.append(move(each));
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
continue if each.offset < to_byte;
|
||
|
|
each.offset = (each.offset as i64 + bytes) as u32;
|
||
|
|
after.append(move(each));
|
||
|
|
}
|
||
|
|
|
||
|
|
notes.clear();
|
||
|
|
for &mut each in fresh_notes { notes.append(move(each)); }
|
||
|
|
val kept := before.length + after.length;
|
||
|
|
for &mut each in before { notes.append(move(each)); }
|
||
|
|
|
||
|
|
# The nodes after the change move with their tokens, and their ends with
|
||
|
|
# them, which are held as how far past their tokens they are.
|
||
|
|
for at in 1..this.node_count {
|
||
|
|
if this.node_tokens[at] >= to {
|
||
|
|
this.node_tokens[at] = (this.node_tokens[at] as i64 + shift) as u32;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
var moved := vector::<u64>::new();
|
||
|
|
for at in 0..this.starts.capacity {
|
||
|
|
if this.starts.key_at(at) != keymap::free && this.starts.value_at(at) >= to {
|
||
|
|
moved.append(this.starts.key_at(at));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
for key in moved {
|
||
|
|
this.starts.set(key, (this.starts.get_or(&key, 0) as i64 + shift) as u32);
|
||
|
|
}
|
||
|
|
|
||
|
|
this.take_tokens(fresh);
|
||
|
|
|
||
|
|
# The declarations around the change are parsed again, and their run takes
|
||
|
|
# the place of theirs among the rest.
|
||
|
|
val limit := (to as i64 + shift) as u32;
|
||
|
|
var parsing := parser::new(this, names, notes, file);
|
||
|
|
parsing.index = from;
|
||
|
|
parsing.limit = limit;
|
||
|
|
parsing.sink.count = kept as u32;
|
||
|
|
parsing.sync();
|
||
|
|
|
||
|
|
val mark := parsing.mark;
|
||
|
|
for i in 0..first { parsing.push(this.run_at(decls, i as u32)); }
|
||
|
|
|
||
|
|
# Where the parser reached as many notes as it makes, a whole parse would have
|
||
|
|
# stopped somewhere else, so it is what tells which are made.
|
||
|
|
val made := parsing.declarations(limit);
|
||
|
|
if parsing.cut || parsing.sink.full {
|
||
|
|
return this.parse_again(document, names, notes, file);
|
||
|
|
}
|
||
|
|
|
||
|
|
for &mut each in after { notes.append(move(each)); }
|
||
|
|
|
||
|
|
for &node in this.run_view(made) { parsing.push(node); }
|
||
|
|
for i in (last + 1)..count { parsing.push(this.run_at(decls, i as u32)); }
|
||
|
|
|
||
|
|
val declared := parsing.make_run(mark);
|
||
|
|
this.node_lefts[document] = declared;
|
||
|
|
this.set_node_end(document, this.token_count as u32 - 1);
|
||
|
|
|
||
|
|
if this.node_count > this.whole_nodes * 2 || this.extras.length > this.whole_extras * 2 {
|
||
|
|
return this.parse_again(document, names, notes, file);
|
||
|
|
}
|
||
|
|
return document;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether the old token and the fresh one are the same, the fresh one having
|
||
|
|
#! moved `bytes` along the text.
|
||
|
|
#! @param this The syntax tree.
|
||
|
|
#! @param fresh The syntax lexed again.
|
||
|
|
#! @param old The old token.
|
||
|
|
#! @param now The fresh token.
|
||
|
|
#! @param bytes How far the edit moved the fresh one, which is negative where
|
||
|
|
#! text was taken out.
|
||
|
|
#! @returns Whether they are.
|
||
|
|
def syntax::same_token(&this, fresh: &syntax, old: u32, now: u32, bytes: i64): bool {
|
||
|
|
val kind := this.token_kinds[old];
|
||
|
|
return kind == fresh.token_kinds[now]
|
||
|
|
&& this.token_names[old] == fresh.token_names[now]
|
||
|
|
&& this.token_offsets[old] as i64 + bytes == fresh.token_offsets[now] as i64
|
||
|
|
&& (kind == tk::name || kind.width != 0 || this.literal_length(old) == fresh.literal_length(now));
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Takes the tokens, documentation, and comments of `fresh` for its own.
|
||
|
|
#! @param this The syntax tree.
|
||
|
|
#! @param fresh The syntax lexed again, which is left without them.
|
||
|
|
def syntax::take_tokens(&mut this, fresh: &mut syntax): void {
|
||
|
|
this.token_kinds = move(&mut fresh.token_kinds);
|
||
|
|
this.token_offsets = move(&mut fresh.token_offsets);
|
||
|
|
this.token_names = move(&mut fresh.token_names);
|
||
|
|
this.literal_tokens = move(&mut fresh.literal_tokens);
|
||
|
|
this.literal_lengths = move(&mut fresh.literal_lengths);
|
||
|
|
this.docs = move(&mut fresh.docs);
|
||
|
|
this.comments = move(&mut fresh.comments);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Parses the whole file again, from the tokens it already took.
|
||
|
|
#! @param this The syntax tree.
|
||
|
|
#! @param document The `document` node to parse into.
|
||
|
|
#! @param names The names its tokens were lexed with.
|
||
|
|
#! @param notes The file's notes, the lexer's first, which are kept, and
|
||
|
|
#! then the parser's, which are made again.
|
||
|
|
#! @param file The file's index.
|
||
|
|
#! @returns The `document` node.
|
||
|
|
def syntax::parse_again(
|
||
|
|
&mut this,
|
||
|
|
document: u32,
|
||
|
|
names: &names,
|
||
|
|
notes: &mut vector<note>,
|
||
|
|
file: u32
|
||
|
|
): u32 {
|
||
|
|
this.reset();
|
||
|
|
this.parses++;
|
||
|
|
var lexed: u64 = 0;
|
||
|
|
while lexed < notes.length && notes[lexed].lexed {
|
||
|
|
lexed++;
|
||
|
|
}
|
||
|
|
while notes.length > lexed {
|
||
|
|
notes.truncate();
|
||
|
|
}
|
||
|
|
|
||
|
|
var parsing := parser::new(this, names, notes, file);
|
||
|
|
return parsing.parse();
|
||
|
|
}
|