summaryrefslogtreecommitdiffhomepage
path: root/mintages/parser.go
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-08-09 15:02:51 +0200
committerThomas Voss <mail@thomasvoss.com> 2024-08-09 15:02:51 +0200
commit8a945da2cf0b2634305a9df88b0711f44968d0af (patch)
treee2b0e237a91a85b3dee51012fb39e7f6ec907405 /mintages/parser.go
parentb47d9aaf6c6d352cb017176690a6bd621e685387 (diff)
Move SyntaxError into parser.go
Diffstat (limited to 'mintages/parser.go')
-rw-r--r--mintages/parser.go31
1 files changed, 24 insertions, 7 deletions
diff --git a/mintages/parser.go b/mintages/parser.go
index 1651115..18e7f33 100644
--- a/mintages/parser.go
+++ b/mintages/parser.go
@@ -12,6 +12,17 @@ import (
"unicode"
)
+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 coinset [8]int
type Data struct {
@@ -29,7 +40,7 @@ func ForCountry(code string) (Data, error) {
return parse(f, path)
}
-func parse(reader io.Reader, path string) (Data, error) {
+func parse(reader io.Reader, file string) (Data, error) {
var (
data Data // Our data struct
slice *[]coinset // Where to append mintages
@@ -48,7 +59,8 @@ func parse(reader io.Reader, path string) (Data, error) {
return Data{}, SyntaxError{
expected: "single argument to ‘BEGIN’",
got: fmt.Sprintf("%d arguments", len(tokens)-1),
- location: location{path, linenr},
+ file: file,
+ linenr: linenr,
}
}
@@ -66,7 +78,8 @@ func parse(reader io.Reader, path string) (Data, error) {
return Data{}, SyntaxError{
expected: "‘CIRC’, ‘BU’, ‘PROOF’, or a year",
got: arg,
- location: location{path, linenr},
+ file: file,
+ linenr: linenr,
}
}
data.StartYear, _ = strconv.Atoi(arg)
@@ -77,13 +90,15 @@ func parse(reader io.Reader, path string) (Data, error) {
return Data{}, SyntaxError{
expected: "coin type declaration",
got: tokens[0],
- location: location{path, linenr},
+ file: file,
+ linenr: linenr,
}
case data.StartYear == 0:
return Data{}, SyntaxError{
expected: "start year declaration",
got: tokens[0],
- location: location{path, linenr},
+ file: file,
+ linenr: linenr,
}
}
@@ -98,7 +113,8 @@ func parse(reader io.Reader, path string) (Data, error) {
return Data{}, SyntaxError{
expected: fmt.Sprintf("%d mintage entries", numcoins),
got: fmt.Sprintf("%d %s", tokcnt, word),
- location: location{path, linenr},
+ file: file,
+ linenr: linenr,
}
}
@@ -115,7 +131,8 @@ func parse(reader io.Reader, path string) (Data, error) {
return Data{}, SyntaxError{
expected: "‘BEGIN’ directive or mintage row",
got: fmt.Sprintf("invalid token ‘%s’", tokens[0]),
- location: location{path, linenr},
+ file: file,
+ linenr: linenr,
}
}
}