-
Notifications
You must be signed in to change notification settings - Fork 22
/
parser.go
63 lines (53 loc) · 1.41 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
package orders
import (
"fmt"
"github.com/zond/godip"
)
type Parser struct {
prototypes []godip.Order
}
func NewParser(prototypes []godip.Order) Parser {
return Parser{prototypes}
}
func (self Parser) OrderTypes() (result []godip.OrderType) {
result = make([]godip.OrderType, len(self.prototypes))
for index, prototype := range self.prototypes {
result[index] = prototype.DisplayType()
}
return
}
func (self Parser) Orders() (result []godip.Order) {
result = make([]godip.Order, len(self.prototypes))
copy(result, self.prototypes)
return
}
func (self Parser) ParseAll(orders map[godip.Nation]map[godip.Province][]string) (result map[godip.Province]godip.Adjudicator, err error) {
merr := MultiError{}
result = map[godip.Province]godip.Adjudicator{}
for _, nationOrders := range orders {
for prov, bits := range nationOrders {
if parsed, e := self.Parse(append([]string{string(prov)}, bits...)); e == nil {
result[prov] = parsed
} else {
merr = append(merr, e)
}
}
}
if len(merr) > 0 {
err = merr
}
return
}
func (self Parser) Parse(bits []string) (result godip.Adjudicator, err error) {
for _, prototype := range self.Orders() {
result, err := prototype.Parse(bits)
if result != nil || err != nil {
return result, err
}
}
return nil, fmt.Errorf("Invalid order %+v", bits)
}
type MultiError []error
func (self MultiError) Error() string {
return fmt.Sprint([]error(self))
}