-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
116 lines (102 loc) · 2.31 KB
/
parser.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
package condition
import (
"encoding/json"
"fmt"
"io"
"strings"
)
type TokenType int
const (
TokenTypeBracketOpen TokenType = iota
TokenTypeBracketClose
TokenTypeBraceOpen
TokenTypeBraceClose
TokenTypeLiteral
)
type LiteralType int
const (
LiteralTypeNumber LiteralType = iota
LiteralTypeString
LiteralTypeBool
LiteralTypeNull
)
type Token struct {
Type TokenType
LiteralType LiteralType
Value interface{}
}
func (t Token) String() string {
switch t.Type {
case TokenTypeBraceOpen:
return "BRACE_OPEN"
case TokenTypeBraceClose:
return "BRACE_CLOSE"
case TokenTypeBracketOpen:
return "BRACKET_OPEN"
case TokenTypeBracketClose:
return "BRACKET_CLOSE"
case TokenTypeLiteral:
switch t.LiteralType {
case LiteralTypeBool:
return fmt.Sprintf("LITERAL<bool::%v>", t.Value)
case LiteralTypeString:
return fmt.Sprintf("LITERAL<string::%v>", t.Value)
case LiteralTypeNumber:
return fmt.Sprintf("LITERAL<number::%v>", t.Value)
case LiteralTypeNull:
return "LITERAL<null>"
}
return fmt.Sprintf("LITERAL<unknown::%v>", t.Value)
}
return "UNKNOWN"
}
func tokenize(value string) ([]Token, error) {
decoder := json.NewDecoder(strings.NewReader(value))
tokens := []Token{}
for {
jsonToken, err := decoder.Token()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
switch v := jsonToken.(type) {
case json.Delim:
switch v {
case json.Delim('['):
tokens = append(tokens, Token{Type: TokenTypeBracketOpen})
case json.Delim(']'):
tokens = append(tokens, Token{Type: TokenTypeBracketClose})
case json.Delim('{'):
tokens = append(tokens, Token{Type: TokenTypeBraceOpen})
case json.Delim('}'):
tokens = append(tokens, Token{Type: TokenTypeBraceClose})
}
case bool:
tokens = append(tokens, Token{
Type: TokenTypeLiteral,
Value: v,
LiteralType: LiteralTypeBool,
})
case string:
tokens = append(tokens, Token{
Type: TokenTypeLiteral,
Value: v,
LiteralType: LiteralTypeString,
})
case float64:
tokens = append(tokens, Token{
Type: TokenTypeLiteral,
Value: v,
LiteralType: LiteralTypeNumber,
})
case nil:
tokens = append(tokens, Token{
Type: TokenTypeLiteral,
LiteralType: LiteralTypeNull,
})
}
}
return tokens, nil
}