From 058ab23fa20b0f92a3ae3ca834dfe571906ab87a Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Fri, 1 Nov 2024 01:08:26 -0400 Subject: [PATCH] This also does almost the same thing but using fgoto to switch between ansi and not. --- ansi_parser.cpp | 227 +++++++++++++++++++++++++----------------------- ansi_parser.rl | 40 ++++----- 2 files changed, 134 insertions(+), 133 deletions(-) diff --git a/ansi_parser.cpp b/ansi_parser.cpp index a9c5386..fbfc5a0 100644 --- a/ansi_parser.cpp +++ b/ansi_parser.cpp @@ -8,103 +8,97 @@ using namespace fmt; -#line 80 ".\\ansi_parser.rl" +#line 74 ".\\ansi_parser.rl" #line 11 ".\\ansi_parser.cpp" static const char _foo_actions[] = { - 0, 1, 0, 1, 1, 1, 8, 1, - 9, 1, 10, 2, 1, 2, 2, 1, - 3, 2, 1, 4, 2, 1, 5, 2, - 6, 0, 2, 7, 0, 2, 9, 0, - 2, 10, 8 + 0, 1, 0, 1, 1, 1, 2, 1, + 3, 1, 4, 1, 5, 1, 6, 1, + 7, 1, 8, 1, 9, 2, 1, 10, + 2, 1, 11 }; static const char _foo_key_offsets[] = { - 0, 0, 1, 2, 7, 11, 13, 16, - 21, 25, 29, 33, 35, 38, 40, 43, - 45, 48, 52, 54, 57, 59, 62, 67, - 71, 75, 76 + 0, 0, 1, 2, 3, 4, 6, 7, + 9, 12, 14, 17, 19, 20, 22, 25, + 27, 28, 29, 31, 31, 35, 37 }; static const char _foo_trans_keys[] = { - 27, 91, 51, 52, 109, 48, 57, 59, - 109, 48, 57, 48, 57, 109, 48, 57, - 56, 59, 109, 48, 57, 59, 109, 48, - 57, 50, 53, 48, 57, 59, 109, 48, - 57, 48, 57, 59, 48, 57, 48, 57, - 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 + 27, 91, 56, 59, 50, 53, 59, 48, + 57, 59, 48, 57, 48, 57, 59, 48, + 57, 48, 57, 59, 48, 57, 59, 48, + 57, 48, 57, 56, 59, 50, 53, 27, + 51, 52, 109, 48, 57, 48, 57, 0 }; static const char _foo_single_lengths[] = { - 0, 1, 1, 3, 2, 0, 1, 3, - 2, 2, 2, 0, 1, 0, 1, 0, - 1, 2, 0, 1, 0, 1, 3, 2, - 2, 1, 1 + 0, 1, 1, 1, 1, 2, 1, 0, + 1, 0, 1, 0, 1, 0, 1, 0, + 1, 1, 2, 0, 4, 0, 0 }; static const char _foo_range_lengths[] = { - 0, 0, 0, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, - 1, 0, 0 + 0, 0, 0, 0, 0, 0, 0, 1, + 1, 1, 1, 1, 0, 1, 1, 1, + 0, 0, 0, 0, 0, 1, 1 }; static const char _foo_index_offsets[] = { - 0, 0, 2, 4, 9, 13, 15, 18, - 23, 27, 31, 35, 37, 40, 42, 45, - 47, 50, 54, 56, 59, 61, 64, 69, - 73, 77, 79 + 0, 0, 2, 4, 6, 8, 11, 13, + 15, 18, 20, 23, 25, 27, 29, 32, + 34, 36, 38, 41, 42, 47, 49 }; static const char _foo_trans_targs[] = { - 2, 1, 3, 0, 7, 22, 26, 4, - 0, 5, 26, 4, 0, 6, 0, 26, - 6, 0, 8, 5, 26, 4, 0, 9, - 26, 4, 0, 10, 17, 6, 0, 11, - 26, 6, 0, 12, 0, 13, 12, 0, - 14, 0, 15, 14, 0, 16, 0, 26, - 16, 0, 18, 26, 6, 0, 19, 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 + 19, 1, 20, 0, 4, 0, 5, 0, + 6, 12, 0, 7, 0, 8, 0, 9, + 8, 0, 10, 0, 11, 10, 0, 21, + 0, 13, 0, 14, 0, 15, 14, 0, + 22, 0, 17, 0, 18, 0, 6, 12, + 0, 0, 2, 3, 16, 20, 0, 21, + 20, 22, 20, 20, 20, 0 }; static const char _foo_trans_actions[] = { - 0, 5, 0, 0, 29, 29, 7, 29, - 0, 3, 17, 0, 0, 1, 0, 20, - 0, 0, 0, 3, 17, 0, 0, 3, - 17, 0, 0, 26, 26, 1, 0, 0, - 20, 0, 0, 1, 0, 3, 0, 0, - 1, 0, 3, 0, 0, 1, 0, 14, - 0, 0, 0, 20, 0, 0, 1, 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 + 11, 9, 17, 0, 0, 0, 0, 0, + 7, 7, 0, 0, 0, 1, 0, 3, + 0, 0, 1, 0, 3, 0, 0, 1, + 0, 0, 0, 1, 0, 3, 0, 0, + 1, 0, 0, 0, 0, 0, 5, 5, + 0, 0, 0, 0, 0, 19, 0, 0, + 24, 0, 21, 24, 21, 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, 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, 9 + 0, 0, 0, 0, 0, 52, 53 }; -static const int foo_start = 25; -static const int foo_first_final = 25; +static const int foo_start = 1; +static const int foo_first_final = 19; 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) { const char *start = NULL; @@ -113,17 +107,22 @@ 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; -#line 111 ".\\ansi_parser.cpp" +#line 107 ".\\ansi_parser.cpp" { 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; unsigned int _trans; @@ -136,6 +135,18 @@ bool parse_ansi(std::string_view codes) { if ( cs == 0 ) goto _out; _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]; _trans = _foo_index_offsets[cs]; @@ -185,6 +196,7 @@ _resume: } _match: +_eof_trans: cs = _foo_trans_targs[_trans]; if ( _foo_trans_actions[_trans] == 0 ) @@ -211,64 +223,66 @@ _match: } break; 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" { println("BG"); } break; - case 7: + case 3: #line 41 ".\\ansi_parser.rl" { println("FG"); } break; - case 8: + case 4: #line 45 ".\\ansi_parser.rl" { println("ANY: {}:{}", int((*p)), (*p)); } break; - case 9: + case 5: +#line 72 ".\\ansi_parser.rl" + { p--; {cs = 20;goto _again;} } + break; + case 8: #line 49 ".\\ansi_parser.rl" - { + {te = p+1;{ println("START"); - } + }} + break; + case 9: +#line 69 ".\\ansi_parser.rl" + {te = p+1;{ {cs = 1;goto _again;} }} break; case 10: -#line 53 ".\\ansi_parser.rl" - { - println("END"); - } +#line 21 ".\\ansi_parser.rl" + {te = p;p--;{ + println("COLOR256"); + }} + break; + case 11: +#line 25 ".\\ansi_parser.rl" + {te = p;p--;{ + println("COLOR24B"); + }} break; -#line 243 ".\\ansi_parser.cpp" +#line 244 ".\\ansi_parser.cpp" } } _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 ) goto _out; if ( ++p != pe ) @@ -276,25 +290,16 @@ _again: _test_eof: {} if ( p == eof ) { - const char *__acts = _foo_actions + _foo_eof_actions[cs]; - unsigned int __nacts = (unsigned int) *__acts++; - while ( __nacts-- > 0 ) { - switch ( *__acts++ ) { - case 10: -#line 53 ".\\ansi_parser.rl" - { - println("END"); - } - break; -#line 263 ".\\ansi_parser.cpp" - } + if ( _foo_eof_trans[cs] > 0 ) { + _trans = _foo_eof_trans[cs] - 1; + goto _eof_trans; } } _out: {} } -#line 95 ".\\ansi_parser.rl" +#line 91 ".\\ansi_parser.rl" print("PROCESSED {} CHARS of {}", p - codes.data(), codes.size()); diff --git a/ansi_parser.rl b/ansi_parser.rl index a6f663f..fdfa7ce 100644 --- a/ansi_parser.rl +++ b/ansi_parser.rl @@ -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; - - colorCode = ( - start %start - ( - (fg|bg) color256 %color256 | - (fg|bg) color24b %color24b | - single %colorSingle | - basic %colorBasic - )** - "m" %end - ); - - other = (any+ @any -- 0x1B)*; - - main := (other :>> colorCode)**; + color256 = "5;"; + color24b = "2;"; + num = (digit+) >tstart %number; + + ansi := |* + start => start; + (fg|bg) color256 num ";" num => color256; + (fg|bg) color24b num ";" num ";" num => color24b; + "m" => { fgoto main; }; + *|; + + main := (any @any -- ESC)* ESC @{ fhold; fgoto ansi; }; + }%% %% 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;