Changed the way scopes are structured for an upcoming design change.

This commit is contained in:
John Wiegley 2008-07-29 18:57:02 -04:00
parent d073a7e8a5
commit 200d919fe7
12 changed files with 29 additions and 3632 deletions

View file

@ -351,7 +351,7 @@ class account_t
account_t(account_t * _parent = NULL,
const string& _name = "",
const optional<string> _note = none)
const optional<string>& _note = none)
: parent(_parent), name(_name), note(_note),
depth(parent ? parent->depth + 1 : 0), data(NULL), ident(0) {
TRACE_CTOR(account_t, "account_t *, const string&, const string&");
@ -502,10 +502,10 @@ public:
{
public:
parser_t() {
TRACE_CTOR(parser_t, "");
TRACE_CTOR(journal_t::parser_t, "");
}
virtual ~parser_t() {
TRACE_DTOR(parser_t);
TRACE_DTOR(journal_t::parser_t);
}
virtual bool test(std::istream& in) const = 0;

1067
parsexp.cc

File diff suppressed because it is too large Load diff

235
parsexp.h
View file

@ -1,235 +0,0 @@
/*
* Copyright (c) 2003-2008, John Wiegley. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of New Artisans LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _PARSEXP_H
#define _PARSEXP_H
#include "valexpr.h"
namespace ledger {
namespace expr {
DECLARE_EXCEPTION(error, parse_error);
class parser_t : public noncopyable
{
#define EXPR_PARSE_NORMAL 0x00
#define EXPR_PARSE_PARTIAL 0x01
#define EXPR_PARSE_RELAXED 0x02
#define EXPR_PARSE_NO_MIGRATE 0x04
#define EXPR_PARSE_NO_REDUCE 0x08
#define EXPR_PARSE_NO_ASSIGN 0x10
#define EXPR_PARSE_NO_DATES 0x20
public:
typedef uint_least8_t flags_t;
private:
struct token_t : public noncopyable
{
enum kind_t {
VALUE, // any kind of literal value
SHORT_ACCOUNT_MASK,
CODE_MASK,
COMMODITY_MASK,
PAYEE_MASK,
NOTE_MASK,
ACCOUNT_MASK,
IDENT, // [A-Za-z_][-A-Za-z0-9_:]*
DOLLAR, // $
AT_SYM, // @
DOT, // .
DOTDOT, // ..
SLASH, // /
LPAREN, // (
RPAREN, // )
LBRACKET, // [
RBRACKET, // ]
EQUAL, // =
NEQUAL, // !=
LESS, // <
LESSEQ, // <=
GREATER, // >
GREATEREQ, // >=
MINUS, // -
PLUS, // +
STAR, // *
KW_DIV,
EXCLAM, // !
KW_AND,
KW_OR,
KW_MOD,
COMMA, // ,
TOK_EOF,
UNKNOWN
} kind;
char symbol[3];
value_t value;
std::size_t length;
explicit token_t() : kind(UNKNOWN), length(0) {
TRACE_CTOR(token_t, "");
}
~token_t() throw() {
TRACE_DTOR(token_t);
}
token_t& operator=(const token_t& other) {
if (&other == this)
return *this;
assert(false);
return *this;
}
void clear() {
kind = UNKNOWN;
length = 0;
value = NULL_VALUE;
symbol[0] = '\0';
symbol[1] = '\0';
symbol[2] = '\0';
}
void parse_ident(std::istream& in);
void next(std::istream& in, flags_t flags);
void rewind(std::istream& in);
void unexpected();
static void unexpected(char c, char wanted = '\0');
};
mutable token_t lookahead;
mutable bool use_lookahead;
token_t& next_token(std::istream& in, flags_t tflags) const
{
if (use_lookahead)
use_lookahead = false;
else
lookahead.next(in, tflags);
return lookahead;
}
void push_token(const token_t& tok) const
{
assert(&tok == &lookahead);
use_lookahead = true;
}
void push_token() const
{
use_lookahead = true;
}
ptr_op_t parse_value_term(std::istream& in, scope_t& scope,
const flags_t flags) const;
ptr_op_t parse_unary_expr(std::istream& in, scope_t& scope,
const flags_t flags) const;
ptr_op_t parse_mul_expr(std::istream& in, scope_t& scope,
const flags_t flags) const;
ptr_op_t parse_add_expr(std::istream& in, scope_t& scope,
const flags_t flags) const;
ptr_op_t parse_logic_expr(std::istream& in, scope_t& scope,
const flags_t flags) const;
ptr_op_t parse_and_expr(std::istream& in, scope_t& scope,
const flags_t flags) const;
ptr_op_t parse_or_expr(std::istream& in, scope_t& scope,
const flags_t flags) const;
ptr_op_t parse_querycolon_expr(std::istream& in, scope_t& scope,
const flags_t flags) const;
ptr_op_t parse_value_expr(std::istream& in, scope_t& scope,
const flags_t flags) const;
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);
if (use_lookahead) {
use_lookahead = false;
lookahead.rewind(in);
}
lookahead.clear();
return value_expr(top_node, str);
}
catch (error * err) {
err->context.push_back
(new line_context(str, (long)in.tellg() - 1,
"While parsing value expression:"));
throw err;
}
}
public:
parser_t() : use_lookahead(false) {}
value_expr parse(std::istream& in,
const flags_t flags = EXPR_PARSE_RELAXED)
{
return parse_expr(in, empty_string, *global_scope, flags);
}
value_expr parse(std::istream& in, scope_t& scope,
const flags_t flags = EXPR_PARSE_RELAXED)
{
return parse_expr(in, empty_string, scope, flags);
}
value_expr parse(string& str, const flags_t flags = EXPR_PARSE_RELAXED)
{
std::istringstream stream(str);
return parse_expr(stream, str, *global_scope, flags);
}
value_expr parse(string& str, scope_t& scope,
const flags_t flags = EXPR_PARSE_RELAXED)
{
std::istringstream stream(str);
return parse_expr(stream, str, scope, flags);
}
};
} // namespace expr
} // namespace ledger
#endif // _PARESXP_H

View file

@ -86,9 +86,8 @@ struct python_run
}
};
python_interpreter_t::python_interpreter_t(expr_t::scope_t& parent)
: expr_t::symbol_scope_t(parent),
mmodule(borrowed(PyImport_AddModule("__main__"))),
python_interpreter_t::python_interpreter_t()
: scope_t(), mmodule(borrowed(PyImport_AddModule("__main__"))),
nspace(handle<>(borrowed(PyModule_GetDict(mmodule.get()))))
{
TRACE_CTOR(python_interpreter_t, "expr_t::scope_t&");

View file

@ -39,8 +39,7 @@
namespace ledger {
class python_interpreter_t
: public noncopyable, public expr_t::symbol_scope_t
class python_interpreter_t : public noncopyable, public scope_t
{
boost::python::handle<> mmodule;
@ -95,8 +94,7 @@ public:
virtual expr_t::ptr_op_t lookup(const string& name) {
if (boost::python::object func = eval(name))
return WRAP_FUNCTOR(functor_t(name, func));
else
return expr_t::symbol_scope_t::lookup(name);
return expr_t::ptr_op_t();
}
class lambda_t : public functor_t {

View file

@ -453,7 +453,7 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
break;
}
return symbol_scope_t::lookup(name);
return session.lookup(name);
}
} // namespace ledger

View file

@ -81,7 +81,7 @@ namespace ledger {
// says that the formatter should be "flushed" after the entities are
// iterated. This does not happen for the commodities iteration, however.
class report_t : public symbol_scope_t
class report_t : public noncopyable, public scope_t
{
report_t();
@ -134,9 +134,7 @@ public:
session_t& session;
explicit report_t(session_t& _session)
: symbol_scope_t(downcast<scope_t>(_session)),
head_entries(0),
: head_entries(0),
tail_entries(0),
show_collapsed(false),

224
scope.h
View file

@ -37,36 +37,17 @@
namespace ledger {
class scope_t : public noncopyable
class scope_t
{
scope_t();
protected:
enum type_t {
CHILD_SCOPE,
SYMBOL_SCOPE,
CALL_SCOPE,
CONTEXT_SCOPE
} type_;
public:
explicit scope_t(type_t _type) : type_(_type) {
TRACE_CTOR(scope_t, "type_t");
explicit scope_t() {
TRACE_CTOR(scope_t, "");
}
virtual ~scope_t() {
TRACE_DTOR(scope_t);
}
const type_t type() const {
return type_;
}
void define(const string& name, const value_t& val) {
define(name, expr_t::op_t::wrap_value(val));
}
void define(const string& name, const function_t& func) {
define(name, expr_t::op_t::wrap_functor(func));
}
virtual expr_t::ptr_op_t lookup(const string& name) = 0;
value_t resolve(const string& name) {
expr_t::ptr_op_t definition = lookup(name);
@ -75,81 +56,29 @@ public:
else
return NULL_VALUE;
}
virtual void define(const string& name, expr_t::ptr_op_t def) = 0;
virtual expr_t::ptr_op_t lookup(const string& name) = 0;
protected:
virtual optional<scope_t&> find_scope(const type_t _type,
bool skip_this = false) = 0;
virtual optional<scope_t&> find_first_scope(const type_t _type1,
const type_t _type2,
bool skip_this = false) = 0;
template <typename T>
T& find_scope(bool skip_this = false) {
assert(false);
}
template <typename T>
optional<T&> maybe_find_scope(bool skip_this = false) {
assert(false);
}
friend class child_scope_t;
friend class expr_t::op_t;
};
class child_scope_t : public scope_t
class child_scope_t : public noncopyable, public scope_t
{
scope_t * parent;
child_scope_t();
public:
explicit child_scope_t(type_t _type = CHILD_SCOPE)
: scope_t(_type), parent(NULL) {
TRACE_CTOR(child_scope_t, "type_t");
explicit child_scope_t() : parent(NULL) {
TRACE_CTOR(child_scope_t, "");
}
explicit child_scope_t(scope_t& _parent, type_t _type = CHILD_SCOPE)
: scope_t(_type), parent(&_parent) {
TRACE_CTOR(child_scope_t, "scope_t&, type_t");
explicit child_scope_t(scope_t& _parent)
: parent(&_parent) {
TRACE_CTOR(child_scope_t, "scope_t&");
}
virtual ~child_scope_t() {
TRACE_DTOR(child_scope_t);
}
virtual void define(const string& name, expr_t::ptr_op_t def) {
if (parent)
parent->define(name, def);
}
virtual expr_t::ptr_op_t lookup(const string& name) {
if (parent)
return parent->lookup(name);
return expr_t::ptr_op_t();
}
protected:
virtual optional<scope_t&> find_scope(type_t _type,
bool skip_this = false) {
for (scope_t * ptr = (skip_this ? parent : this); ptr; ) {
if (ptr->type() == _type)
return *ptr;
ptr = polymorphic_downcast<child_scope_t *>(ptr)->parent;
}
return none;
}
virtual optional<scope_t&> find_first_scope(const type_t _type1,
const type_t _type2,
bool skip_this = false) {
for (scope_t * ptr = (skip_this ? parent : this); ptr; ) {
if (ptr->type() == _type1 || ptr->type() == _type2)
return *ptr;
ptr = polymorphic_downcast<child_scope_t *>(ptr)->parent;
}
return none;
}
};
class symbol_scope_t : public child_scope_t
@ -159,12 +88,10 @@ class symbol_scope_t : public child_scope_t
symbol_map symbols;
public:
explicit symbol_scope_t()
: child_scope_t(SYMBOL_SCOPE) {
explicit symbol_scope_t() {
TRACE_CTOR(symbol_scope_t, "");
}
explicit symbol_scope_t(scope_t& _parent)
: child_scope_t(_parent, SYMBOL_SCOPE) {
explicit symbol_scope_t(scope_t& _parent) : child_scope_t(_parent) {
TRACE_CTOR(symbol_scope_t, "scope_t&");
}
virtual ~symbol_scope_t() {
@ -172,13 +99,13 @@ public:
}
void define(const string& name, const value_t& val) {
scope_t::define(name, val);
define(name, expr_t::op_t::wrap_value(val));
}
void define(const string& name, const function_t& func) {
scope_t::define(name, func);
define(name, expr_t::op_t::wrap_functor(func));
}
virtual void define(const string& name, expr_t::ptr_op_t def);
virtual expr_t::ptr_op_t lookup(const string& name);
};
@ -189,8 +116,7 @@ class call_scope_t : public child_scope_t
call_scope_t();
public:
explicit call_scope_t(scope_t& _parent)
: child_scope_t(_parent, CALL_SCOPE) {
explicit call_scope_t(scope_t& _parent) : child_scope_t(_parent) {
TRACE_CTOR(call_scope_t, "scope_t&");
}
virtual ~call_scope_t() {
@ -250,124 +176,6 @@ public:
T * operator->() { return value; }
};
#if 0
class context_scope_t : public child_scope_t
{
public:
value_t current_element;
std::size_t element_index;
std::size_t sequence_size;
explicit context_scope_t(scope_t& _parent,
const value_t& _element = NULL_VALUE,
const std::size_t _element_index = 0,
const std::size_t _sequence_size = 0)
: child_scope_t(_parent, CONTEXT_SCOPE), current_element(_element),
element_index(_element_index), sequence_size(_sequence_size)
{
TRACE_CTOR(expr::context_scope_t, "scope_t&, const value_t&, ...");
}
virtual ~context_scope_t() {
TRACE_DTOR(expr::context_scope_t);
}
const std::size_t index() const {
return element_index;
}
const std::size_t size() const {
return sequence_size;
}
value_t& value() {
return current_element;
}
};
struct context_t
{
const entry_t * entry() {
return NULL;
}
const transaction_t * xact() {
return NULL;
}
const account_t * account() {
return NULL;
}
};
struct entry_context_t : public context_t
{
const entry_t * entry_;
const entry_t * entry() {
return entry_;
}
};
struct xact_context_t : public context_t
{
const transaction_t * xact_;
const entry_t * entry() {
return xact_->entry;
}
const transaction_t * xact() {
return xact_;
}
const account_t * account() {
return xact_->account;
}
};
struct account_context_t : public context_t
{
const account_t * account_;
const account_t * account() {
return account_;
}
};
#endif
template<>
inline symbol_scope_t&
scope_t::find_scope<symbol_scope_t>(bool skip_this) {
optional<scope_t&> scope = find_scope(SYMBOL_SCOPE, skip_this);
assert(scope);
return downcast<symbol_scope_t>(*scope);
}
template<>
inline call_scope_t&
scope_t::find_scope<call_scope_t>(bool skip_this) {
optional<scope_t&> scope = find_scope(CALL_SCOPE, skip_this);
assert(scope);
return downcast<call_scope_t>(*scope);
}
#if 0
template<>
inline context_scope_t&
scope_t::find_scope<context_scope_t>(bool skip_this) {
optional<scope_t&> scope = find_scope(CONTEXT_SCOPE, skip_this);
assert(scope);
return downcast<context_scope_t>(*scope);
}
#endif
#define FIND_SCOPE(scope_type, scope_ref) \
downcast<scope_t>(scope_ref).find_scope<scope_type>()
#define CALL_SCOPE(scope_ref) \
FIND_SCOPE(call_scope_t, scope_ref)
#define SYMBOL_SCOPE(scope_ref) \
FIND_SCOPE(symbol_scope_t, scope_ref)
#if 0
#define CONTEXT_SCOPE(scope_ref) \
FIND_SCOPE(context_scope_t, scope_ref)
#endif
} // namespace ledger
#endif // _SCOPE_H

View file

@ -67,9 +67,7 @@ void release_session_context()
}
session_t::session_t()
: symbol_scope_t(),
register_format
: register_format
("%D %-.20P %-.22A %12.67t %!12.80T\n%/"
"%32|%-.22A %12.67t %!12.80T\n"),
wide_register_format
@ -328,7 +326,7 @@ expr_t::ptr_op_t session_t::lookup(const string& name)
break;
}
return symbol_scope_t::lookup(name);
return expr_t::ptr_op_t();
}
// jww (2007-04-26): All of Ledger should be accessed through a

View file

@ -37,7 +37,7 @@
namespace ledger {
class session_t : public symbol_scope_t
class session_t : public noncopyable, public scope_t
{
static void initialize();
static void shutdown();

1095
valexpr.cc

File diff suppressed because it is too large Load diff

1007
valexpr.h

File diff suppressed because it is too large Load diff