Added parsing support for the many value directives

This commit is contained in:
John Wiegley 2012-03-07 05:20:42 -06:00
parent 69b25d367e
commit 76f97a63da
7 changed files with 74 additions and 5 deletions

View file

@ -67,6 +67,7 @@ public:
unsigned short depth;
accounts_map accounts;
posts_list posts;
optional<expr_t> value_expr;
mutable string _fullname;

View file

@ -322,10 +322,12 @@ bool journal_t::add_xact(xact_t * xact)
}
extend_xact(xact);
check_all_metadata(*this, xact);
foreach (post_t * post, xact->posts)
foreach (post_t * post, xact->posts) {
extend_post(*post, *this);
check_all_metadata(*this, post);
}
// If a transaction with this UUID has already been seen, simply do
// not add this one to the journal. However, all automated checks

View file

@ -127,16 +127,17 @@ public:
bool fixed_payees;
bool fixed_commodities;
bool fixed_metadata;
bool was_loaded;
bool force_checking;
bool check_payees;
payee_mappings_t payee_mappings;
account_mappings_t account_mappings;
accounts_map account_aliases;
account_mappings_t payees_for_unknown_accounts;
checksum_map_t checksum_map;
tag_check_exprs_map tag_check_exprs;
optional<expr_t> value_expr;
parse_context_t * current_context;
bool was_loaded;
bool force_checking;
bool check_payees;
enum checking_style_t {
CHECK_PERMISSIVE,

View file

@ -36,6 +36,7 @@
#include "account.h"
#include "journal.h"
#include "format.h"
#include "pool.h"
namespace ledger {
@ -638,6 +639,43 @@ void post_t::set_reported_account(account_t * acct)
acct->xdata().reported_posts.push_back(this);
}
void extend_post(post_t& post, journal_t& journal)
{
commodity_t& comm(post.amount.commodity());
annotation_t * details =
(comm.has_annotation() ?
&as_annotated_commodity(comm).details : NULL);
if (! details || ! details->value_expr) {
optional<expr_t> value_expr;
if (optional<value_t> data = post.get_tag(_("Value")))
value_expr = expr_t(data->to_string());
if (! value_expr)
value_expr = post.account->value_expr;
if (! value_expr)
value_expr = post.amount.commodity().value_expr();
if (! value_expr)
value_expr = journal.value_expr;
if (value_expr) {
if (! details) {
annotation_t new_details;
new_details.value_expr = value_expr;
commodity_t * new_comm =
commodity_pool_t::current_pool->find_or_create(comm, new_details);
post.amount.set_commodity(*new_comm);
} else {
details->value_expr = value_expr;
}
}
}
}
void to_xml(std::ostream& out, const post_t& post)
{
push_xml x(out, "posting", true);

View file

@ -259,6 +259,9 @@ private:
#endif // HAVE_BOOST_SERIALIZATION
};
class journal_t;
void extend_post(post_t& post, journal_t& journal);
void to_xml(std::ostream& out, const post_t& post);
} // namespace ledger

View file

@ -126,6 +126,7 @@ namespace {
void account_directive(char * line);
void account_alias_directive(account_t * account, string alias);
void account_payee_directive(account_t * account, string payee);
void account_value_directive(account_t * account, string expr_str);
void account_default_directive(account_t * account);
void default_account_directive(char * line);
@ -166,6 +167,7 @@ namespace {
void eval_directive(char * line);
void assert_directive(char * line);
void check_directive(char * line);
void value_directive(char * line);
void import_directive(char * line);
void python_directive(char * line);
@ -887,6 +889,9 @@ void instance_t::account_directive(char * line)
else if (keyword == "payee") {
account_payee_directive(account, b);
}
else if (keyword == "value") {
account_value_directive(account, b);
}
else if (keyword == "default") {
account_default_directive(account);
}
@ -974,6 +979,11 @@ void instance_t::account_default_directive(account_t * account)
context.journal->bucket = account;
}
void instance_t::account_value_directive(account_t * account, string expr_str)
{
account->value_expr = expr_t(expr_str);
}
void instance_t::payee_directive(char * line)
{
string payee = context.journal->register_payee(line, NULL);
@ -1108,6 +1118,11 @@ void instance_t::check_directive(char * line)
context.warning(STR(_("Check failed: %1") << line));
}
void instance_t::value_directive(char * line)
{
context.journal->value_expr = expr_t(line);
}
void instance_t::comment_directive(char * line)
{
while (in.good() && ! in.eof()) {
@ -1288,6 +1303,13 @@ bool instance_t::general_directive(char * line)
return true;
}
break;
case 'v':
if (std::strcmp(p, "value") == 0) {
value_directive(arg);
return true;
}
break;
}
if (expr_t::ptr_op_t op = lookup(symbol_t::DIRECTIVE, p)) {

View file

@ -779,6 +779,8 @@ void auto_xact_t::extend_xact(xact_base_t& xact, parse_context_t& context)
journal->register_account(account->fullname(), new_post,
journal->master);
extend_post(*new_post, *journal);
xact.add_post(new_post);
new_post->account->add_post(new_post);