-
Notifications
You must be signed in to change notification settings - Fork 1
/
expression.go
210 lines (185 loc) · 5.3 KB
/
expression.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
// Package tokei provides a cron parser and scheduler.
//
// Tokei works by parsing the cron string and generating an Enumerator for each
// part of the expression. It these uses these Enumerators to enumerate possible valid
// combinations of times which match the expression.
package tokei
import (
"errors"
"regexp"
"strconv"
"strings"
)
// CronExpression describes a parsed cron expression.
type CronExpression struct {
minutes enumerator
hours enumerator
dayOfMonth enumerator
month enumerator
dayOfWeek enumerator
}
// Parse parses a cron expression from a string.
func Parse(input string) (*CronExpression, error) {
parts := strings.Split(input, " ")
if len(parts) < 5 {
return nil, errors.New("invalid expression")
}
min, minErr := defaultMultiExpression.Parse(minuteContext, parts[0])
hour, hourErr := defaultMultiExpression.Parse(hourContext, parts[1])
dom, domErr := defaultMultiExpression.Parse(dayOfMonthContext, parts[2])
month, monthErr := defaultMultiExpression.Parse(monthContext, parts[3])
dow, dowErr := defaultMultiExpression.Parse(dayOfWeekContext, parts[4])
for _, err := range []error{minErr, hourErr, domErr, monthErr, dowErr} {
if err != nil {
return nil, err
}
}
return &CronExpression{
minutes: min,
hours: hour,
dayOfMonth: dom,
month: month,
dayOfWeek: dow,
}, nil
}
// parser is anything that can parse an expression part.
type parser interface {
Parse(expressionContext, string) (enumerator, error)
}
// parseFunc allows us to adapt a func to Parser.
type parseFunc func(expressionContext, string) (enumerator, error)
// Parse adapts ParseFunc to parser.
func (f parseFunc) Parse(ex expressionContext, input string) (enumerator, error) {
return f(ex, input)
}
// A multiExpression detects the type of expression and hands off to another parser.
// It uses regex to do this for simplicity rather than a traditional tokenizer.
type multiExpression struct {
rangeRegex *regexp.Regexp
repeatRegex *regexp.Regexp
literalRegex *regexp.Regexp
}
// Parse parses any expression by deferring to other parsers.
func (m multiExpression) Parse(ex expressionContext, input string) (enumerator, error) {
trimmed := strings.TrimSpace(input)
if trimmed == "*" {
return kleeneExpression(ex, input)
}
if m.rangeRegex.MatchString(trimmed) {
return rangeExpression(ex, trimmed)
}
if m.repeatRegex.MatchString(trimmed) {
return repeatExpression(ex, trimmed)
}
if m.literalRegex.MatchString(trimmed) {
return literalExpression(ex, trimmed)
}
return nil, errors.New("unknown expression")
}
var defaultMultiExpression = multiExpression{
rangeRegex: regexp.MustCompile(`\d-\d`),
repeatRegex: regexp.MustCompile(`./\d`),
literalRegex: regexp.MustCompile(`(\d+)(,\s*\d+)*`),
}
// kleeneExpression parses the "*" expression only.
var kleeneExpression = parseFunc(func(ex expressionContext, input string) (enumerator, error) {
if input != "*" {
return nil, errors.New("input must be *")
}
return sequence{
start: ex.Min(),
end: ex.Max(),
step: 1,
}, nil
})
// rangeExpression parses expressions of the for x-y.
var rangeExpression = parseFunc(func(ex expressionContext, input string) (enumerator, error) {
parts := strings.Split(input, "-")
if len(parts) > 2 {
return nil, errors.New("must be of form x-y")
}
start, err := parseStartValue(ex, parts[0])
if err != nil {
return nil, err
}
if len(parts) == 1 {
return newIrregularSequence([]int{start}), nil
}
end, err := parseEndValue(ex, parts[1])
if err != nil {
return nil, err
}
if start > end {
return nil, errors.New("invalid range")
}
return sequence{
start: start,
end: end,
step: 1,
}, nil
})
// repeatExpression parses expressions of the form x/y, including */y.
var repeatExpression = parseFunc(func(ex expressionContext, input string) (enumerator, error) {
parts := strings.Split(input, "/")
if len(parts) != 2 {
return nil, errors.New("Invalid repeat expression, must be of form x/y")
}
end, err := parseEndValue(ex, parts[1])
if err != nil {
return nil, err
}
if parts[0] == "*" {
return sequence{
start: ex.Min(),
end: ex.Max(),
step: end,
}, nil
}
start, err := parseStartValue(ex, parts[0])
if err != nil {
return nil, err
}
return sequence{
start: start,
end: ex.Max(),
step: end,
}, nil
})
// literalExpression parses expressions of the form "x,y[,z].."
var literalExpression = parseFunc(func(ex expressionContext, input string) (enumerator, error) {
parts := strings.Split(input, ",")
times := make([]int, len(parts))
for i, part := range parts {
time, err := strconv.Atoi(strings.TrimSpace(part))
if err != nil {
return nil, err
}
if time < ex.Min() || time > ex.Max() {
return nil, errors.New("invalid time part for literal expression")
}
times[i] = time
}
return irregularSequence{
entries: times,
}, nil
})
func parseStartValue(ex expressionContext, input string) (int, error) {
start, err := strconv.Atoi(strings.TrimSpace(input))
if err != nil {
return 0, err
}
if start < ex.Min() {
return 0, errors.New("invalid start value")
}
return start, nil
}
func parseEndValue(ex expressionContext, input string) (int, error) {
end, err := strconv.Atoi(strings.TrimSpace(input))
if err != nil {
return 0, err
}
if end > ex.Max() {
return 0, errors.New("invalid end value")
}
return end, nil
}