/
css_ast.go
201 lines (165 loc) · 5.03 KB
/
css_ast.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 css_ast
import (
"github.com/evanw/esbuild/internal/ast"
"github.com/evanw/esbuild/internal/css_lexer"
"github.com/evanw/esbuild/internal/logger"
)
// CSS syntax comes in two layers: a minimal syntax that generally accepts
// anything that looks vaguely like CSS, and a large set of built-in rules
// (the things browsers actually interpret). That way CSS parsers can read
// unknown rules and skip over them without having to stop due to errors.
//
// This AST format is mostly just the minimal syntax. It parses unknown rules
// into a tree with enough information that it can write them back out again.
// There are some additional layers of syntax including selectors and @-rules
// which allow for better pretty-printing and minification.
//
// Most of the AST just references ranges of the original file by keeping the
// original "Token" values around from the lexer. This is a memory-efficient
// representation that helps provide good parsing and printing performance.
type AST struct {
ImportRecords []ast.ImportRecord
Rules []R
}
// We create a lot of tokens, so make sure this layout is memory-efficient.
// The layout here isn't optimal because it biases for convenience (e.g.
// "string" could be shorter) but at least the ordering of fields was
// deliberately chosen to minimize size.
type Token struct {
// This is the raw contents of the token most of the time. However, it
// contains the decoded string contents for "TString" tokens.
Text string // 16 bytes
// Contains the child tokens for component values that are simple blocks.
// These are either "(", "{", "[", or function tokens. The closing token is
// implicit and is not stored.
Children *[]Token // 8 bytes
// URL tokens have an associated import record at the top-level of the AST.
// This index points to that import record.
ImportRecordIndex uint32 // 4 bytes
// The division between the number and the unit for "TDimension" tokens.
UnitOffset uint16 // 2 bytes
// This will never be "TWhitespace" because whitespace isn't stored as a
// token directly. Instead it is stored in "HasWhitespaceAfter" on the
// previous token.
Kind css_lexer.T // 1 byte
// This is generally true if there was a "TWhitespace" token before this
// token. This isn't strictly true in some cases because sometimes this flag
// is changed to make the generated code look better (e.g. around commas).
HasWhitespaceAfter bool // 1 byte
}
func (t Token) PercentValue() string {
return t.Text[:len(t.Text)-1]
}
func (t Token) DimensionValue() string {
return t.Text[:t.UnitOffset]
}
func (t Token) DimensionUnit() string {
return t.Text[t.UnitOffset:]
}
// This interface is never called. Its purpose is to encode a variant type in
// Go's type system.
type R interface {
isRule()
}
type RAtCharset struct {
Encoding string
}
type RAtNamespace struct {
Prefix string
Path string
}
type RAtImport struct {
ImportRecordIndex uint32
}
type RAtKeyframes struct {
AtToken string
Name string
Blocks []KeyframeBlock
}
type KeyframeBlock struct {
Selectors []string
Rules []R
}
type RKnownAt struct {
AtToken string
Prelude []Token
Rules []R
}
type RUnknownAt struct {
AtToken string
Prelude []Token
Block []Token
}
type RSelector struct {
Selectors []ComplexSelector
Rules []R
}
type RQualified struct {
Prelude []Token
Rules []R
}
type RDeclaration struct {
KeyText string
Value []Token
KeyRange logger.Range
Key D // Compare using this instead of "Key" for speed
Important bool
}
type RBadDeclaration struct {
Tokens []Token
}
func (*RAtCharset) isRule() {}
func (*RAtNamespace) isRule() {}
func (*RAtImport) isRule() {}
func (*RAtKeyframes) isRule() {}
func (*RKnownAt) isRule() {}
func (*RUnknownAt) isRule() {}
func (*RSelector) isRule() {}
func (*RQualified) isRule() {}
func (*RDeclaration) isRule() {}
func (*RBadDeclaration) isRule() {}
type ComplexSelector struct {
Selectors []CompoundSelector
}
type CompoundSelector struct {
HasNestPrefix bool // "&"
Combinator string // Optional, may be ""
TypeSelector *NamespacedName
SubclassSelectors []SS
PseudoClassSelectors []SSPseudoClass // If present, these follow a ":" character
}
type NameToken struct {
Kind css_lexer.T
Text string
}
type NamespacedName struct {
// If present, this is an identifier or "*" and is followed by a "|" character
NamespacePrefix *NameToken
// This is an identifier or "*" or "&"
Name NameToken
}
// This interface is never called. Its purpose is to encode a variant type in
// Go's type system.
type SS interface {
isSubclassSelector()
}
type SSHash struct {
Name string
}
type SSClass struct {
Name string
}
type SSAttribute struct {
NamespacedName NamespacedName
MatcherOp string
MatcherValue string
MatcherModifier byte
}
type SSPseudoClass struct {
Name string
Args []Token
}
func (*SSHash) isSubclassSelector() {}
func (*SSClass) isSubclassSelector() {}
func (*SSAttribute) isSubclassSelector() {}
func (*SSPseudoClass) isSubclassSelector() {}