Repository navigation
Expand file tree
/
Copy pathcalc.go
More file actions
347 lines (299 loc) · 7.38 KB
/
Copy pathcalc.go
File metadata and controls
347 lines (299 loc) · 7.38 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package csscolorparser
import (
"errors"
"math"
"strconv"
"strings"
)
type tokenKind uint8
const (
tokEOF tokenKind = iota
tokNum
tokIdent
tokAdd
tokSub
tokMul
tokDiv
tokLParen
tokRParen
tokUnknown
)
// token is a lexical token. Only the field matching kind is meaningful.
type token struct {
kind tokenKind
num float64 // tokNum
ident string // tokIdent
}
type lexer struct {
src string
pos int
}
func isSpace(b byte) bool {
return b == ' ' || b == '\t' || b == '\n' || b == '\f' || b == '\r'
}
func isDigit(b byte) bool { return b >= '0' && b <= '9' }
func isAlpha(b byte) bool { return (b >= 'a' && b <= 'z') }
// scan returns the next token. At end of input it returns tokEOF, and keeps
// returning it on every later call.
func (l *lexer) scan() token {
for l.pos < len(l.src) && isSpace(l.src[l.pos]) {
l.pos++
}
if l.pos >= len(l.src) {
return token{kind: tokEOF}
}
b := l.src[l.pos]
l.pos++
switch b {
case '+':
return token{kind: tokAdd}
case '-':
return token{kind: tokSub}
case '*':
return token{kind: tokMul}
case '/':
return token{kind: tokDiv}
case '(':
return token{kind: tokLParen}
case ')':
return token{kind: tokRParen}
}
if isDigit(b) || b == '.' {
return l.number(b)
}
if isAlpha(b) {
return l.ident()
}
return token{kind: tokUnknown}
}
// number is called after the first byte (a digit or '.') has been consumed.
func (l *lexer) number(first byte) token {
start := l.pos - 1
dotSeen := first == '.'
for l.pos < len(l.src) {
c := l.src[l.pos]
if isDigit(c) {
l.pos++
} else if c == '.' && !dotSeen {
dotSeen = true
l.pos++
} else {
break
}
}
// A digit string too long for float64 comes back as ±Inf with ErrRange;
// that's accepted here, and callers reject non-finite results.
val, err := strconv.ParseFloat(l.src[start:l.pos], 64)
if err != nil && !errors.Is(err, strconv.ErrRange) {
return token{kind: tokUnknown} // Triggered by isolated dots (e.g., ".")
}
return token{kind: tokNum, num: val}
}
// ident is called after the first byte (an ASCII letter) has been consumed.
// Identifiers consist solely of ASCII letters: no digits, no underscores.
func (l *lexer) ident() token {
start := l.pos - 1
for l.pos < len(l.src) && isAlpha(l.src[l.pos]) {
l.pos++
}
return token{kind: tokIdent, ident: l.src[start:l.pos]}
}
//---
const maxDepth = 64
// prefixBP is the binding power of unary + and -, which binds tighter than
// any binary operator.
const prefixBP = 5
// VarLookup resolves a variable name. ok is false if the name is undefined.
type varLookup func(name string) (value float64, ok bool)
// Eval evaluates an arithmetic expression made of numbers, variables,
// parentheses, unary + and -, and the binary operators + - * /.
//
// Variable names consist only of ASCII letters and are resolved with vars;
// a nil vars means no variables are defined.
//
// ok is false on a syntax error, an undefined variable, nesting deeper than
// the internal limit, or trailing input after a complete expression.
// Division follows IEEE 754, so the result may be ±Inf or NaN.
func eval(src string, vars varLookup) (value float64, ok bool) {
p := parser{lex: lexer{src: src}, vars: vars}
p.next()
v, ok := p.expr(0)
if !ok || p.tok.kind != tokEOF {
return 0, false
}
return v, true
}
type parser struct {
lex lexer
tok token // current lookahead; kind is tokEOF at end of input
vars varLookup
depth int
}
func (p *parser) next() { p.tok = p.lex.scan() }
// infixBP returns the left and right binding powers of a binary operator.
// ok is false if kind is not a binary operator.
func infixBP(kind tokenKind) (left, right int, ok bool) {
switch kind {
case tokAdd, tokSub:
return 1, 2, true
case tokMul, tokDiv:
return 3, 4, true
}
return 0, 0, false
}
// expr parses and evaluates an expression whose operators all bind at least
// as tightly as minBP.
func (p *parser) expr(minBP int) (float64, bool) {
if p.depth > maxDepth {
return 0, false
}
p.depth++
defer func() { p.depth-- }()
left, ok := p.operand()
if !ok {
return 0, false
}
for {
lbp, rbp, isOp := infixBP(p.tok.kind)
if !isOp || lbp < minBP {
// EOF, a non-operator token, or an operator that binds too
// loosely for this level. The caller decides what's valid.
return left, true
}
op := p.tok.kind
p.next()
right, ok := p.expr(rbp)
if !ok {
return 0, false
}
switch op {
case tokAdd:
left += right
case tokSub:
left -= right
case tokMul:
left *= right
case tokDiv:
left /= right
}
}
}
// operand parses a number, variable, parenthesized expression, or a
// unary + / - applied to an operand.
func (p *parser) operand() (float64, bool) {
switch p.tok.kind {
case tokNum:
v := p.tok.num
p.next()
return v, true
case tokIdent:
v, found := p.vars(p.tok.ident)
if !found {
return 0, false
}
p.next()
return v, true
case tokAdd, tokSub:
negate := p.tok.kind == tokSub
p.next()
v, ok := p.expr(prefixBP)
if !ok {
return 0, false
}
if negate {
v = -v
}
return v, true
case tokLParen:
p.next()
v, ok := p.expr(0)
if !ok || p.tok.kind != tokRParen {
return 0, false
}
p.next()
return v, true
}
return 0, false
}
//---
// Variable is a named constant usable as a bare keyword or inside calc(...).
type variable struct {
Name string
Value float64
}
// parseComponent parses one component, which is one of:
//
// - "none": zero, not a percentage
// - a finite number, or a variable name
// - a percentage like "50%", stored as 0.5 with IsPercent set
// - calc(expr), where expr is evaluated by Eval and must come out finite
//
// s and the variable names are expected to be lowercase; matching is exact.
// returns (t, ok, pct)
func parseComponent(s string, variables [4]variable) (float64, bool, bool) {
lookup := func(name string) (float64, bool) {
if v, ok := parseNumber(name); ok {
if !isFinite(v) {
return 0, false
}
return v, true
}
for _, v := range variables {
if name == v.Name {
return v.Value, true
}
}
return 0, false
}
// none
if s == "none" {
return 0, true, false
}
// bare number or keyword
if v, ok := lookup(s); ok {
return v, true, false
}
// percentage
if rest, ok := strings.CutSuffix(s, "%"); ok {
v, ok := parseNumber(rest)
if !ok || !isFinite(v) {
return 0, false, false
}
return v / 100, true, true
}
// calc(...)
if rest, ok := strings.CutPrefix(s, "calc("); ok {
if inner, ok := strings.CutSuffix(rest, ")"); ok {
if v, ok := eval(inner, lookup); ok && isFinite(v) {
return v, true, false
}
}
}
// invalid value
return 0, false, false
}
// parseNumber parses a decimal floating-point number. strconv.ParseFloat is
// more permissive than wanted here (it accepts digit-separating underscores
// and hex floats such as "0x1p3"), so those forms are rejected up front.
// An out-of-range value parses successfully as ±Inf (strconv.ErrRange)
// instead of failing; callers reject non-finite values.
func parseNumber(s string) (float64, bool) {
if strings.Contains(s, "_") {
return 0, false
}
t := s
if len(t) > 0 && (t[0] == '+' || t[0] == '-') {
t = t[1:]
}
if len(t) >= 2 && t[0] == '0' && t[1] == 'x' { // input is lowercase
return 0, false
}
v, err := strconv.ParseFloat(s, 64)
if err != nil && !errors.Is(err, strconv.ErrRange) {
return 0, false
}
return v, true
}
func isFinite(v float64) bool {
return !math.IsNaN(v) && !math.IsInf(v, 0)
}