-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
59 lines (53 loc) · 1.13 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 coin
import (
"bufio"
"bytes"
"fmt"
"io"
)
type Parser struct {
*bufio.Scanner
finished bool
lineNr uint
}
type Item interface {
}
func NewParser(r io.Reader) *Parser {
p := &Parser{Scanner: bufio.NewScanner(r)}
p.Scan()
return p
}
func (p *Parser) Scan() bool {
p.finished = !p.Scanner.Scan()
if !p.finished {
p.lineNr++
}
return !p.finished
}
func (p *Parser) Next(fn string) (Item, error) {
if p.finished {
return nil, p.Err()
}
switch line := p.Bytes(); {
case len(bytes.TrimSpace(line)) == 0:
p.Scan()
return p.Next(fn)
case bytes.ContainsAny(line[:1], ";#%|*"):
p.Scan()
return p.Next(fn)
case bytes.HasPrefix(line, []byte("include")):
return p.parseInclude(fn)
case bytes.HasPrefix(line, []byte("account")):
return p.parseAccount(fn)
case bytes.HasPrefix(line, []byte("commodity ")):
return p.parseCommodity(fn)
case bytes.HasPrefix(line, []byte("test ")):
return p.parseTest(fn)
case bytes.HasPrefix(line, []byte("P ")):
return p.parsePrice(fn)
case '0' <= line[0] && line[0] <= '9':
return p.parseTransaction(fn)
default:
return nil, fmt.Errorf("unrecognized item: %s", line)
}
}