-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
424 lines (360 loc) · 12.7 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright (c) 2024 Michael D Henderson. All rights reserved.
package tndocx
import (
"bytes"
"errors"
"github.com/playbymail/tndocx/docx"
"regexp"
"time"
)
func ParseReport(filename string, sections []*Section) (*Report, error) {
if len(sections) == 0 {
return nil, ErrEmptyInput
}
report := &Report{
FileName: filename,
Units: make(map[string]*Unit),
}
report.Meta.GeneratedBy = "tn3"
report.Meta.Version = version.String()
report.Meta.Timestamp = time.Now().UTC().Unix()
for _, section := range sections {
if section.Header == nil {
return nil, ErrMissingElementHeader
}
}
return report, nil
}
//func parseUnit(section *Section) (*Unit, error) {
// if section.Header != nil {
// return nil, ErrMissingElementHeader
// }
// // element header should contain four comma separated fields
// var unit *Unit
// fields := bytes.Split(section.Header, []byte{','})
//
// // the code that created the section thinks that it found a section,
// // which means we should have at least an element in the header.
// element := bytes.TrimSpace(fields[0])
// if match := rxCourierHeader.FindSubmatch(section.Header); match != nil {
// unit = &Unit{
// Id: string(match[1]),
// }
// } else if match = rxElementHeader.FindSubmatch(section.Header); match != nil {
// unit = &Unit{
// Id: string(match[1]),
// }
// } else if match = rxFleetHeader.FindSubmatch(section.Header); match != nil {
// unit = &Unit{
// Id: string(match[1]),
// }
// } else if match = rxGarrisonHeader.FindSubmatch(section.Header); match != nil {
// unit = &Unit{
// Id: string(match[1]),
// }
// } else if match = rxTribeHeader.FindSubmatch(section.Header); match != nil {
// unit = &Unit{
// Id: string(match[1]),
// }
// } else {
// // this should not happen.
// unit = &Unit{
// Id: fmt.Sprintf("unit-%03d", section.Id),
// Input: string(section.Header),
// }
// }
// if len(fields) > 1 {
// name = bytes.TrimSpace(fields[1])
// }
// if len(fields) > 2 {
// currentHex = bytes.TrimSpace(fields[2])
// }
// if len(fields) > 3 {
// previousHex = bytes.TrimSpace(fields[3])
// }
// if len(fields) > 4 {
// // this should never happen
// }
//
// return unit, nil
//}
// element header should contain four comma separated fields
func parseElementHeader(elementHeader []byte) *Node {
root := &Node{Kind: "element-header"}
if len(elementHeader) == 0 {
root.Error = ErrMissingElementHeader
root.Input = string(elementHeader)
}
fields := bytes.Split(elementHeader, []byte{','})
element := &Node{Kind: "element-id"}
// the code that created the section thinks that it found a section,
// which means we should have at least an element in the header.
field := bytes.TrimSpace(fields[0])
if match := rxUnitCourier.FindSubmatch(field); match != nil {
element.Value = string(match[1])
} else if match = rxUnitElement.FindSubmatch(field); match != nil {
element.Value = string(match[1])
} else if match = rxUnitFleet.FindSubmatch(field); match != nil {
element.Value = string(match[1])
} else if match = rxUnitGarrison.FindSubmatch(field); match != nil {
element.Value = string(match[1])
} else if match = rxUnitTribe.FindSubmatch(field); match != nil {
element.Value = string(match[1])
} else {
// this should not happen.
// there should always be a match for one of the above regexes.
// there's a bug in the code that created the section.
element.Error = ErrInvalidElementId
element.Input = string(field)
}
root.Children = append(root.Children, element)
name := &Node{Kind: "name"}
if len(fields) < 1 {
name.Error = ErrMissingField
} else {
field = bytes.TrimSpace(fields[1])
name.Value = string(field)
}
root.Children = append(root.Children, name)
currentHex := &Node{Kind: "current-hex"}
if len(fields) < 2 {
currentHex.Error = ErrMissingField
} else {
field = bytes.TrimSpace(fields[2])
}
root.Children = append(root.Children, currentHex)
previousHex := &Node{Kind: "previous-hex"}
if len(fields) < 3 {
previousHex.Error = ErrMissingField
} else {
field = bytes.TrimSpace(fields[3])
}
root.Children = append(root.Children, previousHex)
if len(fields) > 4 {
root.Children = append(root.Children, &Node{
Kind: "extra-input",
Error: ErrUnexpectedInput,
Input: string(bytes.Join(fields[4:], []byte{','})),
})
}
return root
}
func ParseSections(input []byte) ([]*Section, error) {
if len(input) == 0 {
return nil, ErrEmptyInput
}
sections, err := ParseDocx(input)
if err != nil && errors.Is(ErrUnknownFormat, err) {
sections, err = ParseText(input)
}
return sections, err
}
func isascii(b byte) bool {
return 0 < b && b <= 127
}
func ParseDocx(input []byte) ([]*Section, error) {
if docx.DetectWordDocType(input) != docx.Docx {
return nil, ErrUnknownFormat
}
// extract the text from the Word document
text, err := docx.ReadBuffer(input)
if err != nil {
return nil, err
}
return ParseText(text)
}
func ParseText(input []byte) ([]*Section, error) {
if !(len(input) > 3 && isascii(input[0]) && isascii(input[1]) && isascii(input[2])) {
return nil, ErrUnknownFormat
}
// bug: have to force the entire file to lower case
input = bytes.ToLower(input)
// compress spaces within the input
input = CompressSpaces(input)
sections := SectionInput(input)
//log.Printf("sections %8d bytes into %d sections\n", len(input), len(sections))
for _, section := range sections {
section.Moves.Movement = scrubMovementLine(section.Moves.Movement)
//if len(section.Moves.Movement) != 0 {
// log.Printf("section %3d: %s\n", section.Id, section.Moves.Movement)
//}
section.Moves.Follows = scrubFollowsLine(section.Moves.Follows)
section.Moves.GoesTo = scrubGoesToLine(section.Moves.GoesTo)
section.Moves.Fleet = scrubFleetLine(section.Moves.Fleet)
for n, line := range section.Moves.Scouts {
section.Moves.Scouts[n] = scrubScoutLine(line)
//log.Printf("section %3d: %s\n", section.Id, section.Moves.Scouts[n])
}
section.Status = scrubStatusLine(section.Status)
//if len(section.Status) != 0 {
// log.Printf("section %3d: %s\n", section.Id, section.Status)
//}
}
return sections, nil
}
// scrubFleetLine does some pre-processing on the fleet line.
func scrubFleetLine(line []byte) []byte {
if len(line) == 0 {
return line
}
// replace backslash+dash with backslash
line = reBackslashDash.ReplaceAll(line, []byte{'\\'})
// replace backslash+comma and comma+backslash with backslash
line = reBackslashComma.ReplaceAll(line, []byte{'\\'})
line = reCommaBackslash.ReplaceAll(line, []byte{'\\'})
// fix issues with backslash or direction followed by a unit ID
line = reBackslashUnit.ReplaceAll(line, []byte{',', '$', '1'})
line = reDirectionUnit.ReplaceAll(line, []byte{'$', '1', ',', '$', '2'})
// reduce runs of certain punctuation to a single punctuation character
line = reRunOfBackslashes.ReplaceAll(line, []byte{'\\'})
line = reRunOfComma.ReplaceAll(line, []byte{','})
// tweak the fleet movement to remove the trailing comma from the observations
line = bytes.ReplaceAll(line, []byte{',', ')'}, []byte{')'})
// remove all trailing backslashes from the line
line = bytes.TrimRight(line, "\\")
return line
}
// scrubFollowsLine does some pre-processing on the follows line.
func scrubFollowsLine(line []byte) []byte {
if len(line) == 0 {
return line
}
// remove the "tribe follows" prefix
line = bytes.TrimSpace(bytes.TrimPrefix(line, []byte("tribe follows")))
// return the pre-processed line
return line
}
// scrubGoesToLine does some pre-processing on the goes to line.
func scrubGoesToLine(line []byte) []byte {
if len(line) == 0 {
return line
}
// return the pre-processed line
return line
}
// scrubMovementLine does some pre-processing on the movement line.
func scrubMovementLine(line []byte) []byte {
if len(line) == 0 {
return line
}
// replace backslash+dash with backslash
line = reBackslashDash.ReplaceAll(line, []byte{'\\'})
// replace backslash+comma and comma+backslash with backslash
line = reBackslashComma.ReplaceAll(line, []byte{'\\'})
line = reCommaBackslash.ReplaceAll(line, []byte{'\\'})
// fix issues with backslash or direction followed by a unit ID
line = reBackslashUnit.ReplaceAll(line, []byte{',', '$', '1'})
line = reDirectionUnit.ReplaceAll(line, []byte{'$', '1', ',', '$', '2'})
// reduce runs of certain punctuation to a single punctuation character
line = reRunOfBackslashes.ReplaceAll(line, []byte{'\\'})
line = reRunOfComma.ReplaceAll(line, []byte{','})
line = scrubStepResults(line)
// remove all trailing backslashes from the line
line = bytes.TrimRight(line, "\\")
return line
}
// scrubScoutLine does some pre-processing on the scout line.
func scrubScoutLine(line []byte) []byte {
if len(line) == 0 {
return line
}
// replace backslash+dash with backslash
line = reBackslashDash.ReplaceAll(line, []byte{'\\'})
// replace backslash+comma and comma+backslash with backslash
line = reBackslashComma.ReplaceAll(line, []byte{'\\'})
line = reCommaBackslash.ReplaceAll(line, []byte{'\\'})
// fix issues with backslash or direction followed by a unit ID
line = reBackslashUnit.ReplaceAll(line, []byte{',', '$', '1'})
line = reDirectionUnit.ReplaceAll(line, []byte{'$', '1', ',', '$', '2'})
// reduce runs of certain punctuation to a single punctuation character
line = reRunOfBackslashes.ReplaceAll(line, []byte{'\\'})
line = reRunOfComma.ReplaceAll(line, []byte{','})
line = scrubStepResults(line)
// remove all trailing backslashes from the line
line = bytes.TrimRight(line, "\\")
return line
}
// scrubStatusLine does some pre-processing on the status line.
func scrubStatusLine(line []byte) []byte {
if len(line) == 0 {
return line
}
// replace backslash+dash with backslash
line = reBackslashDash.ReplaceAll(line, []byte{'\\'})
// replace backslash+comma and comma+backslash with backslash
line = reBackslashComma.ReplaceAll(line, []byte{'\\'})
line = reCommaBackslash.ReplaceAll(line, []byte{'\\'})
// fix issues with backslash or direction followed by a unit ID
line = reBackslashUnit.ReplaceAll(line, []byte{',', '$', '1'})
line = reDirectionUnit.ReplaceAll(line, []byte{'$', '1', ',', '$', '2'})
// reduce runs of certain punctuation to a single punctuation character
line = reRunOfBackslashes.ReplaceAll(line, []byte{'\\'})
line = reRunOfComma.ReplaceAll(line, []byte{','})
line = scrubStepResults(line)
// remove all trailing backslashes from the line
line = bytes.TrimRight(line, "\\")
return line
}
// scrubStepResults processes a step to normalize commas and spaces around direction codes and unit IDs
//
// while len(step) != 0
//
// if step does not start with a comma
// advance step to the next character
// else if step starts with an edge code (hsm, lcm, ljm, lsm)
// advance step past the edge code
// while step starts with a comma or space followed by a direction code (n, s, ne, se, nw, sw)
// replace the comma with a space
// skip step past the space and the direction code
// else if step starts with a unit ID
// advance past the unit ID
// while step starts with a comma or space followed by a unit ID
// replace the comma with a space
// skip step past the space and the unit ID
// else
// advance skip to the next character
//
// return the line
func scrubStepResults(line []byte) []byte {
step := line
// advance to the first comma
for len(step) > 0 && step[0] != ',' {
step = step[1:]
}
// process all the results
for len(step) > 0 {
// does step start with an edge code?
if match := edgeCodePattern.Find(step); match != nil {
step = step[len(match):] // advance past the initial edge code
// process list of direction codes
for elementMatch := listDirectionPattern.Find(step); elementMatch != nil; elementMatch = listDirectionPattern.Find(step) {
step[0] = ' ' // replace comma with space
step = step[len(elementMatch):] // skip past the matched direction code
}
continue
}
// does step starts with a unit ID?
if match := unitIDPattern.Find(step); match != nil {
step = step[len(match):] // advance past the initial unit ID
// process list of unit IDs
for elementMatch := listUnitIDPattern.Find(step); elementMatch != nil; elementMatch = listUnitIDPattern.Find(step) {
step[0] = ' ' // replace comma with space
step = step[len(elementMatch):] // skip past the matched unit ID
}
continue
}
// no matches, so advance to the next comma
step = step[1:]
for len(step) != 0 && step[0] != ',' {
step = step[1:]
}
}
return line
}
var (
// Regular expressions for edge codes, unit IDs, and lists of directions and units
edgeCodePattern = regexp.MustCompile(`^,(hsm|l|lcm|ljm|lsm|o)\b`)
unitIDPattern = regexp.MustCompile(`^,\d{4}([cefg]\d)?\b`)
listDirectionPattern = regexp.MustCompile(`^[,\s]([ns][ew]?)\b`)
listUnitIDPattern = regexp.MustCompile(`^[,\s]\d{4}([cefg]\d)?\b`)
)