#! @file printer.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the printer, which writes a syntax tree as text, the way the #! parser's tests say what tree they expect. module sakura::analyzer; #! Prints the tree under a node, which is how the parser's tests say what tree #! they expect, and how one is read while debugging. #! #! A node is printed as `(kind:text ...)`, its slots following in the order its #! shape lists them, and one whose slots all hold nothing, being absent nodes, #! empty runs, or no flags, is printed as `kind:text` alone. The text is that of #! its token where it says something, as a name, a literal, or an operator, and #! left out where it is punctuation or a keyword the kind already says. An #! absent node is `_`, a run is `[...]`, and flags are `{mutable,constant}`, #! printed only where any are set. #! @param this The syntax tree. #! @param at The node to print. #! @param source The source the tokens are of, for their text. #! @param names The names the tokens were lexed with, for the length of each #! name. #! @returns The tree printed. pub def syntax::print(&this, at: u32, source: &source, names: &names): string { var out := string::new(nothing {}); this.print_into(at, source, names, &mut out); return out; } def syntax::print_into(&this, at: u32, source: &source, names: &names, out: &mut string): void { if at == 0 { out.append("_"); return; } val held := this.node(at); val shape := held.shape; val bare := this.slot_empty(shape.left, held.left, shape.record) && this.slot_empty(shape.right, held.right, shape.record); if !bare { out.append("("); } out.append(kind_names[held.kind as u64]); val token := this.token(held.token, names); if token.kind.has(tt::text) { out.append(":"); val text := source.make_lexeme(token.offset, token.length); # A parameter an accessor implies has the `get` or `set` for its token. val flags := held.kind == nk::param ? this.flags_at(at) : 0; if (flags & nf::implied as u32) != 0 { out.append(text == "set" && (flags & nf::mutable as u32) == 0 ? "value" : "this"); } else { out.append(text); } } return if bare; this.print_slot(shape.left, held.left, shape.record, source, names, out); this.print_slot(shape.right, held.right, shape.record, source, names, out); out.append(")"); } def syntax::print_slot( &this, kind: slot, value: u32, record: span, source: &source, names: &names, out: &mut string ): void { match kind { slot::node => { out.append(" "); this.print_into(value, source, names, out); } slot::run, slot::bounds => { return if kind == slot::bounds && this.run_length(value) == 0; out.append(" ["); for i in 0..this.run_length(value) { if i != 0 { out.append(" "); } this.print_into(this.run_at(value, i as u32), source, names, out); } out.append("]"); } slot::flags => { return if value == 0; out.append(" {"); var first := true; for i in 0..flag_names.length { if (value & (1 << i as u32)) != 0 { if !first { out.append(","); } out.append(flag_names[i]); first = false; } } out.append("}"); } slot::extra => { for i in 0..record.length { this.print_slot(record[i], this.extras[value as u64 + i], no_record, source, names, out); } } else => {} } } #! Whether the slot holds nothing where it holds `value`. #! @param this The syntax tree. #! @param kind What the slot holds. #! @param value The slot's value. #! @param record The shape of the node's record, where the slot holds one. #! @returns Whether it holds nothing. def syntax::slot_empty(&this, kind: slot, value: u32, record: span): bool { return match kind { slot::node, slot::flags => value == 0, slot::run, slot::bounds => this.run_length(value) == 0, slot::extra => this.record_empty(value, record), else => true }; } def syntax::record_empty(&this, at: u32, record: span): bool { for i in 0..record.length { return false if !this.slot_empty(record[i], this.extras[at as u64 + i], no_record); } return true; } #! The names of the kinds of node, in the order `nk` declares them. const kind_names: strview[nk::count as u64] = [ "error", "document", "block", "import_decl", "module_decl", "extern_import", "extern_module", "when_block", "function", "param", "variable", "field", "spread", "rename", "property", "accessor", "struct_decl", "union_decl", "trait_decl", "assoc_type", "enumeration", "enumerator", "alias", "attribute", "type_param", "const_param", "type_pattern", "array_pattern", "segment", "type_name", "type_ref", "type_ptr", "type_fixed", "type_func", "type_spread", "type_of", "if_stmt", "if_const", "while_stmt", "for_stmt", "match_stmt", "match_arm", "return_stmt", "break_stmt", "continue_stmt", "expr_stmt", "contract", "name", "path", "number", "character", "string", "istring", "boolean", "null_lit", "unary", "postfix", "reference", "binary", "assign", "ternary", "fallback", "call", "type_args", "index", "slice", "member", "member_of", "cast", "is_test", "construct", "field_init", "array", "lambda", "match_value", "range", "old_value", "undefined", ]; #! The names of the bits of `nf`, from its lowest. const flag_names: span = [ "exported", "mutable", "constant", "reference", "variadic", "packed", "negated", "computed", "function", "named", "inclusive", "implied", ];