Annotation of parser3/src/types/pa_value.h, revision 1.88
1.22 paf 1: /** @file
1.24 paf 2: Parser: Value, Method, Junction class decls.
3:
1.78 paf 4: Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.79 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 6: */
7:
8: #ifndef PA_VALUE_H
9: #define PA_VALUE_H
1.87 paf 10:
1.88 ! paf 11: static const char* IDENT_VALUE_H="$Date: pa_value.h,v 1.87 2002/08/01 11:26:53 paf Exp $";
1.1 paf 12:
13: #include "pa_pool.h"
14: #include "pa_string.h"
15: #include "pa_array.h"
16: #include "pa_exception.h"
1.13 paf 17: #include "pa_globals.h"
1.1 paf 18:
1.9 paf 19: class VStateless_class;
1.1 paf 20: class WContext;
21: class VAliased;
22: class Request;
1.35 paf 23: class Table;
1.17 paf 24: class Junction;
25: class Method;
1.20 paf 26: class Hash;
1.42 paf 27: class VFile;
1.64 parser 28: class MethodParams;
1.1 paf 29:
1.31 paf 30: /// grandfather of all @a values in @b Parser
1.1 paf 31: class Value : public Pooled {
32: public: // Value
33:
1.53 paf 34: /// all: value type, used for error reporting and 'is' expression operator
1.1 paf 35: virtual const char *type() const =0;
1.59 parser 36:
1.23 paf 37: /** is this value defined?
1.22 paf 38: @return for
1.63 parser 39: - VVoid: false
1.69 parser 40: - VString: eq ''=false, ne ''=true
1.72 parser 41: - VHash: count!=0
1.71 parser 42: - VTable: count!=0
1.22 paf 43: - others: true
44: */
1.38 paf 45: virtual bool is_defined() const { return true; }
1.59 parser 46:
1.35 paf 47: /** is this value string?
48: @return for
49: - VString: true
50: - others: false
51: */
1.38 paf 52: virtual bool is_string() const { return false; }
1.59 parser 53:
1.23 paf 54: /** what's the meaning of this value in context of expression?
1.22 paf 55: @return for
1.34 paf 56: - VString: fstring as VDouble or this depending on return_string_as_is
1.70 parser 57: - VBool: clone
58: - VDouble: clone
59: - VInt: clone
1.63 parser 60: - VVoid: this
1.27 paf 61: - VFile: this
1.45 paf 62: - VImage: this
1.64 parser 63: - VDate: ftime -> float days
1.72 parser 64: - VTable: count
65: - VHash: count
1.22 paf 66: */
1.75 parser 67: virtual Value *as_expr_result(bool /*return_string_as_is*/=false) {
1.34 paf 68: bark("(%s) can not be used in expression"); return 0;
69: }
1.59 parser 70:
1.23 paf 71: /** extract Hash
1.22 paf 72: @return for
1.25 paf 73: - VHash: fhash
74: - VResponse: ffields
1.22 paf 75: */
1.76 parser 76: virtual Hash *get_hash(const String * /*source*/) { return 0; }
1.59 parser 77:
1.23 paf 78: /** extract const String
1.22 paf 79: @return for
1.25 paf 80: - VString: value
1.63 parser 81: - VVoid: ""
1.25 paf 82: - VDouble: value
1.66 parser 83: - VInt: value
1.25 paf 84: - VBool: must be 0: so in ^if(1>2) it would'nt become "FALSE" string which is 'true'
1.22 paf 85: - others: 0
86: - WContext: accumulated fstring
87: */
1.1 paf 88: virtual const String *get_string() { return 0; }
1.59 parser 89:
1.23 paf 90: /** extract double
1.22 paf 91: @return for
1.25 paf 92: - VString: value
93: - VDouble: value
1.59 parser 94: - VInt: value
1.25 paf 95: - VBool: value
1.63 parser 96: - VVoid: 0
1.64 parser 97: - VDate: ftime -> float days
1.22 paf 98: */
1.72 parser 99: virtual double as_double() const { bark("(%s) does not have numerical (double) value"); return 0; }
1.59 parser 100:
101: /** extract integer
102: - VString: value
103: - VDouble: value
104: - VInt: value
105: - VBool: value
1.63 parser 106: - VVoid: 0
1.73 parser 107: - VTable: count
108: - VHash: count
1.59 parser 109: */
1.72 parser 110: virtual int as_int () const { bark("(%s) does not have numerical (int) value"); return 0; }
1.59 parser 111:
1.23 paf 112: /** extract bool
1.22 paf 113: @return for
1.63 parser 114: - VVoid: false
1.25 paf 115: - VBool: value
116: - VInt: 0 or !0
117: - VDouble: 0 or !0
1.27 paf 118: - VFile: true
1.64 parser 119: - VDate: 0 or !0
1.73 parser 120: - VTable: count
121: - VHash: count
1.22 paf 122: */
1.72 parser 123: virtual bool as_bool() const { bark("(%s) does not have logical value"); return 0; }
1.59 parser 124:
1.42 paf 125: /** extract file
126: @return for
127: - VFile: this
128: - VString: vfile
1.45 paf 129: - VImage: true
1.42 paf 130: */
1.75 parser 131: virtual VFile *as_vfile(String::Untaint_lang /*lang*/=String::UL_UNSPECIFIED,
132: bool /*origins_mode*/=false) {
1.42 paf 133: bark("(%s) does not have file value"); return 0;
134: }
1.59 parser 135:
1.23 paf 136: /** extract Junction
1.22 paf 137: @return for
138: - junction: itself
139: */
1.1 paf 140: virtual Junction *get_junction() { return 0; }
1.59 parser 141:
1.23 paf 142: /** extract Value element
1.22 paf 143: @return for
1.25 paf 144: - VHash: (key)=value
1.67 parser 145: - VAliased: sometimes $CLASS [see VAliased::hide_class()]
1.29 paf 146: - VStateless_class: +$method
147: - VStateless_object: +$method
1.58 paf 148: - VClass: (field)=STATIC value;(method)=method_ref with self=object_class
1.51 paf 149: - VCodeFrame: wcontext_transparent
150: - VMethodFrame: my or self_transparent
1.52 paf 151: - VTable: columns,methods
1.28 paf 152: - VEnv: field
1.67 parser 153: - VForm: CLASS,method,field
1.45 paf 154: - VString: $method
1.30 paf 155: - VRequest: fields
1.45 paf 156: - VResponse: method,fields
1.28 paf 157: - VCookie: field
1.45 paf 158: - VFile: method,field
1.67 parser 159: - VDate: CLASS,method,field
1.26 paf 160: */
1.75 parser 161: virtual Value *get_element(const String& /*name*/) { bark("(%s) has no elements"); return 0; }
1.59 parser 162:
1.31 paf 163: /** store Value element under @a name
1.22 paf 164: @return for
1.25 paf 165: - VHash: (key)=value
1.67 parser 166: - VStateless_object: (CLASS)=vclass;(method)=method_ref
1.26 paf 167: - VStateless_class: (field)=value - static values only
168: - VStateless_object: (field)=value
1.51 paf 169: - VCodeFrame: wcontext_transparent
170: - VMethodFrame: my or self_transparent
1.25 paf 171: - VResponse: (attribute)=value
172: - VCookie: field
1.22 paf 173: */
1.75 parser 174: virtual void put_element(const String& name, Value * /*value*/) {
1.65 parser 175: // to prevent modification of system classes,
176: // created at system startup, and not having exception
177: // handler installed, we neet to bark using request.pool
178: bark("(%s) does not accept elements",
179: "element can not be stored to %s", &name);
180: }
1.59 parser 181:
1.23 paf 182: /** extract VStateless_class
1.22 paf 183: @return for
1.85 paf 184: - VX: x_class
1.25 paf 185: - VStateless_class: this
186: - VStateless_object: fclass_real
1.26 paf 187: - WContext: none yet | transparent
1.85 paf 188: these are methodless classes:
189: - VBool: 0
190: - VJunction: 0
191: - VEnv: 0
192: - VRequest: 0
193: - VCookie: 0
1.22 paf 194: */
1.85 paf 195: virtual VStateless_class *get_class()=0;
1.59 parser 196:
1.23 paf 197: /** extract VAliased
1.22 paf 198: @return for
1.33 paf 199: - VAliased: this
1.26 paf 200: - WContext: transparent
1.51 paf 201: - VMethodFrame: self_transparent
1.22 paf 202: */
1.1 paf 203: virtual VAliased *get_aliased() { return 0; }
1.32 paf 204:
205: /** extract VTable
206: @return for
1.35 paf 207: - VTable: ftable
1.32 paf 208: */
1.35 paf 209: virtual Table *get_table() { return 0; }
1.1 paf 210:
211: public: // usage
212:
1.84 paf 213: Value(Pool& apool) : Pooled(apool) {
1.68 parser 214: }
1.1 paf 215:
1.26 paf 216: /// @return sure String. if it doesn't have string value barks
1.1 paf 217: const String& as_string() {
218: const String *result=get_string();
219: if(!result)
1.35 paf 220: bark("(%s) has no string representation");
1.6 paf 221:
1.1 paf 222: return *result;
223: }
224:
1.6 paf 225: protected:
1.1 paf 226:
1.22 paf 227: /// throws exception specifying bark-reason and name() type() of problematic value
1.65 parser 228: void bark(char *reason,
229: const char *alt_reason=0, const String *problem_source=0) const {
1.80 paf 230: throw Exception("parser.runtime",
1.84 paf 231: problem_source,
232: alt_reason?alt_reason:reason, type());
1.1 paf 233: }
234:
1.17 paf 235: };
236:
1.23 paf 237: /** \b junction is some code joined with context of it's evaluation.
1.22 paf 238:
239: there are code-junctions and method-junctions
240: - code-junctions are used when some parameter passed in cury brackets
241: - method-junctions used in ^method[] calls or $method references
242: */
1.17 paf 243: class Junction : public Pooled {
244: public:
245:
246: Junction(Pool& apool,
247: Value& aself,
248: VStateless_class *avclass, const Method *amethod,
249: Value *aroot,
250: Value *arcontext,
251: WContext *awcontext,
252: const Array *acode) : Pooled(apool),
253:
254: self(aself),
255: vclass(avclass), method(amethod),
256: root(aroot),
257: rcontext(arcontext),
258: wcontext(awcontext),
259: code(acode) {
1.81 paf 260: }
261:
262: void change_context(Junction *source) {
263: if(source) {
264: root=source->root;
265: rcontext=source->rcontext;
266: wcontext=source->wcontext;
267: } else {
268: root=rcontext=0;
269: wcontext=0;
270: }
1.17 paf 271: }
272:
1.22 paf 273: /// always present
1.17 paf 274: Value& self;
1.22 paf 275: //@{
276: /// @name either these // so called 'method-junction'
1.17 paf 277: VStateless_class *vclass; const Method *method;
1.22 paf 278: //@}
279: //@{
280: /// @name or these are present // so called 'code-junction'
1.17 paf 281: Value *root;
282: Value *rcontext;
283: WContext *wcontext;
284: const Array *code;
1.22 paf 285: //@}
1.48 paf 286: };
287:
288: /**
289: native code method
290: params can be NULL when
1.53 paf 291: method min&max params (see VStateless_class::add_native_method)
1.48 paf 292: counts are zero.
293: */
1.39 paf 294: typedef void (*Native_code_ptr)(Request& request,
295: const String& method_name,
1.48 paf 296: MethodParams *params);
1.39 paf 297:
1.23 paf 298: /**
1.22 paf 299: class method.
300:
301: methods can have
302: - named or
303: - numbered parameters
304:
305: methods can be
306: - parser or
307: - native onces
308:
1.40 paf 309: holds
1.22 paf 310: - parameter names or number limits
311: - local names
312: - code [parser or native]
313: */
1.17 paf 314: class Method : public Pooled {
315: public:
1.39 paf 316:
1.41 paf 317: /// allowed method call types
1.39 paf 318: enum Call_type {
1.41 paf 319: CT_ANY, ///< method can be called either statically or dynamically
320: CT_STATIC, ///< method can be called only statically
321: CT_DYNAMIC ///< method can be called only dynamically
1.39 paf 322: };
323:
324: /// name for error reporting
1.17 paf 325: const String& name;
1.39 paf 326: ///
327: Call_type call_type;
1.22 paf 328: //@{
329: /// @name either numbered params // for native-code methods = operators
1.17 paf 330: int min_numbered_params_count, max_numbered_params_count;
1.22 paf 331: //@}
332: //@{
333: /// @name or named params&locals // for parser-code methods
1.17 paf 334: Array *params_names; Array *locals_names;
1.22 paf 335: //@}
336: //@{
337: /// @name the Code
1.17 paf 338: const Array *parser_code;/*OR*/Native_code_ptr native_code;
1.22 paf 339: //@}
1.17 paf 340:
341: Method(
342: Pool& apool,
343: const String& aname,
1.39 paf 344: Call_type call_type,
1.17 paf 345: int amin_numbered_params_count, int amax_numbered_params_count,
346: Array *aparams_names, Array *alocals_names,
347: const Array *aparser_code, Native_code_ptr anative_code) :
348:
349: Pooled(apool),
350: name(aname),
1.39 paf 351: call_type(call_type),
1.17 paf 352: min_numbered_params_count(amin_numbered_params_count),
353: max_numbered_params_count(amax_numbered_params_count),
354: params_names(aparams_names), locals_names(alocals_names),
355: parser_code(aparser_code), native_code(anative_code) {
356: }
357:
1.22 paf 358: /// call this before invoking to ensure proper actual numbered params count
1.75 parser 359: void check_actual_numbered_params(
1.84 paf 360: Value& self, const String& actual_name, Array *actual_numbered_params) const;
1.86 paf 361: };
362:
363: /// Auto-object used for temporarily substituting/removing elements
364: class Temp_value_element {
365: Value& fwhere;
366: const String& fname;
367: Value *saved;
368: public:
369: Temp_value_element(Value& awhere, const String& aname, Value *awhat) :
370: fwhere(awhere),
371: fname(aname),
372: saved(awhere.get_element(aname)) {
373: fwhere.put_element(aname, awhat);
374: }
375: ~Temp_value_element() {
376: fwhere.put_element(fname, saved);
377: }
1.1 paf 378: };
379:
380: #endif
E-mail: