Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions ast_utils/color-custom.c2
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/* Copyright 2025 Charlie Gordon
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

module color;

import ctype local;
import stdio local;
import stdlib local;
import string local;

const char*[] standardColors = {
"black", Black,
"red", Red,
"green", Green,
"yellow", Yellow,
"blue", Blue,
"magenta", Magenta,
"cyan", Cyan,
"grey", Grey,
"darkgrey", Darkgrey,
"bred", Bred,
"bgreen", Bgreen,
"byellow", Byellow,
"bblue", Bblue,
"bmagenta", Bmagenta,
"bcyan", Bcyan,
"white", White,
"normal", Normal,
}

fn bool getStyleDef(char* buf1, u32 size1, char* buf2, u32 size2, const char** pp) {
const char *p = *pp;
while (isspace(*p))
p++;
if (!*p)
return false;
u32 i = 0;
while (isalpha(*p) || *p == '.' || *p == '_') {
char c = (char)tolower(*p++);
if (i + 1 < size1)
buf1[i++] = c;
}
buf1[i] = '\0';
if (*p != '=' && *p != ':')
return false;
p++;
i = 0;
while (*p && *p != ' ' && *p != ',' && *p != ';') {
char c = (char)tolower(*p++);
if (i + 1 < size2 && c != '-' && c != '_')
buf2[i++] = c;
}
buf2[i] = '\0';
if (*p == ',' || *p == ';')
p++;
*pp = p;
return true;
}

fn bool matchColorName(const char *p, const char *name) {
while (*p) {
char c = *p++;
if (c == 'b' && !strncmp(p, "right", 5))
p += 5;
if (c != *name++)
return false;
}
return *name == '\0';
}

fn const char* convertColor(const char *val, const char *def) {
if (*val == '\0')
return "";

for (u32 i = 0; i < elemsof(standardColors); i += 2) {
if (matchColorName(val, standardColors[i]))
return standardColors[i + 1];
}
if (!strcasecmp(val, "default"))
return def;

char[32] buf;
i32 pal, r, g, b;
if (sscanf(val, "%*1[pP]%d", &pal) == 1) {
snprintf(buf, elemsof(buf), "\033[38;5;%dm", pal);
} else
if (sscanf(val, "#%2x%2x%2x", &r, &g, &b) == 3) {
snprintf(buf, elemsof(buf), "\033[38;2;%d;%d;%dm", r, g, b);
} else {
// TODO: complain about unknown color
return def;
}
return strdup(buf);
}

public fn void freeConfigColor(const char* p) {
if (p && *p) {
for (u32 i = 0; i < elemsof(standardColors); i++) {
if (standardColors[i] == p) return;
}
free((void*)p);
}
}

public fn const char* getConfigColor(const char* cat, const char* def) {
const char *c2_colors = getenv("C2_COLORS");
if (c2_colors) {
const char *p = c2_colors;
char[16] style;
char[16] val;
if (!strcmp(p, "none"))
return "";
while (getStyleDef(style, elemsof(style), val, elemsof(val), &p)) {
if (!strcmp(style, cat))
return convertColor(val, def);
}
}
return def;
}
5 changes: 2 additions & 3 deletions parser/c2_tokenizer.c2
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,7 @@ fn void Tokenizer.num_error(Tokenizer* t, Token* result, const char* p, const ch
vsnprintf(t.error_msg, sizeof(t.error_msg), format, args);
va_end(args);

// XXX: error position should be passed separately from token start
result.loc = t.loc_start + (SrcLoc)(p - t.input_start);
SrcLoc err_loc = t.loc_start + (SrcLoc)(p - t.input_start);
// read the rest of the pp-number token
for (;;) {
if ((*p == 'e' || *p == 'E' || *p == 'p' || *p == 'P') && (p[1] == '+' || p[1] == '-')) {
Expand All @@ -712,7 +711,7 @@ fn void Tokenizer.num_error(Tokenizer* t, Token* result, const char* p, const ch
}
t.cur = p;
result.len = (u16)((p - t.input_start) - (result.loc - t.loc_start));
if (t.on_warning) t.on_warning(t.fn_arg, result.loc);
if (t.on_warning) t.on_warning(t.fn_arg, err_loc);
}

fn void Tokenizer.lex_identifier(Tokenizer* t, Token* result) {
Expand Down
1 change: 1 addition & 0 deletions recipe.txt
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ executable c2cat
$backend c

ast_utils/color.c2
ast_utils/color-custom.c2
ast_utils/constants.c2
ast_utils/number_radix.c2
ast_utils/src_loc.c2
Expand Down
Loading