howeyc / ledger

Command line double-entry accounting program
https://howeyc.github.io/ledger/
ISC License
455 stars 43 forks source link

Support including other ledger files #15

Closed kalafut closed 6 years ago

kalafut commented 6 years ago

Fixes #14

This began as a very compact Reader mod with no changes to the parser. Then I realized that approach would swallow up any accurate filename/line number information for errors, which are actually pretty important in this application. Things grew a bit in order to track the active location as one goes into and returns from files, but the intrusion into parser.go and the main.go files is pretty small.

I tried a few approaches. The strategy I settled on was to inject special comments into the reader stream for when the file/line location jumps due to include/return. The parser just has to detect this and update its file count.

I realize you're trying to keep this ledger app really simple but hopefully you'll be interested in bringing this in. Include support and commodities (which I'll tackle next) are the bigees for me to cut over from ledger-cli.

Thanks, Jim

howeyc commented 6 years ago

Looks good.

I'm curious, what's the error message look like when you have an error in an included file? Does it show the path of the file where the error occurred, or the root file.

Also, I just glanced at this, can you nest includes a few levels deep with this?

kalafut commented 6 years ago

The error messages will always show the actual filename (whether root or included) and correct line number where the error was detected. Format is the same as what you had. My first cut which didn't do that (and everything was relative to the root... not very useful), was like 15 lines of code. Price to pay for this clarity 🙂.

Yes, includes can be nested, and cycles will be detected and error out. The entire tree is parsed at the start so such errors (cycles, include files missing) will be reported before the parser even gets the Reader.

I planned to add some tests against error conditions and such but didn't want to get too far down the track before giving you a look.