-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyacc.y
143 lines (126 loc) · 2.61 KB
/
yacc.y
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
%{
package cronparser
import "fmt"
func setScannerData(yylex interface{}, data CronResult) {
yylex.(*Scanner).data = data
}
%}
%union {
empty struct{}
s string
i int
c CronResult
}
%token<empty> COMMA COLON ASTERISK SLASH DASH
%token<s> STRING
%token<i> NUMBER
%type<s> minutes hours days months weeks
%type<c> value
%left SLASH ASTERISK DASH COMMA NUMBER
%%
cron:
value {
setScannerData(yylex, $1)
}
value:
minutes hours days months weeks
{
$$ = CronResult{Minutes: $1, Hours: $2, Days: $3, Months: $4, Weeks: $5}
}
minutes:
ASTERISK { $$ = "*" }
| ASTERISK SLASH minutes {
$$ = fmt.Sprintf("*/%s", $3)
}
| minutes DASH minutes {
$$ = fmt.Sprintf("%s-%s", $1, $3)
}
| minutes COMMA minutes {
$$ = fmt.Sprintf("%s,%s", $1, $3)
}
| NUMBER {
if !isNumValid($1, 0, 59) {
yylex.Error(fmt.Sprintf("minutes:%d should between 0-59", $1))
}
$$ = fmt.Sprintf("%d", $1)
}
hours:
ASTERISK { $$ = "*" }
| ASTERISK SLASH hours {
$$ = fmt.Sprintf("*/%s", $3)
}
| hours DASH hours{
$$ = fmt.Sprintf("%s-%s", $1, $3)
}
| hours COMMA hours{
$$ = fmt.Sprintf("%s,%s", $1, $3)
}
| NUMBER {
if !isNumValid($1, 0, 23) {
yylex.Error(fmt.Sprintf("hours:%d should between 0-23", $1))
}
$$ = fmt.Sprintf("%d", $1)
}
days:
ASTERISK { $$ = "*" }
| ASTERISK SLASH days {
$$ = fmt.Sprintf("*/%s", $3)
}
| days DASH days {
$$ = fmt.Sprintf("%s-%s", $1, $3)
}
| days COMMA days {
$$ = fmt.Sprintf("%s,%s", $1, $3)
}
| NUMBER {
if !isNumValid($1, 1, 31) {
yylex.Error(fmt.Sprintf("days:%d should between 1-31", $1))
}
$$ = fmt.Sprintf("%d", $1)
}
months:
ASTERISK { $$ = "*" }
| ASTERISK SLASH months {
$$ = fmt.Sprintf("*/%s", $3)
}
| months DASH months {
$$ = fmt.Sprintf("%s-%s", $1, $3)
}
| months COMMA months {
$$ = fmt.Sprintf("%s,%s", $1, $3)
}
| NUMBER {
if !isMonthValid($1) {
yylex.Error(fmt.Sprintf("months:%d should between 1–12 or JAN–DEC", $1))
}
$$ = fmt.Sprintf("%d", $1)
}
| STRING {
if !isMonthValid($1) {
yylex.Error(fmt.Sprintf("months:%s should between 1–12 or JAN–DEC", $1))
}
$$ = fmt.Sprintf("%s", $1)
}
weeks:
ASTERISK { $$ = "*" }
| ASTERISK SLASH weeks {
$$ = fmt.Sprintf("*/%s", $3)
}
| weeks DASH weeks {
$$ = fmt.Sprintf("%s-%s", $1, $3)
}
| weeks COMMA weeks {
$$ = fmt.Sprintf("%s,%s", $1, $3)
}
| NUMBER {
if !isWeekValid($1) {
yylex.Error(fmt.Sprintf("weeks:%d should between 0–6 or SUN–SAT", $1))
}
$$ = fmt.Sprintf("%d", $1)
}
| STRING {
if !isWeekValid($1) {
yylex.Error(fmt.Sprintf("months:%s should between 0-6 or SUN–SAT", $1))
}
$$ = fmt.Sprintf("%s", $1)
}