diff options
| author | Henrik Rydberg <rydberg@euromail.se> | 2011-10-08 20:30:28 +0200 |
|---|---|---|
| committer | Henrik Rydberg <rydberg@euromail.se> | 2011-10-08 20:30:28 +0200 |
| commit | 5df79c53745fde5d6c3340a2979b1429cd5892c1 (patch) | |
| tree | 1a81af141708b826e9c61e8a04019994fcca8298 /src/lang/token.h | |
Initial import of htcd system 1.0
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'src/lang/token.h')
| -rw-r--r-- | src/lang/token.h | 230 |
1 files changed, 230 insertions, 0 deletions
diff --git a/src/lang/token.h b/src/lang/token.h new file mode 100644 index 0000000..bf32820 --- /dev/null +++ b/src/lang/token.h | |||
| @@ -0,0 +1,230 @@ | |||
| 1 | /************************************************************************* | ||
| 2 | * | ||
| 3 | * HTCd - Copyright (C) 1998-2006 Henrik Rydberg | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License as published by | ||
| 7 | * the Free Software Foundation; either version 2 of the License, or | ||
| 8 | * (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this program; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #ifndef TOKENH | ||
| 21 | #define TOKENH | ||
| 22 | |||
| 23 | #include <ops/express.h> | ||
| 24 | #include <hdb/db.h> | ||
| 25 | |||
| 26 | #define TOK_ARGS (mstream& out,Env& env) | ||
| 27 | #define TOK_IMPL(func) static void impl_##func TOK_ARGS throw(merror_t) | ||
| 28 | #define TOK_ADD(func,a,mod,help) \ | ||
| 29 | T.add(new binary_t(uscore2dot(#func),sref(help),mod,sref(a),impl_##func),0,1) | ||
| 30 | |||
| 31 | mstring uscore2dot(const char* s); | ||
| 32 | |||
| 33 | class Env; | ||
| 34 | typedef void (*s_func_t)TOK_ARGS; | ||
| 35 | |||
| 36 | class tok_t { | ||
| 37 | public: | ||
| 38 | static const unsigned CONSTANT,RESTRICT,NORMAL,SYSONLY,INIT; | ||
| 39 | static const unsigned RUNNABLE,FUNCTION,BINARY; | ||
| 40 | static const unsigned REFERENCE,DBREF,CRON; | ||
| 41 | static const unsigned PRIVATE,PUBLIC,XMODES; | ||
| 42 | |||
| 43 | static inline unsigned tight(unsigned xmod) { | ||
| 44 | // INIT is ignored here simply because no mode can | ||
| 45 | // read variables in INIT mode... defaults to public. | ||
| 46 | return (xmod&SYSONLY)?SYSONLY:(xmod&NORMAL)?PRIVATE:PUBLIC; | ||
| 47 | } | ||
| 48 | |||
| 49 | mstring name,body; | ||
| 50 | unsigned mod; | ||
| 51 | |||
| 52 | int is_clear(int xmod) const { return (mod&xmod)!=0; } | ||
| 53 | int is_replaceable(const tok_t& t) const { return (mod&t.mod)==t.mod; } | ||
| 54 | |||
| 55 | int is_const() const { return (mod&CONSTANT)!=0; } | ||
| 56 | int is_runnable() const { return (mod&RUNNABLE)!=0; } | ||
| 57 | int is_func() const { return (mod&FUNCTION)!=0; } | ||
| 58 | int is_cron() const { return (mod&CRON)!=0; } | ||
| 59 | int is_binary() const { return (mod&BINARY)!=0; } | ||
| 60 | int is_reference() const { return (mod&REFERENCE)!=0; } | ||
| 61 | int is_dbref() const { return (mod&DBREF)!=0; } | ||
| 62 | |||
| 63 | tok_t(const sref& s,const sref& b,unsigned m) : name(s),body(b),mod(m) {} | ||
| 64 | virtual ~tok_t(); | ||
| 65 | |||
| 66 | static void* operator new(size_t n); | ||
| 67 | static void operator delete(void* p); | ||
| 68 | private: | ||
| 69 | static uniheap<32> tree; | ||
| 70 | }; | ||
| 71 | |||
| 72 | /////////////////////////////////////////////////////////////////// | ||
| 73 | |||
| 74 | class reference_t : public tok_t { | ||
| 75 | public: | ||
| 76 | mstring data; | ||
| 77 | reference_t(const sref& s,const sref& b,unsigned m): | ||
| 78 | tok_t(s,b,m|REFERENCE|CONSTANT) {} | ||
| 79 | |||
| 80 | static void* operator new(size_t n); | ||
| 81 | static void operator delete(void* p); | ||
| 82 | private: | ||
| 83 | static uniheap<44> tree; | ||
| 84 | }; | ||
| 85 | |||
| 86 | /////////////////////////////////////////////////////////////////// | ||
| 87 | |||
| 88 | class dbref_t : public tok_t { | ||
| 89 | public: | ||
| 90 | dbmap db; | ||
| 91 | dbref_t(const sref& s,const sref& b,unsigned m): | ||
| 92 | tok_t(s,b,m|DBREF|CONSTANT) {} | ||
| 93 | |||
| 94 | static void* operator new(size_t n); | ||
| 95 | static void operator delete(void* p); | ||
| 96 | private: | ||
| 97 | static uniheap<96> tree; | ||
| 98 | }; | ||
| 99 | |||
| 100 | /////////////////////////////////////////////////////////////////// | ||
| 101 | |||
| 102 | class runnable_t : public tok_t { | ||
| 103 | public: | ||
| 104 | mstring arg; | ||
| 105 | runnable_t(const sref& s,const sref& b,unsigned m,const sref& a): | ||
| 106 | tok_t(s,b,m|RUNNABLE|CONSTANT),arg(a) {} | ||
| 107 | |||
| 108 | static void* operator new(size_t n); | ||
| 109 | static void operator delete(void* p); | ||
| 110 | private: | ||
| 111 | static uniheap<44> tree; | ||
| 112 | }; | ||
| 113 | |||
| 114 | /////////////////////////////////////////////////////////////////// | ||
| 115 | |||
| 116 | class func_t : public runnable_t { | ||
| 117 | public: | ||
| 118 | expr_t x; | ||
| 119 | func_t(const sref& s,const sref& b,unsigned m,const sref& a): | ||
| 120 | runnable_t(s,b,m|FUNCTION,a) { x=body; } | ||
| 121 | |||
| 122 | static void* operator new(size_t n); | ||
| 123 | static void operator delete(void* p); | ||
| 124 | private: | ||
| 125 | static uniheap<80> tree; | ||
| 126 | }; | ||
| 127 | |||
| 128 | /////////////////////////////////////////////////////////////////// | ||
| 129 | |||
| 130 | class cron_t : public func_t { | ||
| 131 | public: | ||
| 132 | int last,interval; | ||
| 133 | cron_t(const sref& s,const sref& b,unsigned m,int i): | ||
| 134 | func_t(s,b,m|CRON,ms_empty),last(0),interval(i) {} | ||
| 135 | |||
| 136 | static void* operator new(size_t n); | ||
| 137 | static void operator delete(void* p); | ||
| 138 | private: | ||
| 139 | static uniheap<88> tree; | ||
| 140 | }; | ||
| 141 | |||
| 142 | /////////////////////////////////////////////////////////////////// | ||
| 143 | |||
| 144 | class binary_t : public runnable_t { | ||
| 145 | public: | ||
| 146 | s_func_t func; | ||
| 147 | binary_t(const sref& s,const sref& b,unsigned m,const sref& a,s_func_t f): | ||
| 148 | runnable_t(s,b,m|BINARY,a),func(f) {} | ||
| 149 | |||
| 150 | static void* operator new(size_t n); | ||
| 151 | static void operator delete(void* p); | ||
| 152 | private: | ||
| 153 | static uniheap<48> tree; | ||
| 154 | }; | ||
| 155 | |||
| 156 | /////////////////////////////////////////////////////////////////// | ||
| 157 | |||
| 158 | mstream& operator<<(mstream&,const runnable_t& ftok); | ||
| 159 | mstring Mode(const tok_t& tok); | ||
| 160 | mstring Desc(const tok_t& tok); | ||
| 161 | |||
| 162 | /////////////////////////////////////////////////////////////////// | ||
| 163 | |||
| 164 | struct tokmap { | ||
| 165 | typedef tok_t* val_t; | ||
| 166 | typedef mrvec<val_t> map_t; | ||
| 167 | typedef map_t::iterator iterator; | ||
| 168 | typedef map_t::const_iterator const_iterator; | ||
| 169 | |||
| 170 | struct comp_t { | ||
| 171 | sref key; | ||
| 172 | comp_t(const sref& skey) : key(skey) {} | ||
| 173 | bool operator()(val_t a,val_t b) const { | ||
| 174 | return (a?((const sref&)a->name):key)<(b?((const sref&)b->name):key); | ||
| 175 | } | ||
| 176 | }; | ||
| 177 | |||
| 178 | tokmap* up; | ||
| 179 | map_t list; | ||
| 180 | |||
| 181 | tokmap() : up(0),list() {} | ||
| 182 | tokmap(tokmap& p) : up(&p),list() {} | ||
| 183 | ~tokmap() { clear(); } | ||
| 184 | |||
| 185 | iterator begin() { return list.begin(); } | ||
| 186 | const_iterator begin() const { return list.begin(); } | ||
| 187 | |||
| 188 | iterator end() { return list.end(); } | ||
| 189 | const_iterator end() const { return list.end(); } | ||
| 190 | |||
| 191 | void clear(); | ||
| 192 | iterator erase(iterator p) { delete *p; return list.erase(p); } | ||
| 193 | |||
| 194 | tok_t* get_this(const sref& t); | ||
| 195 | const tok_t* get_this(const sref& t) const; | ||
| 196 | const tok_t* get(const sref& t) const; | ||
| 197 | tok_t* get(const sref& t); | ||
| 198 | |||
| 199 | tok_t* set(tok_t* tok,int replace,int force) throw(merror_t); | ||
| 200 | tok_t* add(tok_t* tok,int replace,int force) throw(merror_t); | ||
| 201 | }; | ||
| 202 | |||
| 203 | /////////////////////////////////////////////////////////////////// | ||
| 204 | |||
| 205 | int is_tokname(const sref& s); | ||
| 206 | int is_arglist(const sref& s); | ||
| 207 | |||
| 208 | tok_t* Cond(tokmap& map,const sref& s,const sref& b,unsigned m) throw(merror_t); | ||
| 209 | tok_t* Global(tokmap& map,const sref& s,const sref& b,unsigned m,int rpl=0) throw(merror_t); | ||
| 210 | tok_t* Const(tokmap& map,const sref& s,const sref& b,unsigned m,int rpl=0) throw(merror_t); | ||
| 211 | func_t* Func(tokmap& map,const sref& s,const sref& b, | ||
| 212 | unsigned m,const sref& a,int rpl=0) throw(merror_t); | ||
| 213 | cron_t* Cron(tokmap& map,const sref& s,const sref& b, | ||
| 214 | unsigned m,int i,int rpl=0) throw(merror_t); | ||
| 215 | binary_t* Binary(tokmap& map,const sref& s,const sref& b, | ||
| 216 | unsigned m,const sref& a,s_func_t f,int rpl=0) throw(merror_t); | ||
| 217 | reference_t* Reference(tokmap& map,const sref& s,const sref& b, | ||
| 218 | unsigned m,int rpl=0) throw(merror_t); | ||
| 219 | dbref_t* DBRef(tokmap& map,const sref& s, | ||
| 220 | const sref& b,unsigned m,int rpl=0) throw(merror_t); | ||
| 221 | void ClearToken(tokmap& map,const sref& s) throw(merror_t); | ||
| 222 | |||
| 223 | /////////////////////////////////////////////////////////////////// | ||
| 224 | |||
| 225 | const int XPUBLIC=tok_t::RESTRICT; | ||
| 226 | const int XPRIVATE=XPUBLIC|tok_t::NORMAL; | ||
| 227 | const int XSYSTEM=XPRIVATE|tok_t::SYSONLY; | ||
| 228 | const int XINIT=tok_t::INIT; | ||
| 229 | |||
| 230 | #endif | ||
