This seems to be the best way to do this, but I kepts a few other experiments in scratchpad.
This commit is contained in:
parent
058ab23fa2
commit
2d550978b8
5 changed files with 299 additions and 165 deletions
215
ansi_parser.cpp
215
ansi_parser.cpp
|
@ -4,125 +4,106 @@
|
|||
#include <string_view>
|
||||
#include <charconv>
|
||||
#include "dbc.hpp"
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
using namespace fmt;
|
||||
|
||||
|
||||
#line 74 ".\\ansi_parser.rl"
|
||||
#line 62 ".\\ansi_parser.rl"
|
||||
|
||||
|
||||
|
||||
#line 11 ".\\ansi_parser.cpp"
|
||||
#line 12 ".\\ansi_parser.cpp"
|
||||
static const char _foo_actions[] = {
|
||||
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
|
||||
0, 1, 0, 1, 3, 1, 4, 1,
|
||||
5, 1, 9, 1, 10, 2, 1, 6,
|
||||
2, 1, 7, 2, 10, 5, 3, 1,
|
||||
8, 2
|
||||
};
|
||||
|
||||
static const char _foo_key_offsets[] = {
|
||||
0, 0, 1, 2, 3, 4, 6, 7,
|
||||
9, 12, 14, 17, 19, 20, 22, 25,
|
||||
27, 28, 29, 31, 31, 35, 37
|
||||
0, 0, 1, 2, 4, 6, 7, 8,
|
||||
9, 11, 14, 16, 19, 21, 24, 25,
|
||||
27, 28, 29, 30
|
||||
};
|
||||
|
||||
static const char _foo_trans_keys[] = {
|
||||
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
|
||||
27, 91, 51, 52, 56, 57, 59, 50,
|
||||
59, 48, 57, 59, 48, 57, 48, 57,
|
||||
59, 48, 57, 48, 57, 109, 48, 57,
|
||||
109, 56, 57, 59, 50, 27, 27, 0
|
||||
};
|
||||
|
||||
static const char _foo_single_lengths[] = {
|
||||
0, 1, 1, 1, 1, 2, 1, 0,
|
||||
1, 0, 1, 0, 1, 0, 1, 0,
|
||||
1, 1, 2, 0, 4, 0, 0
|
||||
0, 1, 1, 2, 2, 1, 1, 1,
|
||||
0, 1, 0, 1, 0, 1, 1, 2,
|
||||
1, 1, 1, 1
|
||||
};
|
||||
|
||||
static const char _foo_range_lengths[] = {
|
||||
0, 0, 0, 0, 0, 0, 0, 1,
|
||||
1, 1, 1, 1, 0, 1, 1, 1,
|
||||
0, 0, 0, 0, 0, 1, 1
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
1, 1, 1, 1, 1, 1, 0, 0,
|
||||
0, 0, 0, 0
|
||||
};
|
||||
|
||||
static const char _foo_index_offsets[] = {
|
||||
0, 0, 2, 4, 6, 8, 11, 13,
|
||||
15, 18, 20, 23, 25, 27, 29, 32,
|
||||
34, 36, 38, 41, 42, 47, 49
|
||||
0, 0, 2, 4, 7, 10, 12, 14,
|
||||
16, 18, 21, 23, 26, 28, 31, 33,
|
||||
36, 38, 40, 42
|
||||
};
|
||||
|
||||
static const char _foo_trans_targs[] = {
|
||||
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
|
||||
2, 1, 3, 0, 4, 15, 0, 5,
|
||||
14, 0, 6, 0, 7, 0, 8, 0,
|
||||
9, 0, 10, 9, 0, 11, 0, 12,
|
||||
11, 0, 13, 0, 19, 13, 0, 19,
|
||||
0, 16, 14, 0, 17, 0, 7, 0,
|
||||
2, 1, 2, 1, 0
|
||||
};
|
||||
|
||||
static const char _foo_trans_actions[] = {
|
||||
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
|
||||
0, 7, 0, 0, 9, 9, 0, 0,
|
||||
0, 0, 0, 0, 5, 0, 0, 0,
|
||||
1, 0, 13, 0, 0, 1, 0, 16,
|
||||
0, 0, 1, 0, 22, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 3, 0,
|
||||
0, 7, 11, 19, 0
|
||||
};
|
||||
|
||||
static const char _foo_to_state_actions[] = {
|
||||
0, 13, 0, 0, 0, 0, 0, 0,
|
||||
static const char _foo_eof_actions[] = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 13, 0, 0
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 11
|
||||
};
|
||||
|
||||
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, 52, 53
|
||||
};
|
||||
|
||||
static const int foo_start = 1;
|
||||
static const int foo_first_final = 19;
|
||||
static const int foo_start = 18;
|
||||
static const int foo_first_final = 18;
|
||||
static const int foo_error = 0;
|
||||
|
||||
static const int foo_en_ansi = 20;
|
||||
static const int foo_en_main = 1;
|
||||
static const int foo_en_main = 18;
|
||||
|
||||
|
||||
#line 77 ".\\ansi_parser.rl"
|
||||
#line 65 ".\\ansi_parser.rl"
|
||||
|
||||
bool parse_ansi(std::string_view codes) {
|
||||
const char *start = NULL;
|
||||
int cs = 0;
|
||||
size_t value = 0;
|
||||
uint8_t value = 0;
|
||||
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;
|
||||
bool fgcolor = false;
|
||||
sf::Color color;
|
||||
|
||||
|
||||
#line 107 ".\\ansi_parser.cpp"
|
||||
#line 91 ".\\ansi_parser.cpp"
|
||||
{
|
||||
cs = foo_start;
|
||||
ts = 0;
|
||||
te = 0;
|
||||
act = 0;
|
||||
}
|
||||
|
||||
#line 90 ".\\ansi_parser.rl"
|
||||
#line 77 ".\\ansi_parser.rl"
|
||||
|
||||
#line 113 ".\\ansi_parser.cpp"
|
||||
#line 94 ".\\ansi_parser.cpp"
|
||||
{
|
||||
int _klen;
|
||||
unsigned int _trans;
|
||||
|
@ -135,18 +116,6 @@ 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];
|
||||
|
||||
|
@ -196,7 +165,6 @@ _resume:
|
|||
}
|
||||
|
||||
_match:
|
||||
_eof_trans:
|
||||
cs = _foo_trans_targs[_trans];
|
||||
|
||||
if ( _foo_trans_actions[_trans] == 0 )
|
||||
|
@ -209,13 +177,13 @@ _eof_trans:
|
|||
switch ( *_acts++ )
|
||||
{
|
||||
case 0:
|
||||
#line 11 ".\\ansi_parser.rl"
|
||||
#line 12 ".\\ansi_parser.rl"
|
||||
{
|
||||
start = p;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
#line 15 ".\\ansi_parser.rl"
|
||||
#line 16 ".\\ansi_parser.rl"
|
||||
{
|
||||
auto [ptr, ec] = std::from_chars(start, p, value);
|
||||
dbc::check(ec == std::errc(), "error in number parsing");
|
||||
|
@ -223,66 +191,52 @@ _eof_trans:
|
|||
}
|
||||
break;
|
||||
case 2:
|
||||
#line 37 ".\\ansi_parser.rl"
|
||||
#line 22 ".\\ansi_parser.rl"
|
||||
{
|
||||
println("BG");
|
||||
println("color24b");
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
#line 41 ".\\ansi_parser.rl"
|
||||
{
|
||||
println("FG");
|
||||
}
|
||||
#line 26 ".\\ansi_parser.rl"
|
||||
{ fgcolor = false; }
|
||||
break;
|
||||
case 4:
|
||||
#line 45 ".\\ansi_parser.rl"
|
||||
#line 27 ".\\ansi_parser.rl"
|
||||
{ fgcolor = true; }
|
||||
break;
|
||||
case 5:
|
||||
#line 29 ".\\ansi_parser.rl"
|
||||
{
|
||||
println("ANY: {}:{}", int((*p)), (*p));
|
||||
}
|
||||
break;
|
||||
case 5:
|
||||
#line 72 ".\\ansi_parser.rl"
|
||||
{ p--; {cs = 20;goto _again;} }
|
||||
case 6:
|
||||
#line 33 ".\\ansi_parser.rl"
|
||||
{ color.r = value; }
|
||||
break;
|
||||
case 7:
|
||||
#line 34 ".\\ansi_parser.rl"
|
||||
{ color.g = value; }
|
||||
break;
|
||||
case 8:
|
||||
#line 49 ".\\ansi_parser.rl"
|
||||
{te = p+1;{
|
||||
println("START");
|
||||
}}
|
||||
#line 35 ".\\ansi_parser.rl"
|
||||
{ color.b = value; }
|
||||
break;
|
||||
case 9:
|
||||
#line 69 ".\\ansi_parser.rl"
|
||||
{te = p+1;{ {cs = 1;goto _again;} }}
|
||||
#line 37 ".\\ansi_parser.rl"
|
||||
{ value = 0; }
|
||||
break;
|
||||
case 10:
|
||||
#line 21 ".\\ansi_parser.rl"
|
||||
{te = p;p--;{
|
||||
println("COLOR256");
|
||||
}}
|
||||
#line 39 ".\\ansi_parser.rl"
|
||||
{
|
||||
println("fg? {}, sf:Color: {},{},{},{}", fgcolor, color.r, color.g, color.b, color.a);
|
||||
}
|
||||
break;
|
||||
case 11:
|
||||
#line 25 ".\\ansi_parser.rl"
|
||||
{te = p;p--;{
|
||||
println("COLOR24B");
|
||||
}}
|
||||
break;
|
||||
#line 244 ".\\ansi_parser.cpp"
|
||||
#line 211 ".\\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 )
|
||||
|
@ -290,18 +244,27 @@ _again:
|
|||
_test_eof: {}
|
||||
if ( p == eof )
|
||||
{
|
||||
if ( _foo_eof_trans[cs] > 0 ) {
|
||||
_trans = _foo_eof_trans[cs] - 1;
|
||||
goto _eof_trans;
|
||||
const char *__acts = _foo_actions + _foo_eof_actions[cs];
|
||||
unsigned int __nacts = (unsigned int) *__acts++;
|
||||
while ( __nacts-- > 0 ) {
|
||||
switch ( *__acts++ ) {
|
||||
case 10:
|
||||
#line 39 ".\\ansi_parser.rl"
|
||||
{
|
||||
println("fg? {}, sf:Color: {},{},{},{}", fgcolor, color.r, color.g, color.b, color.a);
|
||||
}
|
||||
break;
|
||||
#line 231 ".\\ansi_parser.cpp"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_out: {}
|
||||
}
|
||||
|
||||
#line 91 ".\\ansi_parser.rl"
|
||||
#line 78 ".\\ansi_parser.rl"
|
||||
|
||||
print("PROCESSED {} CHARS of {}", p - codes.data(), codes.size());
|
||||
print("PROCESSED {} CHARS of {}: {}", p - codes.data(), codes.size(), p);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue