-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodifier.go
190 lines (155 loc) · 4.16 KB
/
modifier.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
package gts
import (
"fmt"
"github.com/go-pars/pars"
)
// Modifier is an interface required to modify coordinate regions.
type Modifier interface {
Apply(head, tail int) (int, int)
fmt.Stringer
}
// Head collapses a region onto its head, offset by the value given.
type Head int
// Apply the modifier to the given bounds.
func (mod Head) Apply(head, tail int) (int, int) {
p := int(mod)
if tail < head {
head, tail = mod.Apply(-head, -tail)
return -head, -tail
}
head += p
return head, head
}
// String returns the textual representation of the Modifier.
func (mod Head) String() string {
if mod == 0 {
return "^"
}
return fmt.Sprintf("^%+d", mod)
}
// Tail collapses a region onto its tail, offset by the value given.
type Tail int
// Apply the modifier to the given bounds.
func (mod Tail) Apply(head, tail int) (int, int) {
q := int(mod)
if tail < head {
head, tail = mod.Apply(-head, -tail)
return -head, -tail
}
tail += q
return tail, tail
}
// String returns the textual representation of the Modifier.
func (mod Tail) String() string {
if mod == 0 {
return "$"
}
return fmt.Sprintf("$%+d", mod)
}
// HeadTail offsets the head and tail coordinates by the values given.
type HeadTail [2]int
// Apply the modifier to the given bounds.
func (mod HeadTail) Apply(head, tail int) (int, int) {
p, q := Unpack(mod)
// Direction is backward. Assume complement.
if tail < head {
head, tail = mod.Apply(-head, -tail)
return -head, -tail
}
head += p
tail += q
return head, Max(head, tail)
}
// String returns the textual representation of the Modifier.
func (mod HeadTail) String() string {
p, q := Unpack(mod)
return fmt.Sprintf("%s..%s", Head(p), Tail(q))
}
// HeadHead offsets the head coordinate by the values given.
type HeadHead [2]int
// Apply the modifier to the given bounds.
func (mod HeadHead) Apply(head, tail int) (int, int) {
p, q := Unpack(mod)
if tail < head {
head, tail = mod.Apply(-head, -tail)
return -head, -tail
}
tail = head + q
head += p
return head, Max(head, tail)
}
// String returns the textual representation of the Modifier.
func (mod HeadHead) String() string {
p, q := Unpack(mod)
return fmt.Sprintf("%s..%s", Head(p), Head(q))
}
// TailTail offsets the head coordinate by the values given.
type TailTail [2]int
// Apply the modifier to the given bounds.
func (mod TailTail) Apply(head, tail int) (int, int) {
p, q := Unpack(mod)
if tail < head {
head, tail = mod.Apply(-head, -tail)
return -head, -tail
}
head = tail + p
tail += q
return head, Max(head, tail)
}
// String returns the textual representation of the Modifier.
func (mod TailTail) String() string {
p, q := Unpack(mod)
return fmt.Sprintf("%s..%s", Tail(p), Tail(q))
}
var parseHead = pars.Any(
pars.Seq('^', pars.Int).Child(1),
pars.Byte('^').Bind(0),
).Map(func(result *pars.Result) error {
n := result.Value.(int)
result.SetValue(Head(n))
return nil
})
var parseTail = pars.Any(
pars.Seq('$', pars.Int).Child(1),
pars.Byte('$').Bind(0),
).Map(func(result *pars.Result) error {
n := result.Value.(int)
result.SetValue(Tail(n))
return nil
})
func mapHeadTail(result *pars.Result) error {
p := int(result.Children[0].Value.(Head))
q := int(result.Children[2].Value.(Tail))
result.SetValue(HeadTail{p, q})
return nil
}
var parseHeadTail = pars.Seq(parseHead, "..", parseTail).Map(mapHeadTail)
func mapHeadHead(result *pars.Result) error {
p := int(result.Children[0].Value.(Head))
q := int(result.Children[2].Value.(Head))
result.SetValue(HeadHead{p, q})
return nil
}
var parseHeadHead = pars.Seq(parseHead, "..", parseHead).Map(mapHeadHead)
func mapTailTail(result *pars.Result) error {
p := int(result.Children[0].Value.(Tail))
q := int(result.Children[2].Value.(Tail))
result.SetValue(TailTail{p, q})
return nil
}
var parseTailTail = pars.Seq(parseTail, "..", parseTail).Map(mapTailTail)
var parseModifier = pars.Any(
parseHeadTail,
parseHeadHead,
parseTailTail,
parseHead,
parseTail,
)
// AsModifier interprets the given string as a Modifier.
func AsModifier(s string) (Modifier, error) {
result, err := pars.Exact(parseModifier).Parse(pars.FromString(s))
if err != nil {
return nil, err
}
return result.Value.(Modifier), nil
}