2
* Copyright 2021 ByteDance Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
27
`github.com/bytedance/sonic/internal/native/types`
28
`github.com/bytedance/sonic/internal/rt`
31
type SyntaxError struct {
34
Code types.ParsingError
38
func (self SyntaxError) Error() string {
39
return fmt.Sprintf("%q", self.Description())
42
func (self SyntaxError) Description() string {
43
return "Syntax error " + self.description()
46
func (self SyntaxError) description() string {
47
/* check for empty source */
49
return fmt.Sprintf("no sources available: %#v", self)
52
p, x, q, y := calcBounds(len(self.Src), self.Pos)
54
/* compose the error description */
56
"at index %d: %s\n\n\t%s\n\t%s^%s\n",
60
strings.Repeat(".", x),
61
strings.Repeat(".", y),
65
func calcBounds(size int, pos int) (lbound int, lwidth int, rbound int, rwidth int) {
66
if pos >= size || pos < 0 {
74
/* prevent slicing before the beginning */
76
lbound, rbound, i = 0, rbound - lbound, i + lbound
79
/* prevent slicing beyond the end */
80
if n := size; rbound > n {
84
/* move the left bound if possible */
91
/* left and right length */
92
lwidth = clamp_zero(i)
93
rwidth = clamp_zero(rbound - lbound - i - 1)
98
func (self SyntaxError) Message() string {
100
return self.Code.Message()
105
func clamp_zero(v int) int {
113
/** JIT Error Helpers **/
115
var stackOverflow = &json.UnsupportedValueError {
116
Str : "Value nesting too deep",
117
Value : reflect.ValueOf("..."),
120
func error_wrap(src string, pos int, code types.ParsingError) error {
121
return *error_wrap_heap(src, pos, code)
125
func error_wrap_heap(src string, pos int, code types.ParsingError) *SyntaxError {
126
return &SyntaxError {
133
func error_type(vt *rt.GoType) error {
134
return &json.UnmarshalTypeError{Type: vt.Pack()}
137
type MismatchTypeError struct {
143
func swithchJSONType (src string, pos int) string {
146
case 'f': fallthrough
147
case 't': val = "bool"
148
case '"': val = "string"
149
case '{': val = "object"
150
case '[': val = "array"
151
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': val = "number"
156
func (self MismatchTypeError) Error() string {
160
Code : types.ERR_MISMATCH,
162
return fmt.Sprintf("Mismatch type %s with value %s %q", self.Type.String(), swithchJSONType(self.Src, self.Pos), se.description())
165
func (self MismatchTypeError) Description() string {
169
Code : types.ERR_MISMATCH,
171
return fmt.Sprintf("Mismatch type %s with value %s %s", self.Type.String(), swithchJSONType(self.Src, self.Pos), se.description())
174
func error_mismatch(src string, pos int, vt *rt.GoType) error {
175
return &MismatchTypeError {
182
func error_field(name string) error {
183
return errors.New("json: unknown field " + strconv.Quote(name))
186
func error_value(value string, vtype reflect.Type) error {
187
return &json.UnmarshalTypeError {