Added support for tag/pop parsing directives

There are now "tag/pop" directives, to apply metadata to a range of
transactions (and their postings).  For example, if you wanted a
conceptual "page" of transactions relating to business trip to Chicago,
you could do this:

    tag Location: Chicago
    tag Purpose: Business

    ... transactions go here

    pop
    pop

It would be as if you'd applied "; Location: Chicago", etc., to every
transaction.
This commit is contained in:
John Wiegley 2009-02-26 05:49:26 -04:00
parent 47ff0b9fed
commit 8216db1f15

View file

@ -46,6 +46,7 @@ namespace {
public: public:
std::list<account_t *>& account_stack; std::list<account_t *>& account_stack;
std::list<string>& tag_stack;
#if defined(TIMELOG_SUPPORT) #if defined(TIMELOG_SUPPORT)
time_log_t& timelog; time_log_t& timelog;
#endif #endif
@ -71,6 +72,7 @@ namespace {
scoped_ptr<auto_xact_finalizer_t> auto_xact_finalizer; scoped_ptr<auto_xact_finalizer_t> auto_xact_finalizer;
instance_t(std::list<account_t *>& _account_stack, instance_t(std::list<account_t *>& _account_stack,
std::list<string>& _tag_stack,
#if defined(TIMELOG_SUPPORT) #if defined(TIMELOG_SUPPORT)
time_log_t& _timelog, time_log_t& _timelog,
#endif #endif
@ -111,6 +113,8 @@ namespace {
void account_directive(char * line); void account_directive(char * line);
void end_directive(char * line); void end_directive(char * line);
void alias_directive(char * line); void alias_directive(char * line);
void tag_directive(char * line);
void pop_directive(char * line);
void define_directive(char * line); void define_directive(char * line);
void general_directive(char * line); void general_directive(char * line);
@ -163,6 +167,7 @@ namespace {
} }
instance_t::instance_t(std::list<account_t *>& _account_stack, instance_t::instance_t(std::list<account_t *>& _account_stack,
std::list<string>& _tag_stack,
#if defined(TIMELOG_SUPPORT) #if defined(TIMELOG_SUPPORT)
time_log_t& _timelog, time_log_t& _timelog,
#endif #endif
@ -173,13 +178,13 @@ instance_t::instance_t(std::list<account_t *>& _account_stack,
const path * _original_file, const path * _original_file,
bool _strict, bool _strict,
instance_t * _parent) instance_t * _parent)
: account_stack(_account_stack), : account_stack(_account_stack), tag_stack(_tag_stack),
#if defined(TIMELOG_SUPPORT) #if defined(TIMELOG_SUPPORT)
timelog(_timelog), timelog(_timelog),
#endif #endif
parent(_parent), in(_in), session_scope(_session_scope), parent(_parent), in(_in), session_scope(_session_scope),
journal(_journal), master(_master), journal(_journal), master(_master),
original_file(_original_file), strict(_strict) original_file(_original_file), strict(_strict)
{ {
TRACE_CTOR(instance_t, "..."); TRACE_CTOR(instance_t, "...");
@ -653,7 +658,7 @@ void instance_t::include_directive(char * line)
ifstream stream(filename); ifstream stream(filename);
instance_t instance(account_stack, instance_t instance(account_stack, tag_stack,
#if defined(TIMELOG_SUPPORT) #if defined(TIMELOG_SUPPORT)
timelog, timelog,
#endif #endif
@ -705,6 +710,20 @@ void instance_t::alias_directive(char * line)
} }
} }
void instance_t::tag_directive(char * line)
{
tag_stack.push_back(trim_ws(line));
}
void instance_t::pop_directive(char *)
{
if (tag_stack.empty())
throw_(std::runtime_error,
_("'pop' directive found, but no tags currently active"));
else
tag_stack.pop_back();
}
void instance_t::define_directive(char * line) void instance_t::define_directive(char * line)
{ {
expr_t def(skip_ws(line)); expr_t def(skip_ws(line));
@ -751,6 +770,20 @@ void instance_t::general_directive(char * line)
return; return;
} }
break; break;
case 'p':
if (std::strcmp(p, "pop") == 0) {
pop_directive(arg);
return;
}
break;
case 't':
if (std::strcmp(p, "tag") == 0) {
tag_directive(arg);
return;
}
break;
} }
// jww (2009-02-10): This needs some serious work. // jww (2009-02-10): This needs some serious work.
@ -1046,6 +1079,10 @@ post_t * instance_t::parse_post(char * line,
post->end_pos = curr_pos; post->end_pos = curr_pos;
post->end_line = linenum; post->end_line = linenum;
if (! tag_stack.empty())
foreach (const string& tag, tag_stack)
post->parse_tags(tag.c_str());
TRACE_STOP(post_details, 1); TRACE_STOP(post_details, 1);
return post.release(); return post.release();
@ -1186,6 +1223,10 @@ xact_t * instance_t::parse_xact(char * line,
xact->end_pos = curr_pos; xact->end_pos = curr_pos;
xact->end_line = linenum; xact->end_line = linenum;
if (! tag_stack.empty())
foreach (const string& tag, tag_stack)
xact->parse_tags(tag.c_str());
TRACE_STOP(xact_details, 1); TRACE_STOP(xact_details, 1);
return xact.release(); return xact.release();
@ -1215,11 +1256,12 @@ std::size_t journal_t::parse(std::istream& in,
TRACE_START(parsing_total, 1, "Total time spent parsing text:"); TRACE_START(parsing_total, 1, "Total time spent parsing text:");
std::list<account_t *> account_stack; std::list<account_t *> account_stack;
std::list<string> tag_stack;
#if defined(TIMELOG_SUPPORT) #if defined(TIMELOG_SUPPORT)
time_log_t timelog(*this); time_log_t timelog(*this);
#endif #endif
instance_t parsing_instance(account_stack, instance_t parsing_instance(account_stack, tag_stack,
#if defined(TIMELOG_SUPPORT) #if defined(TIMELOG_SUPPORT)
timelog, timelog,
#endif #endif