-
Notifications
You must be signed in to change notification settings - Fork 63
/
expr.go
266 lines (235 loc) · 4.92 KB
/
expr.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// SPDX-License-Identifier: Apache-2.0
// Copyright 2020 syzkaller project authors. All rights reserved.
// Copyright 2022 Unikraft GmbH. All rights reserved.
package kconfig
import (
"encoding/json"
"fmt"
"strings"
)
// expr represents an arbitrary kconfig expression used in "depends on",
// "visible if", "if", etc. Currently we can only extract dependent symbols from
// expressions.
type expr interface {
String() string
collectDeps(map[string]bool)
json.Marshaler
}
type exprShell struct {
cmd string
}
func (ex *exprShell) String() string {
return "$" + ex.cmd
}
func (ex *exprShell) MarshalJSON() ([]byte, error) {
ret := ex.String()
if strings.HasPrefix(ret, "\"") && strings.HasSuffix(ret, "\"") {
ret = ret[1 : len(ret)-1]
}
ret = strings.ReplaceAll(ret, "\"", "\\\"")
return []byte("\"" + ret + "\""), nil
}
func (ex *exprShell) collectDeps(deps map[string]bool) {
}
type exprNot struct {
ex expr
}
func (ex *exprNot) String() string {
return fmt.Sprintf("!(%v)", ex.ex)
}
func (ex *exprNot) MarshalJSON() ([]byte, error) {
ret := ex.String()
if strings.HasPrefix(ret, "\"") && strings.HasSuffix(ret, "\"") {
ret = ret[1 : len(ret)-1]
}
ret = strings.ReplaceAll(ret, "\"", "\\\"")
return []byte("\"" + ret + "\""), nil
}
func (ex *exprNot) collectDeps(deps map[string]bool) {
}
type exprIdent struct {
name string
}
func (ex *exprIdent) String() string {
return ex.name
}
func (ex *exprIdent) MarshalJSON() ([]byte, error) {
ret := ex.String()
if strings.HasPrefix(ret, "\"") && strings.HasSuffix(ret, "\"") {
ret = ret[1 : len(ret)-1]
}
ret = strings.ReplaceAll(ret, "\"", "\\\"")
return []byte("\"" + ret + "\""), nil
}
func (ex *exprIdent) collectDeps(deps map[string]bool) {
deps[ex.name] = true
}
type exprString struct {
val string
}
func (ex *exprString) String() string {
return fmt.Sprintf("%q", ex.val)
}
func (ex *exprString) MarshalJSON() ([]byte, error) {
ret := ex.String()
if strings.HasPrefix(ret, "\"") && strings.HasSuffix(ret, "\"") {
ret = ret[1 : len(ret)-1]
}
ret = strings.ReplaceAll(ret, "\"", "\\\"")
return []byte("\"" + ret + "\""), nil
}
func (ex *exprString) collectDeps(deps map[string]bool) {
}
type exprBin struct {
op binOp
lex expr
rex expr
}
type binOp int
const (
opNop binOp = iota
opAnd
opOr
opEq
opNe
opLt
opLe
opGt
opGe
)
func (op binOp) String() string {
switch op {
case opAnd:
return "&&"
case opOr:
return "||"
case opEq:
return "="
case opNe:
return "!="
case opLt:
return "<"
case opLe:
return "<="
case opGt:
return ">"
case opGe:
return ">="
default:
return fmt.Sprintf("???(%v)", int(op))
}
}
func (ex *exprBin) String() string {
return fmt.Sprintf("(%v %v %v)", ex.lex, ex.op, ex.rex)
}
func (ex *exprBin) MarshalJSON() ([]byte, error) {
ret := ex.String()
if strings.HasPrefix(ret, "\"") && strings.HasSuffix(ret, "\"") {
ret = ret[1 : len(ret)-1]
}
ret = strings.ReplaceAll(ret, "\"", "\\\"")
return []byte("\"" + ret + "\""), nil
}
func (ex *exprBin) collectDeps(deps map[string]bool) {
ex.lex.collectDeps(deps)
ex.rex.collectDeps(deps)
}
func exprAnd(lex, rex expr) expr {
if lex == nil {
return rex
}
if rex == nil {
return lex
}
return &exprBin{
op: opAnd,
lex: lex,
rex: rex,
}
}
// Recursive-descent parsing with strict precedence levels.
//
// See kconfig docs for reference:
// https://www.kernel.org/doc/html/latest/kbuild/kconfig-language.html#menu-dependencies
//
// The doc claims that all operators have different precedence levels, e.g. '<'
// has higher precedence than '>' rather than being left-associative with the
// same precedence. This is somewhat strange semantics and here it is
// implemented as simply being left-associative. For now it does not matter
// since we do not evaluate expressions.
func (p *parser) parseExpr() expr {
ex := p.parseExprAnd()
for p.TryConsume("||") {
ex = &exprBin{
op: opOr,
lex: ex,
rex: p.parseExprAnd(),
}
}
return ex
}
func (p *parser) parseExprAnd() expr {
ex := p.parseExprCmp()
for p.TryConsume("&&") {
ex = &exprBin{
op: opAnd,
lex: ex,
rex: p.parseExprCmp(),
}
}
return ex
}
func (p *parser) parseExprCmp() expr {
ex := p.parseExprTerm()
for {
op := opNop
switch {
case p.TryConsume("="):
op = opEq
case p.TryConsume("!="):
op = opNe
case p.TryConsume("<="):
op = opLe
case p.TryConsume(">="):
op = opGe
case p.TryConsume("<"):
op = opLt
case p.TryConsume(">"):
op = opGt
}
if op == opNop {
break
}
ex = &exprBin{
op: op,
lex: ex,
rex: p.parseExprTerm(),
}
}
return ex
}
func (p *parser) parseExprTerm() expr {
if p.TryConsume("$") {
return &exprShell{
cmd: p.Shell(),
}
}
if str, ok := p.TryQuotedString(); ok {
return &exprString{
val: str,
}
}
if p.TryConsume("!") {
return &exprNot{
ex: p.parseExprTerm(),
}
}
if p.TryConsume("(") {
ex := p.parseExpr()
p.MustConsume(")")
return ex
}
return &exprIdent{
name: p.Ident(),
}
}