/
types.go
130 lines (111 loc) · 3.11 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright 2021 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package types
import (
`fmt`
`sync`
)
type ValueType int
type ParsingError uint
type SearchingError uint
// !NOT MODIFIED ONLY.
// This definitions are followed in native/types.h.
const (
V_EOF ValueType = 1
V_NULL ValueType = 2
V_TRUE ValueType = 3
V_FALSE ValueType = 4
V_ARRAY ValueType = 5
V_OBJECT ValueType = 6
V_STRING ValueType = 7
V_DOUBLE ValueType = 8
V_INTEGER ValueType = 9
_ ValueType = 10 // V_KEY_SEP
_ ValueType = 11 // V_ELEM_SEP
_ ValueType = 12 // V_ARRAY_END
_ ValueType = 13 // V_OBJECT_END
V_MAX
)
const (
B_DOUBLE_UNQUOTE = 0
B_UNICODE_REPLACE = 1
B_VALIDATE_STRING = 5
)
const (
F_DOUBLE_UNQUOTE = 1 << B_DOUBLE_UNQUOTE
F_UNICODE_REPLACE = 1 << B_UNICODE_REPLACE
F_VALIDATE_STRING = 1 << B_VALIDATE_STRING
)
const (
MAX_RECURSE = 4096
)
const (
SPACE_MASK = (1 << ' ') | (1 << '\t') | (1 << '\r') | (1 << '\n')
)
const (
ERR_EOF ParsingError = 1
ERR_INVALID_CHAR ParsingError = 2
ERR_INVALID_ESCAPE ParsingError = 3
ERR_INVALID_UNICODE ParsingError = 4
ERR_INTEGER_OVERFLOW ParsingError = 5
ERR_INVALID_NUMBER_FMT ParsingError = 6
ERR_RECURSE_EXCEED_MAX ParsingError = 7
ERR_FLOAT_INFINITY ParsingError = 8
)
var _ParsingErrors = []string{
0 : "ok",
ERR_EOF : "eof",
ERR_INVALID_CHAR : "invalid char",
ERR_INVALID_ESCAPE : "invalid escape char",
ERR_INVALID_UNICODE : "invalid unicode escape",
ERR_INTEGER_OVERFLOW : "integer overflow",
ERR_INVALID_NUMBER_FMT : "invalid number format",
ERR_RECURSE_EXCEED_MAX : "recursion exceeded max depth",
ERR_FLOAT_INFINITY : "float number is infinity",
}
func (self ParsingError) Error() string {
return "json: error when parsing input: " + self.Message()
}
func (self ParsingError) Message() string {
if int(self) < len(_ParsingErrors) {
return _ParsingErrors[self]
} else {
return fmt.Sprintf("unknown error %d", self)
}
}
type JsonState struct {
Vt ValueType
Dv float64
Iv int64
Ep int
Dbuf *byte
Dcap int
}
type StateMachine struct {
Sp int
Vt [MAX_RECURSE]int
}
var stackPool = sync.Pool{
New: func()interface{}{
return &StateMachine{}
},
}
func NewStateMachine() *StateMachine {
return stackPool.Get().(*StateMachine)
}
func FreeStateMachine(fsm *StateMachine) {
stackPool.Put(fsm)
}