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