-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseKeyValue.go
173 lines (155 loc) · 5.22 KB
/
ParseKeyValue.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// =================================================================
//
// Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
// Released as open source under the MIT License. See LICENSE file.
//
// =================================================================
package dfl
import (
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/spatialcurrent/go-dfl/pkg/dfl/syntax"
)
func parseKeyOrValueString(s string) (Node, error) {
if syntax.IsQuoted(s) {
return &Literal{Value: UnescapeString(s[1 : len(s)-1])}, nil
} else if syntax.IsAttribute(s) {
attr, _, err := ParseAttribute(s, "")
if err != nil {
return attr, errors.Wrap(err, "error parsing attribute in list "+s)
}
return attr, nil
} else if syntax.IsVariable(s) {
variable, _, err := ParseVariable(s, "")
if err != nil {
return variable, errors.Wrap(err, "error parsing variable in list "+s)
}
return variable, nil
} else if syntax.IsArray(s) {
arr, _, err := ParseArray(strings.TrimSpace(s[1:len(s)-1]), "")
if err != nil {
return arr, errors.Wrap(err, "error parsing array in list "+s)
}
return arr, nil
} else if syntax.IsSetOrDictionary(s) {
setOrDictionary, _, err := ParseSetOrDictionary(strings.TrimSpace(s[1:len(s)-1]), "")
if err != nil {
return setOrDictionary, errors.Wrap(err, "error parsing set in list "+s)
}
return setOrDictionary, nil
} else if syntax.IsSub(s) {
sub, _, err := ParseSub(strings.TrimSpace(s[1:len(s)-1]), "")
if err != nil {
return sub, errors.Wrap(err, "error parsing sub in list "+s)
}
return sub, nil
} else if syntax.IsFunction(s) {
f, _, err := ParseFunction(s, "")
if err != nil {
return f, errors.Wrap(err, "error parsing function in list "+s)
}
return f, nil
}
return &Literal{Value: TryConvertString(s)}, nil
}
// ParseKeyValue parses a sequence of key value pairs
func ParseKeyValue(in string) (map[Node]Node, error) {
nodes := map[Node]Node{}
singlequotes := 0
doublequotes := 0
backticks := 0
leftparentheses := 0
rightparentheses := 0
leftcurlybrackets := 0
rightcurlybrackets := 0
leftsquarebrackets := 0
rightsquarebrackets := 0
in = strings.TrimSpace(in)
s := ""
key := ""
for i, c := range in {
// If you're note in a quoted string, then you can start one
// If you're in a quoted string, only exit with matching quote.
if singlequotes == 0 && doublequotes == 0 && backticks == 0 {
switch c {
case '\'':
singlequotes += 1
case '"':
doublequotes += 1
case '`':
backticks += 1
case '(':
leftparentheses += 1
case '[':
leftsquarebrackets += 1
case '{':
leftcurlybrackets += 1
case ')':
rightparentheses += 1
case ']':
rightsquarebrackets += 1
case '}':
rightcurlybrackets += 1
}
} else if singlequotes == 1 && c == '\'' {
singlequotes -= 1
} else if doublequotes == 1 && c == '"' {
doublequotes -= 1
} else if backticks == 1 && c == '`' {
backticks -= 1
}
// If not (within string/array/set/sub and c == ,)
if !(singlequotes == 0 &&
doublequotes == 0 &&
backticks == 0 &&
leftparentheses == rightparentheses &&
leftcurlybrackets == rightcurlybrackets &&
leftsquarebrackets == rightsquarebrackets &&
(c == ',' || c == ':')) {
s += string(c)
}
// If sub/array/set and string are closed.
if singlequotes == 0 &&
doublequotes == 0 &&
backticks == 0 &&
leftparentheses == rightparentheses &&
leftsquarebrackets == rightsquarebrackets &&
leftcurlybrackets == rightcurlybrackets {
// If end of input or argument
if i+1 == len(in) || in[i+1] == ',' || in[i+1] == ':' {
if i+1 != len(in) && in[i+1] == ':' {
key = strings.TrimSpace(s)
} else if len(key) > 0 {
keyNode, err := parseKeyOrValueString(key)
if err != nil {
return nodes, errors.Wrap(err, "error parsing key for key-pair")
}
valueNode, err := parseKeyOrValueString(strings.TrimSpace(s))
if err != nil {
return nodes, errors.Wrap(err, "error parsing value for key-pair")
}
nodes[keyNode] = valueNode
key = ""
} else {
return nodes, errors.New("missing key when parsing key-value pairs")
}
s = ""
}
}
}
if leftparentheses > rightparentheses {
return nodes, errors.New("too few closing parentheses " + fmt.Sprint(leftparentheses) + " | " + fmt.Sprint(rightparentheses))
} else if leftparentheses < rightparentheses {
return nodes, errors.New("too many closing parentheses " + fmt.Sprint(leftparentheses) + " | " + fmt.Sprint(rightparentheses))
} else if leftcurlybrackets > rightcurlybrackets {
return nodes, errors.New("too few closing curly brackets " + fmt.Sprint(leftcurlybrackets) + " | " + fmt.Sprint(rightcurlybrackets))
} else if leftcurlybrackets < rightcurlybrackets {
return nodes, errors.New("too many closing curly brackets " + fmt.Sprint(leftcurlybrackets) + " | " + fmt.Sprint(rightparentheses))
} else if leftsquarebrackets > rightsquarebrackets {
return nodes, errors.New("too few closing square brackets " + fmt.Sprint(leftsquarebrackets) + " | " + fmt.Sprint(rightsquarebrackets))
} else if leftsquarebrackets < rightsquarebrackets {
return nodes, errors.New("too many closing square brackets " + fmt.Sprint(leftsquarebrackets) + " | " + fmt.Sprint(rightsquarebrackets))
}
return nodes, nil
}