#! @file nodes.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains how the checker reads the nodes of the file it is checking: their #! kinds, tokens, slots, records, and runs, and the names and text of tokens. module sakura::analyzer; #! The syntax of the file being checked. #! @param this The checker. #! @returns The syntax. pub def checker::syn(&this): &syntax { return this.units[this.at.file].syntax; } #! The source of the file being checked. #! @param this The checker. #! @returns The source. pub def checker::src(&this): &source { return this.units[this.at.file].source; } #! The kind of a node of the file being checked. #! @param this The checker. #! @param node The node. #! @returns Its kind. pub def checker::kind_of(&this, node: u32): nk { return this.at.kinds[node]; } #! The token a node of the file being checked says what it is by. #! @param this The checker. #! @param node The node. #! @returns The token's index. pub def checker::token_of(&this, node: u32): u32 { return this.at.tokens[node]; } #! A node's first slot, which its kind's shape says the meaning of. #! @param this The checker. #! @param node The node. #! @returns The slot. pub def checker::left_of(&this, node: u32): u32 { return this.at.lefts[node]; } #! A node's second slot, which its kind's shape says the meaning of. #! @param this The checker. #! @param node The node. #! @returns The slot. pub def checker::right_of(&this, node: u32): u32 { return this.at.rights[node]; } #! The name the node's token is, as a declaration's name or a `name` node's. #! @param this The checker. #! @param node The node. #! @returns The name's ID, or 0 where its token is no word. pub def checker::name_of(&this, node: u32): u32 { return this.at.names[this.at.tokens[node]]; } #! The name a parameter is known by: its own, or of one an accessor implies, #! whose token is the `get` or `set`, `this`, or a `set`'s `value`. #! @param this The checker. #! @param node The parameter's node, or any other. #! @returns The ID of the name. pub def checker::param_name(&this, node: u32): u32 { return this.name_of(node) if this.kind_of(node) != nk::param || !this.node_has(node, nf::implied); return this.name_of(node) == names::set_id && !this.node_has(node, nf::mutable) ? names::value_id : names::this_id; } #! The kind of the node's token. #! @param this The checker. #! @param node The node. #! @returns The kind. pub def checker::token_kind(&this, node: u32): tk { return this.at.tkinds[this.at.tokens[node]]; } #! The text of the node's token, as it is written. #! @param this The checker. #! @param node The node. #! @returns The text, which views the file's source. pub def checker::text_of(&this, node: u32): strview { val syntax := &this.syn(); val token := this.token_of(node); return this.src().make_lexeme(syntax.token_offsets[token], syntax.token_length(token, this.names)); } #! Writes an expression as it is written, from its first token to its last, as #! `v.items` or `b != 0`. #! @param this The checker. #! @param node The expression. #! @param out Where it is written. pub def checker::written_text(&this, node: u32, out: &mut string): void { val syntax := &this.syn(); # A node's own token may come after those of what it was parsed from, as an # operator's comes after its left operand, so the first is the least of all. var first: u32 = syntax.node_tokens[node]; var walk := node_walk::new(node); while walk.more() { val at := walk.next(); val token := syntax.node_tokens[at]; first = token < first ? token : first; walk.enter(syntax, at); } val last := syntax.node_end(node); val begin := syntax.token_offsets[first]; val end := syntax.token_offsets[last] + syntax.token_length(last, this.names); return if end <= begin; out.append(this.src().make_lexeme(begin, end - begin)); } #! The slot of the node's record at the index, in whichever of its slots its #! shape says holds the record. #! @param this The checker. #! @param node The node. #! @param index The slot's position in the record. #! @returns The slot, or 0 where the node has no record. pub def checker::record(&this, node: u32, index: u32): u32 { val shape := shapes[this.at.kinds[node] as u64]; asserts(index < shape.record.length, "The node's record has the slot."); val at: u32 = shape.left == slot::extra ? this.at.lefts[node] : this.at.rights[node]; return this.at.extras[at + index]; } #! The flags of the node, wherever its shape keeps them. #! @param this The checker. #! @param node The node. #! @returns The flags, as `nf` says, or 0. pub def checker::node_flags(&this, node: u32): u32 { return this.syn().flags_at(node); } #! Whether a node has a flag. #! @param this The checker. #! @param node The node. #! @param flag The flag. #! @returns Whether it has it. pub def checker::node_has(&this, node: u32, flag: nf): bool { return (this.node_flags(node) & flag as u32) != 0; } #! How many nodes a run of the file being checked holds. #! @param this The checker. #! @param run Where the run begins. #! @returns How many nodes it holds. pub def checker::run_length(&this, run: u32): u32 { return this.at.extras[run]; } #! A node of a run, by its position. #! @param this The checker. #! @param run Where the run begins. #! @param index The position, below its length. #! @returns The node. pub def checker::run_at(&this, run: u32, index: u32): u32 { return this.at.extras[run + 1 + index]; } #! The nodes of a run of the file being checked, where they are, which checking #! never moves. #! @param this The checker. #! @param run Where the run begins. #! @returns The nodes. pub def checker::run_view(&this, run: u32): span { return span::::over(this.at.extras + run + 1, this.at.extras[run] as u64); } #! How many segments a `type_name` or `path` has. #! @param this The checker. #! @param node The `type_name` or `path` node. #! @returns How many. pub def checker::segment_count(&this, node: u32): u32 { val run := this.left_of(node); return run == 0 ? 1 : this.run_length(run); } #! The name of a segment of a `type_name` or `path`. #! @param this The checker. #! @param node The `type_name` or `path` node. #! @param index The segment's position. #! @returns The ID of its name. pub def checker::segment_name(&this, node: u32, index: u32): u32 { val run := this.left_of(node); return run == 0 ? this.name_of(node) : this.name_of(this.run_at(run, index)); } #! The node of a segment, where it holds type arguments, or the `type_name` #! itself for one of a single segment. #! @param this The checker. #! @param node The `type_name` or `path` node. #! @param index The segment's position. #! @returns The node. pub def checker::segment_node(&this, node: u32, index: u32): u32 { val run := this.left_of(node); return run == 0 ? node : this.run_at(run, index); } #! The run of type arguments a segment is given, or 0. #! @param this The checker. #! @param node The `type_name` or `path` node. #! @param index The segment's position. #! @returns The run, or 0. pub def checker::segment_args(&this, node: u32, index: u32): u32 { val run := this.left_of(node); return run == 0 ? 0 : this.left_of(this.run_at(run, index)); }