This also does almost the same thing but using fgoto to switch between ansi and not.

This commit is contained in:
Zed A. Shaw 2024-11-01 01:08:26 -04:00
parent f32b39afe2
commit 058ab23fa2
2 changed files with 133 additions and 132 deletions

View file

@ -54,29 +54,23 @@ using namespace fmt;
println("END");
}
start = 0x1B "[";
ESC = 0x1B;
start = ESC "[";
fg = "38;" %fg;
bg = "48;" %bg;
num = digit+ >tstart %number;
color256 = "5;" num ";" num;
color24b = "2;" num ";" num ";" num;
basic = num ";" num;
single = num;
color256 = "5;";
color24b = "2;";
num = (digit+) >tstart %number;
colorCode = (
start %start
(
(fg|bg) color256 %color256 |
(fg|bg) color24b %color24b |
single %colorSingle |
basic %colorBasic
)**
"m" %end
);
ansi := |*
start => start;
(fg|bg) color256 num ";" num => color256;
(fg|bg) color24b num ";" num ";" num => color24b;
"m" => { fgoto main; };
*|;
other = (any+ @any -- 0x1B)*;
main := (any @any -- ESC)* ESC @{ fhold; fgoto ansi; };
main := (other :>> colorCode)**;
}%%
%% write data;
@ -88,7 +82,9 @@ bool parse_ansi(std::string_view codes) {
const char *p = codes.data();
const char *pe = p + codes.size();
const char *eof = pe;
const char *ts = p;
const char *te = p;
int act = 0;
%% write init;
%% write exec;