package decoder

import (
	
	
)

// The values which are not decoded, as the ones of the keys which match no field, are skipped by the grammar of
// JSON, as encoding/json checks the whole input: a value which is not valid is a syntax error wherever it is.

// skipState is where a skip of an object or an array is: what it looks for next.
type skipState uint8

const (
	// skipValueStart looks for a value.
	skipValueStart skipState = iota
	// skipAfterValue looks for what follows a value: a comma or the end of the object or the array it is in.
	skipAfterValue
)

// skipRestOfObject returns the position after the end of the object whose value ends at cursor, which it checks by
// the grammar of JSON: nesting is how deep the object is nested in the input.
func skipRestOfObject( []byte, ,  int64) (int64, error) {
	return skipGrammarFast(, , -1, 1, 1, resumeAfterValue)
}

// skipResume is where skipFast resumes: at a value, after a value, or after the key of an object.
type skipResume uint8

const (
	resumeValue skipResume = iota
	resumeAfterValue
	resumeAfterKey
	// resumeKey is where a key is waited for, which skipFast doesn't resume at: it tells the error of the byte.
	resumeKey
)

// skipEvent is why skipFast returns: the end of the value, or what it doesn't do itself, which its caller does
// before it resumes it.
type skipEvent uint8

const (
	skipDone skipEvent = iota
	// skipAString is a string which has an escape or a control character, or is at the end of the buffer.
	skipAString
	// skipANumber is a number which starts with a minus sign or is not an integer.
	skipANumber
	// skipDeep is an object or an array deeper than the levels which the bits of a word hold.
	skipDeep
	// skipInvalid is a byte which the grammar doesn't have where it is, for which the caller makes the error.
	skipInvalid
)

// skipGrammarFast skips the objects and the arrays which are open, level of them whose kinds are the bits of
// objects, from cursor, where it resumes, by skipFast, which returns for what it doesn't do: a string with an
// escape, a number which is not an integer, a byte which is not valid, which is done here, or an object or an array
// deeper than skipLevels levels, which the rest of the value is skipped by skipGrammar for.
func skipGrammarFast( []byte, , ,  int64,  uint64,  skipResume) (int64, error) {
	var  skipEvent
	, , , ,  = skipFast(, , , , , skipMaxLevel())
	if  == skipDone {
		return , nil
	}
	return skipGrammarEvent(, , , , , , )
}

// skipLevels is the number of the levels of objects and arrays which skipFast follows at most, by the bits of a
// uint64 ( see skipFast ).
const skipLevels = 64

// skipMaxLevel returns the number of the levels of objects and arrays which skipFast opens at most, for a value
// nested nesting levels deep.
func skipMaxLevel( int64) int64 {
	return min(maxDecodeNestingDepth-, skipLevels)
}

// skipGrammarEvent does what skipFast stopped for at cursor, ev, and resumes it, up to the end of the value.
func skipGrammarEvent( []byte, , ,  int64,  uint64,  skipResume,  skipEvent) (int64, error) {
	 := skipMaxLevel()
	for {
		switch  {
		case skipDone:
			return , nil
		case skipAString:
			,  := skipString(, )
			if  != nil {
				return 0, stringError(, , )
			}
			 = 
		case skipANumber:
			,  := skipNumber(, )
			if  != nil {
				return 0, 
			}
			 = 
		case skipDeep:
			// the value at cursor opens the level after skipLevels, or the limit of the nesting
			return skipGrammar(, , , , , skipValueStart)
		default:
			return 0, skipFastError(, , , , )
		}
		, , , ,  = skipFast(, , , , , )
	}
}

// skipFastError returns the syntax error of the byte at cursor, which skipFast stopped at, where it resumes.
func skipFastError( []byte, ,  int64,  uint64,  skipResume) error {
	switch  {
	case resumeKey:
		return syntaxErrorAt(, , whereKey)
	case resumeAfterKey:
		return syntaxErrorAt(, , whereAfterKey)
	case resumeAfterValue:
		if &(1<<uint(-1)) != 0 {
			return syntaxErrorAt(, , whereAfterPair)
		}
		return syntaxErrorAt(, , whereAfterElement)
	}
	switch [] {
	case 't':
		return literalSyntaxError(, , "true")
	case 'f':
		return literalSyntaxError(, , "false")
	case 'n':
		return literalSyntaxError(, , "null")
	}
	return syntaxErrorAt(, , whereValue)
}

// skipFast skips from cursor, where it resumes, the objects and the arrays which are open, level of them whose
// kinds are the bits of objects, set for an object, up to the end of the outermost of them, and returns where it
// is and why it stops: at the end, or at what it doesn't do itself ( see skipEvent ), after which its caller
// resumes it. It calls nothing, so that its loop keeps its state in the registers.
//
// For skipInvalid, resume is the state at the byte: resumeValue for a value, resumeAfterValue after a value,
// resumeKey where a key is waited for and resumeAfterKey where its colon is.
//
//nolint:maintidx // one loop of gotos without calls, whose state stays in the registers
func skipFast( []byte, ,  int64,  uint64,  skipResume,  int64) (int64, int64, uint64, skipResume, skipEvent) {
	 := int64(len())
	// the bytes are read without the checks of the bounds: every loop stops at the nul byte at the end of the
	// buffer, and a literal is compared byte by byte up to the first byte which differs
	 := (*sliceHeader)(unsafe.Pointer(&)).data
	// c is the byte at cursor after the white spaces are skipped: a byte above ' ' isn't one, so that the table
	// of the white spaces is read only for the bytes which may be
	var  byte
	switch  {
	case resumeAfterValue:
		goto 
	case resumeAfterKey:
		goto 
	}
:
	if  = char(, );  <= ' ' {
		for isWhiteSpace[] {
			++
			 = char(, )
		}
	}
	// a string, which most values are, before the switch of the others
	if  == '"' {
		// a string of plain bytes up to its quote, eight bytes at a time
		for  :=  + 1; +8 <= ;  += 8 {
			if  := keyEndBytes(load64(, ));  != 0 {
				if  += int64(bits.TrailingZeros64() / 8); char(, ) == '"' {
					 =  + 1
					goto 
				}
				break
			}
		}
		return , , , resumeAfterValue, skipAString
	}
	switch  {
	case '{', '[':
		if +1 >  {
			return , , , resumeValue, skipDeep
		}
		if  == '{' {
			 |= 1 << uint()
		} else {
			 &^= 1 << uint()
		}
		++
		++
		 := 
		if  = char(, );  <= ' ' {
			for isWhiteSpace[] {
				++
				 = char(, )
			}
		}
		if  == '{' {
			if  == '}' {
				++
				goto 
			}
			goto 
		}
		if  == ']' {
			++
			goto 
		}
		goto 
	case '1', '2', '3', '4', '5', '6', '7', '8', '9':
		 := 
		++
		for char(, )-'0' <= 9 {
			++
		}
		if  := char(, );  == '.' ||  == 'e' ||  == 'E' {
			// a number which is not an integer, which skipNumber checks from its start
			return , , , resumeAfterValue, skipANumber
		}
	case '0':
		if  := char(, +1);  == '.' ||  == 'e' ||  == 'E' {
			return , , , resumeAfterValue, skipANumber
		}
		++
	case '-':
		return , , , resumeAfterValue, skipANumber
	case 't':
		if char(, +1) != 'r' || char(, +2) != 'u' || char(, +3) != 'e' {
			return , , , resumeValue, skipInvalid
		}
		 += 4
	case 'f':
		if char(, +1) != 'a' || char(, +2) != 'l' || char(, +3) != 's' || char(, +4) != 'e' {
			return , , , resumeValue, skipInvalid
		}
		 += 5
	case 'n':
		if char(, +1) != 'u' || char(, +2) != 'l' || char(, +3) != 'l' {
			return , , , resumeValue, skipInvalid
		}
		 += 4
	default:
		return , , , resumeValue, skipInvalid
	}
:
	// a value here is in an object or an array: the value which skipFast starts at is one of them, and the end of
	// the outermost is returned at closed
	if  = char(, );  <= ' ' {
		for isWhiteSpace[] {
			++
			 = char(, )
		}
	}
	if &(1<<uint(-1)) != 0 {
		switch  {
		case ',':
			++
			if  = char(, );  <= ' ' {
				for isWhiteSpace[] {
					++
					 = char(, )
				}
			}
			goto 
		case '}':
			++
			goto 
		}
		return , , , resumeAfterValue, skipInvalid
	}
	switch  {
	case ',':
		++
		goto 
	case ']':
		++
		goto 
	}
	return , , , resumeAfterValue, skipInvalid
:
	if  != '"' {
		return , , , resumeKey, skipInvalid
	}
	// a key of plain bytes, as the strings of the values
	for  :=  + 1; +8 <= ;  += 8 {
		if  := keyEndBytes(load64(, ));  != 0 {
			if  += int64(bits.TrailingZeros64() / 8); char(, ) == '"' {
				 =  + 1
				goto 
			}
			break
		}
	}
	return , , , resumeAfterKey, skipAString
:
	if  = char(, );  <= ' ' {
		for isWhiteSpace[] {
			++
			 = char(, )
		}
	}
	if  != ':' {
		return , , , resumeAfterKey, skipInvalid
	}
	++
	// the single space after the colon, which most objects have, is skipped before the loop over the others
	if char(, ) == ' ' {
		++
	}
	goto 
:
	// the end of an object or an array, which cursor is after
	if --;  == 0 {
		return , , , resumeAfterValue, skipDone
	}
	goto 
}

// skipGrammar skips from cursor, in state, the values of the objects and the arrays which are open, level of them
// the innermost of which is an object, up to the end of the outermost of them, or the value at cursor if none is
// open, and returns the position after it.
//
// The kinds of the open values are the bits of a word, set for an object, for the first 64 levels, and of the
// words of deeper, which are allocated as the nesting gets deeper: the loop keeps them in its registers.
//
//nolint:maintidx // one loop of gotos without calls, whose state stays in the registers
func skipGrammar( []byte, , ,  int64,  uint64,  skipState) (int64, error) {
	 := maxDecodeNestingDepth - 
	 := int64(len())
	// the bytes are read without the checks of the bounds: every loop stops at the nul byte at the end of the
	// buffer, and a literal is compared byte by byte up to the first byte which differs
	 := (*sliceHeader)(unsafe.Pointer(&)).data
	var  []uint64
	if  == skipAfterValue {
		goto 
	}
:
	for isWhiteSpace[char(, )] {
		++
	}
	switch  := char(, );  {
	case '{', '[':
		if +1 >  {
			return 0, depthSyntaxError(, )
		}
		if  < 64 {
			if  == '{' {
				 |= 1 << uint()
			} else {
				 &^= 1 << uint()
			}
		} else {
			 = pushDeeper(, ,  == '{')
		}
		++
		++
		for isWhiteSpace[char(, )] {
			++
		}
		if  == '{' {
			if char(, ) == '}' {
				++
				--
				goto 
			}
			goto 
		}
		if char(, ) == ']' {
			++
			--
			goto 
		}
		goto 
	case '"':
		// a string of plain bytes up to its quote without a call, eight bytes at a time, and any other by
		// skipString, which checks its escapes
		for  :=  + 1; +8 <= ;  += 8 {
			if  := keyEndBytes(load64(, ));  != 0 {
				if  += int64(bits.TrailingZeros64() / 8); char(, ) == '"' {
					 =  + 1
					goto 
				}
				break
			}
		}
		,  := skipString(, )
		if  != nil {
			return 0, stringError(, , )
		}
		 = 
	case '1', '2', '3', '4', '5', '6', '7', '8', '9':
		// an integer, which most numbers are, without a call
		 := 
		++
		for char(, )-'0' <= 9 {
			++
		}
		if  := char(, );  == '.' ||  == 'e' ||  == 'E' {
			,  := skipNumberRest(, , )
			if  != nil {
				return 0, 
			}
			 = 
		}
	case '0':
		 := 
		++
		if  := char(, );  == '.' ||  == 'e' ||  == 'E' {
			,  := skipNumberRest(, , )
			if  != nil {
				return 0, 
			}
			 = 
		}
	case '-':
		,  := skipNumber(, )
		if  != nil {
			return 0, 
		}
		 = 
	case 't':
		if char(, +1) != 'r' || char(, +2) != 'u' || char(, +3) != 'e' {
			return 0, literalSyntaxError(, , "true")
		}
		 += 4
	case 'f':
		if char(, +1) != 'a' || char(, +2) != 'l' || char(, +3) != 's' || char(, +4) != 'e' {
			return 0, literalSyntaxError(, , "false")
		}
		 += 5
	case 'n':
		if char(, +1) != 'u' || char(, +2) != 'l' || char(, +3) != 'l' {
			return 0, literalSyntaxError(, , "null")
		}
		 += 4
	default:
		return 0, syntaxErrorAt(, , whereValue)
	}
:
	if  == 0 {
		return , nil
	}
	for isWhiteSpace[char(, )] {
		++
	}
	if inObject(, , -1) {
		switch char(, ) {
		case ',':
			++
			for isWhiteSpace[char(, )] {
				++
			}
			goto 
		case '}':
			++
			--
			goto 
		}
		return 0, syntaxErrorAt(, , whereAfterPair)
	}
	switch char(, ) {
	case ',':
		++
		goto 
	case ']':
		++
		--
		goto 
	}
	return 0, syntaxErrorAt(, , whereAfterElement)
:
	if char(, ) != '"' {
		return 0, syntaxErrorAt(, , whereKey)
	}
	// a key of plain bytes without a call, as the strings of the values
	for  :=  + 1; +8 <= ;  += 8 {
		if  := keyEndBytes(load64(, ));  != 0 {
			if  += int64(bits.TrailingZeros64() / 8); char(, ) == '"' {
				 =  + 1
				goto 
			}
			break
		}
	}
	{
		,  := skipString(, )
		if  != nil {
			return 0, stringError(, , )
		}
		 = 
	}
:
	for isWhiteSpace[char(, )] {
		++
	}
	if char(, ) != ':' {
		return 0, syntaxErrorAt(, , whereAfterKey)
	}
	++
	goto 
}

// inObject reports whether the value open at level is an object.
func inObject( uint64,  []uint64,  int64) bool {
	if  < 64 {
		return &(1<<uint()) != 0
	}
	return [(-64)/64]&(1<<uint((-64)%64)) != 0
}

// pushDeeper sets the kind of the value open at level, which is 64 or deeper, in deeper, which it returns.
func pushDeeper( []uint64,  int64,  bool) []uint64 {
	,  := (-64)/64, uint((-64)%64)
	for int64(len()) <=  {
		 = append(, 0)
	}
	if  {
		[] |= 1 << 
	} else {
		[] &^= 1 << 
	}
	return 
}

// skipNumber returns the position after the number at cursor, which it checks by the grammar of JSON.
func skipNumber( []byte,  int64) (int64, error) {
	 := 
	if [] == '-' {
		++
	}
	switch {
	case [] == '0':
		++
	case []-'1' <= 8:
		 = skipDigits(, +1)
	default:
		return 0, numberSyntaxError(, , , whereNumber)
	}
	return skipNumberRest(, , )
}

// skipNumberRest returns the position after the fraction and the exponent of the number which starts at start and
// whose integer ends at c, which it checks by the grammar of JSON.
func skipNumberRest( []byte, ,  int64) (int64, error) {
	if [] == '.' {
		++
		if []-'0' > 9 {
			return 0, numberSyntaxError(, , , whereFraction)
		}
		 = skipDigits(, )
	}
	if [] == 'e' || [] == 'E' {
		++
		if [] == '+' || [] == '-' {
			++
		}
		if []-'0' > 9 {
			return 0, numberSyntaxError(, , , whereExponent)
		}
		 = skipDigits(, )
	}
	return , nil
}

// stringError returns the syntax error of the string at cursor, which skipString failed with err, as encoding/json
// has it.
func stringError( []byte,  int64,  error) error {
	if  := stringSyntaxError(, );  != nil {
		return 
	}
	return 
}

// InputSyntaxError returns the first syntax error of the input of the decoding, as encoding/json reports it, or
// nil if the input is valid: the decoders report a syntax error as they meet it, which this reports again as
// encoding/json does, which checks the whole input first. The input is copied again, since the decoding writes
// its buffer, as the escapes of the keys are decoded in it.
func ( *RuntimeContext) () error {
	return inputSyntaxError(NewInput(.origin))
}

// inputSyntaxError returns the first syntax error of buf, the input followed by the nul byte, or nil.
func inputSyntaxError( []byte) error {
	,  := skipValue(, skipWhiteSpace(, 0), 0)
	if  != nil {
		return 
	}
	return ValidateEnd(, )
}

// Valid reports whether buf, the input followed by the nul byte, is a JSON value, by the grammar which the values
// which are not decoded are skipped by.
func ( []byte) bool {
	,  := skipValue(, skipWhiteSpace(, 0), 0)
	return  == nil && skipWhiteSpace(, ) == int64(len())-1
}

// ValidateEnd returns the syntax error of what follows the value which ends at cursor in buf, the input followed by
// the nul byte: white spaces only may follow it, up to the end of the input. A nul byte in the input is a byte which
// the grammar doesn't have there, as encoding/json has it.
func ( []byte,  int64) error {
	 = skipWhiteSpace(, )
	if  == int64(len())-1 {
		return nil
	}
	return syntaxErrorAt(, , whereAfterTop)
}