/
test_classic.go
196 lines (183 loc) · 3.6 KB
/
test_classic.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
// Copyright (c) 2017, Daniel Martí <mvdan@mvdan.cc>
// See LICENSE for licensing information
package interp
import (
"fmt"
"mvdan.cc/sh/syntax"
)
const illegalTok = 0
type testParser struct {
eof bool
val string
rem []string
err func(err error)
}
func (p *testParser) errf(format string, a ...interface{}) {
p.err(fmt.Errorf(format, a...))
}
func (p *testParser) next() {
if p.eof || len(p.rem) == 0 {
p.eof = true
p.val = ""
return
}
p.val = p.rem[0]
p.rem = p.rem[1:]
}
func (p *testParser) followWord(fval string) *syntax.Word {
if p.eof {
p.errf("%s must be followed by a word", fval)
}
w := &syntax.Word{Parts: []syntax.WordPart{
&syntax.Lit{Value: p.val},
}}
p.next()
return w
}
func (p *testParser) classicTest(fval string, pastAndOr bool) syntax.TestExpr {
var left syntax.TestExpr
if pastAndOr {
left = p.testExprBase(fval)
} else {
left = p.classicTest(fval, true)
}
if left == nil || p.eof {
return left
}
opStr := p.val
op := testBinaryOp(p.val)
if op == illegalTok {
p.errf("not a valid test operator: %s", p.val)
}
b := &syntax.BinaryTest{
Op: op,
X: left,
}
p.next()
switch b.Op {
case syntax.AndTest, syntax.OrTest:
if b.Y = p.classicTest(opStr, false); b.Y == nil {
p.errf("%s must be followed by an expression", opStr)
}
default:
b.Y = p.followWord(opStr)
}
return b
}
func (p *testParser) testExprBase(fval string) syntax.TestExpr {
if p.eof {
return nil
}
op := testUnaryOp(p.val)
switch op {
case syntax.TsNot:
u := &syntax.UnaryTest{Op: op}
p.next()
u.X = p.classicTest(op.String(), false)
return u
case illegalTok:
return p.followWord(fval)
default:
u := &syntax.UnaryTest{Op: op}
p.next()
if p.eof {
// make [ -e ] fall back to [ -n -e ], i.e. use
// the operator as an argument
return &syntax.Word{Parts: []syntax.WordPart{
&syntax.Lit{Value: op.String()},
}}
}
u.X = p.followWord(op.String())
return u
}
}
// testUnaryOp is an exact copy of syntax's.
func testUnaryOp(val string) syntax.UnTestOperator {
switch val {
case "!":
return syntax.TsNot
case "-e", "-a":
return syntax.TsExists
case "-f":
return syntax.TsRegFile
case "-d":
return syntax.TsDirect
case "-c":
return syntax.TsCharSp
case "-b":
return syntax.TsBlckSp
case "-p":
return syntax.TsNmPipe
case "-S":
return syntax.TsSocket
case "-L", "-h":
return syntax.TsSmbLink
case "-k":
return syntax.TsSticky
case "-g":
return syntax.TsGIDSet
case "-u":
return syntax.TsUIDSet
case "-G":
return syntax.TsGrpOwn
case "-O":
return syntax.TsUsrOwn
case "-N":
return syntax.TsModif
case "-r":
return syntax.TsRead
case "-w":
return syntax.TsWrite
case "-x":
return syntax.TsExec
case "-s":
return syntax.TsNoEmpty
case "-t":
return syntax.TsFdTerm
case "-z":
return syntax.TsEmpStr
case "-n":
return syntax.TsNempStr
case "-o":
return syntax.TsOptSet
case "-v":
return syntax.TsVarSet
case "-R":
return syntax.TsRefVar
default:
return illegalTok
}
}
// testBinaryOp is like syntax's, but with -a and -o, and without =~.
func testBinaryOp(val string) syntax.BinTestOperator {
switch val {
case "-a":
return syntax.AndTest
case "-o":
return syntax.OrTest
case "==", "=":
return syntax.TsMatch
case "!=":
return syntax.TsNoMatch
case "-nt":
return syntax.TsNewer
case "-ot":
return syntax.TsOlder
case "-ef":
return syntax.TsDevIno
case "-eq":
return syntax.TsEql
case "-ne":
return syntax.TsNeq
case "-le":
return syntax.TsLeq
case "-ge":
return syntax.TsGeq
case "-lt":
return syntax.TsLss
case "-gt":
return syntax.TsGtr
default:
return illegalTok
}
}