-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
59 lines (54 loc) · 1.53 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
package hedi
import (
"errors"
"io"
)
var (
// ErrSegmentIdentifierExpected is returned when a segment identifier is expected but not found.
ErrSegmentIdentifierExpected = errors.New("segment identifier expected")
// ErrElementExpected is returned when an element is expected but not found.
ErrElementExpected = errors.New("element expected")
)
// Parser encapsulates the parsing logic for EDI files.
type Parser struct {
reader io.Reader
}
// NewParser creates a new Parser instance with the given io.Reader.
func NewParser(reader io.Reader) *Parser {
return &Parser{
reader: reader,
}
}
// Segments reads from the underlying reader and converts the token stream into Segments.
// It returns an error if the token stream does not conform to the expected structure.
func (p *Parser) Segments() (Segments, error) {
lexer := NewLexer(p.reader)
tokens, err := lexer.Tokens()
if err != nil {
return nil, err
}
segments := Segments{}
for _, token := range tokens {
switch token.Type {
case SegmentIdentifier:
segments = append(segments, *NewSegment(token.Value))
case ElementValue:
lastSegment, ok := segments.Last()
if !ok {
return nil, ErrSegmentIdentifierExpected
}
lastSegment.AddElement(Element{Value: token.Value})
case SubElementValue:
lastSegment, ok := segments.Last()
if !ok {
return nil, ErrSegmentIdentifierExpected
}
lastElement, ok := lastSegment.Elements.Last()
if !ok {
return nil, ErrElementExpected
}
lastElement.AddSubElement(token.Value)
}
}
return segments, nil
}