The next little game in the series where I make a fancy rogue game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
roguish/ansi_parser.cpp

303 lines
5.4 KiB

#line 1 ".\\ansi_parser.rl"
#include <fmt/core.h>
#include <string_view>
#include <charconv>
#include "dbc.hpp"
using namespace fmt;
#line 80 ".\\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
};
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
};
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
};
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
};
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
};
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
};
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
};
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
};
static const char _foo_eof_actions[] = {
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
};
static const int foo_start = 25;
static const int foo_first_final = 25;
static const int foo_error = 0;
static const int foo_en_main = 25;
#line 83 ".\\ansi_parser.rl"
bool parse_ansi(std::string_view codes) {
const char *start = NULL;
int cs = 0;
size_t value = 0;
const char *p = codes.data();
const char *pe = p + codes.size();
const char *eof = pe;
#line 111 ".\\ansi_parser.cpp"
{
cs = foo_start;
}
#line 94 ".\\ansi_parser.rl"
#line 114 ".\\ansi_parser.cpp"
{
int _klen;
unsigned int _trans;
const char *_acts;
unsigned int _nacts;
const char *_keys;
if ( p == pe )
goto _test_eof;
if ( cs == 0 )
goto _out;
_resume:
_keys = _foo_trans_keys + _foo_key_offsets[cs];
_trans = _foo_index_offsets[cs];
_klen = _foo_single_lengths[cs];
if ( _klen > 0 ) {
const char *_lower = _keys;
const char *_mid;
const char *_upper = _keys + _klen - 1;
while (1) {
if ( _upper < _lower )
break;
_mid = _lower + ((_upper-_lower) >> 1);
if ( (*p) < *_mid )
_upper = _mid - 1;
else if ( (*p) > *_mid )
_lower = _mid + 1;
else {
_trans += (unsigned int)(_mid - _keys);
goto _match;
}
}
_keys += _klen;
_trans += _klen;
}
_klen = _foo_range_lengths[cs];
if ( _klen > 0 ) {
const char *_lower = _keys;
const char *_mid;
const char *_upper = _keys + (_klen<<1) - 2;
while (1) {
if ( _upper < _lower )
break;
_mid = _lower + (((_upper-_lower) >> 1) & ~1);
if ( (*p) < _mid[0] )
_upper = _mid - 2;
else if ( (*p) > _mid[1] )
_lower = _mid + 2;
else {
_trans += (unsigned int)((_mid - _keys)>>1);
goto _match;
}
}
_trans += _klen;
}
_match:
cs = _foo_trans_targs[_trans];
if ( _foo_trans_actions[_trans] == 0 )
goto _again;
_acts = _foo_actions + _foo_trans_actions[_trans];
_nacts = (unsigned int) *_acts++;
while ( _nacts-- > 0 )
{
switch ( *_acts++ )
{
case 0:
#line 11 ".\\ansi_parser.rl"
{
start = p;
}
break;
case 1:
#line 15 ".\\ansi_parser.rl"
{
auto [ptr, ec] = std::from_chars(start, p, value);
dbc::check(ec == std::errc(), "error in number parsing");
println("NUM: {}", value);
}
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:
#line 41 ".\\ansi_parser.rl"
{
println("FG");
}
break;
case 8:
#line 45 ".\\ansi_parser.rl"
{
println("ANY: {}:{}", int((*p)), (*p));
}
break;
case 9:
#line 49 ".\\ansi_parser.rl"
{
println("START");
}
break;
case 10:
#line 53 ".\\ansi_parser.rl"
{
println("END");
}
break;
#line 243 ".\\ansi_parser.cpp"
}
}
_again:
if ( cs == 0 )
goto _out;
if ( ++p != pe )
goto _resume;
_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"
}
}
}
_out: {}
}
#line 95 ".\\ansi_parser.rl"
print("PROCESSED {} CHARS of {}", p - codes.data(), codes.size());
return true;
}