-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfirstpass.go
190 lines (166 loc) · 4.49 KB
/
firstpass.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
package main
import "fmt"
import "strings"
import "io/ioutil"
import "runtime/debug"
import "path/filepath"
import "github.com/prataprc/goparsec"
import "github.com/bnclabs/golog"
import "github.com/tn47/goledger/dblentry"
import "github.com/tn47/goledger/api"
import "github.com/tn47/goledger/reports"
func dofirstpass(
reporter api.Reporter,
db *dblentry.Datastore, journalfile string) (err error) {
data, err := ioutil.ReadFile(journalfile)
if err != nil {
log.Errorf("%v\n", err)
return err
}
if db.Hasjournal(data) {
log.Debugf("%q already processed !!\n", journalfile)
return nil
}
db.Addjournal(journalfile, data)
var lineno int
var block []string
var eof bool
defer func() {
if r := recover(); r != nil {
log.Errorf("panic at %q:%v : %v\n", journalfile, lineno, r)
log.Errorf("\n%s", api.GetStacktrace(2, debug.Stack()))
err = fmt.Errorf("%s", r)
}
}()
log.Debugf("firstpass %v\n", journalfile)
var node parsec.ParsecNode
lines, err := readlines(journalfile)
if err != nil {
return err
}
iterate := blockiterate(lines)
lineno, block, eof, err = iterate()
for len(block) > 0 {
lineno -= len(block)
if err != nil {
log.Errorf("iterate at %q:%v : %v\n", journalfile, lineno, err)
return err
}
log.Debugf("parsing block: %v\n", block[0])
node, lineno, err = parseentry(lineno, block, journalfile, db)
if err != nil {
log.Errorf("parsec at %q:%v : %v\n", journalfile, lineno, err)
return err
} else if node != nil {
if err := db.Firstpass(node); err != nil {
fmsg := "%T at %q:%v : %v\n"
log.Errorf(fmsg, node, journalfile, lineno-len(block)+1, err)
return err
}
}
tryinclude(reporter, db, node, journalfile)
lineno, block, eof, err = iterate()
}
if err != nil {
log.Errorf("%q : %v\n", journalfile, err)
} else if eof == false {
log.Errorf("%q : expected eof\n", journalfile)
}
return nil
}
func parseentry(
lineno int,
block []string,
journalfile string,
db *dblentry.Datastore) (parsec.ParsecNode, int, error) {
var err error
var index int
scanner := parsec.NewScanner([]byte(block[0]))
ytrans := dblentry.NewTransaction(journalfile).Yledger(db)
yprice := dblentry.NewPrice().Yledger(db)
ydirective := dblentry.NewDirective().Yledger(db)
ycomment := dblentry.NewComment().Yledger(db)
y := parsec.OrdChoice(
dblentry.Vector2scalar,
ytrans, yprice, ydirective, ycomment,
)
node, _ := y(scanner)
switch obj := node.(type) {
case *dblentry.Transaction:
obj.Addlines(block[0])
payee := strings.Trim(obj.Payee(), " \t")
if api.Options.Stitch && payee == reports.PayeeOpeningBalance {
return nil, lineno, nil
}
index, err = obj.Yledgerblock(db, block[1:])
lineno += 1 + index
obj.SetLineno(lineno)
obj.Addlines(block[1:]...)
case *dblentry.Directive:
index, err = obj.Yledgerblock(db, block[1:])
lineno += 1 + index
case error:
err = obj
case *dblentry.Comment, *dblentry.Price:
}
return node, lineno, err
}
func tryinclude(
reporter api.Reporter, db *dblentry.Datastore, node parsec.ParsecNode,
includedby string) bool {
if node == nil {
return false
}
d, ok := node.(*dblentry.Directive)
if ok && d.Type() == "include" {
journalfile := d.Includefile()
journalfile = strings.Trim(journalfile, "/")
journalfile = filepath.Join(filepath.Dir(includedby), journalfile)
reporter.Startjournal(journalfile, true /*included*/)
dofirstpass(reporter, db, journalfile)
return true
}
return false
}
func blockiterate(lines []string) func() (int, []string, bool, error) {
row := 0
parseblock := func() []string {
blocklines := []string{}
for ; row < len(lines); row++ {
line := lines[row]
if (len(line) > 0) && (line[0] == ' ' || line[0] == '\t') {
if line1 := strings.TrimLeft(line, " \t"); line1 == "" {
break
}
blocklines = append(blocklines, line)
} else {
break
}
}
return blocklines
}
return func() (int, []string, bool, error) {
blocklines := []string{}
for ; row < len(lines); row++ {
line := lines[row]
if len(line) == 0 {
continue
}
if line[0] == ' ' || line[0] == '\t' {
line1 := strings.TrimLeft(line, " \t")
if line1 == "" { // emptyline
continue
} else {
fmsg := "must be at the beginning: row:%v column: 0"
return row + 1, nil, false, fmt.Errorf(fmsg, row+1)
}
} else { // begin block
row++
blocklines = append(blocklines, line)
blocklines = append(blocklines, parseblock()...)
return row + 1, blocklines, row >= len(lines), nil
}
}
return row + 1, blocklines, true, nil
}
}