hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit dcc798b0778075e952183f4b814feed6620f8f89
parent e871f45282929e6c7fb6c4305ab9a6e61ff68bbd
Author: Sebastian <sebastian@sebsite.pw>
Date:   Sat,  4 Feb 2023 01:32:47 -0500

haredoc: separate highlighting for functions, globals, and typedefs

No changes to the default highlighting are done, but this allows the
user to set different colors for different top-level declarations in
HAREDOC_COLORS.

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mcmd/haredoc/color.ha | 24+++++++++++++++++-------
Mcmd/haredoc/tty.ha | 9+++++----
2 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/cmd/haredoc/color.ha b/cmd/haredoc/color.ha @@ -12,13 +12,16 @@ type syn = enum uint { NORMAL, COMMENT, PRIMARY, + CONSTANT, + FUNCTION, + GLOBAL, + TYPEDEF, SECONDARY, KEYWORD, TYPE, ATTRIBUTE, OPERATOR, PUNCTUATION, - CONSTANT, STRING, NUMBER, }; @@ -28,13 +31,16 @@ let COLORS: [_](str, str) = [ ("normal" , "0"), ("comment" , "1"), ("primary" , "0"), + ("constant" , "0"), + ("function" , "0"), + ("global" , "0"), + ("typedef" , "0"), ("secondary" , "0"), ("keyword" , "94"), ("type" , "96"), ("attribute" , "33"), ("operator" , "1"), ("punctuation" , "0"), - ("constant" , "91"), ("string" , "91"), ("number" , "95"), ]; @@ -77,10 +83,14 @@ fn init_colors() void = { }; fn render(h: io::handle, syntax: syn) (size | io::error) = { - switch (COLORS[syntax].1) { - case "_" => - return fmt::fprintf(h, "\x1b[0;{}m", COLORS[syn::NORMAL].1)?; - case => - return fmt::fprintf(h, "\x1b[0;{}m", COLORS[syntax].1)?; + if (COLORS[syntax].1 == "_") { + syntax = switch (syntax) { + case syn::CONSTANT, syn::FUNCTION, syn::GLOBAL, syn::TYPEDEF => + yield if (COLORS[syn::PRIMARY].1 == "_") syn::NORMAL + else syn::PRIMARY; + case => + yield syn::NORMAL; + }; }; + return fmt::fprintf(h, "\x1b[0;{}m", COLORS[syntax].1)?; }; diff --git a/cmd/haredoc/tty.ha b/cmd/haredoc/tty.ha @@ -157,7 +157,7 @@ fn unparse_tty(out: io::handle, d: ast::decl) (size | io::error) = { n += fmt::fprintf(out, ") ")?; n += render(out, syn::NORMAL)?; }; - n += render(out, syn::PRIMARY)?; + n += render(out, syn::GLOBAL)?; n += unparse::ident(out, g[i].ident)?; n += render(out, syn::PUNCTUATION)?; n += fmt::fprint(out, ": ")?; @@ -172,7 +172,7 @@ fn unparse_tty(out: io::handle, d: ast::decl) (size | io::error) = { n += render(out, syn::KEYWORD)?; n += fmt::fprintf(out, "def ")?; for (let i = 0z; i < len(c); i += 1) { - n += render(out, syn::PRIMARY)?; + n += render(out, syn::CONSTANT)?; n += unparse::ident(out, c[i].ident)?; n += render(out, syn::PUNCTUATION)?; n += fmt::fprint(out, ": ")?; @@ -186,7 +186,7 @@ fn unparse_tty(out: io::handle, d: ast::decl) (size | io::error) = { n += render(out, syn::KEYWORD)?; n += fmt::fprint(out, "type ")?; for (let i = 0z; i < len(t); i += 1) { - n += render(out, syn::PRIMARY)?; + n += render(out, syn::TYPEDEF)?; n += unparse::ident(out, t[i].ident)?; n += render(out, syn::PUNCTUATION)?; n += fmt::fprint(out, " = ")?; @@ -227,8 +227,9 @@ fn unparse_tty(out: io::handle, d: ast::decl) (size | io::error) = { }; n += render(out, syn::KEYWORD)?; n += fmt::fprint(out, "fn ")?; - n += render(out, syn::PRIMARY)?; + n += render(out, syn::FUNCTION)?; n += unparse::ident(out, f.ident)?; + n += fmt::fprint(out, "\x1b[0m")?; n += prototype_tty(out, 0, f.prototype.repr as ast::func_type)?; };