-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
101 lines (85 loc) · 1.93 KB
/
parser.go
File metadata and controls
101 lines (85 loc) · 1.93 KB
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
package main
func parse(tokens []interface{}) (interface{}, []interface{}) {
token := tokens[0]
switch token.(type) {
case string:
if token == JSON_LEFTBRACKET {
return parse_array(tokens[1:])
} else if token == JSON_LEFTBRACE {
return parse_object(tokens[1:])
} else {
return token, tokens[1:]
}
default:
return token, tokens[1:]
}
}
func parse_array(tokens []interface{}) ([]interface{}, []interface{}) {
var json_array []interface{}
token := tokens[0]
switch token.(type) {
case string:
if token == JSON_RIGHTBRACKET {
return json_array, tokens[1:]
}
}
for {
var json interface{}
json, tokens = parse(tokens)
json_array = append(json_array, json)
token = tokens[0]
switch token.(type) {
case string:
if token == JSON_RIGHTBRACKET {
return json_array, tokens[1:]
} else if token != JSON_COMMA {
panic("Expected comma after object in array")
} else {
tokens = tokens[1:]
}
default:
tokens = tokens[1:]
}
}
}
func parse_object(tokens []interface{}) (map[string]interface{}, []interface{}) {
var json_object = make(map[string]interface{})
token := tokens[0]
switch token.(type) {
case string:
if token == JSON_RIGHTBRACE {
return json_object, tokens[1:]
}
}
for {
json_key := tokens[0]
switch json_key.(type) {
case string:
tokens = tokens[1:]
default:
panic("Expected string key")
}
switch tokens[0].(type) {
case string:
if tokens[0] != JSON_COLON {
panic("Expected colon after key in object")
}
}
var json_value interface{}
json_value, tokens = parse(tokens[1:])
if key, ok := json_key.(string); ok {
json_object[key] = json_value
}
token = tokens[0]
switch token.(type) {
case string:
if token == JSON_RIGHTBRACE {
return json_object, tokens[1:]
} else if token != JSON_COMMA {
panic("Expected comma after pair in object")
}
}
tokens = tokens[1:]
}
// panic("Expected end-of-object brace")
}