-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
278 lines (227 loc) · 4.4 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
package fasthl7
import (
"fmt"
)
type tokenType int
const (
eof = byte(rune(0))
tokErr tokenType = iota
tokSegStart
tokSegEnd
tokFieldStart
tokFieldEnd
tokRepStart
tokRepEnd
tokCompStart
tokCompEnd
tokSubStart
tokSubEnd
tokEscapeStart
tokEOF
)
var tokString = [...]string{
tokErr: "error",
tokSegStart: "segment_start",
tokSegEnd: "segment_end",
tokFieldStart: "field_start",
tokFieldEnd: "field_end",
tokRepStart: "repitiion_start",
tokRepEnd: "repitition_end",
tokCompStart: "component_start",
tokSubStart: "subcomponent_start",
tokEscapeStart: "escape_start",
tokEOF: "EOF",
}
type token struct {
typ tokenType
val interface{}
}
func (t token) String() string {
switch t.typ {
case tokEOF:
return "EOF"
case tokErr:
return t.val.(error).Error()
}
return fmt.Sprintf("%s\n val: %v", tokString[t.typ], t.val)
}
type stateFn func(*parser) stateFn
type parser struct {
msg []byte
start int
pos int
width int
out chan token
delims *Delimiters
ds []byte
message Message
segment Segment
field Field
rep Repetition
comp Component
sub []byte
}
func parse(msg []byte) (*parser, chan token) {
p := &parser{
msg: msg,
out: make(chan token),
}
go p.run()
return p, p.out
}
func (p *parser) run() {
for state := msgStart(p); state != nil; {
state = state(p)
}
close(p.out)
}
func (p *parser) emit(t token) {
p.out <- t
p.start = p.pos
}
func (p *parser) next() byte {
if p.pos >= len(p.msg)-1 {
return eof
}
p.pos++
return p.msg[p.pos]
}
func (p *parser) nextDelim() byte {
for {
b := p.next()
for _, d := range p.ds {
if b == d {
return b
}
}
}
}
func (p *parser) ignore() {
p.start = p.pos
}
func (p *parser) backup() {
p.pos--
if p.start > p.pos {
p.start = p.pos
}
}
func (p *parser) current() byte {
return p.msg[p.pos]
}
func (p *parser) peek() byte {
n := p.next()
p.backup()
return n
}
func (p *parser) accept(valid byte) bool {
if valid == p.next() {
return true
}
p.backup()
return false
}
func (p *parser) acceptRun(valid byte) {
for valid == p.next() {
}
p.backup()
}
func (p *parser) errorf(f string, args ...interface{}) stateFn {
p.out <- token{
tokErr,
[]byte(fmt.Sprintf(f, args...)),
}
return nil
}
func (p *parser) commitBuffer(force bool) {
if p.sub != nil || force {
p.comp = append(p.comp, Subcomponent(p.sub))
p.sub = nil
}
}
func (p *parser) commitComp(force bool) {
p.commitBuffer(false)
if p.comp != nil || force {
p.rep = append(p.rep, p.comp)
p.comp = nil
}
}
func (p *parser) commitRep(force bool) {
p.commitComp(false)
if p.rep != nil || force {
p.field = append(p.field, p.rep)
p.rep = nil
}
}
func (p *parser) commitField(force bool) {
p.commitRep(false)
if p.field != nil || force {
p.segment = append(p.segment, p.field)
p.field = nil
}
}
func (p *parser) commitSeg(force bool) {
p.commitField(false)
if p.segment != nil || force {
p.message = append(p.message, p.segment)
p.emit(token{typ: tokSegEnd, val: p.segment[0][0][0][0]})
p.segment = nil
}
}
func msgStart(p *parser) stateFn {
return parseHeader
}
func parseHeader(p *parser) stateFn {
d, err := GetDelimiters(p.msg)
if err != nil {
return p.errorf("error parsing header: %v", err)
}
p.delims = d
p.ds = []byte{d.Escape, d.Subcomponent, d.Component, d.Repeat, d.Field, SegmentSeparator, eof}
p.segment = Segment{
Field{Repetition{Component{Subcomponent(p.msg[0:3])}}},
Field{Repetition{Component{Subcomponent([]byte{p.msg[3]})}}},
Field{Repetition{Component{Subcomponent(p.msg[4:8])}}},
}
p.start = 8
p.pos = 8
if !(p.current() == p.delims.Field) {
return p.errorf("error: invalid header expected %q, got %q", p.delims.Field, p.peek())
}
return parseMsg
}
func parseMsg(p *parser) stateFn {
nl := false
for c := p.next(); c != eof; {
//log.Printf("eval pos: %d val: %q", p.pos, c)
switch c {
case SegmentSeparator:
if !nl {
p.commitSeg(true)
}
nl = true
case '\n':
nl = true
case p.delims.Field:
nl = false
p.commitField(true)
case p.delims.Repeat:
nl = false
p.commitRep(true)
case p.delims.Component:
nl = false
p.commitComp(true)
case p.delims.Subcomponent:
nl = false
p.commitBuffer(true)
default:
nl = false
p.sub = append(p.sub, c)
}
c = p.next()
}
if p.sub != nil {
p.commitSeg(true)
}
p.emit(token{typ: tokEOF, val: p.message})
return nil
}