-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.go
199 lines (161 loc) · 3.6 KB
/
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
187
188
189
190
191
192
193
194
195
196
197
198
199
package traindown
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/araddon/dateparse"
)
// ParseByte takes in a Traindown byte slice and returns a pointer to a Session.
func ParseByte(txt []byte) (*Session, error) {
s, err := parse("", txt)
if err != nil {
return &Session{}, err
}
return s, nil
}
// ParseString takes in a Traindown string and returns a pointer to a Session.
func ParseString(txt string) (*Session, error) {
s, err := parse(txt, []byte(""))
if err != nil {
return &Session{}, err
}
return s, nil
}
func floatValue(s string, t string) (float32, error) {
f, err := strconv.ParseFloat(s, 32)
if err != nil {
return 0.0, fmt.Errorf("Failed to parse %q: %q", t, s)
}
return float32(f), nil
}
func intValue(s string, t string) (int, error) {
i, err := strconv.Atoi(s)
if err != nil {
return 0, fmt.Errorf("Failed to parse %q: %q", t, s)
}
return i, nil
}
func parse(str string, b []byte) (*Session, error) {
s := NewSession()
lexer, err := NewLexer()
if err != nil {
return s, err
}
var tokens []*Token
if str != "" {
tokens, err = lexer.Scan([]byte(str))
} else {
tokens, err = lexer.Scan(b)
}
if err != nil {
return s, err
}
m := NewMovement()
p := NewPerformance()
inSession := true
inPerformance := false
mSeq := 0
pSeq := 0
for _, tok := range tokens {
switch tok.Name() {
case "DATE":
d, err := dateparse.ParseAny(tok.Value())
if err != nil {
s.Errors = append(s.Errors, fmt.Errorf("Failed to parse date: %q. Using today UTC", err))
s.Date = time.Now()
} else {
s.Date = d
}
case "FAILS":
i, err := intValue(tok.Value(), "fails")
if err != nil {
s.Errors = append(s.Errors, err)
}
p.Fails = i
case "LOAD":
if inPerformance {
p.Sequence = pSeq
p.maybeInheritUnit(s, m)
m.Performances = append(m.Performances, p)
p = NewPerformance()
pSeq++
}
f, err := floatValue(tok.Value(), "load")
if err != nil {
s.Errors = append(s.Errors, err)
}
p.Load = f
inPerformance = true
case "METADATA":
pair := strings.Split(tok.Value(), ":")
key := strings.Trim(pair[0], " ")
value := strings.Trim(pair[1], " ")
if inSession {
if !s.assignSpecial(key, value) {
s.Metadata[key] = value
}
} else if inPerformance {
if !p.assignSpecial(key, value) {
p.Metadata[key] = value
}
} else {
if !m.assignSpecial(key, value) {
m.Metadata[key] = value
}
}
case "MOVEMENT", "MOVEMENT_SS":
inSession = false
if inPerformance {
p.Sequence = pSeq
p.maybeInheritUnit(s, m)
m.Performances = append(m.Performances, p)
p = NewPerformance()
pSeq++
}
inPerformance = false
if m.Name != "" {
m.Sequence = mSeq
s.Movements = append(s.Movements, m)
m = NewMovement()
mSeq++
pSeq = 0
}
m.Name = tok.Value()
if tok.Name() == "MOVEMENT_SS" {
m.SuperSet = true
}
case "NOTE":
if inSession {
s.Notes = append(s.Notes, tok.Value())
} else if inPerformance {
p.Notes = append(p.Notes, tok.Value())
} else {
m.Notes = append(m.Notes, tok.Value())
}
case "REPS":
i, err := intValue(tok.Value(), "reps")
if err != nil {
s.Errors = append(s.Errors, err)
}
p.Reps = i
case "SETS":
i, err := intValue(tok.Value(), "sets")
if err != nil {
s.Errors = append(s.Errors, err)
}
p.Sets = i
}
}
if !reflect.DeepEqual(p, NewPerformance()) {
p.Sequence = pSeq
p.maybeInheritUnit(s, m)
m.Performances = append(m.Performances, p)
}
if m.Name != "" {
m.Sequence = mSeq
s.Movements = append(s.Movements, m)
}
return s, nil
}