summaryrefslogtreecommitdiff
path: root/src/lang/token.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/lang/token.h')
-rw-r--r--src/lang/token.h230
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) \
29T.add(new binary_t(uscore2dot(#func),sref(help),mod,sref(a),impl_##func),0,1)
30
31mstring uscore2dot(const char* s);
32
33class Env;
34typedef void (*s_func_t)TOK_ARGS;
35
36class tok_t {
37public:
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);
68private:
69 static uniheap<32> tree;
70};
71
72///////////////////////////////////////////////////////////////////
73
74class reference_t : public tok_t {
75public:
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);
82private:
83 static uniheap<44> tree;
84};
85
86///////////////////////////////////////////////////////////////////
87
88class dbref_t : public tok_t {
89public:
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);
96private:
97 static uniheap<96> tree;
98};
99
100///////////////////////////////////////////////////////////////////
101
102class runnable_t : public tok_t {
103public:
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);
110private:
111 static uniheap<44> tree;
112};
113
114///////////////////////////////////////////////////////////////////
115
116class func_t : public runnable_t {
117public:
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);
124private:
125 static uniheap<80> tree;
126};
127
128///////////////////////////////////////////////////////////////////
129
130class cron_t : public func_t {
131public:
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);
138private:
139 static uniheap<88> tree;
140};
141
142///////////////////////////////////////////////////////////////////
143
144class binary_t : public runnable_t {
145public:
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);
152private:
153 static uniheap<48> tree;
154};
155
156///////////////////////////////////////////////////////////////////
157
158mstream& operator<<(mstream&,const runnable_t& ftok);
159mstring Mode(const tok_t& tok);
160mstring Desc(const tok_t& tok);
161
162///////////////////////////////////////////////////////////////////
163
164struct 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
205int is_tokname(const sref& s);
206int is_arglist(const sref& s);
207
208tok_t* Cond(tokmap& map,const sref& s,const sref& b,unsigned m) throw(merror_t);
209tok_t* Global(tokmap& map,const sref& s,const sref& b,unsigned m,int rpl=0) throw(merror_t);
210tok_t* Const(tokmap& map,const sref& s,const sref& b,unsigned m,int rpl=0) throw(merror_t);
211func_t* Func(tokmap& map,const sref& s,const sref& b,
212 unsigned m,const sref& a,int rpl=0) throw(merror_t);
213cron_t* Cron(tokmap& map,const sref& s,const sref& b,
214 unsigned m,int i,int rpl=0) throw(merror_t);
215binary_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);
217reference_t* Reference(tokmap& map,const sref& s,const sref& b,
218 unsigned m,int rpl=0) throw(merror_t);
219dbref_t* DBRef(tokmap& map,const sref& s,
220 const sref& b,unsigned m,int rpl=0) throw(merror_t);
221void ClearToken(tokmap& map,const sref& s) throw(merror_t);
222
223///////////////////////////////////////////////////////////////////
224
225const int XPUBLIC=tok_t::RESTRICT;
226const int XPRIVATE=XPUBLIC|tok_t::NORMAL;
227const int XSYSTEM=XPRIVATE|tok_t::SYSONLY;
228const int XINIT=tok_t::INIT;
229
230#endif