Skip to content

Commit c546d14

Browse files
committed
c2cat: improve output consistency
* output tokens with the original spelling * use Style enum to support custom colors * show syntax errors * add `--color` and `--nocolor` to force/disable color output
1 parent adb108a commit c546d14

File tree

4 files changed

+374
-224
lines changed

4 files changed

+374
-224
lines changed

ast_utils/color-custom.c2

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/* Copyright 2025 Charlie Gordon
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
module color;
17+
18+
import ctype local;
19+
import stdio local;
20+
import stdlib local;
21+
import string local;
22+
23+
const char*[] standardColors = {
24+
"black", Black,
25+
"red", Red,
26+
"green", Green,
27+
"yellow", Yellow,
28+
"blue", Blue,
29+
"magenta", Magenta,
30+
"cyan", Cyan,
31+
"grey", Grey,
32+
"darkgrey", Darkgrey,
33+
"bred", Bred,
34+
"bgreen", Bgreen,
35+
"byellow", Byellow,
36+
"bblue", Bblue,
37+
"bmagenta", Bmagenta,
38+
"bcyan", Bcyan,
39+
"white", White,
40+
"normal", Normal,
41+
}
42+
43+
fn bool getStyleDef(char* buf1, u32 size1, char* buf2, u32 size2, const char** pp) {
44+
const char *p = *pp;
45+
while (isspace(*p))
46+
p++;
47+
if (!*p)
48+
return false;
49+
u32 i = 0;
50+
while (isalpha(*p) || *p == '.' || *p == '_') {
51+
char c = (char)tolower(*p++);
52+
if (i + 1 < size1)
53+
buf1[i++] = c;
54+
}
55+
buf1[i] = '\0';
56+
if (*p != '=' && *p != ':')
57+
return false;
58+
p++;
59+
i = 0;
60+
while (*p && *p != ' ' && *p != ',' && *p != ';') {
61+
char c = (char)tolower(*p++);
62+
if (i + 1 < size2 && c != '-' && c != '_')
63+
buf2[i++] = c;
64+
}
65+
buf2[i] = '\0';
66+
if (*p == ',' || *p == ';')
67+
p++;
68+
*pp = p;
69+
return true;
70+
}
71+
72+
fn bool matchColorName(const char *p, const char *name) {
73+
while (*p) {
74+
char c = *p++;
75+
if (c == 'b' && !strncmp(p, "right", 5))
76+
p += 5;
77+
if (c != *name++)
78+
return false;
79+
}
80+
return *name == '\0';
81+
}
82+
83+
fn const char* convertColor(const char *val, const char *def) {
84+
if (*val == '\0')
85+
return "";
86+
87+
for (u32 i = 0; i < elemsof(standardColors); i += 2) {
88+
if (matchColorName(val, standardColors[i]))
89+
return standardColors[i + 1];
90+
}
91+
if (!strcasecmp(val, "default"))
92+
return def;
93+
94+
char[32] buf;
95+
i32 pal, r, g, b;
96+
if (sscanf(val, "%*1[pP]%d", &pal) == 1) {
97+
snprintf(buf, elemsof(buf), "\033[38;5;%dm", pal);
98+
} else
99+
if (sscanf(val, "#%2x%2x%2x", &r, &g, &b) == 3) {
100+
snprintf(buf, elemsof(buf), "\033[38;2;%d;%d;%dm", r, g, b);
101+
} else {
102+
// TODO: complain about unknown color
103+
return def;
104+
}
105+
return strdup(buf);
106+
}
107+
108+
public fn void freeConfigColor(const char* p) {
109+
if (p && *p) {
110+
for (u32 i = 0; i < elemsof(standardColors); i++) {
111+
if (standardColors[i] == p) return;
112+
}
113+
free((void*)p);
114+
}
115+
}
116+
117+
public fn const char* getConfigColor(const char* cat, const char* def) {
118+
const char *c2_colors = getenv("C2_COLORS");
119+
if (c2_colors) {
120+
const char *p = c2_colors;
121+
char[16] style;
122+
char[16] val;
123+
if (!strcmp(p, "none"))
124+
return "";
125+
while (getStyleDef(style, elemsof(style), val, elemsof(val), &p)) {
126+
if (!strcmp(style, cat))
127+
return convertColor(val, def);
128+
}
129+
}
130+
return def;
131+
}

parser/c2_tokenizer.c2

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,7 @@ fn void Tokenizer.num_error(Tokenizer* t, Token* result, const char* p, const ch
694694
vsnprintf(t.error_msg, sizeof(t.error_msg), format, args);
695695
va_end(args);
696696

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

718717
fn void Tokenizer.lex_identifier(Tokenizer* t, Token* result) {

recipe.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ executable c2cat
378378
$backend c
379379

380380
ast_utils/color.c2
381+
ast_utils/color-custom.c2
381382
ast_utils/constants.c2
382383
ast_utils/number_radix.c2
383384
ast_utils/src_loc.c2

0 commit comments

Comments
 (0)