summaryrefslogtreecommitdiffhomepage
path: root/lib/mintage/parser.go
blob: 364b6e870016865bc8e7a2ba1dd1f4031bb7e275 (plain) (blame)
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
package mintage

import (
	"bufio"
	"fmt"
	"io"
	"strconv"
	"strings"
	"time"
)

type SyntaxError struct {
	expected, got string
	file          string
	linenr        int
}

func (e SyntaxError) Error() string {
	return fmt.Sprintf("%s:%d: syntax error: expected %s but got %s",
		e.file, e.linenr, e.expected, e.got)
}

type Data struct {
	Standard      []SRow
	Commemorative []CRow
}

type SRow struct {
	Year     int
	Mintmark string
	Mintages [typeCount][denoms]int
}

type CRow struct {
	Year     int
	Mintmark string
	Name     string
	Mintage  [typeCount]int
}

const (
	TypeCirc = iota
	TypeNIFC
	TypeProof
	typeCount
)

const (
	Unknown = -iota - 1
	Invalid
)

const (
	denoms = 8
	ws     = " \t"
)

func Parse(r io.Reader, file string) (Data, error) {
	yearsSince := time.Now().Year() - 1999 + 1
	data := Data{
		Standard:      make([]SRow, 0, yearsSince),
		Commemorative: make([]CRow, 0, yearsSince),
	}

	scanner := bufio.NewScanner(r)
	for linenr := 1; scanner.Scan(); linenr++ {
		line := strings.Trim(scanner.Text(), ws)
		if isBlankOrComment(line) {
			continue
		}

		if len(line) < 4 || !isNumeric(line[:4], false) {
			return Data{}, SyntaxError{
				expected: "4-digit year",
				got:      line,
				linenr:   linenr,
				file:     file,
			}
		}

		var (
			commem   bool
			mintmark string
		)
		year, _ := strconv.Atoi(line[:4])
		line = line[4:]

		if len(line) != 0 {
			if strings.ContainsRune(ws, rune(line[0])) {
				commem = true
				goto out
			}
			if line[0] != '-' {
				return Data{}, SyntaxError{
					expected: "end-of-line or mintmark",
					got:      line,
					linenr:   linenr,
					file:     file,
				}
			}

			if line = line[1:]; len(line) == 0 {
				return Data{}, SyntaxError{
					expected: "mintmark",
					got:      "end-of-line",
					linenr:   linenr,
					file:     file,
				}
			}

			switch i := strings.IndexAny(line, ws); i {
			case 0:
				return Data{}, SyntaxError{
					expected: "mintmark",
					got:      "whitespace",
					linenr:   linenr,
					file:     file,
				}
			case -1:
				mintmark = line
			default:
				mintmark, line = line[:i], line[i:]
				commem = true
			}
		}
	out:

		if !commem {
			row := SRow{
				Year:     year,
				Mintmark: mintmark,
			}
			for i := range row.Mintages {
				line = ""
				for isBlankOrComment(line) {
					if !scanner.Scan() {
						return Data{}, SyntaxError{
							expected: "mintage row",
							got:      "end-of-file",
							linenr:   linenr,
							file:     file,
						}
					}
					line = strings.Trim(scanner.Text(), ws)
					linenr++
				}

				tokens := strings.FieldsFunc(line, func(r rune) bool {
					return strings.ContainsRune(ws, r)
				})
				if tokcnt := len(tokens); tokcnt != denoms {
					word := "entries"
					if tokcnt == 1 {
						word = "entry"
					}
					return Data{}, SyntaxError{
						expected: fmt.Sprintf("%d mintage entries", denoms),
						got:      fmt.Sprintf("%d %s", tokcnt, word),
						linenr:   linenr,
						file:     file,
					}
				}

				for j, tok := range tokens {
					if tok != "?" && !isNumeric(tok, true) {
						return Data{}, SyntaxError{
							expected: "numeric mintage figure or ‘?’",
							got:      tok,
							linenr:   linenr,
							file:     file,
						}
					}

					if tok == "?" {
						row.Mintages[i][j] = Unknown
					} else {
						row.Mintages[i][j] = atoiWithDots(tok)
					}
				}
			}

			data.Standard = append(data.Standard, row)
		} else {
			row := CRow{
				Year:     year,
				Mintmark: mintmark,
			}
			line = strings.TrimLeft(line, ws)
			if line[0] != '"' {
				return Data{}, SyntaxError{
					expected: "string",
					got:      line,
					linenr:   linenr,
					file:     file,
				}
			}

			line = line[1:]
			switch i := strings.IndexByte(line, '"'); i {
			case -1:
				return Data{}, SyntaxError{
					expected: "closing quote",
					got:      "end-of-line",
					linenr:   linenr,
					file:     file,
				}
			case 0:
				return Data{}, SyntaxError{
					expected: "commemorated event",
					got:      "empty string",
					linenr:   linenr,
					file:     file,
				}
			default:
				row.Name, line = line[:i], line[i+1:]
			}

			if len(line) != 0 {
				return Data{}, SyntaxError{
					expected: "end-of-line",
					got:      line,
					linenr:   linenr,
					file:     file,
				}
			}

			for isBlankOrComment(line) {
				if !scanner.Scan() {
					return Data{}, SyntaxError{
						expected: "mintage row",
						got:      "end-of-file",
						linenr:   linenr,
						file:     file,
					}
				}
				line = strings.Trim(scanner.Text(), ws)
				linenr++
			}

			tokens := strings.FieldsFunc(line, func(r rune) bool {
				return strings.ContainsRune(ws, r)
			})
			if tokcnt := len(tokens); tokcnt != typeCount {
				word := "entries"
				if tokcnt == 1 {
					word = "entry"
				}
				return Data{}, SyntaxError{
					expected: fmt.Sprintf("%d mintage entries", typeCount),
					got:      fmt.Sprintf("%d %s", tokcnt, word),
					linenr:   linenr,
					file:     file,
				}
			}

			for i, tok := range tokens {
				if tok == "?" {
					row.Mintage[i] = Unknown
				} else {
					row.Mintage[i] = atoiWithDots(tok)
				}
			}

			data.Commemorative = append(data.Commemorative, row)
		}
	}

	return data, nil
}

func isNumeric(s string, dot bool) bool {
	for _, ch := range s {
		switch ch {
		case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
		default:
			if ch != '.' || !dot {
				return false
			}
		}
	}
	return true
}

func atoiWithDots(s string) int {
	n := 0
	for _, ch := range s {
		if ch == '.' {
			continue
		}
		n = n*10 + int(ch) - '0'
	}
	return n
}

func isBlankOrComment(s string) bool {
	return len(s) == 0 || s[0] == '#'
}