-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Compiler.go
232 lines (187 loc) · 5.54 KB
/
Compiler.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
package scarlet
import (
"strings"
"unicode"
"github.com/aerogo/codetree"
)
// compileChildren returns the CSS rules for a given code tree.
// It iterates over the child nodes and finds the CSS rules.
func compileChildren(node *codetree.CodeTree, parent *CSSRule, state *State) ([]*CSSRule, []*MediaGroup, []*MediaQuery, []*Animation) {
// Comments
if strings.HasPrefix(node.Line, "//") {
return nil, nil, nil, nil
}
var rules []*CSSRule
var mediaGroups []*MediaGroup
var mediaQueries []*MediaQuery
var animations []*Animation
var selectorsOnPreviousLines []string
// Iterate over child nodes
for _, child := range node.Children {
// Nodes with no children
if len(child.Children) == 0 {
// Comments
if strings.HasPrefix(child.Line, "//") {
continue
}
// Selector on previous line
if strings.HasSuffix(child.Line, ",") {
selectorsOnPreviousLines = append(selectorsOnPreviousLines, child.Line[:len(child.Line)-1])
continue
}
equal := strings.IndexByte(child.Line, '=')
switch {
case equal != -1:
value := strings.TrimSpace(child.Line[equal+1:])
value = insertVariableValues(value, state)
value = optimizeColors(value)
if strings.HasPrefix(child.Line, "const ") {
// Constants
name := strings.TrimSpace(child.Line[len("const "):equal])
state.Constants[name] = value
} else {
// Variables
name := strings.TrimSpace(child.Line[:equal])
state.VariableNames = append(state.VariableNames, name)
state.Variables[name] = value
}
case parent != nil && strings.IndexByte(child.Line, ' ') != -1:
// Statements
statement := compileStatement(child.Line, state)
parent.Statements = append(parent.Statements, statement)
default:
// Mixin calls
mixin, exists := state.Mixins[child.Line]
if exists && parent != nil {
mixinRules := mixin.Apply(parent)
rules = append(rules, mixinRules...)
} else {
panic("Invalid statement: " + child.Line)
}
}
}
// Mixin
if strings.HasPrefix(child.Line, "mixin ") {
name := child.Line[len("mixin "):]
mixin := &Mixin{
Root: &CSSRule{},
Rules: []*CSSRule{},
}
childRules, _, _, _ := compileChildren(child, mixin.Root, state)
mixin.Rules = append(mixin.Rules, childRules...)
state.Mixins[name] = mixin
continue
}
// Media query
if strings.HasPrefix(child.Line, "@") {
selector := strings.TrimSpace(child.Line)
media := &MediaQuery{
Selector: selector,
}
media.Rules, _, _, _ = compileChildren(child, nil, state)
mediaQueries = append(mediaQueries, media)
continue
}
// Media query by size
if strings.HasPrefix(child.Line, "< ") || strings.HasPrefix(child.Line, "> ") {
// If this is a top level definition, it's a screen size query
if child.Indent == 0 {
media := &MediaGroup{}
parts := strings.Split(child.Line, " ")
media.Operator = parts[0]
media.Size = parts[1]
if len(parts) >= 3 {
media.Property = parts[2]
} else {
media.Property = "width"
}
media.Rules, _, _, _ = compileChildren(child, nil, state)
mediaGroups = append(mediaGroups, media)
continue
}
}
// Animation
if strings.HasPrefix(child.Line, "animation ") {
anim := &Animation{
Name: child.Line[len("animation "):],
}
anim.Keyframes, _, _, _ = compileChildren(child, nil, state)
animations = append(animations, anim)
continue
}
// This isn't 100% correct but works in 99.9% of cases.
// TODO: Make this work for funky stuff like a[href$="a,b"]
selectors := strings.Split(child.Line, ",")
// Append selectors from previous lines
selectors = append(selectors, selectorsOnPreviousLines...)
selectorsOnPreviousLines = selectorsOnPreviousLines[:0]
for _, selector := range selectors {
selector = strings.TrimSpace(selector)
// Child rule
rule := &CSSRule{
Selector: selector,
Parent: parent,
}
rules = append(rules, rule)
childRules, _, _, _ := compileChildren(child, rule, state)
rules = append(rules, childRules...)
}
}
return rules, mediaGroups, mediaQueries, animations
}
// compileStatement compiles a Scarlet statement to CSS.
func compileStatement(statement string, state *State) *CSSStatement {
space := strings.IndexByte(statement, ' ')
if space == -1 {
panic("Invalid statement: " + statement)
}
value := strings.TrimSpace(statement[space:])
// Optimize color values
value = insertVariableValues(value, state)
value = optimizeColors(value)
return &CSSStatement{
Property: statement[:space],
Value: value,
}
}
// insertVariableValues inserts the values of variables directly into the string.
func insertVariableValues(expression string, state *State) string {
// EOF
runes := append([]rune(expression), ' ')
buffer := strings.Builder{}
ignore := ignoreReader{}
cursor := 0
for index, char := range runes {
if ignore.canIgnore(char) {
buffer.WriteRune(char)
cursor = index + 1
continue
}
if char != '-' && (unicode.IsSpace(char) || unicode.IsPunct(char)) {
if index != cursor {
token := string(runes[cursor:index])
// Check dynamic CSS variables
_, exists := state.Variables[token]
if exists {
buffer.WriteString("var(--")
buffer.WriteString(token)
buffer.WriteString(")")
} else {
// Check constants
value, exists := state.Constants[token]
if exists {
buffer.WriteString(value)
} else {
buffer.WriteString(token)
}
}
}
if index == len(runes)-1 {
break
}
buffer.WriteRune(char)
cursor = index + 1
}
}
return buffer.String()
}