-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_parser.go
186 lines (163 loc) · 4.13 KB
/
text_parser.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
package calendatext
import (
"regexp"
"strconv"
"strings"
"github.com/pkg/errors"
)
type BuildMatcher func(s string) (DateMatcher, error)
type textParser struct {
Patterns Patterns
matcherBuilders []BuildMatcher
}
func newTextParser(d Date) *textParser {
return &textParser{
Patterns: Patterns{},
matcherBuilders: newMatcherBuilders(&d),
}
}
func (tp *textParser) Run(s string) error {
lines := strings.Split(s, "\n")
for _, rawLine := range lines {
line := strings.TrimSpace(rawLine)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
pattern, err := tp.parseLine(line)
if err != nil {
return err
}
tp.Patterns = append(tp.Patterns, pattern)
}
return nil
}
func (tp *textParser) parseLine(line string) (*Pattern, error) {
var enabled bool
if strings.HasPrefix(line, "+") {
enabled = true
} else if strings.HasPrefix(line, "-") {
enabled = false
} else {
return nil, errors.Errorf("Invalid first charactor. It must be '+' or '-': %q\n", line)
}
line = line[1:]
bodies := strings.SplitN(line, ":", 2)
description := ""
if len(bodies) == 2 {
description = strings.TrimSpace(bodies[1])
}
matcher, err := tp.parseMatcher(strings.TrimSpace(bodies[0]))
if err != nil {
return nil, errors.WithMessagef(err, "Failed to build matcher for %s", description)
}
return &Pattern{
Enabled: enabled,
Description: description,
DateMatcher: matcher,
}, nil
}
func (tp *textParser) parseMatcher(body string) (DateMatcher, error) {
for _, build := range tp.matcherBuilders {
m, err := build(body)
if err != nil {
return nil, err
}
if m != nil {
return m, nil
}
}
return nil, errors.Errorf("No build function found for %q", body)
}
var (
slashDateRE = regexp.MustCompile(`\A(?:\d+/)?(?:\d+/)?\d+\z`)
slashPeriodRE = regexp.MustCompile(`\A(?:\d+/)?(?:\d+/)?\d+\s*-\s*(?:\d+/)?(?:\d+/)?\d+\z`)
weeklyRE = regexp.MustCompile(`\A毎週`)
monthlyDayRE = regexp.MustCompile(`\A毎月[^\d]*(\d+)日`)
monthlyWeekdayRE = regexp.MustCompile(`\A毎月.*第(\d)(.+)`)
)
func newMatcherBuilders(date *Date) []BuildMatcher {
delimiter := "/"
contextualParser := NewContextualDateParser(delimiter, date)
return []BuildMatcher{
func(s string) (DateMatcher, error) {
if s != "平日" {
return nil, nil
}
return Weekdays{Monday, Tuesday, Wednesday, Thursday, Friday}, nil
},
// 毎週***
func(s string) (DateMatcher, error) {
if !weeklyRE.MatchString(s) {
return nil, nil
}
r := Weekdays{}
for d, c := range WeekdayNameMap {
if strings.ContainsRune(s, c) {
r = append(r, d)
}
}
if len(r) == 0 {
return nil, nil
}
return r, nil
},
// 毎月***
func(s string) (DateMatcher, error) {
m := monthlyDayRE.FindAllStringSubmatch(s, -1)
if len(m) < 1 {
return nil, nil
}
if len(m[0]) < 2 {
return nil, errors.Errorf("something wrong to parse %q", s)
}
d, err := strconv.ParseInt(m[0][1], 10, 10)
if err != nil {
return nil, err
}
return MonthlyDay(d), nil
},
// 毎月第N***
func(s string) (DateMatcher, error) {
m := monthlyWeekdayRE.FindAllStringSubmatch(s, -1)
if len(m) < 1 {
return nil, nil
}
if len(m[0]) < 3 {
return nil, errors.Errorf("something wrong to parse %q", s)
}
n, err := strconv.Atoi(m[0][1])
if err != nil {
return nil, err
}
wd, err := ParseWeekdayName(m[0][2])
if err != nil {
return nil, err
}
return &MonthlyWeekday{Num: n, Weekday: *wd}, nil
},
func(s string) (DateMatcher, error) {
if !slashDateRE.MatchString(s) {
return nil, nil
}
return contextualParser.Parse(strings.TrimSpace(s))
},
func(s string) (DateMatcher, error) {
if !slashPeriodRE.MatchString(s) {
return nil, nil
}
parts := strings.SplitN(s, "-", 2)
if len(parts) < 2 {
return nil, errors.Errorf("Failed to split string as Period: %q", s)
}
st, err := contextualParser.Parse(strings.TrimSpace(parts[0]))
if err != nil {
return nil, err
}
ed, err := contextualParser.Parse(strings.TrimSpace(parts[1]))
if err != nil {
return nil, err
}
return NewPeriod(*st, *ed), nil
},
}
}