summaryrefslogtreecommitdiff
path: root/vendor/github.com/bytedance/sonic/ast/parser.go
diff options
context:
space:
mode:
authorLibravatar dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>2023-11-27 13:15:03 +0000
committerLibravatar GitHub <noreply@github.com>2023-11-27 13:15:03 +0000
commit66b77acb1c8b86f0be3836ccaf31683c0bfa317a (patch)
tree9a255a8ea8ef97229b6d75d17de45bdac1755be9 /vendor/github.com/bytedance/sonic/ast/parser.go
parent[bugfix] Add Actor to outgoing poll vote Create; other fixes (#2384) (diff)
downloadgotosocial-66b77acb1c8b86f0be3836ccaf31683c0bfa317a.tar.xz
[chore]: Bump github.com/gin-contrib/cors from 1.4.0 to 1.5.0 (#2388)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Diffstat (limited to 'vendor/github.com/bytedance/sonic/ast/parser.go')
-rw-r--r--vendor/github.com/bytedance/sonic/ast/parser.go101
1 files changed, 69 insertions, 32 deletions
diff --git a/vendor/github.com/bytedance/sonic/ast/parser.go b/vendor/github.com/bytedance/sonic/ast/parser.go
index 0a8e7b068..cb16f20bb 100644
--- a/vendor/github.com/bytedance/sonic/ast/parser.go
+++ b/vendor/github.com/bytedance/sonic/ast/parser.go
@@ -18,11 +18,15 @@ package ast
import (
`fmt`
+
`github.com/bytedance/sonic/internal/native/types`
`github.com/bytedance/sonic/internal/rt`
)
-const _DEFAULT_NODE_CAP int = 16
+const (
+ _DEFAULT_NODE_CAP int = 8
+ _APPEND_GROW_SHIFT = 1
+)
const (
_ERR_NOT_FOUND types.ParsingError = 33
@@ -30,7 +34,10 @@ const (
)
var (
+ // ErrNotExist means both key and value doesn't exist
ErrNotExist error = newError(_ERR_NOT_FOUND, "value not exists")
+
+ // ErrUnsupportType means API on the node is unsupported
ErrUnsupportType error = newError(_ERR_UNSUPPORT_TYPE, "unsupported type")
)
@@ -39,6 +46,7 @@ type Parser struct {
s string
noLazy bool
skipValue bool
+ dbuf *byte
}
/** Parser Private Methods **/
@@ -107,7 +115,7 @@ func (self *Parser) lspace(sp int) int {
return sp
}
-func (self *Parser) decodeArray(ret []Node) (Node, types.ParsingError) {
+func (self *Parser) decodeArray(ret *linkedNodes) (Node, types.ParsingError) {
sp := self.p
ns := len(self.s)
@@ -119,7 +127,7 @@ func (self *Parser) decodeArray(ret []Node) (Node, types.ParsingError) {
/* check for empty array */
if self.s[self.p] == ']' {
self.p++
- return emptyArrayNode, 0
+ return Node{t: types.V_ARRAY}, 0
}
/* allocate array space and parse every element */
@@ -149,7 +157,7 @@ func (self *Parser) decodeArray(ret []Node) (Node, types.ParsingError) {
}
/* add the value to result */
- ret = append(ret, val)
+ ret.Add(val)
self.p = self.lspace(self.p)
/* check for EOF */
@@ -160,17 +168,17 @@ func (self *Parser) decodeArray(ret []Node) (Node, types.ParsingError) {
/* check for the next character */
switch self.s[self.p] {
case ',' : self.p++
- case ']' : self.p++; return NewArray(ret), 0
- default:
- if val.isLazy() {
- return newLazyArray(self, ret), 0
- }
- return Node{}, types.ERR_INVALID_CHAR
+ case ']' : self.p++; return newArray(ret), 0
+ default:
+ // if val.isLazy() {
+ // return newLazyArray(self, ret), 0
+ // }
+ return Node{}, types.ERR_INVALID_CHAR
}
}
}
-func (self *Parser) decodeObject(ret []Pair) (Node, types.ParsingError) {
+func (self *Parser) decodeObject(ret *linkedPairs) (Node, types.ParsingError) {
sp := self.p
ns := len(self.s)
@@ -182,7 +190,7 @@ func (self *Parser) decodeObject(ret []Pair) (Node, types.ParsingError) {
/* check for empty object */
if self.s[self.p] == '}' {
self.p++
- return emptyObjectNode, 0
+ return Node{t: types.V_OBJECT}, 0
}
/* decode each pair */
@@ -235,7 +243,8 @@ func (self *Parser) decodeObject(ret []Pair) (Node, types.ParsingError) {
}
/* add the value to result */
- ret = append(ret, Pair{Key: key, Value: val})
+ // FIXME: ret's address may change here, thus previous referred node in ret may be invalid !!
+ ret.Add(Pair{Key: key, Value: val})
self.p = self.lspace(self.p)
/* check for EOF */
@@ -246,11 +255,11 @@ func (self *Parser) decodeObject(ret []Pair) (Node, types.ParsingError) {
/* check for the next character */
switch self.s[self.p] {
case ',' : self.p++
- case '}' : self.p++; return NewObject(ret), 0
+ case '}' : self.p++; return newObject(ret), 0
default:
- if val.isLazy() {
- return newLazyObject(self, ret), 0
- }
+ // if val.isLazy() {
+ // return newLazyObject(self, ret), 0
+ // }
return Node{}, types.ERR_INVALID_CHAR
}
}
@@ -290,15 +299,23 @@ func (self *Parser) Parse() (Node, types.ParsingError) {
case types.V_FALSE : return falseNode, 0
case types.V_STRING : return self.decodeString(val.Iv, val.Ep)
case types.V_ARRAY:
+ if p := skipBlank(self.s, self.p); p >= self.p && self.s[p] == ']' {
+ self.p = p + 1
+ return Node{t: types.V_ARRAY}, 0
+ }
if self.noLazy {
- return self.decodeArray(make([]Node, 0, _DEFAULT_NODE_CAP))
+ return self.decodeArray(new(linkedNodes))
}
- return newLazyArray(self, make([]Node, 0, _DEFAULT_NODE_CAP)), 0
+ return newLazyArray(self), 0
case types.V_OBJECT:
+ if p := skipBlank(self.s, self.p); p >= self.p && self.s[p] == '}' {
+ self.p = p + 1
+ return Node{t: types.V_OBJECT}, 0
+ }
if self.noLazy {
- return self.decodeObject(make([]Pair, 0, _DEFAULT_NODE_CAP))
+ return self.decodeObject(new(linkedPairs))
}
- return newLazyObject(self, make([]Pair, 0, _DEFAULT_NODE_CAP)), 0
+ return newLazyObject(self), 0
case types.V_DOUBLE : return NewNumber(self.s[val.Ep:self.p]), 0
case types.V_INTEGER : return NewNumber(self.s[val.Ep:self.p]), 0
default : return Node{}, types.ParsingError(-val.Vt)
@@ -429,7 +446,7 @@ func (self *Node) skipNextNode() *Node {
}
parser, stack := self.getParserAndArrayStack()
- ret := stack.v
+ ret := &stack.v
sp := parser.p
ns := len(parser.s)
@@ -458,7 +475,8 @@ func (self *Node) skipNextNode() *Node {
}
/* add the value to result */
- ret = append(ret, val)
+ ret.Add(val)
+ self.l++
parser.p = parser.lspace(parser.p)
/* check for EOF */
@@ -470,12 +488,11 @@ func (self *Node) skipNextNode() *Node {
switch parser.s[parser.p] {
case ',':
parser.p++
- self.setLazyArray(parser, ret)
- return &ret[len(ret)-1]
+ return ret.At(ret.Len()-1)
case ']':
parser.p++
self.setArray(ret)
- return &ret[len(ret)-1]
+ return ret.At(ret.Len()-1)
default:
return newSyntaxError(parser.syntaxError(types.ERR_INVALID_CHAR))
}
@@ -487,7 +504,7 @@ func (self *Node) skipNextPair() (*Pair) {
}
parser, stack := self.getParserAndObjectStack()
- ret := stack.v
+ ret := &stack.v
sp := parser.p
ns := len(parser.s)
@@ -541,7 +558,8 @@ func (self *Node) skipNextPair() (*Pair) {
}
/* add the value to result */
- ret = append(ret, Pair{Key: key, Value: val})
+ ret.Add(Pair{Key: key, Value: val})
+ self.l++
parser.p = parser.lspace(parser.p)
/* check for EOF */
@@ -553,12 +571,11 @@ func (self *Node) skipNextPair() (*Pair) {
switch parser.s[parser.p] {
case ',':
parser.p++
- self.setLazyObject(parser, ret)
- return &ret[len(ret)-1]
+ return ret.At(ret.Len()-1)
case '}':
parser.p++
self.setObject(ret)
- return &ret[len(ret)-1]
+ return ret.At(ret.Len()-1)
default:
return &Pair{key, *newSyntaxError(parser.syntaxError(types.ERR_INVALID_CHAR))}
}
@@ -601,10 +618,30 @@ func LoadsUseNumber(src string) (int, interface{}, error) {
}
}
+// NewParser returns pointer of new allocated parser
func NewParser(src string) *Parser {
return &Parser{s: src}
}
+// NewParser returns new allocated parser
+func NewParserObj(src string) Parser {
+ return Parser{s: src}
+}
+
+// decodeNumber controls if parser decodes the number values instead of skip them
+// WARN: once you set decodeNumber(true), please set decodeNumber(false) before you drop the parser
+// otherwise the memory CANNOT be reused
+func (self *Parser) decodeNumber(decode bool) {
+ if !decode && self.dbuf != nil {
+ types.FreeDbuf(self.dbuf)
+ self.dbuf = nil
+ return
+ }
+ if decode && self.dbuf == nil {
+ self.dbuf = types.NewDbuf()
+ }
+}
+
// ExportError converts types.ParsingError to std Error
func (self *Parser) ExportError(err types.ParsingError) error {
if err == _ERR_NOT_FOUND {
@@ -615,4 +652,4 @@ func (self *Parser) ExportError(err types.ParsingError) error {
Src : self.s,
Code: err,
}.Description())
-} \ No newline at end of file
+}