From 84ab3aee7921fc334a6ef3ce5d03558b94e502f1 Mon Sep 17 00:00:00 2001 From: theMackabu Date: Tue, 10 Mar 2026 14:27:17 -0700 Subject: [PATCH] migrate theme to theme.toml --- meson.build | 13 +++++++++++- meson/theme.py | 35 ++++++++++++++++++++++++++++++ src/highlight/emit.c | 46 +++------------------------------------- src/highlight/theme.toml | 43 +++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 44 deletions(-) create mode 100644 meson/theme.py create mode 100644 src/highlight/theme.toml diff --git a/meson.build b/meson.build index 921d487..32539d0 100644 --- a/meson.build +++ b/meson.build @@ -24,6 +24,13 @@ messages_h = custom_target('messages_h', capture: true, ) +theme_h = custom_target('theme_h', + input: 'src/highlight/theme.toml', + output: 'theme.h', + command: [python, files('meson/theme.py'), '@INPUT@'], + capture: true, +) + lib_sources = files(run_command( python, files('meson/sources.py'), files('sources.json'), 'engine', check: true @@ -31,7 +38,11 @@ lib_sources = files(run_command( libant = static_library( 'ant', - lib_sources + [snapshot_h, messages_h], + lib_sources + [ + snapshot_h, + messages_h, + theme_h + ], install: true, include_directories: include, dependencies: ant_deps diff --git a/meson/theme.py b/meson/theme.py new file mode 100644 index 0000000..0e16c33 --- /dev/null +++ b/meson/theme.py @@ -0,0 +1,35 @@ +import sys + +try: + import tomllib +except ModuleNotFoundError: + try: + import tomli as tomllib + except ModuleNotFoundError as exc: + raise ModuleNotFoundError( + "No TOML parser available. Use Python 3.11+ or install 'tomli'." + ) from exc + +with open(sys.argv[1], "rb") as f: + theme = tomllib.load(f) + +colors = theme.get("colors", {}) + +def to_enum(name): + return "HL_" + name.upper() + +print("#ifndef THEME_H") +print("#define THEME_H") +print("") +print('#include "highlight.h"') +print("") +print("static const char *hl_theme_color(hl_token_class cls) {") +print(" switch (cls) {") +for name, value in colors.items(): + escaped = value.replace("\\", "\\\\").replace('"', '\\"') + print(f' case {to_enum(name)}: return "{escaped}";') +print(" default: return NULL;") +print(" }") +print("}") +print("") +print("#endif") diff --git a/src/highlight/emit.c b/src/highlight/emit.c index 1a255d4..467fd83 100644 --- a/src/highlight/emit.c +++ b/src/highlight/emit.c @@ -1,4 +1,6 @@ #include + +#include "theme.h" #include "highlight/emit.h" static inline void ob_putc(hl_outbuf *o, char c) { @@ -30,52 +32,10 @@ void hl_outbuf_write_escaped(hl_outbuf *o, const char *s, size_t n) { for (size_t i = 0; i < n; i++) ob_put_escaped(o, s[i]); } -static const char *class_to_crvar(hl_token_class cls) { - switch (cls) { - case HL_NUMBER: return "#E8CD7C"; - case HL_NUMBER_PREFIX: return "#EADBAD"; - case HL_BOOLEAN: return "#65B2FF"; - case HL_LITERAL_NULL: return "#65B2FF"; - - case HL_STRING: return "#FF8A7F"; - case HL_STRING_DELIMITER: return "#FF7265"; - case HL_STRING_ESCAPE: return "#F4AAA3"; - case HL_STRING_KEY: return "#CCA3F4"; - case HL_STRING_TEMPLATE: return "#FFB265"; - - case HL_REGEX: return "#FFB265"; - case HL_REGEX_ESCAPE: return "#FFCC99"; - case HL_REGEX_DELIMITER: return "#FF9932"; - case HL_REGEX_CDATA: return "#65B2FF"; - - case HL_KEYWORD: return "#65B2FF"; - case HL_KEYWORD_DELETE: return "#F43D3D"; - case HL_TYPE: return "#59D8F1"; - case HL_TYPE_STRING: return "#30E8AA"; - case HL_TYPE_BOOLEAN: return "#30E8AA"; - case HL_COMMENT: return "#758CA3"; - case HL_FUNCTION_NAME: return "#30E8AA"; - case HL_FUNCTION: return "#30E8AA"; - case HL_ARGUMENT: return "#CCA3F4"; - case HL_PROPERTY: return "#CCA3F4"; - case HL_OPERATOR: return "#8CB2D8"; - case HL_OPTIONAL_CHAIN: return "#8CB2D8"; - case HL_BRACKET: return "#8CB2D8"; - case HL_SEMICOLON: return "#B2CCE5"; - - case HL_KEYWORD_ITALIC: return "italic+#65B2FF"; - case HL_CLASS_NAME: return "bold+#F7B76D"; - case HL_PARENT_CLASS: return "bold+#59D8F1"; - case HL_KEYWORD_EXTENDS: return "italic+#59D8F1"; - - default: return NULL; - } -} - static inline void ob_write_with_class(hl_outbuf *o, hl_token_class cls, const char *s, size_t n) { if (n == 0) return; - const char *var = class_to_crvar(cls); + const char *var = hl_theme_color(cls); if (var) { ob_putc(o, '<'); ob_puts(o, var); diff --git a/src/highlight/theme.toml b/src/highlight/theme.toml new file mode 100644 index 0000000..663a593 --- /dev/null +++ b/src/highlight/theme.toml @@ -0,0 +1,43 @@ +# Highlight theme — token class → crprintf color/style string +# Styles: hex colors (#RRGGBB), with optional prefix modifiers (bold+, italic+) + +[colors] +number = "#E8CD7C" +number_prefix = "#EADBAD" +boolean = "#65B2FF" +literal_null = "#65B2FF" + +string = "#FF8A7F" +string_delimiter = "#FF7265" +string_escape = "#F4AAA3" +string_key = "#CCA3F4" +string_template = "#FFB265" + +regex = "#FFB265" +regex_escape = "#FFCC99" +regex_delimiter = "#FF9932" +regex_cdata = "#65B2FF" + +keyword = "#65B2FF" +keyword_delete = "#F43D3D" +keyword_italic = "italic+#65B2FF" +keyword_extends = "italic+#59D8F1" + +type = "#59D8F1" +type_string = "#30E8AA" +type_boolean = "#30E8AA" + +comment = "#758CA3" + +function_name = "#30E8AA" +function = "#30E8AA" +argument = "#CCA3F4" +property = "#CCA3F4" + +operator = "#8CB2D8" +optional_chain = "#8CB2D8" +bracket = "#8CB2D8" +semicolon = "#B2CCE5" + +class_name = "bold+#F7B76D" +parent_class = "bold+#59D8F1" -- 2.51.2