package decoder

import (
	
)

// The syntax errors are the ones of encoding/json of the Go version, whose messages and offsets differ between Go
// 1.27, which is made of encoding/json/v2, and the earlier versions ( see syntax_error_legacy.go and
// syntax_error_v2.go ).

// syntaxWhere is where in the grammar a byte is which the grammar doesn't have there.
type syntaxWhere uint8

const (
	whereValue syntaxWhere = iota
	whereKey
	whereAfterKey
	whereAfterPair
	whereAfterElement
	whereAfterTop
	whereString
	whereNumber
	whereFraction
	whereExponent
)

// isEnd reports whether cursor is the nul byte which ends buf, the input followed by it.
func isEnd( []byte,  int64) bool {
	return  == int64(len())-1 && [] == nul
}

// endError returns the error of the end of the input at the total offset at.
func endError( int64) error {
	return errors.ErrSyntaxAtEnd("unexpected end of JSON input", )
}

// syntaxErrorAt returns the syntax error of the byte at cursor, which the grammar doesn't have where it is: the end
// of the input if it is the nul byte which ends the buffer. It is not inlined, so that its callers keep their size.
//
//go:noinline
func syntaxErrorAt( []byte,  int64,  syntaxWhere) error {
	if isEnd(, ) {
		return endError()
	}
	 := invalidCharacterError(, , .text())
	if  == whereValue {
		// a stream of Go 1.27 tells a bracket there by the object or the array which Token opened
		 = errors.WithValueStartAt(, )
	}
	return 
}

// numberSyntaxError returns the syntax error of the byte at cursor in the number which starts at start.
//
//go:noinline
func numberSyntaxError( []byte, ,  int64,  syntaxWhere) error {
	if isEnd(, ) {
		return scalarEndError(, , , .text())
	}
	return invalidCharacterError(, , .text())
}

// literalSyntaxError returns the syntax error of the literal at cursor, which is not lit: the first byte which
// differs from lit.
func literalSyntaxError( []byte,  int64,  string) error {
	for  := 1;  < len(); ++ {
		 :=  + int64()
		if  := [];  != [] {
			 := "in literal " +  + " (expecting " + quoteChar([]) + ")"
			if isEnd(, ) {
				return scalarEndError(, , int64(len())-1, )
			}
			return invalidCharacterError(, , )
		}
	}
	return nil
}

// stringSyntaxError returns the syntax error of the string at cursor: a control character, an escape which is not
// one, or the end of the input.
func stringSyntaxError( []byte,  int64) error {
	for  :=  + 1; ; ++ {
		switch  := []; {
		case  == '"':
			return nil
		case  < ' ':
			return syntaxErrorAt(, , whereString)
		case  == '\\':
			switch [+1] {
			case '"', '\\', '/', 'b', 'f', 'n', 'r', 't':
				++
			case 'u':
				for  := int64(2);  <= 5; ++ {
					if !isHexDigit([+]) {
						return escapeSyntaxError(, , +)
					}
				}
				 += 5
			default:
				return escapeSyntaxError(, , +1)
			}
		}
	}
}

func isHexDigit( byte) bool {
	return -'0' <= 9 || (|0x20)-'a' <= 5
}