Skip to content

Commit 08828f6

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 * add color customisation
1 parent 7d5de80 commit 08828f6

File tree

5 files changed

+465
-211
lines changed

5 files changed

+465
-211
lines changed

ast_utils/color.c2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public type Color enum u8 {
3535
Bcyan,
3636
White,
3737
Normal,
38+
39+
CustomStart,
40+
CustomEnd = Color.CustomStart + 15,
3841
}
3942

4043
public const Color Black = Black;

ast_utils/color_config.c2

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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+
[Color.Black] = "black",
25+
[Color.Red] = "red",
26+
[Color.Green] = "green",
27+
[Color.Yellow] = "yellow",
28+
[Color.Blue] = "blue",
29+
[Color.Magenta] = "magenta",
30+
[Color.Cyan] = "cyan",
31+
[Color.Grey] = "grey",
32+
[Color.Darkgrey] = "darkgrey",
33+
[Color.Bred] = "bred",
34+
[Color.Bgreen] = "bgreen",
35+
[Color.Byellow] = "byellow",
36+
[Color.Bblue] = "bblue",
37+
[Color.Bmagenta] = "bmagenta",
38+
[Color.Bcyan] = "bcyan",
39+
[Color.White] = "white",
40+
[Color.Normal] = "normal",
41+
}
42+
43+
fn Color findStandardColor(const char* name) {
44+
for (u32 i = 0; i < elemsof(standardColors); i++) {
45+
if (standardColors[i] && !strcmp(standardColors[i], name))
46+
return (Color)i;
47+
}
48+
if (!strncmp(name, "custom", 6)) {
49+
i32 n = atoi(name + 6);
50+
if (n > 0 && n <= Color.CustomEnd - Color.CustomStart + 1)
51+
return Color.CustomStart + n - 1;
52+
}
53+
return Color.None;
54+
}
55+
56+
fn bool getStyleDef(char* buf1, u32 size1, char* buf2, u32 size2, const char** pp) {
57+
const char *p = *pp;
58+
while (isspace(*p))
59+
p++;
60+
if (!*p)
61+
return false;
62+
u32 i = 0;
63+
while (isalpha(*p) || *p == '.' || *p == '_') {
64+
char c = (char)tolower(*p++);
65+
if (i + 1 < size1)
66+
buf1[i++] = c;
67+
}
68+
buf1[i] = '\0';
69+
if (*p != '=' && *p != ':')
70+
return false;
71+
p++;
72+
i = 0;
73+
while (*p && *p != ' ' && *p != ',' && *p != ';') {
74+
char c = (char)tolower(*p++);
75+
if (i + 1 < size2 && c != '-' && c != '_')
76+
buf2[i++] = c;
77+
}
78+
buf2[i] = '\0';
79+
if (*p == ',' || *p == ';')
80+
p++;
81+
*pp = p;
82+
return true;
83+
}
84+
85+
fn bool matchColorName(const char *p, const char *name) {
86+
if (!name) return false;
87+
while (*p) {
88+
char c = *p++;
89+
if (c == 'b' && !strncmp(p, "right", 5))
90+
p += 5;
91+
if (c != *name++)
92+
return false;
93+
}
94+
return *name == '\0';
95+
}
96+
97+
fn const char* makeColorString(const char *val) {
98+
if (!val || *val == '\0' || !strcasecmp(val, "default"))
99+
return nil;
100+
101+
for (u32 i = 0; i < elemsof(standardColors); i++) {
102+
if (matchColorName(val, standardColors[i]))
103+
return defaultColors[i];
104+
}
105+
106+
char[32] buf;
107+
i32 pal, r, g, b;
108+
if (sscanf(val, "%*1[pP]%d", &pal) == 1) {
109+
snprintf(buf, elemsof(buf), "\033[38;5;%dm", pal);
110+
return strdup(buf);
111+
} else
112+
if (sscanf(val, "#%2x%2x%2x", &r, &g, &b) == 3) {
113+
snprintf(buf, elemsof(buf), "\033[38;2;%d;%d;%dm", r, g, b);
114+
return strdup(buf);
115+
} else {
116+
// TODO: complain about unknown color
117+
return nil;
118+
}
119+
}
120+
121+
fn Color convertColor(const char *val, Color def) {
122+
if (!val || *val == '\0')
123+
return None;
124+
125+
for (u32 i = 0; i < elemsof(standardColors); i++) {
126+
if (matchColorName(val, standardColors[i]))
127+
return (Color)i;
128+
}
129+
if (!strcasecmp(val, "default"))
130+
return def;
131+
132+
char[32] buf;
133+
i32 pal, r, g, b;
134+
if (sscanf(val, "%*1[pP]%d", &pal) == 1) {
135+
snprintf(buf, elemsof(buf), "\033[38;5;%dm", pal);
136+
} else
137+
if (sscanf(val, "#%2x%2x%2x", &r, &g, &b) == 3) {
138+
snprintf(buf, elemsof(buf), "\033[38;2;%d;%d;%dm", r, g, b);
139+
} else {
140+
// TODO: complain about unknown color
141+
return def;
142+
}
143+
144+
for (u32 i = Color.CustomStart; i <= Color.CustomEnd; i++) {
145+
if (!defaultColors[i]) {
146+
defaultColors[i] = strdup(buf);
147+
return (Color)i;
148+
}
149+
}
150+
151+
// TODO: complain about out of custom colors
152+
return def;
153+
}
154+
155+
public fn void freeCustomColors(Color start, Color end) {
156+
#if 0
157+
if (start < Color.CustomStart)
158+
start = Color.CustomStart;
159+
if (end > Color.CustomEnd)
160+
end = Color.CustomEnd;
161+
for (u32 i = start; i <= end; i++) {
162+
if (standardColors[i]) {
163+
free((char *)standardColors[i]);
164+
standardColors[i] = nil;
165+
}
166+
}
167+
#endif
168+
}
169+
170+
public fn Color getConfigColor(const char* varname, const char* cat, Color def) {
171+
const char *c2_colors = getenv(varname);
172+
if (c2_colors) {
173+
const char *p = c2_colors;
174+
char[16] style;
175+
char[16] val;
176+
if (!strcmp(p, "none"))
177+
return None;
178+
while (getStyleDef(style, elemsof(style), val, elemsof(val), &p)) {
179+
if (!strcmp(style, cat))
180+
return convertColor(val, def);
181+
}
182+
}
183+
return def;
184+
}
185+
186+
public fn void getConfigColors(const char *p, Color* styles, const char** names, u32 n) {
187+
if (!p) return;
188+
189+
char[16] name;
190+
char[16] definition;
191+
if (!strcmp(p, "none")) {
192+
for (u32 i = 0; i < n; i++) styles[i] = None;
193+
return;
194+
}
195+
while (getStyleDef(name, elemsof(name), definition, elemsof(definition), &p)) {
196+
// check for standard color customisation
197+
if (Color col = findStandardColor(name)) {
198+
const char* s = makeColorString(definition);
199+
if (s) defaultColors[col] = s;
200+
continue;
201+
}
202+
// check for style definition
203+
for (u32 i = 0; i < n; i++) {
204+
if (names[i] && !strcmp(names[i], name)) {
205+
styles[i] = convertColor(definition, styles[i]);
206+
break;
207+
}
208+
}
209+
}
210+
}

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
@@ -385,6 +385,7 @@ executable c2cat
385385
$backend c
386386

387387
ast_utils/color.c2
388+
ast_utils/color_config.c2
388389
ast_utils/constants.c2
389390
ast_utils/number_radix.c2
390391
ast_utils/src_loc.c2

0 commit comments

Comments
 (0)