
#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 40 ".\\scratchpad\\testragel.rl"



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

static const char _foo_key_offsets[] = {
	0, 0, 1, 3, 5, 6, 7, 12, 
	15, 17, 19, 22, 24
};

static const char _foo_trans_keys[] = {
	91, 48, 57, 50, 53, 59, 59, 27, 
	59, 109, 48, 57, 59, 48, 57, 48, 
	57, 48, 57, 59, 48, 57, 48, 57, 
	59, 48, 57, 0
};

static const char _foo_single_lengths[] = {
	0, 1, 0, 2, 1, 1, 3, 1, 
	0, 0, 1, 0, 1
};

static const char _foo_range_lengths[] = {
	0, 0, 1, 0, 0, 0, 1, 1, 
	1, 1, 1, 1, 1
};

static const char _foo_index_offsets[] = {
	0, 0, 2, 4, 7, 9, 11, 16, 
	19, 21, 23, 26, 28
};

static const char _foo_trans_targs[] = {
	6, 0, 8, 6, 4, 5, 0, 9, 
	0, 11, 0, 1, 3, 6, 7, 0, 
	2, 7, 6, 8, 6, 10, 6, 9, 
	10, 6, 12, 6, 11, 12, 6, 6, 
	6, 6, 6, 6, 6, 6, 0
};

static const char _foo_trans_actions[] = {
	11, 0, 1, 19, 0, 0, 0, 0, 
	0, 0, 0, 0, 0, 13, 33, 0, 
	3, 9, 21, 0, 24, 1, 17, 3, 
	0, 30, 1, 15, 3, 0, 27, 19, 
	21, 24, 17, 30, 15, 27, 0
};

static const char _foo_to_state_actions[] = {
	0, 0, 0, 0, 0, 0, 5, 0, 
	0, 0, 0, 0, 0
};

static const char _foo_from_state_actions[] = {
	0, 0, 0, 0, 0, 0, 7, 0, 
	0, 0, 0, 0, 0
};

static const char _foo_eof_trans[] = {
	0, 0, 32, 0, 0, 0, 0, 33, 
	34, 35, 36, 37, 38
};

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

static const int foo_en_main = 6;


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

void parse_ansi(std::string_view &codes) {
  const char *start = NULL;
  int cs = 0;
  size_t act = 0;
  const char *p = codes.data();
  const char *pe = p + codes.size();
  const char *ts = p;
  const char *te = pe;
  const char *eof = pe;

  
#line 96 ".\\scratchpad\\testragel.cpp"
	{
	cs = foo_start;
	ts = 0;
	te = 0;
	act = 0;
	}

#line 55 ".\\scratchpad\\testragel.rl"
  
#line 102 ".\\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:
	_acts = _foo_actions + _foo_from_state_actions[cs];
	_nacts = (unsigned int) *_acts++;
	while ( _nacts-- > 0 ) {
		switch ( *_acts++ ) {
	case 3:
#line 1 "NONE"
	{ts = p;}
	break;
#line 121 ".\\scratchpad\\testragel.cpp"
		}
	}

	_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:
_eof_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"
	{
    size_t value = 0;
    auto [ptr, ec] = std::from_chars(start, p, value);
    dbc::check(ec == std::errc(), "error in number parsing");
    println("NUMBER {}", value);
  }
	break;
	case 4:
#line 1 "NONE"
	{te = p+1;}
	break;
	case 5:
#line 33 ".\\scratchpad\\testragel.rl"
	{te = p+1;{ println("START"); }}
	break;
	case 6:
#line 38 ".\\scratchpad\\testragel.rl"
	{te = p+1;{ println("END"); }}
	break;
	case 7:
#line 34 ".\\scratchpad\\testragel.rl"
	{te = p;p--;{ println("single"); }}
	break;
	case 8:
#line 35 ".\\scratchpad\\testragel.rl"
	{te = p;p--;{ println("basic"); }}
	break;
	case 9:
#line 36 ".\\scratchpad\\testragel.rl"
	{te = p;p--;{ println("256 color"); }}
	break;
	case 10:
#line 37 ".\\scratchpad\\testragel.rl"
	{te = p;p--;{ println("true color"); }}
	break;
	case 11:
#line 34 ".\\scratchpad\\testragel.rl"
	{{p = ((te))-1;}{ println("single"); }}
	break;
#line 222 ".\\scratchpad\\testragel.cpp"
		}
	}

_again:
	_acts = _foo_actions + _foo_to_state_actions[cs];
	_nacts = (unsigned int) *_acts++;
	while ( _nacts-- > 0 ) {
		switch ( *_acts++ ) {
	case 2:
#line 1 "NONE"
	{ts = 0;}
	break;
#line 233 ".\\scratchpad\\testragel.cpp"
		}
	}

	if ( cs == 0 )
		goto _out;
	if ( ++p != pe )
		goto _resume;
	_test_eof: {}
	if ( p == eof )
	{
	if ( _foo_eof_trans[cs] > 0 ) {
		_trans = _foo_eof_trans[cs] - 1;
		goto _eof_trans;
	}
	}

	_out: {}
	}

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

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


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

  return 0;
}
