
#line 1 ".\\scratchpad\\testragel.rl"
#include <fmt/core.h>
#include <string>
#include <vector>
#include <string_view>
#include <charconv>
#include "dbc.hpp"

using namespace fmt;


#line 66 ".\\scratchpad\\testragel.rl"



#line 13 ".\\scratchpad\\testragel.cpp"
static const char _foo_actions[] = {
	0, 1, 0, 1, 1, 1, 8, 1, 
	9, 2, 1, 2, 2, 1, 3, 2, 
	1, 4, 2, 1, 5, 2, 6, 0, 
	2, 7, 0, 2, 8, 0
};

static const char _foo_key_offsets[] = {
	0, 0, 1, 6, 10, 12, 15, 20, 
	24, 28, 32, 34, 37, 39, 42, 44, 
	47, 51, 53, 56, 58, 61, 66, 70, 
	74, 75
};

static const char _foo_trans_keys[] = {
	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, 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, 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, 7, 11, 13, 16, 21, 
	25, 29, 33, 35, 38, 40, 43, 45, 
	48, 52, 54, 57, 59, 62, 67, 71, 
	75, 77
};

static const char _foo_indicies[] = {
	0, 1, 3, 4, 5, 2, 1, 7, 
	8, 6, 1, 9, 1, 11, 10, 1, 
	12, 7, 8, 6, 1, 13, 8, 6, 
	1, 14, 15, 9, 1, 16, 11, 10, 
	1, 17, 1, 19, 18, 1, 20, 1, 
	22, 21, 1, 23, 1, 25, 24, 1, 
	26, 11, 10, 1, 27, 1, 29, 28, 
	1, 30, 1, 32, 31, 1, 33, 7, 
	8, 6, 1, 34, 8, 6, 1, 35, 
	36, 9, 1, 37, 1, 38, 1, 0
};

static const char _foo_trans_targs[] = {
	2, 0, 3, 6, 21, 25, 3, 4, 
	25, 5, 5, 25, 7, 8, 9, 16, 
	10, 11, 11, 12, 13, 13, 14, 15, 
	15, 25, 17, 18, 18, 19, 20, 20, 
	25, 22, 23, 9, 16, 1, 1
};

static const char _foo_trans_actions[] = {
	0, 0, 27, 27, 27, 5, 0, 3, 
	15, 1, 0, 18, 0, 3, 24, 24, 
	0, 1, 0, 3, 1, 0, 3, 1, 
	0, 12, 0, 1, 0, 3, 1, 0, 
	9, 0, 3, 21, 21, 0, 7
};

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, 7
};

static const int foo_start = 24;
static const int foo_first_final = 24;
static const int foo_error = 0;

static const int foo_en_main = 24;


#line 69 ".\\scratchpad\\testragel.rl"

void 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 112 ".\\scratchpad\\testragel.cpp"
	{
	cs = foo_start;
	}

#line 79 ".\\scratchpad\\testragel.rl"
  
#line 115 ".\\scratchpad\\testragel.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:
	_trans = _foo_indicies[_trans];
	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 13 ".\\scratchpad\\testragel.rl"
	{
    start = p;
  }
	break;
	case 1:
#line 17 ".\\scratchpad\\testragel.rl"
	{
    auto [ptr, ec] = std::from_chars(start, p, value);
    dbc::check(ec == std::errc(), "error in number parsing");
    println("NUMBER {}", value);
  }
	break;
	case 2:
#line 23 ".\\scratchpad\\testragel.rl"
	{
   println("256 color");
  }
	break;
	case 3:
#line 27 ".\\scratchpad\\testragel.rl"
	{
    println("true color");
  }
	break;
	case 4:
#line 31 ".\\scratchpad\\testragel.rl"
	{
    println("single color");
  }
	break;
	case 5:
#line 35 ".\\scratchpad\\testragel.rl"
	{
    println("basic color");
  }
	break;
	case 6:
#line 39 ".\\scratchpad\\testragel.rl"
	{
    println("BG");
  }
	break;
	case 7:
#line 43 ".\\scratchpad\\testragel.rl"
	{
    println("FG");
  }
	break;
	case 8:
#line 57 ".\\scratchpad\\testragel.rl"
	{ println("START"); }
	break;
	case 9:
#line 64 ".\\scratchpad\\testragel.rl"
	{ println("END"); }
	break;
#line 236 ".\\scratchpad\\testragel.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 9:
#line 64 ".\\scratchpad\\testragel.rl"
	{ println("END"); }
	break;
#line 254 ".\\scratchpad\\testragel.cpp"
		}
	}
	}

	_out: {}
	}

#line 80 ".\\scratchpad\\testragel.rl"
}

int main() {
  // possibly put alphtype unsigned int?
  std::vector<std::string_view> tests = {
    "\x1B[38;5;78;98m",
    "\x1B[48;5;78;98m",
    "\x1B[38;2;36;46;23m",
    "\x1B[48;2;36;46;23m",
    "\x1B[36;46m",
    "\x1B[36m",
  };

  for(auto test : tests) {
    println("--- PARSING");
    parse_ansi(test);
  }

  return 0;
}
