summaryrefslogtreecommitdiff
path: root/src/scope.h
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-11-04 20:40:07 -0500
committerJohn Wiegley <johnw@newartisans.com>2009-11-04 20:40:48 -0500
commit78e6770c4c276db3647952f21a6bf3ea465edb88 (patch)
tree64190d80ea0c3933dffaee3cf0e43cde5ea0e6a6 /src/scope.h
parent4a14f3224b9063202ca39a67c9aff42ae4274942 (diff)
downloadfork-ledger-78e6770c4c276db3647952f21a6bf3ea465edb88.tar.gz
fork-ledger-78e6770c4c276db3647952f21a6bf3ea465edb88.tar.bz2
fork-ledger-78e6770c4c276db3647952f21a6bf3ea465edb88.zip
Segregated symbols into 5 separate namespaces
The different namespaces are: Function Value expression functions, which receive a "context" Option Command-line options Precommand Commands which are invoked before reading the journal Command Commands which are invoked after reading the journal Directive Directives that occur at column 0 in a data file This greatly eases the ability for Python uses to add intercept hooks to change how the basic Ledger module functions. An example of what should be possible soon: import ledger def my_foo_handler(value): print "--foo received:", value ledger.add_handler(ledger.Option, "foo=", my_foo_handler)
Diffstat (limited to 'src/scope.h')
-rw-r--r--src/scope.h93
1 files changed, 78 insertions, 15 deletions
diff --git a/src/scope.h b/src/scope.h
index 36eb54f1..7cf89c3e 100644
--- a/src/scope.h
+++ b/src/scope.h
@@ -55,6 +55,61 @@ namespace ledger {
*
* Long.
*/
+struct symbol_t
+{
+ enum kind_t {
+ UNKNOWN,
+ FUNCTION,
+ OPTION,
+ PRECOMMAND,
+ COMMAND,
+ DIRECTIVE
+ };
+
+ kind_t kind;
+ string name;
+ expr_t::ptr_op_t definition;
+
+ symbol_t() : kind(UNKNOWN), name(""), definition(NULL) {
+ TRACE_CTOR(symbol_t, "");
+ }
+ symbol_t(kind_t _kind, string _name, expr_t::ptr_op_t _definition = NULL)
+ : kind(_kind), name(_name), definition(_definition) {
+ TRACE_CTOR(symbol_t, "symbol_t::kind_t, string");
+ }
+ symbol_t(const symbol_t& sym)
+ : kind(sym.kind), name(sym.name),
+ definition(sym.definition) {
+ TRACE_CTOR(symbol_t, "copy");
+ }
+ ~symbol_t() throw() {
+ TRACE_DTOR(symbol_t);
+ }
+
+ bool operator<(const symbol_t& sym) const {
+ return kind < sym.kind || name < sym.name;
+ }
+
+#if defined(HAVE_BOOST_SERIALIZATION)
+private:
+ /** Serialization. */
+
+ friend class boost::serialization::access;
+
+ template<class Archive>
+ void serialize(Archive& ar, const unsigned int /* version */) {
+ ar & kind;
+ ar & name;
+ ar & definition;
+ }
+#endif // HAVE_BOOST_SERIALIZATION
+};
+
+/**
+ * @brief Brief
+ *
+ * Long.
+ */
class scope_t
{
public:
@@ -65,8 +120,10 @@ public:
TRACE_DTOR(scope_t);
}
- virtual void define(const string&, expr_t::ptr_op_t) {}
- virtual expr_t::ptr_op_t lookup(const string& name) = 0;
+ virtual void define(const symbol_t::kind_t, const string&,
+ expr_t::ptr_op_t) {}
+ virtual expr_t::ptr_op_t lookup(const symbol_t::kind_t kind,
+ const string& name) = 0;
#if defined(HAVE_BOOST_SERIALIZATION)
private:
@@ -100,14 +157,16 @@ public:
TRACE_DTOR(child_scope_t);
}
- virtual void define(const string& name, expr_t::ptr_op_t def) {
+ virtual void define(const symbol_t::kind_t kind,
+ const string& name, expr_t::ptr_op_t def) {
if (parent)
- parent->define(name, def);
+ parent->define(kind, name, def);
}
- virtual expr_t::ptr_op_t lookup(const string& name) {
+ virtual expr_t::ptr_op_t lookup(const symbol_t::kind_t kind,
+ const string& name) {
if (parent)
- return parent->lookup(name);
+ return parent->lookup(kind, name);
return NULL;
}
@@ -132,7 +191,7 @@ private:
*/
class symbol_scope_t : public child_scope_t
{
- typedef std::map<const string, expr_t::ptr_op_t> symbol_map;
+ typedef std::map<symbol_t, expr_t::ptr_op_t> symbol_map;
symbol_map symbols;
@@ -147,9 +206,11 @@ public:
TRACE_DTOR(symbol_scope_t);
}
- virtual void define(const string& name, expr_t::ptr_op_t def);
+ virtual void define(const symbol_t::kind_t kind, const string& name,
+ expr_t::ptr_op_t def);
- virtual expr_t::ptr_op_t lookup(const string& name);
+ virtual expr_t::ptr_op_t lookup(const symbol_t::kind_t kind,
+ const string& name);
#if defined(HAVE_BOOST_SERIALIZATION)
private:
@@ -259,15 +320,17 @@ public:
TRACE_DTOR(bind_scope_t);
}
- virtual void define(const string& name, expr_t::ptr_op_t def) {
- parent->define(name, def);
- grandchild.define(name, def);
+ virtual void define(const symbol_t::kind_t kind, const string& name,
+ expr_t::ptr_op_t def) {
+ parent->define(kind, name, def);
+ grandchild.define(kind, name, def);
}
- virtual expr_t::ptr_op_t lookup(const string& name) {
- if (expr_t::ptr_op_t def = grandchild.lookup(name))
+ virtual expr_t::ptr_op_t lookup(const symbol_t::kind_t kind,
+ const string& name) {
+ if (expr_t::ptr_op_t def = grandchild.lookup(kind, name))
return def;
- return child_scope_t::lookup(name);
+ return child_scope_t::lookup(kind, name);
}
#if defined(HAVE_BOOST_SERIALIZATION)