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

main
Zed A. Shaw 3 weeks ago
parent f32b39afe2
commit 058ab23fa2
  1. 227
      ansi_parser.cpp
  2. 40
      ansi_parser.rl

@ -8,103 +8,97 @@
using namespace fmt; using namespace fmt;
#line 80 ".\\ansi_parser.rl" #line 74 ".\\ansi_parser.rl"
#line 11 ".\\ansi_parser.cpp" #line 11 ".\\ansi_parser.cpp"
static const char _foo_actions[] = { static const char _foo_actions[] = {
0, 1, 0, 1, 1, 1, 8, 1, 0, 1, 0, 1, 1, 1, 2, 1,
9, 1, 10, 2, 1, 2, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1,
3, 2, 1, 4, 2, 1, 5, 2, 7, 1, 8, 1, 9, 2, 1, 10,
6, 0, 2, 7, 0, 2, 9, 0, 2, 1, 11
2, 10, 8
}; };
static const char _foo_key_offsets[] = { static const char _foo_key_offsets[] = {
0, 0, 1, 2, 7, 11, 13, 16, 0, 0, 1, 2, 3, 4, 6, 7,
21, 25, 29, 33, 35, 38, 40, 43, 9, 12, 14, 17, 19, 20, 22, 25,
45, 48, 52, 54, 57, 59, 62, 67, 27, 28, 29, 31, 31, 35, 37
71, 75, 76
}; };
static const char _foo_trans_keys[] = { static const char _foo_trans_keys[] = {
27, 91, 51, 52, 109, 48, 57, 59, 27, 91, 56, 59, 50, 53, 59, 48,
109, 48, 57, 48, 57, 109, 48, 57, 57, 59, 48, 57, 48, 57, 59, 48,
56, 59, 109, 48, 57, 59, 109, 48, 57, 48, 57, 59, 48, 57, 59, 48,
57, 50, 53, 48, 57, 59, 109, 48, 57, 48, 57, 56, 59, 50, 53, 27,
57, 48, 57, 59, 48, 57, 48, 57, 51, 52, 109, 48, 57, 48, 57, 0
59, 48, 57, 48, 57, 109, 48, 57,
59, 109, 48, 57, 48, 57, 59, 48,
57, 48, 57, 109, 48, 57, 56, 59,
109, 48, 57, 59, 109, 48, 57, 50,
53, 48, 57, 27, 27, 0
}; };
static const char _foo_single_lengths[] = { static const char _foo_single_lengths[] = {
0, 1, 1, 3, 2, 0, 1, 3, 0, 1, 1, 1, 1, 2, 1, 0,
2, 2, 2, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
1, 2, 0, 1, 0, 1, 3, 2, 1, 1, 2, 0, 4, 0, 0
2, 1, 1
}; };
static const char _foo_range_lengths[] = { static const char _foo_range_lengths[] = {
0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1
1, 0, 0
}; };
static const char _foo_index_offsets[] = { static const char _foo_index_offsets[] = {
0, 0, 2, 4, 9, 13, 15, 18, 0, 0, 2, 4, 6, 8, 11, 13,
23, 27, 31, 35, 37, 40, 42, 45, 15, 18, 20, 23, 25, 27, 29, 32,
47, 50, 54, 56, 59, 61, 64, 69, 34, 36, 38, 41, 42, 47, 49
73, 77, 79
}; };
static const char _foo_trans_targs[] = { static const char _foo_trans_targs[] = {
2, 1, 3, 0, 7, 22, 26, 4, 19, 1, 20, 0, 4, 0, 5, 0,
0, 5, 26, 4, 0, 6, 0, 26, 6, 12, 0, 7, 0, 8, 0, 9,
6, 0, 8, 5, 26, 4, 0, 9, 8, 0, 10, 0, 11, 10, 0, 21,
26, 4, 0, 10, 17, 6, 0, 11, 0, 13, 0, 14, 0, 15, 14, 0,
26, 6, 0, 12, 0, 13, 12, 0, 22, 0, 17, 0, 18, 0, 6, 12,
14, 0, 15, 14, 0, 16, 0, 26, 0, 0, 2, 3, 16, 20, 0, 21,
16, 0, 18, 26, 6, 0, 19, 0, 20, 22, 20, 20, 20, 0
20, 19, 0, 21, 0, 26, 21, 0,
23, 5, 26, 4, 0, 24, 26, 4,
0, 10, 17, 6, 0, 2, 1, 2,
1, 0
}; };
static const char _foo_trans_actions[] = { static const char _foo_trans_actions[] = {
0, 5, 0, 0, 29, 29, 7, 29, 11, 9, 17, 0, 0, 0, 0, 0,
0, 3, 17, 0, 0, 1, 0, 20, 7, 7, 0, 0, 0, 1, 0, 3,
0, 0, 0, 3, 17, 0, 0, 3, 0, 0, 1, 0, 3, 0, 0, 1,
17, 0, 0, 26, 26, 1, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0,
20, 0, 0, 1, 0, 3, 0, 0, 1, 0, 0, 0, 0, 0, 5, 5,
1, 0, 3, 0, 0, 1, 0, 14, 0, 0, 0, 0, 0, 19, 0, 0,
0, 0, 0, 20, 0, 0, 1, 0, 24, 0, 21, 24, 21, 0
3, 0, 0, 1, 0, 11, 0, 0,
0, 3, 17, 0, 0, 3, 17, 0,
0, 23, 23, 1, 0, 0, 5, 9,
32, 0
}; };
static const char _foo_eof_actions[] = { static const char _foo_to_state_actions[] = {
0, 13, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 13, 0, 0
};
static const char _foo_from_state_actions[] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 15, 0, 0
};
static const char _foo_eof_trans[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 9 0, 0, 0, 0, 0, 52, 53
}; };
static const int foo_start = 25; static const int foo_start = 1;
static const int foo_first_final = 25; static const int foo_first_final = 19;
static const int foo_error = 0; static const int foo_error = 0;
static const int foo_en_main = 25; static const int foo_en_ansi = 20;
static const int foo_en_main = 1;
#line 83 ".\\ansi_parser.rl" #line 77 ".\\ansi_parser.rl"
bool parse_ansi(std::string_view codes) { bool parse_ansi(std::string_view codes) {
const char *start = NULL; const char *start = NULL;
@ -113,17 +107,22 @@ bool parse_ansi(std::string_view codes) {
const char *p = codes.data(); const char *p = codes.data();
const char *pe = p + codes.size(); const char *pe = p + codes.size();
const char *eof = pe; const char *eof = pe;
const char *ts = p;
const char *te = p;
int act = 0;
#line 107 ".\\ansi_parser.cpp"
#line 111 ".\\ansi_parser.cpp"
{ {
cs = foo_start; cs = foo_start;
ts = 0;
te = 0;
act = 0;
} }
#line 94 ".\\ansi_parser.rl" #line 90 ".\\ansi_parser.rl"
#line 114 ".\\ansi_parser.cpp" #line 113 ".\\ansi_parser.cpp"
{ {
int _klen; int _klen;
unsigned int _trans; unsigned int _trans;
@ -136,6 +135,18 @@ bool parse_ansi(std::string_view codes) {
if ( cs == 0 ) if ( cs == 0 )
goto _out; goto _out;
_resume: _resume:
_acts = _foo_actions + _foo_from_state_actions[cs];
_nacts = (unsigned int) *_acts++;
while ( _nacts-- > 0 ) {
switch ( *_acts++ ) {
case 7:
#line 1 "NONE"
{ts = p;}
break;
#line 132 ".\\ansi_parser.cpp"
}
}
_keys = _foo_trans_keys + _foo_key_offsets[cs]; _keys = _foo_trans_keys + _foo_key_offsets[cs];
_trans = _foo_index_offsets[cs]; _trans = _foo_index_offsets[cs];
@ -185,6 +196,7 @@ _resume:
} }
_match: _match:
_eof_trans:
cs = _foo_trans_targs[_trans]; cs = _foo_trans_targs[_trans];
if ( _foo_trans_actions[_trans] == 0 ) if ( _foo_trans_actions[_trans] == 0 )
@ -211,64 +223,66 @@ _match:
} }
break; break;
case 2: case 2:
#line 21 ".\\ansi_parser.rl"
{
println("COLOR256");
}
break;
case 3:
#line 25 ".\\ansi_parser.rl"
{
println("COLOR24B");
}
break;
case 4:
#line 29 ".\\ansi_parser.rl"
{
println("SINGLE");
}
break;
case 5:
#line 33 ".\\ansi_parser.rl"
{
println("BASIC");
}
break;
case 6:
#line 37 ".\\ansi_parser.rl" #line 37 ".\\ansi_parser.rl"
{ {
println("BG"); println("BG");
} }
break; break;
case 7: case 3:
#line 41 ".\\ansi_parser.rl" #line 41 ".\\ansi_parser.rl"
{ {
println("FG"); println("FG");
} }
break; break;
case 8: case 4:
#line 45 ".\\ansi_parser.rl" #line 45 ".\\ansi_parser.rl"
{ {
println("ANY: {}:{}", int((*p)), (*p)); println("ANY: {}:{}", int((*p)), (*p));
} }
break; break;
case 9: case 5:
#line 72 ".\\ansi_parser.rl"
{ p--; {cs = 20;goto _again;} }
break;
case 8:
#line 49 ".\\ansi_parser.rl" #line 49 ".\\ansi_parser.rl"
{ {te = p+1;{
println("START"); println("START");
} }}
break;
case 9:
#line 69 ".\\ansi_parser.rl"
{te = p+1;{ {cs = 1;goto _again;} }}
break; break;
case 10: case 10:
#line 53 ".\\ansi_parser.rl" #line 21 ".\\ansi_parser.rl"
{ {te = p;p--;{
println("END"); println("COLOR256");
} }}
break;
case 11:
#line 25 ".\\ansi_parser.rl"
{te = p;p--;{
println("COLOR24B");
}}
break; break;
#line 243 ".\\ansi_parser.cpp" #line 244 ".\\ansi_parser.cpp"
} }
} }
_again: _again:
_acts = _foo_actions + _foo_to_state_actions[cs];
_nacts = (unsigned int) *_acts++;
while ( _nacts-- > 0 ) {
switch ( *_acts++ ) {
case 6:
#line 1 "NONE"
{ts = 0;}
break;
#line 255 ".\\ansi_parser.cpp"
}
}
if ( cs == 0 ) if ( cs == 0 )
goto _out; goto _out;
if ( ++p != pe ) if ( ++p != pe )
@ -276,25 +290,16 @@ _again:
_test_eof: {} _test_eof: {}
if ( p == eof ) if ( p == eof )
{ {
const char *__acts = _foo_actions + _foo_eof_actions[cs]; if ( _foo_eof_trans[cs] > 0 ) {
unsigned int __nacts = (unsigned int) *__acts++; _trans = _foo_eof_trans[cs] - 1;
while ( __nacts-- > 0 ) { goto _eof_trans;
switch ( *__acts++ ) {
case 10:
#line 53 ".\\ansi_parser.rl"
{
println("END");
}
break;
#line 263 ".\\ansi_parser.cpp"
}
} }
} }
_out: {} _out: {}
} }
#line 95 ".\\ansi_parser.rl" #line 91 ".\\ansi_parser.rl"
print("PROCESSED {} CHARS of {}", p - codes.data(), codes.size()); print("PROCESSED {} CHARS of {}", p - codes.data(), codes.size());

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

Loading…
Cancel
Save