-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
141 lines (118 loc) · 3.16 KB
/
parser.go
File metadata and controls
141 lines (118 loc) · 3.16 KB
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
package showgone
import (
"fmt"
"bufio"
"strings"
"unicode"
"reflect"
)
func (p *Pokemon) FillDefaults() {
p.Level = 100
p.Happiness = 255
for i := 0; i < 6; i++ {
p.IVs[i] = 31
}
}
func Parse(r *bufio.Reader) (*Pokemon, error) {
var line string
var err error
var idx int
var poke Pokemon
poke.FillDefaults()
pokeR := reflect.ValueOf(&poke).Elem()
// skip spaces
for {
var rr rune
rr, _, err = r.ReadRune()
if err != nil {
return nil, err
}
if !unicode.IsSpace(rr) {
err = r.UnreadRune()
if err != nil {
return nil, err
}
break
}
}
// get poke species
line, err = r.ReadString('\n')
if err != nil {
return nil, err
}
idx = strings.Index(line, " @ ")
if idx != -1 {
poke.Species = String(line[:idx])
poke.Item = String(strings.TrimRightFunc(line[idx+3:], unicode.IsSpace))
} else {
poke.Species = String(strings.TrimRightFunc(line, unicode.IsSpace))
}
// check if name includes sex
sz := len(poke.Species)
if sz > 3 &&
poke.Species[sz-4] == ' ' &&
poke.Species[sz-3] == '(' &&
poke.Species[sz-1] == ')' {
poke.Gender = Gender(poke.Species[sz-2])
poke.Species = poke.Species[:sz-4]
}
// rest of the fields...
for move := 0;; {
// get new line
line, err = r.ReadString('\n')
if err != nil || line[0] == '\r' || line[0] == '\n' {
break
}
// check for moves
if line[0] == '-' {
if move > 3 {
return nil, ErrInvalidFmt
}
poke.Moves[move] = String(strings.TrimRightFunc(line[2:], unicode.IsSpace))
move++
continue
}
// none apparently, determine what else
// to do with the line...
idx = strings.Index(line, ": ")
if idx == -1 {
// try to find Nature
idx = strings.Index(line, " Nature")
if idx == -1 {
return nil, ErrInvalidFmt
}
// save found nature
poke.Nature = natIdx[line[:idx]]
continue
}
key := line[:idx]
// either IVs or EVs
if key[1] == 'V' {
var vs []uint8
if key[0] == 'E' {
vs = poke.EVs[:]
} else {
vs = poke.IVs[:]
}
var val uint8
var which string
evtok := line[idx+2:]
idx = -3
for idx != -1 {
evtok = evtok[idx+3:]
fmt.Sscan(evtok, &val, &which)
vs[statIdx[which]] = val
idx = strings.Index(evtok, " / ")
}
} else {
field := pokeR.FieldByName(key)
value := strings.TrimRightFunc(line[idx+2:], unicode.IsSpace)
conv := convAttrib[key]
if conv == nil {
return nil, ErrInvalidFmt
}
field.Set(conv(value))
}
}
return &poke, nil
}