Enriched the context report during textual parsing

This commit is contained in:
John Wiegley 2009-02-26 05:42:59 -04:00
parent a0d8d84b87
commit b302c340b6

View file

@ -525,19 +525,27 @@ void instance_t::option_directive(char * line)
void instance_t::automated_xact_directive(char * line) void instance_t::automated_xact_directive(char * line)
{ {
istream_pos_type pos = line_beg_pos;
std::size_t lnum = linenum;
bool reveal_context = true;
try {
if (! auto_xact_finalizer.get()) { if (! auto_xact_finalizer.get()) {
auto_xact_finalizer.reset(new auto_xact_finalizer_t(&journal)); auto_xact_finalizer.reset(new auto_xact_finalizer_t(&journal));
journal.add_xact_finalizer(auto_xact_finalizer.get()); journal.add_xact_finalizer(auto_xact_finalizer.get());
} }
istream_pos_type pos = curr_pos;
std::size_t lnum = linenum;
std::auto_ptr<auto_xact_t> ae std::auto_ptr<auto_xact_t> ae
(new auto_xact_t(item_predicate(skip_ws(line + 1), (new auto_xact_t(item_predicate(skip_ws(line + 1),
keep_details_t(true, true, true)))); keep_details_t(true, true, true))));
reveal_context = false;
if (parse_posts(account_stack.front(), *ae.get())) { if (parse_posts(account_stack.front(), *ae.get())) {
reveal_context = true;
journal.auto_xacts.push_back(ae.get()); journal.auto_xacts.push_back(ae.get());
ae->pathname = pathname; ae->pathname = pathname;
@ -548,18 +556,35 @@ void instance_t::automated_xact_directive(char * line)
ae.release(); ae.release();
} }
}
catch (const std::exception& err) {
if (reveal_context) {
add_error_context(_("While parsing periodic transaction:"));
add_error_context(source_context(pathname, pos, curr_pos, "> "));
}
throw;
}
} }
void instance_t::period_xact_directive(char * line) void instance_t::period_xact_directive(char * line)
{ {
istream_pos_type pos = line_beg_pos;
std::size_t lnum = linenum;
bool reveal_context = true;
try {
std::auto_ptr<period_xact_t> pe(new period_xact_t(skip_ws(line + 1))); std::auto_ptr<period_xact_t> pe(new period_xact_t(skip_ws(line + 1)));
if (! pe->period) if (! pe->period)
throw_(parse_error, _("Parsing time period '%1'") << line); throw_(parse_error, _("Parsing time period '%1'") << line);
istream_pos_type pos = curr_pos; reveal_context = false;
std::size_t lnum = linenum;
if (parse_posts(account_stack.front(), *pe.get())) { if (parse_posts(account_stack.front(), *pe.get())) {
reveal_context = true;
if (pe->finalize()) { if (pe->finalize()) {
extend_xact_base(&journal, *pe.get(), true); extend_xact_base(&journal, *pe.get(), true);
@ -576,6 +601,15 @@ void instance_t::period_xact_directive(char * line)
throw parse_error(_("Period transaction failed to balance")); throw parse_error(_("Period transaction failed to balance"));
} }
} }
}
catch (const std::exception& err) {
if (reveal_context) {
add_error_context(_("While parsing periodic transaction:"));
add_error_context(source_context(pathname, pos, curr_pos, "> "));
}
throw;
}
} }
void instance_t::xact_directive(char * line, std::streamsize len) void instance_t::xact_directive(char * line, std::streamsize len)
@ -1043,17 +1077,21 @@ bool instance_t::parse_posts(account_t * account,
return added; return added;
} }
xact_t * instance_t::parse_xact(char * line, xact_t * instance_t::parse_xact(char * line,
std::streamsize len, std::streamsize len,
account_t * account) account_t * account)
{ {
TRACE_START(xact_text, 1, "Time spent parsing transaction text:"); TRACE_START(xact_text, 1, "Time spent parsing transaction text:");
std::auto_ptr<xact_t> curr(new xact_t); std::auto_ptr<xact_t> xact(new xact_t);
curr->pathname = pathname; xact->pathname = pathname;
curr->beg_pos = line_beg_pos; xact->beg_pos = line_beg_pos;
curr->beg_line = linenum; xact->beg_line = linenum;
bool reveal_context = true;
try {
// Parse the date // Parse the date
@ -1061,20 +1099,20 @@ xact_t * instance_t::parse_xact(char * line,
if (char * p = std::strchr(line, '=')) { if (char * p = std::strchr(line, '=')) {
*p++ = '\0'; *p++ = '\0';
curr->_date_eff = parse_date(p, current_year); xact->_date_eff = parse_date(p, current_year);
} }
curr->_date = parse_date(line, current_year); xact->_date = parse_date(line, current_year);
// Parse the optional cleared flag: * // Parse the optional cleared flag: *
if (next) { if (next) {
switch (*next) { switch (*next) {
case '*': case '*':
curr->_state = item_t::CLEARED; xact->_state = item_t::CLEARED;
next = skip_ws(++next); next = skip_ws(++next);
break; break;
case '!': case '!':
curr->_state = item_t::PENDING; xact->_state = item_t::PENDING;
next = skip_ws(++next); next = skip_ws(++next);
break; break;
} }
@ -1085,7 +1123,7 @@ xact_t * instance_t::parse_xact(char * line,
if (next && *next == '(') { if (next && *next == '(') {
if (char * p = std::strchr(next++, ')')) { if (char * p = std::strchr(next++, ')')) {
*p++ = '\0'; *p++ = '\0';
curr->code = next; xact->code = next;
next = skip_ws(p); next = skip_ws(p);
} }
} }
@ -1093,16 +1131,16 @@ xact_t * instance_t::parse_xact(char * line,
// Parse the description text // Parse the description text
if (next && *next) { if (next && *next) {
curr->payee = next; xact->payee = next;
next = next_element(next, true); next = next_element(next, true);
} else { } else {
curr->payee = _("<Unspecified payee>"); xact->payee = _("<Unspecified payee>");
} }
// Parse the xact note // Parse the xact note
if (next && *next == ';') if (next && *next == ';')
curr->append_note(next, current_year); xact->append_note(next, current_year);
TRACE_STOP(xact_text, 1); TRACE_STOP(xact_text, 1);
@ -1124,26 +1162,39 @@ xact_t * instance_t::parse_xact(char * line,
if (last_post) if (last_post)
item = last_post; item = last_post;
else else
item = curr.get(); item = xact.get();
// This is a trailing note, and possibly a metadata info tag // This is a trailing note, and possibly a metadata info tag
item->append_note(p + 1, current_year); item->append_note(p + 1, current_year);
item->end_pos = curr_pos; item->end_pos = curr_pos;
item->end_line++; item->end_line++;
} } else {
else if (post_t * post = parse_post(p, len - (p - line), account, reveal_context = false;
curr.get())) {
curr->add_post(post); if (post_t * post =
last_post = post; parse_post(p, len - (p - line), account, xact.get())) {
xact->add_post(post);
last_post = post;
}
} }
} }
curr->end_pos = curr_pos; xact->end_pos = curr_pos;
curr->end_line = linenum; xact->end_line = linenum;
TRACE_STOP(xact_details, 1); TRACE_STOP(xact_details, 1);
return curr.release(); return xact.release();
}
catch (const std::exception& err) {
if (reveal_context) {
add_error_context(_("While parsing transaction:"));
add_error_context(source_context(xact->pathname,
xact->beg_pos, curr_pos, "> "));
}
throw;
}
} }
expr_t::ptr_op_t instance_t::lookup(const string& name) expr_t::ptr_op_t instance_t::lookup(const string& name)