#line 1 "ansi_parser.rl" #include #include #include "dbc.hpp" #include #include "ansi_parser.hpp" #include using namespace fmt; #line 122 "ansi_parser.rl" #line 13 "ansi_parser.cpp" static const char _ansi_parser_actions[] = { 0, 1, 0, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8, 1, 9, 1, 10, 1, 11, 1, 15, 1, 16, 2, 1, 12, 2, 1, 13, 2, 6, 7, 2, 16, 5, 3, 1, 14, 2 }; static const char _ansi_parser_key_offsets[] = { 0, 0, 1, 2, 11, 12, 14, 17, 18, 22, 23, 27, 28, 29, 30, 31, 33, 36, 38, 41, 43, 46, 47, 50, 51, 52, 53, 54, 55 }; static const int _ansi_parser_trans_keys[] = { 27, 91, 48, 49, 50, 51, 52, 55, 57, 53, 54, 109, 48, 109, 34, 48, 55, 109, 50, 52, 55, 109, 109, 49, 56, 57, 109, 109, 59, 50, 59, 48, 57, 59, 48, 57, 48, 57, 59, 48, 57, 48, 57, 109, 48, 57, 109, 56, 57, 109, 59, 50, 109, 109, 27, 27, 0 }; static const char _ansi_parser_single_lengths[] = { 0, 1, 1, 7, 1, 2, 3, 1, 4, 1, 4, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 3, 1, 1, 1, 1, 1, 1 }; static const char _ansi_parser_range_lengths[] = { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }; static const char _ansi_parser_index_offsets[] = { 0, 0, 2, 4, 13, 15, 18, 22, 24, 29, 31, 36, 38, 40, 42, 44, 46, 49, 51, 54, 56, 59, 61, 65, 67, 69, 71, 73, 75 }; static const char _ansi_parser_trans_targs[] = { 2, 1, 3, 0, 4, 5, 8, 10, 22, 26, 6, 7, 0, 28, 0, 6, 28, 0, 7, 7, 7, 0, 28, 0, 7, 7, 9, 28, 0, 28, 0, 11, 12, 21, 28, 0, 28, 0, 13, 0, 14, 0, 15, 0, 16, 0, 17, 16, 0, 18, 0, 19, 18, 0, 20, 0, 28, 20, 0, 28, 0, 23, 25, 28, 0, 24, 0, 14, 0, 28, 0, 28, 0, 2, 1, 2, 1, 0 }; static const char _ansi_parser_trans_actions[] = { 0, 7, 0, 0, 21, 21, 21, 21, 21, 21, 21, 21, 0, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 0, 15, 0, 0, 0, 0, 0, 0, 19, 0, 0, 0, 3, 0, 0, 0, 1, 0, 25, 0, 0, 1, 0, 28, 0, 0, 1, 0, 37, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 5, 0, 11, 0, 13, 0, 0, 7, 23, 34, 0 }; static const char _ansi_parser_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, 0, 0, 23 }; static const int ansi_parser_start = 27; static const int ansi_parser_first_final = 27; static const int ansi_parser_error = 0; static const int ansi_parser_en_main = 27; #line 125 "ansi_parser.rl" #include ANSIParser::ANSIParser(sf::Color default_fg, sf::Color default_bg) : $default_fg(default_fg), $default_bg(default_bg) { } bool ANSIParser::parse(std::wstring_view codes, ColorCB color_cb, WriteCB write_cb) { const wchar_t *start = NULL; int cs = 0; unsigned int value = 0; const wchar_t *p = codes.data(); const wchar_t *pe = p + codes.size(); const wchar_t *eof = pe; sf::Color bgcolor($default_bg); sf::Color color($default_fg); sf::Color* target = &color; #line 120 "ansi_parser.cpp" { cs = ansi_parser_start; } #line 146 "ansi_parser.rl" #line 123 "ansi_parser.cpp" { int _klen; unsigned int _trans; const char *_acts; unsigned int _nacts; const int *_keys; if ( p == pe ) goto _test_eof; if ( cs == 0 ) goto _out; _resume: _keys = _ansi_parser_trans_keys + _ansi_parser_key_offsets[cs]; _trans = _ansi_parser_index_offsets[cs]; _klen = _ansi_parser_single_lengths[cs]; if ( _klen > 0 ) { const int *_lower = _keys; const int *_mid; const int *_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 = _ansi_parser_range_lengths[cs]; if ( _klen > 0 ) { const int *_lower = _keys; const int *_mid; const int *_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 = _ansi_parser_trans_targs[_trans]; if ( _ansi_parser_trans_actions[_trans] == 0 ) goto _again; _acts = _ansi_parser_actions + _ansi_parser_trans_actions[_trans]; _nacts = (unsigned int) *_acts++; while ( _nacts-- > 0 ) { switch ( *_acts++ ) { case 0: #line 14 "ansi_parser.rl" { start = p; } break; case 1: #line 18 "ansi_parser.rl" { value = 0; size_t len = p - start; dbc::check(start[0] != '-', "negative numbers not supported"); switch(len) { case 10: value += (start[len-10] - '0') * 1000000000; case 9: value += (start[len- 9] - '0') * 100000000; case 8: value += (start[len- 8] - '0') * 10000000; case 7: value += (start[len- 7] - '0') * 1000000; case 6: value += (start[len- 6] - '0') * 100000; case 5: value += (start[len- 5] - '0') * 10000; case 4: value += (start[len- 4] - '0') * 1000; case 3: value += (start[len- 3] - '0') * 100; case 2: value += (start[len- 2] - '0') * 10; case 1: value += (start[len- 1] - '0'); break; default: dbc::sentinel("can't process > 10 digits"); } } break; case 2: #line 40 "ansi_parser.rl" { color_cb(color, bgcolor); } break; case 3: #line 43 "ansi_parser.rl" { target = &color; } break; case 4: #line 46 "ansi_parser.rl" { target = &bgcolor; } break; case 5: #line 50 "ansi_parser.rl" { write_cb((*p)); } break; case 6: #line 54 "ansi_parser.rl" { color = $default_fg; color_cb(color, bgcolor); } break; case 7: #line 58 "ansi_parser.rl" { bgcolor = $default_bg; color_cb(color, bgcolor); } break; case 8: #line 62 "ansi_parser.rl" { color = $default_bg; bgcolor = $default_fg; color_cb(color, bgcolor); } break; case 9: #line 67 "ansi_parser.rl" { color = $default_fg; bgcolor = $default_bg; color_cb(color, bgcolor); } break; case 10: #line 72 "ansi_parser.rl" { color = sf::Color(100,100,100); color_cb(color, bgcolor); } break; case 11: #line 76 "ansi_parser.rl" { color = sf::Color::Red; color_cb(color, bgcolor); } break; case 12: #line 81 "ansi_parser.rl" { target->r = value; } break; case 13: #line 82 "ansi_parser.rl" { target->g = value; } break; case 14: #line 83 "ansi_parser.rl" { target->b = value; } break; case 15: #line 84 "ansi_parser.rl" { value = 0; } break; case 16: #line 85 "ansi_parser.rl" {} break; #line 296 "ansi_parser.cpp" } } _again: if ( cs == 0 ) goto _out; if ( ++p != pe ) goto _resume; _test_eof: {} if ( p == eof ) { const char *__acts = _ansi_parser_actions + _ansi_parser_eof_actions[cs]; unsigned int __nacts = (unsigned int) *__acts++; while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 16: #line 85 "ansi_parser.rl" {} break; #line 314 "ansi_parser.cpp" } } } _out: {} } #line 147 "ansi_parser.rl" bool good = pe - p == 0; if(!good) { p -= 10; // dear cthuhlu, save me from the pain that is wstring for(int i = 0; i < 100; i++) { try { print("{}", p[i] == 0x1B ? '^' : char(p[i])); } catch(...) { print("?=", int(p[i])); } } } return good; }