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