Barely working ansi color codes parser but I _really_ don't like this one. Too much code to just get it to process correctly which means it'll be brittle as hell later.

This commit is contained in:
Zed A. Shaw 2024-11-01 00:40:42 -04:00
parent a44a9a04f9
commit f32b39afe2
9 changed files with 621 additions and 152 deletions

View file

@ -15,30 +15,54 @@ using namespace fmt;
}
action number {
size_t value = 0;
auto [ptr, ec] = std::from_chars(start, fpc, value);
dbc::check(ec == std::errc(), "error in number parsing");
println("NUMBER {}", value);
}
action color256 {
println("256 color");
}
action color24b {
println("true color");
}
action colorSingle {
println("single color");
}
action colorBasic {
println("basic color");
}
action bg {
println("BG");
}
action fg {
println("FG");
}
start = 0x1B "[";
fg = "38;";
bg = "48;";
fg = "38;" %fg;
bg = "48;" %bg;
num = digit+ >tstart %number;
color256 = "5;" num ";" num;
color24b = "2;" num ";" num ";" num;
basic = num ";" num;
single = num;
main := |*
start => { println("START"); };
fg color256 "m" => { println("fg 256 color"); };
bg color256 "m" => { println("bg 256 color"); };
fg color24b "m" => { println("fg true color"); };
bg color24b "m" => { println("bg true color"); };
single "m" => { println("single"); };
basic "m" => { println("basic"); };
*|;
main := (
start %{ println("START"); }
(
(fg|bg) color256 %color256 |
(fg|bg) color24b %color24b |
single %colorSingle |
basic %colorBasic
)**
"m" %{ println("END"); }
)**;
}%%
%% write data;
@ -46,11 +70,9 @@ using namespace fmt;
void parse_ansi(std::string_view &codes) {
const char *start = NULL;
int cs = 0;
size_t act = 0;
size_t value = 0;
const char *p = codes.data();
const char *pe = p + codes.size();
const char *ts = p;
const char *te = pe;
const char *eof = pe;
%% write init;