hare

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

commit e6eab611b890f818f42ef60736c3818ecea90eaf
parent aa43e62efcb03cfd95d0eeedf7d84421d512bbd1
Author: Sebastian <sebastian@sebsite.pw>
Date:   Sat, 28 Oct 2023 02:34:09 -0400

haredoc: fix HAREDOC_COLORS bugs

Pretty sure all of these were my fault.

- Setting color values to empty strings now correctly resets them to
  their default.
- Memory bugs with invalid key error messages are fixed.
- Default color values which used to be "0" are now "_", so "normal" and
  "primary" work as intended.
- Some logic surrounding "normal" and "primary" has been fixed as well.

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

Diffstat:
Mcmd/haredoc/doc/color.ha | 65+++++++++++++++++++++++++++++++++++++----------------------------
Mcmd/haredoc/doc/types.ha | 12+++++-------
Mdocs/haredoc.1.scd | 20++++++++++----------
3 files changed, 52 insertions(+), 45 deletions(-)

diff --git a/cmd/haredoc/doc/color.ha b/cmd/haredoc/doc/color.ha @@ -2,6 +2,7 @@ // (c) Hare authors <https://harelang.org> use ascii; +use fmt; use hare::unparse; use io; use os; @@ -9,27 +10,28 @@ use regex; use strings; // Colors/Renditions with defaults; SGR parameters for ANSI escape sequences. -let colors: [_]str = [ - "0", // ident +const default_colors = [ + "_", // ident "1", // comment - "0", // constant - "0", // function - "0", // global - "0", // typedef - "0", // import_alias - "0", // secondary + "_", // constant + "_", // function + "_", // global + "_", // typedef + "_", // import_alias + "_", // secondary "94", // keyword "96", // type "33", // attribute "1", // operator - "0", // punctuation + "_", // punctuation "91", // rune_string "95", // number - "0", // label + "_", // label ]; -let normal_color = "0"; -let primary_color = "0"; +let colors: [len(default_colors)]str = [""...]; +let normal_color = ""; +let primary_color = ""; fn init_colors() (void | error) = { const env_colors = os::tryenv("HAREDOC_COLORS", ""); @@ -41,10 +43,7 @@ fn init_colors() (void | error) = { defer regex::result_freeall(matches); for (let i = 0z; i < len(matches); i += 1) :colors { - let (k, v) = (matches[i][1].content, matches[i][2].content); - if (v == "") { - continue; - }; + let (k, v) = (matches[i][1].content, matches[i][2].content); let idx = 0z; let out: *str = switch (k) { @@ -85,24 +84,34 @@ fn init_colors() (void | error) = { case "primary" => yield &primary_color; case => - return k: haredoc_colors_error; + static let err: [64]u8 = [0...]; + if (len(k) > len(err)) { + return "": haredoc_colors_error; + }; + return fmt::bsprint(err, k): haredoc_colors_error; }; - *out = if (v == "_" && k != "normal") "0" else v; + *out = if (v == "_" && k == "normal") "0" else v; }; }; fn color(kind: unparse::synkind) str = { - if (colors[kind] != "_") { - return colors[kind]; + const color = if (colors[kind] != "") colors[kind] + else default_colors[kind]; + if (color != "_") { + return color; }; - switch (kind) { - case unparse::synkind::CONSTANT, - unparse::synkind::FUNCTION, - unparse::synkind::GLOBAL, - unparse::synkind::TYPEDEF => - return primary_color; - case => - return normal_color; + + if (primary_color != "" && primary_color != "_") { + switch (kind) { + case unparse::synkind::CONSTANT, + unparse::synkind::FUNCTION, + unparse::synkind::GLOBAL, + unparse::synkind::TYPEDEF => + return primary_color; + case => void; + }; }; + + return if (normal_color == "") "0" else normal_color; }; diff --git a/cmd/haredoc/doc/types.ha b/cmd/haredoc/doc/types.ha @@ -15,8 +15,6 @@ export type haredoc_colors_error = !str; export type error = !(lex::error | parse::error | io::error | module::error | exec::error | fs::error | haredoc_colors_error); -def HAREDOC_COLORS_ERROR_MSG = "Error parsing HAREDOC_COLORS: invalid key"; - export fn strerror(err: error) str = { match (err) { case let err: lex::error => @@ -32,12 +30,12 @@ export fn strerror(err: error) str = { case let err: fs::error => return fs::strerror(err); case let err: haredoc_colors_error => - let buf: [128]u8 = [0...]; - if (len(HAREDOC_COLORS_ERROR_MSG) + len(err) + 3 > len(buf)) { - return HAREDOC_COLORS_ERROR_MSG; + def ERRMSG = "Error parsing HAREDOC_COLORS: invalid key"; + if (len(err) == 0) { + return ERRMSG; }; - return fmt::bsprintf(buf, "{} '{}'", - HAREDOC_COLORS_ERROR_MSG, err); + static let buf: [len(ERRMSG) + 64 + 3]u8 = [0...]; + return fmt::bsprintf(buf, "{} '{}'", ERRMSG, err); }; }; diff --git a/docs/haredoc.1.scd b/docs/haredoc.1.scd @@ -52,23 +52,23 @@ category. A valid _seq_ must consist only of digits and semicolons, or must be a single underscore "\_". Here are the initial default entries: . normal "0" -. primary "0" -. ident "0" +. primary "\_" (-> normal) +. ident "\_" (-> normal) . comment "1" -. constant "0" -. function "0" -. global "0" -. typedef "0" -. import_alias "0" -. secondary "0" +. constant "\_" (-> primary) +. function "\_" (-> primary) +. global "\_" (-> primary) +. typedef "\_" (-> primary) +. import_alias "\_" (-> normal) +. secondary "\_" (-> normal) . keyword "94" . type "96" . attribute "33" . operator "1" -. punctuation "0" +. punctuation "\_" (-> normal) . rune_string "91" . number "95" -. label "0" +. label "\_" (-> normal) Any number of entries can be specified. If a _seq_ is an underscore "\_", then the sequence specified for "normal" is used, unless _key_ is "constant",