diff options
author | John Wiegley <johnw@newartisans.com> | 2008-07-22 03:48:35 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2008-07-22 03:48:35 -0400 |
commit | f0f2b34ea9db64bdabe65119d3fd265308e6d205 (patch) | |
tree | f574c98a695009a734a764991c095237c661334b | |
parent | 4bc29e1351faeba56cbfc1146fa1af33eba15f9d (diff) | |
download | ledger-f0f2b34ea9db64bdabe65119d3fd265308e6d205.tar.gz ledger-f0f2b34ea9db64bdabe65119d3fd265308e6d205.tar.bz2 ledger-f0f2b34ea9db64bdabe65119d3fd265308e6d205.zip |
More work toward getting my ledger data to parse.
-rw-r--r-- | amount.cc | 2 | ||||
-rw-r--r-- | journal.cc | 5 | ||||
-rw-r--r-- | parsexp.h | 61 | ||||
-rw-r--r-- | textual.cc | 56 | ||||
-rw-r--r-- | valexpr.cc | 6 |
5 files changed, 60 insertions, 70 deletions
@@ -695,7 +695,7 @@ int amount_t::sign() const bool amount_t::is_zero() const { if (! quantity) - throw_(amount_error, "Cannot determine sign if an uninitialized amount is zero"); + throw_(amount_error, "Cannot determine if an uninitialized amount is zero"); if (has_commodity()) { if (quantity->prec <= commodity().precision()) @@ -140,7 +140,8 @@ bool entry_base_t::finalize() balance += p; } - assert((*x)->amount); + assert(! (*x)->amount.is_null()); + if ((*x)->cost && (*x)->amount.commodity().annotated) { annotated_commodity_t& ann_comm(static_cast<annotated_commodity_t&> @@ -222,7 +223,7 @@ bool entry_base_t::finalize() for (transactions_list::const_iterator x = transactions.begin(); x != transactions.end(); x++) { - if ((*x)->amount || + if (! (*x)->amount.is_null() || ((*x)->has_flags(TRANSACTION_VIRTUAL) && ! (*x)->has_flags(TRANSACTION_BALANCE))) continue; @@ -194,29 +194,17 @@ private: ptr_op_t parse_value_expr(std::istream& in, scope_t& scope, const flags_t flags) const; - void parse_expr(std::istream& in, string& str, - scope_t& scope, const flags_t flags) { + value_expr& parse_expr(std::istream& in, string& str, + scope_t& scope, const flags_t flags) { try { ptr_op_t top_node = parse_value_expr(in, scope, flags); expr = value_expr(top_node, str); -#if 0 - // jww (2008-07-20): This code should no longer be needed, since we - // can't re-use parser_t anymore. if (use_lookahead) { use_lookahead = false; -#ifdef THREADSAFE lookahead.rewind(in); -#else - lookahead->rewind(in); -#endif } -#ifdef THREADSAFE lookahead.clear(); -#else - lookahead->clear(); -#endif -#endif } catch (error * err) { err->context.push_back @@ -224,44 +212,37 @@ private: "While parsing value expression:")); throw err; } + + return expr; } public: - parser_t(std::istream& in, const flags_t flags = EXPR_PARSE_RELAXED) - : use_lookahead(false) + parser_t() : use_lookahead(false) {} + + value_expr& parse(std::istream& in, + const flags_t flags = EXPR_PARSE_RELAXED) { - TRACE_CTOR(parser_t, "std::istream&, const flags_t"); - parse_expr(in, empty_string, *global_scope, flags); + return parse_expr(in, empty_string, *global_scope, flags); } - parser_t(std::istream& in, const flags_t flags = EXPR_PARSE_RELAXED, scope_t& scope) - : use_lookahead(false) + + value_expr& parse(std::istream& in, + const flags_t flags = EXPR_PARSE_RELAXED, + scope_t& scope) { - TRACE_CTOR(parser_t, "std::istream&, const flags_t, scope_t&"); - parse_expr(in, empty_string, scope, flags); + return parse_expr(in, empty_string, scope, flags); } - parser_t(string& str, const flags_t flags = EXPR_PARSE_RELAXED) - : use_lookahead(false) - { - TRACE_CTOR(parser_t, "string&, const flags_t"); - std::istringstream stream(str); - parse_expr(stream, str, *global_scope, flags); - } - parser_t(string& str, const flags_t flags = EXPR_PARSE_RELAXED, scope_t& scope) - : use_lookahead(false) + value_expr& parse(string& str, const flags_t flags = EXPR_PARSE_RELAXED) { - TRACE_CTOR(parser_t, "string&, const flags_t, scope_t&"); - std::istringstream stream(str); - parse_expr(stream, str, scope, flags); + return parse_expr(stream, str, *global_scope, flags); } - ~parser_t() throw() { - TRACE_DTOR(parser_t); - } - - operator value_expr() const { - return expr; + value_expr& parse(string& str, const flags_t flags = EXPR_PARSE_RELAXED, + scope_t& scope) + { + std::istringstream stream(str); + return parse_expr(stream, str, scope, flags); } }; @@ -40,44 +40,48 @@ struct time_entry_t { }; #endif -static value_expr parse_amount_expr(std::istream& in, amount_t& amount, - transaction_t * xact, - unsigned short flags = 0) -{ - value_expr expr(expr::parser_t(in, flags | EXPR_PARSE_RELAXED | - EXPR_PARSE_PARTIAL)); +namespace { + expr::parser_t amount_parser; - DEBUG("ledger.textual.parse", "line " << linenum << ": " << - "Parsed an amount expression"); + static value_expr parse_amount_expr(std::istream& in, amount_t& amount, + transaction_t * xact, + unsigned short flags = 0) + { + value_expr expr = + amount_parser.parse(in, flags | EXPR_PARSE_RELAXED | EXPR_PARSE_PARTIAL); + + DEBUG("ledger.textual.parse", "line " << linenum << ": " << + "Parsed an amount expression"); #ifdef DEBUG_ENABLED - DEBUG_IF("ledger.textual.parse") { - if (_debug_stream) { - ledger::dump_value_expr(*_debug_stream, expr); - *_debug_stream << std::endl; + DEBUG_IF("ledger.textual.parse") { + if (_debug_stream) { + ledger::dump_value_expr(*_debug_stream, expr); + *_debug_stream << std::endl; + } } - } #endif - if (! expr::compute_amount(expr, amount, xact)) - throw new parse_error("Amount expression failed to compute"); + if (! expr::compute_amount(expr, amount, xact)) + throw new parse_error("Amount expression failed to compute"); #if 0 - if (expr->kind == expr::node_t::VALUE) { - expr = NULL; - } else { - DEBUG_IF("ledger.textual.parse") { - std::cout << "Value expression tree:" << std::endl; - ledger::dump_value_expr(std::cout, expr.get()); + if (expr->kind == expr::node_t::VALUE) { + expr = NULL; + } else { + DEBUG_IF("ledger.textual.parse") { + std::cout << "Value expression tree:" << std::endl; + ledger::dump_value_expr(std::cout, expr.get()); + } } - } #else - expr = value_expr(); + expr = value_expr(); #endif - DEBUG("ledger.textual.parse", "line " << linenum << ": " << - "The transaction amount is " << xact->amount); - return expr; + DEBUG("ledger.textual.parse", "line " << linenum << ": " << + "The transaction amount is " << xact->amount); + return expr; + } } transaction_t * parse_transaction(char * line, account_t * account, @@ -1083,12 +1083,16 @@ value_t op_t::calc(scope_t& scope) } // namespace expr +namespace { + expr::parser_t value_expr_parser; +} + value_expr::value_expr(const string& _expr_str) : expr_str(_expr_str) { TRACE_CTOR(value_expr, "const string&"); if (! _expr_str.empty()) - ptr = expr::parser_t(expr_str).expr.ptr; + ptr = value_expr_parser.parse(expr_str).ptr; } } // namespace ledger |