-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathState.go
155 lines (139 loc) · 3.15 KB
/
State.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
package cssminify
import (
"bytes"
"strings"
)
type State struct {
state byte
commentState byte
current []byte
previous []byte
currentBlock Block
currentPair Pair
blocks []Block
}
// Error constants
const (
NOT_IN_SELECTOR = "Met { while not being in selector"
NOT_AFTER_VALUE = "Met } after a non-value"
NOT_IN_PROPERTY = "Met : after a non-property"
NOT_IN_VALUE = "Met ; after a non-value"
)
// Parser constants
const (
STARTING_COMMENT = iota
IN_COMMENT = iota
CLOSING_COMMENT = iota
COMMENT_CLOSED = iota
IN_SELECTOR = iota
IN_PROPERTY = iota
IN_VALUE = iota
)
func (s *State) parse(letter byte) {
switch letter {
case '/':
s.slash(letter)
case '*':
s.star(letter)
case '{':
s.openBracket(letter)
case '}':
s.closeBracket(letter)
case ':':
s.colon(letter)
case ';':
s.semicolon(letter)
default:
s.rest(letter)
}
}
func (s *State) slash(letter byte) {
switch s.commentState {
case CLOSING_COMMENT:
// Since we don't keep comments
s.current = s.previous
s.commentState = COMMENT_CLOSED
default:
if s.commentState != IN_COMMENT {
s.commentState = STARTING_COMMENT
s.current = append(s.current, letter)
}
}
}
func (s *State) star(letter byte) {
switch s.commentState {
case STARTING_COMMENT:
s.previous = s.current[:len(s.current)-1]
s.commentState = IN_COMMENT
s.current = append(s.current, letter)
case IN_COMMENT:
s.commentState = CLOSING_COMMENT
s.current = append(s.current, letter)
}
}
func (s *State) openBracket(letter byte) {
if s.commentState != IN_COMMENT {
if s.state == IN_SELECTOR {
s.state = IN_PROPERTY
s.currentBlock.selector = s.current
s.current = []byte{}
} else {
panic(NOT_IN_SELECTOR)
}
}
}
func (s *State) closeBracket(letter byte) {
if s.commentState != IN_COMMENT {
if s.state == IN_VALUE && !bytes.Equal(nil, s.current) {
s.state = IN_PROPERTY
s.currentPair.value = s.current
s.currentBlock.pairs = append(s.currentBlock.pairs, s.currentPair)
}
if s.state == IN_PROPERTY && strings.Trim(string(s.current), " ") != "" {
s.current = []byte{}
s.blocks = append(s.blocks, s.currentBlock)
s.currentBlock = Block{}
s.state = IN_SELECTOR
} else {
panic(NOT_AFTER_VALUE)
}
}
}
func (s *State) colon(letter byte) {
if s.commentState != IN_COMMENT {
if s.state == IN_PROPERTY && !bytes.Equal(nil, s.current) {
s.state = IN_VALUE
s.currentPair.property = s.current
// Cleanup
s.current = []byte{}
} else {
if s.state != IN_VALUE && s.state != IN_SELECTOR {
panic(NOT_IN_PROPERTY)
}
// If there, it means we're in a value
s.current = append(s.current, letter)
}
}
}
func (s *State) semicolon(letter byte) {
if s.commentState != IN_COMMENT {
if s.state == IN_VALUE {
s.state = IN_PROPERTY
s.currentPair.value = s.current
s.currentBlock.pairs = append(s.currentBlock.pairs, s.currentPair)
// Cleanup
s.currentPair = Pair{}
s.current = []byte{}
} else {
panic(NOT_IN_VALUE)
}
}
}
func (s *State) rest(letter byte) {
if s.commentState != IN_COMMENT {
if s.state == 0 {
s.state = IN_SELECTOR
}
s.current = append(s.current, letter)
}
}