///| Turns WIT source into narrative prose for the Interface Explorer. /// /// Output is one paragraph per line (the renderer word-wraps), with `""` between /// paragraphs. A `# ` prefix marks a heading. Terms are wrapped in `**bold**`. ///| fn trim(s : String) -> String { s.trim(chars=" \t\r").to_string() } ///| "window-state" -> "window state" fn dashes(s : String) -> String { let sb = StringBuilder::new() for ch in s { sb.write_char(if ch == '-' { ' ' } else { ch }) } sb.to_string() } ///| fn pad(n : Int) -> String { let sb = StringBuilder::new() for _i = 0; _i < n; _i = _i + 1 { sb.write_char(' ') } sb.to_string() } ///| Split on `sep` outside <...>, (...) and {...}. fn split_top(s : String, sep : Char) -> Array[String] { let out : Array[String] = [] let mut depth = 0 let mut start = 0 for i = 0; i < s.length(); i = i + 1 { let ch = s[i] if ch == '<' || ch == '(' || ch == '{' { depth = depth + 1 } else if ch == '>' || ch == ')' || ch == '}' { depth = depth - 1 } else if ch == sep.to_int().to_uint16() && depth == 0 { out.push(trim(s[start:i].to_string())) start = i + 1 } } let last = trim(s[start:].to_string()) if last.length() > 0 { out.push(last) } out } ///| Index of `ch` in `s`, or -1. fn index_of(s : String, ch : Char) -> Int { for i = 0; i < s.length(); i = i + 1 { if s[i] == ch.to_int().to_uint16() { return i } } -1 } ///| Index of the last `ch` in `s`, or -1. fn last_index_of(s : String, ch : Char) -> Int { for i = s.length() - 1; i >= 0; i = i - 1 { if s[i] == ch.to_int().to_uint16() { return i } } -1 } ///| A WIT type in words. fn ty(t0 : String) -> String { let t = trim(t0) match t { "string" => "text" "bool" => "yes/no" "u8" | "u16" | "u32" | "u64" | "s8" | "s16" | "s32" | "s64" | "f32" | "f64" => "number" "char" => "a character" "_" => "nothing" _ => { let lt = index_of(t, '<') if lt > 0 && t.has_suffix(">") { let head = t[:lt].to_string() let args = split_top(t[lt + 1:t.length() - 1].to_string(), ',') let a0 = if args.length() > 0 { ty(args[0]) } else { "" } match head { "list" => "list of " + a0 "option" => "optional " + a0 "borrow" | "own" => a0 "tuple" => "(" + args.map(ty).join(", ") + ")" "result" => { let err = if args.length() > 1 { ty(args[1]) } else { "" } let ok = if a0 == "nothing" { "success" } else { a0 } if err == "" || err == "nothing" { ok + ", or failure" } else { ok + ", or an error (" + err + ")" } } _ => dashes(t) } } else { dashes(t) } } } } ///| Strip "///" and one space. fn doc_text(l : String) -> String { trim(trim(l)[3:].to_string()) } ///| fn bold(s : String) -> String { "**" + s + "**" } ///| Join with commas and a final "and". fn and_list(items : Array[String]) -> String { if items.length() == 0 { "nothing" } else if items.length() == 1 { items[0] } else { items[:items.length() - 1].to_array().join(", ") + " and " + items[items.length() - 1] } } ///| Docs as running text, with a trailing space if non-empty. fn doc_prose(docs : Array[String]) -> String { if docs.length() == 0 { "" } else { let t = docs.join(" ") (if t.has_suffix(".") { t } else { t + "." }) + " " } } ///| "name: func(a: t, b: t) -> r" as one sentence. fn func_sentence(entry : String) -> String { let c = index_of(entry, ':') let name = dashes(trim(entry[:c].to_string())) let rest = entry[c + 1:].to_string() let open = index_of(rest, '(') let mut depth = 0 let mut close = -1 for i = open; i >= 0 && i < rest.length(); i = i + 1 { if rest[i] == '(' { depth = depth + 1 } else if rest[i] == ')' { depth = depth - 1 if depth == 0 { close = i break } } } let params = if open >= 0 && close > open { split_top(rest[open + 1:close].to_string(), ',') } else { [] } let after = if close >= 0 { rest[close + 1:].to_string() } else { "" } let arrow = after.find("->") let ret = match arrow { Some(i) => trim(after[i + 2:].to_string()) None => "" } let gives = if ret == "" { "returns nothing" } else { "returns " + ty(ret) } if params.length() == 0 { bold(name) + " " + gives + "." } else { let ps = params.map(fn(p) { let pc = index_of(p, ':') if pc < 0 { bold(dashes(p)) } else { bold(dashes(trim(p[:pc].to_string()))) + " (" + ty(p[pc + 1:].to_string()) + ")" } }) bold(name) + " takes " + and_list(ps) + " and " + gives + "." } } ///| One field / case / call as a clause, plus its docs. fn entry_sentence(kind : String, entry : String, docs : Array[String]) -> String { let e = trim(entry) let body = if e.contains(": func(") { func_sentence(e) } else if kind == "record" { let c = index_of(e, ':') if c >= 0 { bold(dashes(trim(e[:c].to_string()))) + " is " + ty(e[c + 1:].to_string()) + "." } else { bold(dashes(e)) + "." } } else if kind == "variant" { let p = index_of(e, '(') if p > 0 && e.has_suffix(")") { bold(dashes(e[:p].to_string())) + " carries " + ty(e[p + 1:e.length() - 1].to_string()) + "." } else { bold(dashes(e)) + " carries nothing." } } else { bold(dashes(e)) + "." } body + (if docs.length() > 0 { " " + doc_prose(docs).trim(chars=" ").to_string() } else { "" }) } ///| Plain-English rendering of a `interface { ... }` block (as returned by extract_block). fn humanise_interface(name : String, block : Array[String]) -> Array[String] { let out : Array[String] = [] let mut i = 0 let head_docs : Array[String] = [] while i < block.length() && trim(block[i]).has_prefix("///") { head_docs.push(doc_text(block[i])) i = i + 1 } out.push("# " + dashes(name) + " (interface)") if head_docs.length() > 0 { out.push(doc_prose(head_docs).trim(chars=" ").to_string()) } i = i + 1 // header line let mut pending : Array[String] = [] let mut calls_shown = false let kinds = ["record", "enum", "variant", "flags", "resource"] while i < block.length() { let l = trim(block[i]) i = i + 1 if l.has_prefix("///") { pending.push(doc_text(l)) continue } if l == "" || l == "}" { continue } if l.has_prefix("use ") { pending = [] continue } let kw = first_word(l) if kinds.contains(kw) { let nm = dashes(first_word(trim(l[kw.length():].to_string()))) let entries : Array[(Array[String], String)] = [] let lb = index_of(l, '{') let rb = last_index_of(l, '}') if lb >= 0 && rb > lb { for e in split_top(l[lb + 1:rb].to_string(), ',') { entries.push(([], e)) } } else { let mut ed : Array[String] = [] while i < block.length() { let el = trim(block[i]) i = i + 1 if el.has_prefix("}") { break } if el.has_prefix("///") { ed.push(doc_text(el)) } else if el != "" { let clean = if el.has_suffix(",") || el.has_suffix(";") { el[:el.length() - 1].to_string() } else { el } entries.push((ed, clean)) ed = [] } } } out.push("") let intro = match kw { "record" => "A " + bold(nm) + " is a group of fields. " "enum" => "A " + bold(nm) + " is exactly one of " + and_list(entries.map(fn(e) { bold(dashes(e.1)) })) + ". " "variant" => "A " + bold(nm) + " is one of several cases, some carrying data. " "flags" => "A " + bold(nm) + " is any combination of " + and_list(entries.map(fn(e) { bold(dashes(e.1)) })) + ". " _ => "A " + bold(nm) + " is a handle owned by the host. " } let sb = StringBuilder::new() sb.write_string(intro + doc_prose(pending)) if kw == "record" || kw == "variant" { let parts = entries.map(fn(e) { entry_sentence(kw, e.1, e.0) }) sb.write_string(parts.join(" ")) } out.push(sb.to_string().trim(chars=" ").to_string()) } else if kw == "type" && l.contains("=") { let eq = index_of(l, '=') let nm = dashes(first_word(trim(l[kw.length():].to_string()))) let tgt = trim(l[eq + 1:].to_string()) let tgt = if tgt.has_suffix(";") { tgt[:tgt.length() - 1].to_string() } else { tgt } out.push("") out.push(bold(nm) + " is just another name for " + ty(tgt) + ". " + doc_prose(pending).trim(chars=" ").to_string()) } else if l.contains(": func(") { if !calls_shown { out.push("") out.push("What it can do:") calls_shown = true out.push("") } let clean = if l.has_suffix(";") { l[:l.length() - 1].to_string() } else { l } out.push(entry_sentence("func", clean, pending)) } pending = [] } out } ///| "chandra:desktop/theme@0.1.0;" -> "theme" (raw WIT identifier, dashes kept) fn ref_name(s : String) -> String { let mut r = trim(s) if r.has_suffix(";") { r = r[:r.length() - 1].to_string() } let sl = last_index_of(r, '/') if sl >= 0 { r = r[sl + 1:].to_string() } let at = index_of(r, '@') if at >= 0 { r = r[:at].to_string() } r } ///| "chandra:desktop/app-catalog@0.1.0" -> "app catalog" fn short_ref(s : String) -> String { dashes(ref_name(s)) } ///| Interfaces a world uses, resolved through `include`: exports first, then imports. fn world_interfaces(text : String, name : String) -> Array[String] { let imports : Array[String] = [] let exports : Array[String] = [] collect_world_raw(text, name, imports, exports, 0) let out : Array[String] = [] for e in exports { if !out.contains(e) { out.push(e) } } for i in imports { if !out.contains(i) { out.push(i) } } out } ///| fn collect_world_raw( text : String, name : String, imports : Array[String], exports : Array[String], depth : Int, ) -> Unit { if depth > 3 { return } for l in extract_block(text, "world", name) { let t = trim(l) if t.has_prefix("import ") { imports.push(ref_name(t[7:].to_string())) } else if t.has_prefix("export ") { exports.push(ref_name(t[7:].to_string())) } else if t.has_prefix("include ") { collect_world_raw(text, ref_name(t[8:].to_string()), imports, exports, depth + 1) } } } ///| fn collect_world( text : String, name : String, imports : Array[String], exports : Array[String], depth : Int, ) -> Unit { if depth > 3 { return } for l in extract_block(text, "world", name) { let t = trim(l) if t.has_prefix("import ") { imports.push(short_ref(t[7:].to_string())) } else if t.has_prefix("export ") { exports.push(short_ref(t[7:].to_string())) } else if t.has_prefix("include ") { collect_world(text, trim(short_ref(t[8:].to_string())).replace_all(old=" ", new="-"), imports, exports, depth + 1) } } } ///| Plain-English rendering of a world: what it provides and what it needs. fn humanise_world(text : String, name : String) -> Array[String] { let out : Array[String] = [] let block = extract_block(text, "world", name) let docs : Array[String] = [] for l in block { if trim(l).has_prefix("///") { docs.push(doc_text(l)) } else { break } } out.push("# " + dashes(name) + " (component)") if docs.length() > 0 { out.push(doc_prose(docs).trim(chars=" ").to_string()) } let imports : Array[String] = [] let exports : Array[String] = [] collect_world(text, name, imports, exports, 0) out.push("") out.push("It provides " + and_list(exports.map(bold)) + ", and needs " + and_list(imports.map(bold)) + " from the host.") out }