#! @file syntax.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the syntax type, which keeps the tokens, nodes, and extras of a #! parsed file, its comments, and the documentation and starts of its #! declarations. import std::containers; module sakura::analyzer; #! Represents a parsed syntax for a given file. #! #! As the lexer reads the source code, it stores the tokens into this #! structure, then as the parser builds new nodes, it stores those into this #! structure too. #! #! Nodes and tokens are each kept as one vector per field, indexed alike, so a #! pass reading one field, as the kind of every node, reads only that, and no #! padding sits between fields. `node` and `token` are values gathered from #! them, for reading one whole. #! #! What a token's kind or name says is not kept again: how many bytes a keyword #! or a punctuator covers is its kind's, as `token_widths` says, and a name's is #! its text's. Only the lengths of the rest, the literals, a few of every #! hundred tokens, are kept, as `token_length` reads them. pub struct syntax { # Nodes, by index. pub var node_kinds: vector; #! The kind of each node. pub var node_tokens: vector; #! The token each says what it is by. var node_lefts: vector; #! The first slot of each, which its kind's shape says the meaning of. var node_rights: vector; #! The second slot of each. var node_spans: vector; #! How many tokens past its own each was parsed through, or `syntax::wide`, where `wide_spans` holds how many. # How many tokens past its own each node spanning more than a byte counts was # parsed through, a few of every thousand, as a document or a function body. var wide_spans: keymap; #! How many, by the node. # What nodes hold beyond their two slots: records and runs. var extras: vector; #! The records and runs, each where a slot says it begins. # Tokens, by index. var token_kinds: vector; #! The kind of each token. pub var token_offsets: vector; #! The byte of the text each begins at. var token_names: vector; #! The ID of the name each is, or 0 where it is no word. # The tokens whose kind and name do not say how many bytes they cover, the # literals, in order, and how many each covers. var literal_tokens: vector; #! Each literal token, in increasing order. var literal_lengths: vector; #! How many bytes each covers. # The documentation comments, `#!` lines, in order, and where each # declaration begins, its attributes and `pub` included, which says which of # them document it. var docs: vector; #! The documentation comments. var starts: keymap; #! The token each declaration begins at, by its node. # The other comments, in order, which are no part of the tree but are kept for # the tools that write the text back out, as a formatter. var comments: vector; #! The other comments. # How many nodes and extras the last whole parse left. A reparse leaves the # nodes it replaced behind, unreachable, and these say when they are enough # to parse whole again and let go of them. var whole_nodes: u64; #! The node count the last whole parse left. var whole_extras: u64; #! The extras count the last whole parse left. # How many times a reparse parsed the file whole again, which makes every # node anew, so what was kept of the old ones by their indexes is no longer of # them. pub var parses: u32; #! How many times the file was parsed whole again. } #! A line of documentation, `#!` and what follows it on its line. pub struct doc { var offset: u32; #! The byte of the text it begins at. var length: u32; #! How many bytes it covers. var token: u32; #! The token it is written before. } #! A comment that is not documentation, `#` and what follows it on its line. pub struct comment { var offset: u32; #! The byte of the text it begins at. var length: u32; #! How many bytes it covers. var token: u32; #! The token after it. } #! The documentation of a declaration: `count` lines from `first` of `docs`. pub struct doc_range { var first: u32; #! The first line, in `docs`. var count: u32; #! How many lines. } #! How many entries at the start of `extras` are 0, which stand for both an #! empty run and a record whose slots are all absent, as large as the largest #! record. pub const syntax::zeros: u32 = 6; #! Creates a new instance of a syntax object. #! @returns The instantiated syntax. pub def syntax::new(): syntax { var made := syntax { node_kinds = vector::::new(), node_tokens = vector::::new(), node_lefts = vector::::new(), node_rights = vector::::new(), node_spans = vector::::new(), wide_spans = keymap::new(), extras = vector::::new(), token_kinds = vector::::new(), token_offsets = vector::::new(), token_names = vector::::new(), literal_tokens = vector::::new(), literal_lengths = vector::::new(), docs = vector::::new(), starts = keymap::new(), comments = vector::::new() }; made.reset(); return made; } #! Lets go of every node and extra, leaving only what stands for absent: node 0, #! an `error` node holding nothing, and the zeros at the start of `extras`. #! @param this The syntax tree. pub def syntax::reset(&mut this): void { this.node_kinds.clear(); this.node_tokens.clear(); this.node_lefts.clear(); this.node_rights.clear(); this.node_spans.clear(); this.wide_spans.clear(); this.extras.clear(); this.starts.clear(); this.add_node(nk::error, 0, 0, 0, 0); this.extras.resize(syntax::zeros as u64); } #! How many nodes there are, node 0 included. #! @param this The syntax tree. #! @returns The count. pub val syntax::node_count: u64 { get => this.node_kinds.length; } #! How many tokens there are, the `eof` ending them included. #! @param this The syntax tree. #! @returns The count. pub val syntax::token_count: u64 { get => this.token_kinds.length; } #! Appends a node. #! @param this The syntax tree. #! @param kind Its kind. #! @param token The token it says what it is by. #! @param left Its first slot. #! @param right Its second slot. #! @param end The last token of what it was parsed from. #! @returns Its index. pub def syntax::add_node(&mut this, kind: nk, token: u32, left: u32, right: u32, end: u32): u32 { this.node_kinds.append(kind); this.node_tokens.append(token); this.node_lefts.append(left); this.node_rights.append(right); this.node_spans.append(0); val at := (this.node_kinds.length - 1) as u32; this.set_node_end(at, end); return at; } #! A node's span, in a byte, that says `wide_spans` holds it. pub const syntax::wide: u8 = 255; #! The last token of what the node was parsed from. #! @param this The syntax tree. #! @param at The node's index. #! @returns The token. pub def syntax::node_end(&this, at: u32): u32 { val span := this.node_spans[at]; return this.node_tokens[at] + (span != syntax::wide ? span as u32 : this.wide_spans.get_or(&(at as u64), 0)); } #! Says the last token of what the node was parsed from, which its token is #! set before. #! @param this The syntax tree. #! @param at The node's index. #! @param end The token. pub def syntax::set_node_end(&mut this, at: u32, end: u32): void { # Held as a count past its token, which wraps where it ends before it, so # adding the count back gives the end. val span := end - this.node_tokens[at]; if span < syntax::wide as u32 { if this.node_spans[at] == syntax::wide { this.wide_spans.remove(&(at as u64)); } this.node_spans[at] = span as u8; return; } this.node_spans[at] = syntax::wide; this.wide_spans.set(at as u64, span); } #! The node at the index, gathered from its fields. #! @param this The syntax tree. #! @param at The node's index. #! @returns The node. pub def syntax::node(&this, at: u32): node { return node { kind = this.node_kinds[at], token = this.node_tokens[at], left = this.node_lefts[at], right = this.node_rights[at], end = this.node_end(at) }; } #! Appends a token of the kind, spanning `length` bytes from `offset`, which is #! the word of the ID `name`, or 0 where it is no word. #! @param this The syntax tree. #! @param kind Its kind. #! @param offset The byte of the text it begins at. #! @param length How many bytes it covers. #! @param name The ID of the word it is, or 0. pub def syntax::add_token(&mut this, kind: tk, offset: u32, length: u32, name: u32): void { if kind != tk::name && kind.width == 0 && length != 0 { this.literal_tokens.append(this.token_kinds.length as u32); this.literal_lengths.append(length); } this.token_kinds.append(kind); this.token_offsets.append(offset); this.token_names.append(name); } #! How many bytes the token covers: as many as its kind says, or its name's #! text, or as were kept of a literal. #! @param this The syntax tree. #! @param at The token's index. #! @param names The names the file was lexed with, or joined. #! @returns How many. pub def syntax::token_length(&this, at: u32, names: &names): u32 { val kind := this.token_kinds[at]; val width := kind.width; return width if width != 0; return names.text(this.token_names[at]).length as u32 if kind == tk::name; return this.literal_length(at); } #! How many bytes a literal token covers, or 0 for a token not kept as one, as #! `eof`. #! @param this The syntax tree. #! @param at The token's index. #! @returns How many. def syntax::literal_length(&this, at: u32): u32 { val found := this.literal_tokens.view().lower_bound(&at); return found < this.literal_tokens.length && this.literal_tokens[found] == at ? this.literal_lengths[found] : 0; } #! Gives each token the ID of its name in another table of names, as where a #! file lexed with names of its own joins those of the program. #! @param this The syntax tree. #! @param ids The ID in the other table of each name, by its ID in the one #! the file was lexed with. pub def syntax::rename(&mut this, ids: span): void { for i in 0..this.token_names.length { this.token_names[i] = ids[this.token_names[i] as u64]; } } #! The token at the index, gathered from its fields. #! @param this The syntax tree. #! @param at The token's index. #! @param names The names the file was lexed with, or joined, which give a #! name's length. #! @returns The token. pub def syntax::token(&this, at: u32, names: &names): token { return token { kind = this.token_kinds[at], offset = this.token_offsets[at], length = this.token_length(at, names), name = this.token_names[at] }; } #! Makes room for the tokens lexing text of so many bytes makes, which are #! seldom more than one for every four bytes, so lexing seldom moves them; the #! room left is let go of once lexing ends, as `fit_tokens` does. #! @param this The syntax tree. #! @param bytes How many bytes the text holds. pub def syntax::reserve_for_text(&mut this, bytes: u32): void { val tokens := this.token_count + bytes as u64 / 4 + 16; this.token_kinds.reserve(tokens); this.token_offsets.reserve(tokens); this.token_names.reserve(tokens); } #! Makes room for the nodes and extras parsing the tokens makes, which are #! seldom more than three quarters of the tokens, so parsing seldom moves them; #! the room left is let go of once parsing ends, as `fit_nodes` does. #! @param this The syntax tree. pub def syntax::reserve_for_tokens(&mut this): void { val tokens := this.token_count; val nodes := this.node_count + tokens * 3 / 4 + 16; this.node_kinds.reserve(nodes); this.node_tokens.reserve(nodes); this.node_lefts.reserve(nodes); this.node_rights.reserve(nodes); this.node_spans.reserve(nodes); this.extras.reserve(this.extras.length + tokens * 3 / 4 + 16); } #! Lets go of the room the tokens were given beyond those lexed, which is #! about a third of it. #! @param this The syntax tree. pub def syntax::fit_tokens(&mut this): void { fitted(&mut this.token_kinds); fitted(&mut this.token_offsets); fitted(&mut this.token_names); fitted(&mut this.literal_tokens); fitted(&mut this.literal_lengths); } #! Lets go of the room the nodes and extras were given beyond those parsed. #! @param this The syntax tree. pub def syntax::fit_nodes(&mut this): void { fitted(&mut this.node_kinds); fitted(&mut this.node_tokens); fitted(&mut this.node_lefts); fitted(&mut this.node_rights); fitted(&mut this.node_spans); fitted(&mut this.extras); } # Moves the items into a block of their own size, where the one they are in # has room for more than an eighth as many again. def fitted(items: &mut vector): void { return if items.capacity - items.length <= items.length / 8 + 16; var made := vector::::new(); made.reserve(items.length); made.extend(items.view()); items = move(&mut made); } #! The token a declaration begins at, its attributes and `pub` included, or its #! own token where it was not recorded. #! @param this The syntax tree. #! @param at The declaration's node. #! @returns The token. pub def syntax::start_of(&this, at: u32): u32 { return this.starts.get_or(&(at as u64), this.node_tokens[at]); } #! The documentation comments written before a declaration. #! @param this The syntax tree. #! @param at The declaration's node. #! @returns The lines, which are none where it has no documentation. pub def syntax::docs_of(&this, at: u32): doc_range { val start := this.starts.get(&(at as u64)) else { return doc_range {}; }; val low := this.docs.view().partition_point(def(each: &doc): bool { return each.token < start; }); var count: u32 = 0; while low + (count as u64) < this.docs.length && this.docs[low + count as u64].token == start { count++; } return doc_range { first = low as u32, count = count }; }