-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseList.go
154 lines (138 loc) · 4.79 KB
/
ParseList.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
// =================================================================
//
// 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"
)
// ParseList parses a list of values.
func ParseList(in string) ([]Node, error) {
nodes := make([]Node, 0)
singlequotes := 0
doublequotes := 0
backticks := 0
leftparentheses := 0
rightparentheses := 0
leftcurlybrackets := 0
rightcurlybrackets := 0
leftsquarebrackets := 0
rightsquarebrackets := 0
in = strings.TrimSpace(in)
s := ""
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 == ',') {
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] == ',' {
s = strings.TrimSpace(s)
if syntax.IsQuoted(s) {
nodes = append(nodes, &Literal{Value: UnescapeString(s[1 : len(s)-1])})
} else if syntax.IsAttribute(s) {
attr, _, err := ParseAttribute(s, "")
if err != nil {
return nodes, errors.Wrap(err, "error parsing attribute in list "+s)
}
nodes = append(nodes, attr)
} else if syntax.IsVariable(s) {
variable, _, err := ParseVariable(s, "")
if err != nil {
return nodes, errors.Wrap(err, "error parsing variable in list "+s)
}
nodes = append(nodes, variable)
} else if syntax.IsArray(s) {
arr, _, err := ParseArray(strings.TrimSpace(s[1:len(s)-1]), "")
if err != nil {
return nodes, errors.Wrap(err, "error parsing array in list "+s)
}
nodes = append(nodes, arr)
} else if syntax.IsSetOrDictionary(s) {
setOrDictionary, _, err := ParseSetOrDictionary(strings.TrimSpace(s[1:len(s)-1]), "")
if err != nil {
return nodes, errors.Wrap(err, "error parsing set in list "+s)
}
nodes = append(nodes, setOrDictionary)
} else if syntax.IsSub(s) {
sub, _, err := ParseSub(strings.TrimSpace(s[1:len(s)-1]), "")
if err != nil {
return nodes, errors.Wrap(err, "error parsing sub in list "+s)
}
nodes = append(nodes, sub)
} else if syntax.IsFunction(s) {
f, _, err := ParseFunction(s, "")
if err != nil {
return nodes, errors.Wrap(err, "error parsing function in list "+s)
}
nodes = append(nodes, f)
} else {
nodes = append(nodes, &Literal{Value: TryConvertString(s)})
}
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
}