This repository has been archived by the owner on Aug 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
201 lines (191 loc) · 4.74 KB
/
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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package bracketconf
import (
"errors"
"fmt"
"io"
"text/scanner"
)
type tcls uint8
const (
tnilc tcls = iota
teof
tmeh
tobrack
tcbrack
toparen
tcparen
tosqbrack
tcsqbrack
tcomma
tsemicolon
)
func classify(s *scanner.Scanner) (tcls, string, scanner.Position) {
n := s.Scan()
tt := s.TokenText()
pos := s.Pos()
class := tmeh
if n == scanner.EOF {
class = teof
goto ret
}
switch tt {
case "":
class = tnilc
case "{":
class = tobrack
case "}":
class = tcbrack
case "(":
class = toparen
case ")":
class = tcparen
case "[":
class = tosqbrack
case "]":
class = tcsqbrack
case ",":
class = tcomma
case ";":
class = tsemicolon
}
ret:
return class, tt, pos
}
func mehToAST(s string, p scanner.Position) *ASTNode {
return &ASTNode{p, tval, s}
}
func parseParenArr(p scanner.Position, s *scanner.Scanner) *ASTNode {
vals := astArr{}
for class, val, pos := classify(s); class != tcparen; {
switch class {
case tnilc:
case tmeh:
vals = append(vals, *mehToAST(val, pos))
case teof:
panic(ConfErr{pos, io.ErrUnexpectedEOF})
case tcbrack:
panic(ConfErr{pos, errors.New("Closing bracket does not correspond to open bracket")})
case tcomma:
panic(ConfErr{pos, errors.New("Commas not allowed in shell-style arrays (consider using a JS-style array)")})
case tcsqbrack:
panic(ConfErr{pos, errors.New("Closing square bracket does not correspond to open square bracket")})
default:
panic(ConfErr{pos, fmt.Errorf("Token %q not allowed in shell-style array", val)})
}
class, val, pos = classify(s)
}
return &ASTNode{p, tarr, vals}
}
func parseSqBrackArr(p scanner.Position, s *scanner.Scanner) *ASTNode {
vals := astArr{}
var v *ASTNode
for class, val, pos := classify(s); class != tcsqbrack; {
switch class {
case tnilc:
case tmeh:
if v != nil {
panic(ConfErr{pos, errors.New("Multiple values not seperated by commas")})
}
v = mehToAST(val, pos)
case toparen:
if v != nil {
panic(ConfErr{pos, errors.New("Multiple values not seperated by commas")})
}
v = parseParenArr(pos, s)
case tosqbrack:
if v != nil {
panic(ConfErr{pos, errors.New("Multiple values not seperated by commas")})
}
v = parseSqBrackArr(pos, s)
case tcomma:
if v == nil {
panic(ConfErr{pos, errors.New("No value preceding comma")})
}
vals = append(vals, *v)
v = nil
case tcparen:
panic(ConfErr{pos, errors.New("Closing parenthese does not correspond to open parenthese")})
case teof:
panic(ConfErr{pos, io.ErrUnexpectedEOF})
default:
panic(ConfErr{pos, fmt.Errorf("Token %q not allowed in JS-style array", val)})
}
class, val, pos = classify(s)
}
switch {
case v != nil:
vals = append(vals, *v)
case len(vals) != 0:
panic(ConfErr{s.Pos(), errors.New("No value after comma")})
}
return &ASTNode{p, tarr, vals}
}
func parseDirective(name string, p scanner.Position, s *scanner.Scanner) *ASTNode {
if name == "" {
panic(ConfErr{p, errors.New("Empty directive")})
}
for _, c := range []rune(name) {
if c < 'a' || c > 'z' {
panic(ConfErr{p, errors.New("Directives must only contain lowercase letters")})
}
}
args := []ASTNode{}
for class, val, pos := classify(s); class != tsemicolon; {
switch class {
case tnilc:
case tmeh:
args = append(args, *mehToAST(val, pos))
case teof:
panic(ConfErr{pos, io.ErrUnexpectedEOF})
case tobrack:
args = append(args, *parseBracket(pos, s))
case tosqbrack:
args = append(args, *parseSqBrackArr(pos, s))
case toparen:
args = append(args, *parseParenArr(pos, s))
default:
panic(ConfErr{pos, fmt.Errorf("Token %q not allowed in directive", val)})
}
class, val, pos = classify(s)
}
return &ASTNode{p, tdir, astDir{name: name, args: args}}
}
func parseBracket(p scanner.Position, s *scanner.Scanner) *ASTNode {
dirs := astBrack{}
for class, val, pos := classify(s); class != tcbrack; {
switch class {
case tnilc:
case tmeh:
dirs = append(dirs, *parseDirective(val, pos, s))
default:
panic(ConfErr{pos, fmt.Errorf("Token %q not allowed in current context", val)})
}
class, val, pos = classify(s)
}
return &ASTNode{p, tbrack, dirs}
}
//ParseAST parses the data from an io.Reader into an AST and returns the root node
func ParseAST(r io.Reader, filnename string) (a *ASTNode, err error) {
defer func() {
e := recover()
if e != nil {
a = nil
err = e.(error)
}
}()
s := new(scanner.Scanner)
s.Filename = filnename
s.Init(r)
posi := s.Position
dirs := astBrack{}
for class, val, pos := classify(s); class != teof; class, val, pos = classify(s) {
switch class {
case tnilc:
case tmeh:
dirs = append(dirs, *parseDirective(val, pos, s))
default:
panic(ConfErr{pos, fmt.Errorf("Token %q not allowed in current context", val)})
}
}
return &ASTNode{posi, tbrack, dirs}, nil
}