diff options
author | John Wiegley <johnw@newartisans.com> | 2014-12-29 16:55:50 -0600 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2014-12-29 16:55:50 -0600 |
commit | dafb99df0d2a57ae012f7b39726c3f7ef9ffd3bf (patch) | |
tree | 5a72f6f1deabd7140806c2ede802a279cb5b5756 /src | |
parent | 66ce4ac48f88b81eab1a2c04eeef83ee4fa06335 (diff) | |
parent | ba35340ca9c7351beab0ff9fa7a58591eb6ad39f (diff) | |
download | fork-ledger-dafb99df0d2a57ae012f7b39726c3f7ef9ffd3bf.tar.gz fork-ledger-dafb99df0d2a57ae012f7b39726c3f7ef9ffd3bf.tar.bz2 fork-ledger-dafb99df0d2a57ae012f7b39726c3f7ef9ffd3bf.zip |
Merge pull request #356 from afh/pull/1038
Fix bug 1038; Improve error handling when parsing year directives
Diffstat (limited to 'src')
-rw-r--r-- | src/textual.cc | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/src/textual.cc b/src/textual.cc index b2f19be4..8007ca0d 100644 --- a/src/textual.cc +++ b/src/textual.cc @@ -285,6 +285,11 @@ void instance_t::parse() } } + if (apply_stack.front().value.type() == typeid(optional<datetime_t>)) + epoch = boost::get<optional<datetime_t> >(apply_stack.front().value); + + apply_stack.pop_front(); + #if defined(TIMELOG_SUPPORT) timelog.close(); #endif // TIMELOG_SUPPORT @@ -419,7 +424,9 @@ void instance_t::read_next_directive(bool& error_flag) price_xact_directive(line); break; case 'Y': // set the current year - apply_year_directive(line + 1); + if (std::strlen(line+1) == 0) + throw_(parse_error, _f("Directive '%1%' requires an argument") % line[0]); + apply_year_directive(line+1); break; } } @@ -863,14 +870,17 @@ void instance_t::apply_rate_directive(char * line) void instance_t::apply_year_directive(char * line) { - apply_stack.push_front(application_t("year", epoch)); - - // This must be set to the last day of the year, otherwise partial - // dates like "11/01" will refer to last year's november, not the - // current year. - unsigned short year(lexical_cast<unsigned short>(skip_ws(line))); - DEBUG("times.epoch", "Setting current year to " << year); - epoch = datetime_t(date_t(year, 12, 31)); + try { + unsigned short year(lexical_cast<unsigned short>(skip_ws(line))); + apply_stack.push_front(application_t("year", epoch)); + DEBUG("times.epoch", "Setting current year to " << year); + // This must be set to the last day of the year, otherwise partial + // dates like "11/01" will refer to last year's november, not the + // current year. + epoch = datetime_t(date_t(year, 12, 31)); + } catch(bad_lexical_cast &) { + throw_(parse_error, _f("Argument '%1%' not a valid year") % skip_ws(line)); + } } void instance_t::end_apply_directive(char * kind) @@ -1375,6 +1385,13 @@ bool instance_t::general_directive(char * line) return true; } break; + + case 'y': + if (std::strcmp(p, "year") == 0) { + apply_year_directive(arg); + return true; + } + break; } if (expr_t::ptr_op_t op = lookup(symbol_t::DIRECTIVE, p)) { |