-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
244 lines (215 loc) · 6.34 KB
/
parse.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package lege
import (
"bufio"
"errors"
"fmt"
"io"
"unicode/utf8"
)
// ParseOptions are options passed to a parser
type ParseOptions struct {
Boundaries []Boundary
}
// Boundary are boundaries to use when collecting strings
type Boundary struct {
Start string
End string
}
func (options *ParseOptions) maxStartLength() (max int) {
for _, s := range options.getAllStarts() {
if l := utf8.RuneCountInString(s); l > max {
max = l
}
}
return max
}
func (options *ParseOptions) maxEndLength() (max int) {
for _, s := range options.getAllEnds() {
if l := utf8.RuneCountInString(s); l > max {
max = l
}
}
return max
}
func (options *ParseOptions) getAllStarts() []string {
starts := make([]string, 0)
for _, boundary := range options.Boundaries {
starts = append(starts, boundary.Start)
}
return starts
}
func (options *ParseOptions) getAllEnds() []string {
ends := make([]string, 0)
for _, boundary := range options.Boundaries {
ends = append(ends, boundary.End)
}
return ends
}
func (options *ParseOptions) getCorrespondingBoundary(start string) *Boundary {
for _, boundary := range options.Boundaries {
if start == boundary.Start {
return &boundary
}
}
return nil
}
func (options *ParseOptions) mustGetCorrespondingBoundary(start string) *Boundary {
b := options.getCorrespondingBoundary(start)
if b == nil {
panic(fmt.Sprintf("boundary not found for start: %s", start))
}
return b
}
// Validate checks the parse options and returns an error if they are invalid
func (options *ParseOptions) Validate() error {
if options.Boundaries == nil {
return errors.New("must supply boundary options")
}
allBoundaries := options.Boundaries
allStarts := options.getAllStarts()
allEnds := options.getAllEnds()
if len(allBoundaries) == 0 {
return errors.New("must supply at least one boundary")
}
if len(allStarts) == 0 {
return errors.New("must supply at least one start string")
}
if len(allEnds) == 0 {
return errors.New("must supply at least one end string")
}
for _, start := range allStarts {
if start == "" {
return errors.New("start cannot be an empty string")
}
}
for _, end := range allEnds {
if end == "" {
return errors.New("end cannot be an empty string")
}
}
for _, start := range allStarts {
if boundary := options.getCorrespondingBoundary(start); boundary == nil { // TODO reinspect this check, not sure if it makes sense
return fmt.Errorf("start boundary %q must have a corresponding end boundary", start)
}
}
return nil
}
// Parser is used to parse a source for collections to extract
type Parser struct {
options *ParseOptions
}
// NewParser creates a *Parser
func NewParser(options *ParseOptions) (*Parser, error) {
if options == nil {
return nil, errors.New("must supply options to parser")
}
err := options.Validate()
if err != nil {
return nil, err
}
parser := &Parser{options: options}
return parser, nil
}
// newWindow produces a window for a parser, from the boundary options
func (parser *Parser) newWindow() []rune {
maxStartLen := parser.options.maxStartLength()
maxEndLen := parser.options.maxEndLength()
windowSize := 0
if maxStartLen > maxEndLen {
windowSize = maxStartLen
} else {
windowSize = maxEndLen
}
return make([]rune, windowSize)
}
// windowMatchesString checks if the runes in the window are equivalent to a string
func (parser *Parser) windowMatchesString(window []rune, compareTo string) (bool, string) {
var winString string
runesInOption := utf8.RuneCountInString(compareTo)
if runesInOption < len(window) {
winString = string(window[len(window)-runesInOption:])
} else {
winString = string(window)
}
return compareTo == winString, winString
}
// Parse takes a reader
func (parser *Parser) Parse(reader io.Reader) (Collections, error) {
r := bufio.NewReader(reader)
window := parser.newWindow()
windowSize := len(window)
index := 0
lineCounter := 1
positionCounter := 1
collections := make(Collections, 0)
collecting := false
for {
c, _, err := r.ReadRune()
if err != nil {
if err == io.EOF {
if collecting { // if we're still collecting at the EOF, drop the last collection
collections = collections[:len(collections)-1]
}
break
} else {
return nil, err
}
}
// fmt.Printf("%q : %q : %v : %d : %d\n", string(window), c, collecting, lineCounter, positionCounter)
if index < windowSize { // the window needs to be initially populated
window[index] = c
index++
positionCounter++
continue
}
if !collecting { // if we're not collecting, we're looking for a start match
for _, boundary := range parser.options.Boundaries {
startOption := boundary.Start // find a match with any of the possible starts
if match, _ := parser.windowMatchesString(window, startOption); match { // if the window matches a start option
collecting = true // go into collecting mode
collections = append(collections, &Collection{
runes: []rune{c},
Boundary: boundary,
StartLocation: Location{
Line: lineCounter,
Pos: positionCounter,
},
}) // create a new collection, starting with this rune
break
}
}
} else { // if we're collecting, we're looking for an end match and storing runes along the way
currentCollection := collections.getLast()
endOption := currentCollection.Boundary.End
if match, _ := parser.windowMatchesString(window, endOption); match { // if the window matches an end option
collecting = false // leave collecting mode
// if we're stopping collection, since the window trails the current index, we need to reslice the current collection to take off
// the runes we just matched
runeCount := utf8.RuneCountInString(endOption)
currentCollection.trimRightRunes(runeCount)
currentCollection.EndLocation = Location{
Line: lineCounter,
Pos: positionCounter - runeCount - 1,
}
}
if collecting {
currentCollection.addRune(c)
}
}
// shift the window by one rune
for i := range window {
if i == len(window)-1 { // if we're at the last spot in the window
window[i] = c // assign it to the current rune
} else { // otherwise, assign the current spot in the window to what's in the next spot
window[i] = window[i+1]
}
}
index++
positionCounter++
if string(c) == "\n" {
lineCounter++
positionCounter = 1
}
}
return collections, nil
}