-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse.go
49 lines (43 loc) · 850 Bytes
/
parse.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
package jsonast
import (
"github.com/go-functional/jsonast/parse"
)
type state struct {
inObj bool
inArr bool
inStr bool
curVal Value
aggVal Value
}
func newState() state {
return state{}
}
func (s state) addToken(tkn parse.Token) error {
// char := tkn.char
switch tkn {
case parse.QuoteToken():
if !s.inStr {
// start a new string
} else {
// end a string
}
}
return nil
}
func (s state) value() Value {
// TODO
return nil
}
// Parse parses jsonStr from JSON to a Value. Returns nil and an appropriate
// error if jsonStr was an invalid JSON string
func Parse(jsonStr string) (Value, error) {
tokensCh := make(chan parse.Token)
go parse.Tokenize(jsonStr, tokensCh)
st := newState()
for token := range tokensCh {
if err := st.addToken(token); err != nil {
return nil, err
}
}
return st.value(), nil
}