| 1 |
// SPDX-License-Identifier: GPL-3.0-only |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use bufio; |
| 5 |
use fmt; |
| 6 |
use hare::ast; |
| 7 |
use hare::lex; |
| 8 |
use hare::unparse; |
| 9 |
use io; |
| 10 |
use os; |
| 11 |
use strings; |
| 12 |
use unix::tty; |
| 13 |
|
| 14 |
let no_color: bool = false; |
| 15 |
|
| 16 |
// Formats output as Hare source code (prototypes) with syntax highlighting |
| 17 |
export fn emit_tty(ctx: *context) (void | error) = { |
| 18 |
if (os::tryenv("FORCE_COLOR", "") != "" || |
| 19 |
(os::tryenv("NO_COLOR", "") == "" && |
| 20 |
tty::isatty(os::stdout_file))) { |
| 21 |
init_colors()?; |
| 22 |
} else { |
| 23 |
no_color = true; |
| 24 |
}; |
| 25 |
|
| 26 |
const summary = ctx.summary; |
| 27 |
|
| 28 |
if (ctx.ambiguous) { |
| 29 |
const id = unparse::identstr(ctx.ident); |
| 30 |
defer free(id); |
| 31 |
|
| 32 |
if (!no_color) fmt::fprintf(ctx.out, "\x1b[{}m", |
| 33 |
color(unparse::synkind::COMMENT))?; |
| 34 |
fmt::fprint(ctx.out, "// ")?; |
| 35 |
if (!no_color) fmt::fprint(ctx.out, "\x1b[93m")?; |
| 36 |
fmt::fprint(ctx.out, "NOTE")?; |
| 37 |
if (!no_color) fmt::fprintf(ctx.out, "\x1b[m" "\x1b[{}m", |
| 38 |
color(unparse::synkind::COMMENT))?; |
| 39 |
fmt::fprintf(ctx.out, ": {} also refers to module [[{}::]]", |
| 40 |
id, id)?; |
| 41 |
if (!no_color) fmt::fprint(ctx.out, "\x1b[m")?; |
| 42 |
fmt::fprintln(ctx.out, "\n")?; |
| 43 |
}; |
| 44 |
|
| 45 |
if (len(ctx.parse_errs) > 0) { |
| 46 |
if (!no_color) fmt::fprintf(ctx.out, "\x1b[{}m", |
| 47 |
color(unparse::synkind::COMMENT))?; |
| 48 |
fmt::fprint(ctx.out, "// ")?; |
| 49 |
if (!no_color) fmt::fprint(ctx.out, "\x1b[93m")?; |
| 50 |
fmt::fprint(ctx.out, "WARNING")?; |
| 51 |
if (!no_color) fmt::fprintf(ctx.out, "\x1b[m" "\x1b[{}m", |
| 52 |
color(unparse::synkind::COMMENT))?; |
| 53 |
fmt::fprintln(ctx.out, |
| 54 |
": parsing errors occurred; documentation may be incomplete")?; |
| 55 |
if (!no_color) fmt::fprint(ctx.out, "\x1b[m")?; |
| 56 |
}; |
| 57 |
for (let i = 0z; i < len(ctx.parse_errs); i += 1) { |
| 58 |
fmt::fprintln(ctx.out, "//", lex::strerror(ctx.parse_errs[i]))?; |
| 59 |
}; |
| 60 |
if (len(ctx.parse_errs) > 0) { |
| 61 |
fmt::fprintln(ctx.out)?; |
| 62 |
}; |
| 63 |
|
| 64 |
match (ctx.readme) { |
| 65 |
case let readme: io::file => |
| 66 |
let rbuf: [os::BUFSZ]u8 = [0...]; |
| 67 |
let readme = bufio::init(readme, rbuf, []); |
| 68 |
let sc = bufio::newscanner(&readme); |
| 69 |
defer bufio::finish(&sc); |
| 70 |
for (let line => bufio::scan_line(&sc)?) { |
| 71 |
firstline = false; |
| 72 |
if (!no_color) fmt::fprintf(ctx.out, "\x1b[{}m", |
| 73 |
color(unparse::synkind::COMMENT))?; |
| 74 |
fmt::fprint(ctx.out, "//", line)?; |
| 75 |
if (!no_color) fmt::fprint(ctx.out, "\x1b[m")?; |
| 76 |
fmt::fprintln(ctx.out)?; |
| 77 |
}; |
| 78 |
case void => void; |
| 79 |
}; |
| 80 |
|
| 81 |
emit_submodules_tty(ctx)?; |
| 82 |
|
| 83 |
// XXX: Should we emit the dependencies, too? |
| 84 |
let printed = false; |
| 85 |
for (let t &.. summary.types) { |
| 86 |
if (details_tty(ctx, t)?) { |
| 87 |
printed = true; |
| 88 |
}; |
| 89 |
}; |
| 90 |
for (let c &.. summary.constants) { |
| 91 |
if (details_tty(ctx, c)?) { |
| 92 |
printed = true; |
| 93 |
}; |
| 94 |
}; |
| 95 |
for (let e &.. summary.errors) { |
| 96 |
if (details_tty(ctx, e)?) { |
| 97 |
printed = true; |
| 98 |
}; |
| 99 |
}; |
| 100 |
for (let g &.. summary.globals) { |
| 101 |
if (details_tty(ctx, g)?) { |
| 102 |
printed = true; |
| 103 |
}; |
| 104 |
}; |
| 105 |
for (let f &.. summary.funcs) { |
| 106 |
if (details_tty(ctx, f)?) { |
| 107 |
printed = true; |
| 108 |
}; |
| 109 |
}; |
| 110 |
|
| 111 |
if (!printed) { |
| 112 |
if (!firstline) { |
| 113 |
fmt::fprintln(ctx.out)?; |
| 114 |
}; |
| 115 |
if (!no_color) fmt::fprintf(ctx.out, "\x1b[{}m", |
| 116 |
color(unparse::synkind::COMMENT))?; |
| 117 |
fmt::fprint(ctx.out, "// No exported declarations")?; |
| 118 |
if (!no_color) fmt::fprint(ctx.out, "\x1b[m")?; |
| 119 |
fmt::fprintln(ctx.out)?; |
| 120 |
}; |
| 121 |
}; |
| 122 |
|
| 123 |
fn emit_submodules_tty(ctx: *context) (void | error) = { |
| 124 |
const ident = unparse::identstr(ctx.ident); |
| 125 |
defer free(ident); |
| 126 |
|
| 127 |
let max_submod = 0z; |
| 128 |
for (let submod .. ctx.submods) { |
| 129 |
const ln = len(submod.name); |
| 130 |
if (max_submod < ln) { |
| 131 |
max_submod = ln; |
| 132 |
}; |
| 133 |
}; |
| 134 |
|
| 135 |
if (len(ctx.submods) != 0) { |
| 136 |
if (!firstline) { |
| 137 |
fmt::fprintln(ctx.out)?; |
| 138 |
}; |
| 139 |
firstline = false; |
| 140 |
if (!no_color) fmt::fprintf(ctx.out, "\x1b[{}m", |
| 141 |
color(unparse::synkind::COMMENT))?; |
| 142 |
if (len(ctx.ident) == 0) { |
| 143 |
fmt::fprintln(ctx.out, "// Modules")?; |
| 144 |
} else { |
| 145 |
fmt::fprintln(ctx.out, "// Submodules")?; |
| 146 |
}; |
| 147 |
for (let submod .. ctx.submods) { |
| 148 |
const id = if (len(ident) != 0) { |
| 149 |
yield strings::concat(ident, "::", submod.name)!; |
| 150 |
} else { |
| 151 |
yield strings::dup(submod.name)!; |
| 152 |
}; |
| 153 |
defer free(id); |
| 154 |
|
| 155 |
if (submod.desc != "") { |
| 156 |
const padding = max_submod - len(submod.name); |
| 157 |
fmt::fprintfln(ctx.out, "// - [[{}::]] {%}{}", |
| 158 |
id, |
| 159 |
"", &fmt::mods { |
| 160 |
pad = ' ', |
| 161 |
width = padding, |
| 162 |
... |
| 163 |
}, |
| 164 |
submod.desc)?; |
| 165 |
} else { |
| 166 |
fmt::fprintfln(ctx.out, "// - [[{}::]]", id)?; |
| 167 |
}; |
| 168 |
}; |
| 169 |
}; |
| 170 |
}; |
| 171 |
|
| 172 |
fn details_tty(ctx: *context, decl: *ast::decl) (bool | error) = { |
| 173 |
if (len(decl.docs) == 0 && !ctx.show_undocumented) { |
| 174 |
return false; |
| 175 |
}; |
| 176 |
|
| 177 |
if (!no_color) fmt::fprint(ctx.out, "\x1b[m")?; // reset styling |
| 178 |
if (!firstline) { |
| 179 |
fmt::fprintln(ctx.out)?; |
| 180 |
}; |
| 181 |
firstline = false; |
| 182 |
|
| 183 |
match (&decl.decl) { |
| 184 |
case let d: *ast::decl_func => |
| 185 |
d.symbol = ""; |
| 186 |
case => void; |
| 187 |
}; |
| 188 |
|
| 189 |
unparse::decl(ctx.out, &syn_tty, decl)?; |
| 190 |
fmt::fprintln(ctx.out)?; |
| 191 |
|
| 192 |
if (ctx.show_lineno) { |
| 193 |
if (!no_color) fmt::fprintf(ctx.out, "\x1b[{}m", |
| 194 |
color(unparse::synkind::COMMENT))?; |
| 195 |
fmt::fprintfln(ctx.out, "// See {}:{}", |
| 196 |
cwdpath(decl.loc.start.path), decl.loc.start.line)?; |
| 197 |
}; |
| 198 |
|
| 199 |
return true; |
| 200 |
}; |
| 201 |
|
| 202 |
fn syn_tty( |
| 203 |
ctx: *unparse::context, |
| 204 |
s: str, |
| 205 |
kind: unparse::synkind, |
| 206 |
) (size | io::error) = { |
| 207 |
let z = 0z; |
| 208 |
if (!no_color) z += fmt::fprintf(ctx.out, "\x1b[{}m", color(kind))?; |
| 209 |
z += unparse::syn_wrap(ctx, s, kind)?; |
| 210 |
if (!no_color) z += fmt::fprint(ctx.out, "\x1b[m")?; |
| 211 |
return z; |
| 212 |
}; |