-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
170 lines (148 loc) · 4.45 KB
/
parser.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
package nmea
// FIXME add ignore trailing data discipline
// FIXME communicate whether checksum was valid, invalid, or absent
import (
"bytes"
"errors"
"fmt"
"regexp"
)
type ChecksumDiscipline int
const (
ChecksumDisciplineStrict ChecksumDiscipline = 0
ChecksumDisciplineRequire ChecksumDiscipline = 1
ChecksumDisciplineIgnore ChecksumDiscipline = 2
ChecksumDisciplineNever ChecksumDiscipline = 3
)
type LineEndingDiscipline int
const (
LineEndingDisciplineStrict LineEndingDiscipline = 0
LineEndingDisciplineRequire LineEndingDiscipline = 1
LineEndingDisciplineIgnore LineEndingDiscipline = 2
LineEndingDisciplineNever LineEndingDiscipline = 3
)
var (
sentenceRx = regexp.MustCompile(`\A\$([^$*]+)\*(.{2})?(\r?\n)?\z`)
errFraming = errors.New("framing error")
errInvalidLineEnding = errors.New("invalid line ending")
errMissingChecksum = errors.New("missing checksum")
errMissingLineEnding = errors.New("missing line ending")
errUnexpectedChecksum = errors.New("unexpected checksum")
errUnexpectedLineEnding = errors.New("unexpected line ending")
)
type InvalidChecksumError struct {
Expected byte
Got byte
}
func (e InvalidChecksumError) Error() string {
return fmt.Sprintf("invalid checksum: expected %02X, got %02X", e.Expected, e.Got)
}
type Parser struct {
checksumDiscipline ChecksumDiscipline
lineEndingDiscipline LineEndingDiscipline
sentenceParserFuncs []func(string) SentenceParser
}
type ParserOption func(*Parser)
func WithChecksumDiscipline(checksumDiscipline ChecksumDiscipline) ParserOption {
return func(p *Parser) {
p.checksumDiscipline = checksumDiscipline
}
}
func WithLineEndingDiscipline(lineEndingDiscipline LineEndingDiscipline) ParserOption {
return func(p *Parser) {
p.lineEndingDiscipline = lineEndingDiscipline
}
}
func WithSentenceParserFunc(sentenceParserFunc func(string) SentenceParser) ParserOption {
return func(p *Parser) {
p.sentenceParserFuncs = append(p.sentenceParserFuncs, sentenceParserFunc)
}
}
func WithSentenceParserMap(sentenceParserMap SentenceParserMap) ParserOption {
return func(p *Parser) {
sentenceParserFunc := func(addr string) SentenceParser {
return sentenceParserMap[addr]
}
p.sentenceParserFuncs = append(p.sentenceParserFuncs, sentenceParserFunc)
}
}
func NewParser(options ...ParserOption) *Parser {
p := &Parser{
checksumDiscipline: ChecksumDisciplineStrict,
lineEndingDiscipline: LineEndingDisciplineStrict,
}
for _, option := range options {
option(p)
}
return p
}
func (p *Parser) Parse(data []byte) (Sentence, error) {
m := sentenceRx.FindSubmatch(data)
if m == nil {
return nil, errFraming
}
var checksum Optional[byte]
if len(m[2]) != 0 {
hexDigit1, _ := hexDigitValue(m[2][0])
hexDigit2, _ := hexDigitValue(m[2][1])
checksum = NewOptional(16*byte(hexDigit1) + byte(hexDigit2))
}
// FIXME add lax checksum checking which returns any checksum error but also the parsed sentence
// FIXME add hex check checksum that at least ensures that the checksum contains hex digits
var calculatedChecksumValue byte
for _, c := range m[1] {
calculatedChecksumValue ^= c
}
switch p.checksumDiscipline {
case ChecksumDisciplineStrict:
switch {
case !checksum.Valid:
return nil, errMissingChecksum
case checksum.Value != calculatedChecksumValue:
return nil, InvalidChecksumError{
Expected: calculatedChecksumValue,
Got: checksum.Value,
}
}
case ChecksumDisciplineRequire:
if !checksum.Valid {
return nil, errMissingChecksum
}
case ChecksumDisciplineIgnore:
// Do nothing.
case ChecksumDisciplineNever:
if checksum.Valid {
return nil, errUnexpectedChecksum
}
}
switch p.lineEndingDiscipline {
case LineEndingDisciplineStrict:
if !bytes.Equal(m[3], []byte{'\r', '\n'}) {
return nil, errInvalidLineEnding
}
case LineEndingDisciplineRequire:
if len(m[3]) == 0 {
return nil, errMissingLineEnding
}
case LineEndingDisciplineIgnore:
// Do nothing.
case LineEndingDisciplineNever:
if len(m[3]) != 0 {
return nil, errUnexpectedLineEnding
}
}
tokenizer := NewTokenizer(m[1])
address := tokenizer.String()
if err := tokenizer.Err(); err != nil {
return nil, err
}
for _, sentenceParserFunc := range p.sentenceParserFuncs {
if sentenceParser := sentenceParserFunc(address); sentenceParser != nil {
return sentenceParser(address, tokenizer)
}
}
return ParseUnknown(address, tokenizer)
}
func (p *Parser) ParseString(s string) (Sentence, error) {
return p.Parse([]byte(s))
}