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