#include #include #include #include "dbc.hpp" #include using namespace fmt; %%{ machine foo; action tstart { start = fpc; } action number { auto [ptr, ec] = std::from_chars(start, fpc, value); dbc::check(ec == std::errc(), "error in number parsing"); println("NUM: {}", value); } action color24b { println("color24b"); } action is_fg { is_fg = true; } action is_bg { is_fg = false; } action any { println("ANY: {}:{}", int(fc), fc); } action red { color.r = value; } action blue { color.g = value; } action green { color.b = value; } action start { value = 0; } action end { println("fg? {}, sf:Color: {},{},{},{}", is_fg, color.r, color.g, color.b, color.a); } start = 0x1B "["; fg = "38;" %is_fg; bg = "48;" %is_bg; reset = ("39" | "49"); num = digit+ >tstart %number; color256 = "5;"; color24b = "2;"; ansi = ( start %start ( reset | (fg|bg) color24b num %red ";" num %blue ";" num %green %color24b ) "m" %end ); other = (any+ @any -- 0x1B)*; main := (other :> ansi)**; }%% %% write data; bool parse_ansi(std::string_view codes) { const char *start = NULL; int cs = 0; unsigned int value = 0; const char *p = codes.data(); const char *pe = p + codes.size(); const char *eof = pe; bool is_fg = false; sf::Color color; %% write init; %% write exec; print("PROCESSED {} CHARS of {}: {}", p - codes.data(), codes.size(), p); return true; }