-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
187 lines (182 loc) · 5.93 KB
/
parse.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
package cronfab
import (
"unicode"
"unicode/utf8"
)
type State int
const (
StateExpectSplatOrNumberOrName = State(iota)
StateInNumber // next must be '0'-'9' or ',' or ' ' or '-'
StateInName // next must be 'a'-'z' or ',' or ' ' or '-'
StateExpectEndRangeNumber // next must be '0'-'9'
StateInEndRangeNumber // next must be '0'-'9' or '/' or ',' or ' '
StateExpectEndRangeName // next must be '0'-'9'
StateInEndRangeName // next must be 'a'-'z' or '/' or ',' or ' '
StateExpectStep // next must be '/' or ',' or ' '
StateExpectStepNumber // next must be '0'-'9'
StateInStepNumber // next must be '0'-'9' or ',' or ' '
)
// StateString returns a human readable string for the parser state. Used in error messages
func StateString(x State) string {
switch x {
case StateExpectSplatOrNumberOrName:
return "expecting '*' or number or name"
case StateInNumber:
return "in number"
case StateInName:
return "in name"
case StateExpectEndRangeName:
return "expecting end-range name"
case StateInEndRangeName:
return "int end-range name"
case StateExpectEndRangeNumber:
return "expecting end-range number"
case StateInEndRangeNumber:
return "in end-range number"
case StateExpectStep:
return "expecting '/'"
case StateExpectStepNumber:
return "expecting step number"
case StateInStepNumber:
return "in step number"
}
return "unknown"
}
// ParseCronTab parses a crontab string using the crontab configuration
func (cc *CrontabConfig) ParseCronTab(s string) (CrontabLine, error) {
if len(s) == 0 {
return CrontabLine{}, nil
}
i := 0
j := 0
fieldi := 0
listi := 0
ss := s
numbers := CrontabConstraint([3]int{cc.Fields[fieldi].min, cc.Fields[fieldi].max, 1})
markers := CrontabLine{{{}}}
state := StateExpectSplatOrNumberOrName
r, n := utf8.DecodeRuneInString(ss)
for r != utf8.RuneError && len(ss) > 0 {
if r >= '0' && r <= '9' {
if state == StateInNumber {
} else if state == StateInStepNumber {
} else if state == StateInEndRangeNumber {
} else if state == StateInName {
} else if state == StateInEndRangeName {
} else if state == StateExpectSplatOrNumberOrName {
state = StateInNumber
j = i
} else if state == StateExpectEndRangeNumber {
state = StateInEndRangeNumber
j = i
} else if state == StateExpectStepNumber {
state = StateInStepNumber
j = i
} else {
return CrontabLine{}, &ErrorParse{Index: i, State: state}
}
} else if unicode.IsLetter(r) {
if state == StateInName {
} else if state == StateInEndRangeName {
} else if state == StateExpectSplatOrNumberOrName {
state = StateInName
j = i
} else if state == StateExpectEndRangeName {
state = StateInEndRangeName
j = i
} else {
return CrontabLine{}, &ErrorParse{Index: i, State: state}
}
} else if r == '*' {
if state == StateExpectSplatOrNumberOrName {
numbers = CrontabConstraint([3]int{cc.Fields[fieldi].min, cc.Fields[fieldi].max, 1})
state = StateExpectStep
} else {
return CrontabLine{}, &ErrorParse{Index: i, State: state}
}
} else if r == '-' {
if state == StateInNumber {
err := cc.SetGroupNumber(state, i, fieldi, &numbers, s[j:i])
if err != nil {
return CrontabLine{}, err
}
state = StateExpectEndRangeNumber
} else if state == StateInName && len(cc.Fields[fieldi].rangeNames) > 0 {
err := cc.SetGroupName(state, i, fieldi, &numbers, s[j:i])
if err != nil {
return CrontabLine{}, err
}
state = StateExpectEndRangeName
} else {
return CrontabLine{}, &ErrorParse{Index: i, State: state}
}
} else if r == '/' {
if state == StateExpectStep {
state = StateExpectStepNumber
} else if state == StateInEndRangeNumber {
err := cc.SetGroupNumber(state, i, fieldi, &numbers, s[j:i])
if err != nil {
return CrontabLine{}, err
}
state = StateExpectStepNumber
} else if state == StateInEndRangeName && len(cc.Fields[fieldi].rangeNames) > 0 {
err := cc.SetGroupName(state, i, fieldi, &numbers, s[j:i])
if err != nil {
return CrontabLine{}, err
}
state = StateExpectStepNumber
} else {
return CrontabLine{}, &ErrorParse{Index: i, State: state}
}
} else if r == ',' || r == ' ' {
if state == StateExpectStep {
} else if state == StateInEndRangeNumber || state == StateInStepNumber || state == StateInNumber {
err := cc.SetGroupNumber(state, i, fieldi, &numbers, s[j:i])
if err != nil {
return CrontabLine{}, err
}
} else if state == StateInName && len(cc.Fields[fieldi].rangeNames) > 0 {
err := cc.SetGroupName(state, i, fieldi, &numbers, s[j:i])
if err != nil {
return CrontabLine{}, err
}
} else {
return CrontabLine{}, &ErrorParse{Index: i, State: state}
}
if r == ',' {
markers.SetField(fieldi, append(markers[fieldi], CrontabConstraint([3]int{})))
markers.SetConstraint(fieldi, listi, numbers)
listi++
} else if r == ' ' {
markers = append(markers, CrontabField([][3]int{{}}))
markers[fieldi][listi] = numbers
fieldi++
listi = 0
}
numbers = CrontabConstraint([3]int{cc.Fields[fieldi].min, cc.Fields[fieldi].max, 1})
state = StateExpectSplatOrNumberOrName
} else {
return CrontabLine{}, &ErrorParse{Index: i, State: state}
}
i += n
ss = ss[n:]
r, n = utf8.DecodeRuneInString(ss)
}
if state == StateExpectStep {
} else if state == StateInEndRangeNumber || state == StateInStepNumber || state == StateInNumber {
err := cc.SetGroupNumber(state, i, fieldi, &numbers, s[j:i])
if err != nil {
return CrontabLine{}, err
}
} else if (state == StateInEndRangeName || state == StateInName) && len(cc.Fields[fieldi].rangeNames) > 0 {
err := cc.SetGroupName(state, i, fieldi, &numbers, s[j:i])
if err != nil {
return CrontabLine{}, err
}
} else {
return CrontabLine{}, &ErrorParse{Index: i, State: state}
}
markers[fieldi][listi] = numbers
markers.Sort()
return markers, nil
}