Skip to content

Commit 22312a9

Browse files
committed
internal/*: add support for parenthesized math expressions
1 parent 3279aea commit 22312a9

File tree

6 files changed

+150
-60
lines changed

6 files changed

+150
-60
lines changed

internal/ast/value.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,33 @@ type MathExpression struct {
8989
Right Value
9090
}
9191

92+
// MathParenthesizedExpression is a parenthesized math expression.
93+
type MathParenthesizedExpression struct {
94+
Span
95+
96+
Value Value
97+
}
98+
9299
// Comma is a single comma. Some declarations require commas,
93100
// e.g. font-family fallbacks or transitions.
94101
type Comma struct {
95102
Span
96103
}
97104

98-
func (String) isValue() {}
99-
func (Dimension) isValue() {}
100-
func (Function) isValue() {}
101-
func (MathExpression) isValue() {}
102-
func (Comma) isValue() {}
103-
func (Identifier) isValue() {}
104-
func (HexColor) isValue() {}
105+
func (String) isValue() {}
106+
func (Dimension) isValue() {}
107+
func (Function) isValue() {}
108+
func (MathExpression) isValue() {}
109+
func (MathParenthesizedExpression) isValue() {}
110+
func (Comma) isValue() {}
111+
func (Identifier) isValue() {}
112+
func (HexColor) isValue() {}
105113

106114
var _ Value = String{}
107115
var _ Value = Dimension{}
108116
var _ Value = Function{}
109117
var _ Value = MathExpression{}
118+
var _ Value = MathParenthesizedExpression{}
110119
var _ Value = Comma{}
111120
var _ Value = Identifier{}
112121
var _ Value = HexColor{}

internal/ast/walk.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ func Walk(start Node, visit func(n Node)) {
7474
Walk(s.LeftValue, visit)
7575
Walk(s.RightValue, visit)
7676

77+
case *MathParenthesizedExpression:
78+
Walk(s.Value, visit)
79+
7780
case *MathExpression:
7881
Walk(s.Left, visit)
7982
Walk(s.Right, visit)

internal/parser/parser.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,13 @@ func (p *parser) parseMathSum() ast.Value {
248248
}
249249

250250
func (p *parser) parseMathProduct() ast.Value {
251-
left := p.parseValue()
251+
left := p.parseMathParenthesizedExpression()
252252

253253
for p.lexer.Current == lexer.Delim && (p.lexer.CurrentString == "*" || p.lexer.CurrentString == "/") {
254254
op := p.lexer.CurrentString
255255
p.lexer.Expect(lexer.Delim)
256256

257-
right := p.parseValue()
257+
right := p.parseMathParenthesizedExpression()
258258

259259
span := left.Location()
260260
span.End = right.Location().End
@@ -270,6 +270,22 @@ func (p *parser) parseMathProduct() ast.Value {
270270
return left
271271
}
272272

273+
func (p *parser) parseMathParenthesizedExpression() ast.Value {
274+
if p.lexer.Current != lexer.LParen {
275+
return p.parseValue()
276+
}
277+
278+
loc := p.lexer.TokenSpan()
279+
p.lexer.Expect(lexer.LParen)
280+
expr := &ast.MathParenthesizedExpression{
281+
Span: loc,
282+
Value: p.parseMathSum(),
283+
}
284+
expr.End = p.lexer.TokenSpan().End
285+
p.lexer.Expect(lexer.RParen)
286+
return expr
287+
}
288+
273289
// parseValue parses a possible ast value at the current position. Callers
274290
// can set allowMathOperators if the enclosing context allows math expressions.
275291
// See: https://www.w3.org/TR/css-values-4/#math-function.

internal/parser/span_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func TestSpans(t *testing.T) {
4545
.big#THINGS + div, span::after, a:href, a:visited, not.a:problem + a[link="thing"], a[other] {
4646
color: purple;
4747
background-color: rgba(calc(0 + 1), 2, 3);
48+
height: calc(2px / 2 + ( 1rem + 3rem + 6rem) * 8);
4849
width: calc(2px / 2 + 1rem * 8)
4950
}
5051

0 commit comments

Comments
 (0)