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