-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenize.go
More file actions
186 lines (171 loc) · 3.88 KB
/
tokenize.go
File metadata and controls
186 lines (171 loc) · 3.88 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
package csshtml
import (
"fmt"
"log/slog"
"os"
"path/filepath"
scanner "github.com/speedata/css"
)
// tokenizeAndApplyImport converts a CSS string to a Tokenstream. Also read
// linked (@import) stylesheets.
func (c *CSS) tokenizeAndApplyImport(css string) (tokenstream, error) {
var tokens tokenstream
tokens = tokenizeCSSString(css)
var finalTokens []*scanner.Token
for i := 0; i < len(tokens); i++ {
tok := tokens[i]
if tok.Type == scanner.AtKeyword && tok.Value == "import" {
i++
for {
if tokens[i].Type == scanner.S {
i++
} else {
break
}
}
importvalue := tokens[i]
toks, err := c.tokenizeCSSFile(importvalue.Value)
if err != nil {
return nil, err
}
// if the last token of the imported file is a space, remove it.
lasttoc := toks[len(toks)-1]
if lasttoc.Type == scanner.S {
finalTokens = append(toks[:len(toks)-1], finalTokens...)
} else {
finalTokens = append(toks, finalTokens...)
}
// hopefully there is no keyword before the semicolon
for {
i++
if i >= len(tokens) {
break
}
if tokens[i].Value == ";" {
break
}
}
} else {
finalTokens = append(finalTokens, tok)
}
}
return finalTokens, nil
}
// tokenizeCSSFile converts a CSS file into a Tokenstream and applies import
// statements.
func (c *CSS) tokenizeCSSFile(filename string) (tokenstream, error) {
if filename == "" {
return nil, fmt.Errorf("tokenizeCSSFile: no filename given")
}
var tokens tokenstream
var err error
dir, fn := filepath.Split(filename)
c.PushDir(dir)
loc, err := c.findFile(fn)
if err != nil {
return nil, err
}
tokens, err = parseCSSBody(loc)
if err != nil {
return nil, err
}
var finalTokens []*scanner.Token
for i := 0; i < len(tokens); i++ {
tok := tokens[i]
if tok.Type == scanner.AtKeyword && tok.Value == "import" {
i++
for {
if tokens[i].Type == scanner.S {
i++
} else {
break
}
}
importvalue := tokens[i]
toks, err := c.tokenizeCSSFile(importvalue.Value)
if err != nil {
return nil, err
}
// if the last token of the imported file is a space, remove it.
lasttoc := toks[len(toks)-1]
if lasttoc.Type == scanner.S {
finalTokens = append(toks[:len(toks)-1], finalTokens...)
} else {
finalTokens = append(toks, finalTokens...)
}
// hopefully there is no keyword before the semicolon
for {
i++
if i >= len(tokens) {
break
}
if tokens[i].Value == ";" {
break
}
}
} else if tok.Type == scanner.URI {
fs, err := c.findFile(tok.Value)
if err != nil {
return nil, err
}
tok.Value = fs
finalTokens = append(finalTokens, tok)
} else {
finalTokens = append(finalTokens, tok)
}
}
c.PopDir()
return finalTokens, nil
}
func parseCSSBody(filename string) (tokenstream, error) {
slog.Debug("parse CSS file", "filename", filename)
b, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
var tokens tokenstream
s := scanner.New(string(b))
for {
token := s.Next()
if token.Type == scanner.EOF || token.Type == scanner.Error {
break
}
switch token.Type {
case scanner.Comment:
// ignore
case scanner.S:
if len(tokens) > 0 && tokens[len(tokens)-1].Type == scanner.S {
// ignore
} else {
tokens = append(tokens, token)
}
default:
tokens = append(tokens, token)
}
}
return tokens, nil
}
// tokenizeCSSString converts a string into a Tokenstream.
func tokenizeCSSString(contents string) tokenstream {
var tokens tokenstream
s := scanner.New(contents)
for {
token := s.Next()
if token.Type == scanner.EOF || token.Type == scanner.Error {
break
}
switch token.Type {
case scanner.Comment:
// ignore
case scanner.S:
if len(tokens) > 0 && tokens[len(tokens)-1].Type == scanner.S {
// ignore
} else {
tokens = append(tokens, token)
}
default:
tokens = append(tokens, token)
}
}
return tokens
}