package parser

// Table values are generated like this:
//
//	index:  currentState << IndexStateShift | charCode
//	value:  action << TransitionActionShift | nextState
const (
	TransitionActionShift = 4
	TransitionStateMask   = 15
	IndexStateShift       = 8

	// DefaultTableSize is the default size of the transition table.
	DefaultTableSize = 4096
)

// Table is a DEC ANSI transition table.
var Table = GenerateTransitionTable()

// TransitionTable is a DEC ANSI transition table.
// https://vt100.net/emu/dec_ansi_parser
type TransitionTable []byte

// NewTransitionTable returns a new DEC ANSI transition table.
func ( int) TransitionTable {
	if  <= 0 {
		 = DefaultTableSize
	}
	return TransitionTable(make([]byte, ))
}

// SetDefault sets default transition.
func ( TransitionTable) ( Action,  State) {
	for  := range  {
		[] = <<TransitionActionShift | 
	}
}

// AddOne adds a transition.
func ( TransitionTable) ( byte,  State,  Action,  State) {
	 := int()<<IndexStateShift | int()
	 := <<TransitionActionShift | 
	[] = 
}

// AddMany adds many transitions.
func ( TransitionTable) ( []byte,  State,  Action,  State) {
	for ,  := range  {
		.AddOne(, , , )
	}
}

// AddRange adds a range of transitions.
func ( TransitionTable) (,  byte,  State,  Action,  State) {
	for  := int();  <= int(); ++ {
		.AddOne(byte(), , , ) //nolint:gosec
	}
}

// Transition returns the next state and action for the given state and byte.
func ( TransitionTable) ( State,  byte) (State, Action) {
	 := int()<<IndexStateShift | int()
	 := []
	return  & TransitionStateMask,  >> TransitionActionShift
}

// byte range macro.
func (,  byte) []byte {
	var  []byte
	for  := int();  <= int(); ++ {
		 = append(, byte()) //nolint:gosec
	}
	return 
}

// GenerateTransitionTable generates a DEC ANSI transition table compatible
// with the VT500-series of terminals. This implementation includes a few
// modifications that include:
//   - A new Utf8State is introduced to handle UTF8 sequences.
//   - Osc and Dcs data accept UTF8 sequences by extending the printable range
//     to 0xFF and 0xFE respectively.
//   - We don't ignore 0x3A (':') when building Csi and Dcs parameters and
//     instead use it to denote sub-parameters.
//   - Support dispatching SosPmApc sequences.
//   - The DEL (0x7F) character is executed in the Ground state.
//   - The DEL (0x7F) character is collected in the DcsPassthrough string state.
//   - The ST C1 control character (0x9C) is executed and not ignored.
func () TransitionTable {
	 := NewTransitionTable(DefaultTableSize)
	.SetDefault(NoneAction, GroundState)

	// Anywhere
	for ,  := range r(GroundState, Utf8State) {
		// Anywhere -> Ground
		.AddMany([]byte{0x18, 0x1a, 0x99, 0x9a}, , ExecuteAction, GroundState)
		.AddRange(0x80, 0x8F, , ExecuteAction, GroundState)
		.AddRange(0x90, 0x97, , ExecuteAction, GroundState)
		.AddOne(0x9C, , ExecuteAction, GroundState)
		// Anywhere -> Escape
		.AddOne(0x1B, , ClearAction, EscapeState)
		// Anywhere -> SosStringState
		.AddOne(0x98, , StartAction, SosStringState)
		// Anywhere -> PmStringState
		.AddOne(0x9E, , StartAction, PmStringState)
		// Anywhere -> ApcStringState
		.AddOne(0x9F, , StartAction, ApcStringState)
		// Anywhere -> CsiEntry
		.AddOne(0x9B, , ClearAction, CsiEntryState)
		// Anywhere -> DcsEntry
		.AddOne(0x90, , ClearAction, DcsEntryState)
		// Anywhere -> OscString
		.AddOne(0x9D, , StartAction, OscStringState)
		// Anywhere -> Utf8
		.AddRange(0xC2, 0xDF, , CollectAction, Utf8State) // UTF8 2 byte sequence
		.AddRange(0xE0, 0xEF, , CollectAction, Utf8State) // UTF8 3 byte sequence
		.AddRange(0xF0, 0xF4, , CollectAction, Utf8State) // UTF8 4 byte sequence
	}

	// Ground
	.AddRange(0x00, 0x17, GroundState, ExecuteAction, GroundState)
	.AddOne(0x19, GroundState, ExecuteAction, GroundState)
	.AddRange(0x1C, 0x1F, GroundState, ExecuteAction, GroundState)
	.AddRange(0x20, 0x7E, GroundState, PrintAction, GroundState)
	.AddOne(0x7F, GroundState, ExecuteAction, GroundState)

	// EscapeIntermediate
	.AddRange(0x00, 0x17, EscapeIntermediateState, ExecuteAction, EscapeIntermediateState)
	.AddOne(0x19, EscapeIntermediateState, ExecuteAction, EscapeIntermediateState)
	.AddRange(0x1C, 0x1F, EscapeIntermediateState, ExecuteAction, EscapeIntermediateState)
	.AddRange(0x20, 0x2F, EscapeIntermediateState, CollectAction, EscapeIntermediateState)
	.AddOne(0x7F, EscapeIntermediateState, IgnoreAction, EscapeIntermediateState)
	// EscapeIntermediate -> Ground
	.AddRange(0x30, 0x7E, EscapeIntermediateState, DispatchAction, GroundState)

	// Escape
	.AddRange(0x00, 0x17, EscapeState, ExecuteAction, EscapeState)
	.AddOne(0x19, EscapeState, ExecuteAction, EscapeState)
	.AddRange(0x1C, 0x1F, EscapeState, ExecuteAction, EscapeState)
	.AddOne(0x7F, EscapeState, IgnoreAction, EscapeState)
	// Escape -> Ground
	.AddRange(0x30, 0x4F, EscapeState, DispatchAction, GroundState)
	.AddRange(0x51, 0x57, EscapeState, DispatchAction, GroundState)
	.AddOne(0x59, EscapeState, DispatchAction, GroundState)
	.AddOne(0x5A, EscapeState, DispatchAction, GroundState)
	.AddOne(0x5C, EscapeState, DispatchAction, GroundState)
	.AddRange(0x60, 0x7E, EscapeState, DispatchAction, GroundState)
	// Escape -> Escape_intermediate
	.AddRange(0x20, 0x2F, EscapeState, CollectAction, EscapeIntermediateState)
	// Escape -> Sos_pm_apc_string
	.AddOne('X', EscapeState, StartAction, SosStringState) // SOS
	.AddOne('^', EscapeState, StartAction, PmStringState)  // PM
	.AddOne('_', EscapeState, StartAction, ApcStringState) // APC
	// Escape -> Dcs_entry
	.AddOne('P', EscapeState, ClearAction, DcsEntryState)
	// Escape -> Csi_entry
	.AddOne('[', EscapeState, ClearAction, CsiEntryState)
	// Escape -> Osc_string
	.AddOne(']', EscapeState, StartAction, OscStringState)

	// Sos_pm_apc_string
	for ,  := range r(SosStringState, ApcStringState) {
		.AddRange(0x00, 0x17, , PutAction, )
		.AddOne(0x19, , PutAction, )
		.AddRange(0x1C, 0x1F, , PutAction, )
		.AddRange(0x20, 0x7F, , PutAction, )
		// ESC, ST, CAN, and SUB terminate the sequence
		.AddOne(0x1B, , DispatchAction, EscapeState)
		.AddOne(0x9C, , DispatchAction, GroundState)
		.AddMany([]byte{0x18, 0x1A}, , IgnoreAction, GroundState)
	}

	// Dcs_entry
	.AddRange(0x00, 0x07, DcsEntryState, IgnoreAction, DcsEntryState)
	.AddRange(0x0E, 0x17, DcsEntryState, IgnoreAction, DcsEntryState)
	.AddOne(0x19, DcsEntryState, IgnoreAction, DcsEntryState)
	.AddRange(0x1C, 0x1F, DcsEntryState, IgnoreAction, DcsEntryState)
	.AddOne(0x7F, DcsEntryState, IgnoreAction, DcsEntryState)
	// Dcs_entry -> Dcs_intermediate
	.AddRange(0x20, 0x2F, DcsEntryState, CollectAction, DcsIntermediateState)
	// Dcs_entry -> Dcs_param
	.AddRange(0x30, 0x3B, DcsEntryState, ParamAction, DcsParamState)
	.AddRange(0x3C, 0x3F, DcsEntryState, PrefixAction, DcsParamState)
	// Dcs_entry -> Dcs_passthrough
	.AddRange(0x08, 0x0D, DcsEntryState, PutAction, DcsStringState) // Follows ECMA-48 ยง 8.3.27
	// XXX: allows passing ESC (not a ECMA-48 standard) this to allow for
	// passthrough of ANSI sequences like in Screen or Tmux passthrough mode.
	.AddOne(0x1B, DcsEntryState, PutAction, DcsStringState)
	.AddRange(0x40, 0x7E, DcsEntryState, StartAction, DcsStringState)

	// Dcs_intermediate
	.AddRange(0x00, 0x17, DcsIntermediateState, IgnoreAction, DcsIntermediateState)
	.AddOne(0x19, DcsIntermediateState, IgnoreAction, DcsIntermediateState)
	.AddRange(0x1C, 0x1F, DcsIntermediateState, IgnoreAction, DcsIntermediateState)
	.AddRange(0x20, 0x2F, DcsIntermediateState, CollectAction, DcsIntermediateState)
	.AddOne(0x7F, DcsIntermediateState, IgnoreAction, DcsIntermediateState)
	// Dcs_intermediate -> Dcs_passthrough
	.AddRange(0x30, 0x3F, DcsIntermediateState, StartAction, DcsStringState)
	.AddRange(0x40, 0x7E, DcsIntermediateState, StartAction, DcsStringState)

	// Dcs_param
	.AddRange(0x00, 0x17, DcsParamState, IgnoreAction, DcsParamState)
	.AddOne(0x19, DcsParamState, IgnoreAction, DcsParamState)
	.AddRange(0x1C, 0x1F, DcsParamState, IgnoreAction, DcsParamState)
	.AddRange(0x30, 0x3B, DcsParamState, ParamAction, DcsParamState)
	.AddOne(0x7F, DcsParamState, IgnoreAction, DcsParamState)
	.AddRange(0x3C, 0x3F, DcsParamState, IgnoreAction, DcsParamState)
	// Dcs_param -> Dcs_intermediate
	.AddRange(0x20, 0x2F, DcsParamState, CollectAction, DcsIntermediateState)
	// Dcs_param -> Dcs_passthrough
	.AddRange(0x40, 0x7E, DcsParamState, StartAction, DcsStringState)

	// Dcs_passthrough
	.AddRange(0x00, 0x17, DcsStringState, PutAction, DcsStringState)
	.AddOne(0x19, DcsStringState, PutAction, DcsStringState)
	.AddRange(0x1C, 0x1F, DcsStringState, PutAction, DcsStringState)
	.AddRange(0x20, 0x7E, DcsStringState, PutAction, DcsStringState)
	.AddOne(0x7F, DcsStringState, PutAction, DcsStringState)
	.AddRange(0x80, 0xFF, DcsStringState, PutAction, DcsStringState) // Allow Utf8 characters by extending the printable range from 0x7F to 0xFF
	// ST, CAN, SUB, and ESC terminate the sequence
	.AddOne(0x1B, DcsStringState, DispatchAction, EscapeState)
	.AddOne(0x9C, DcsStringState, DispatchAction, GroundState)
	.AddMany([]byte{0x18, 0x1A}, DcsStringState, IgnoreAction, GroundState)

	// Csi_param
	.AddRange(0x00, 0x17, CsiParamState, ExecuteAction, CsiParamState)
	.AddOne(0x19, CsiParamState, ExecuteAction, CsiParamState)
	.AddRange(0x1C, 0x1F, CsiParamState, ExecuteAction, CsiParamState)
	.AddRange(0x30, 0x3B, CsiParamState, ParamAction, CsiParamState)
	.AddOne(0x7F, CsiParamState, IgnoreAction, CsiParamState)
	.AddRange(0x3C, 0x3F, CsiParamState, IgnoreAction, CsiParamState)
	// Csi_param -> Ground
	.AddRange(0x40, 0x7E, CsiParamState, DispatchAction, GroundState)
	// Csi_param -> Csi_intermediate
	.AddRange(0x20, 0x2F, CsiParamState, CollectAction, CsiIntermediateState)

	// Csi_intermediate
	.AddRange(0x00, 0x17, CsiIntermediateState, ExecuteAction, CsiIntermediateState)
	.AddOne(0x19, CsiIntermediateState, ExecuteAction, CsiIntermediateState)
	.AddRange(0x1C, 0x1F, CsiIntermediateState, ExecuteAction, CsiIntermediateState)
	.AddRange(0x20, 0x2F, CsiIntermediateState, CollectAction, CsiIntermediateState)
	.AddOne(0x7F, CsiIntermediateState, IgnoreAction, CsiIntermediateState)
	// Csi_intermediate -> Ground
	.AddRange(0x40, 0x7E, CsiIntermediateState, DispatchAction, GroundState)
	// Csi_intermediate -> Csi_ignore
	.AddRange(0x30, 0x3F, CsiIntermediateState, IgnoreAction, GroundState)

	// Csi_entry
	.AddRange(0x00, 0x17, CsiEntryState, ExecuteAction, CsiEntryState)
	.AddOne(0x19, CsiEntryState, ExecuteAction, CsiEntryState)
	.AddRange(0x1C, 0x1F, CsiEntryState, ExecuteAction, CsiEntryState)
	.AddOne(0x7F, CsiEntryState, IgnoreAction, CsiEntryState)
	// Csi_entry -> Ground
	.AddRange(0x40, 0x7E, CsiEntryState, DispatchAction, GroundState)
	// Csi_entry -> Csi_intermediate
	.AddRange(0x20, 0x2F, CsiEntryState, CollectAction, CsiIntermediateState)
	// Csi_entry -> Csi_param
	.AddRange(0x30, 0x3B, CsiEntryState, ParamAction, CsiParamState)
	.AddRange(0x3C, 0x3F, CsiEntryState, PrefixAction, CsiParamState)

	// Osc_string
	.AddRange(0x00, 0x06, OscStringState, IgnoreAction, OscStringState)
	.AddRange(0x08, 0x17, OscStringState, IgnoreAction, OscStringState)
	.AddOne(0x19, OscStringState, IgnoreAction, OscStringState)
	.AddRange(0x1C, 0x1F, OscStringState, IgnoreAction, OscStringState)
	.AddRange(0x20, 0xFF, OscStringState, PutAction, OscStringState) // Allow Utf8 characters by extending the printable range from 0x7F to 0xFF

	// ST, CAN, SUB, ESC, and BEL terminate the sequence
	.AddOne(0x1B, OscStringState, DispatchAction, EscapeState)
	.AddOne(0x07, OscStringState, DispatchAction, GroundState)
	.AddOne(0x9C, OscStringState, DispatchAction, GroundState)
	.AddMany([]byte{0x18, 0x1A}, OscStringState, IgnoreAction, GroundState)

	return 
}