Annotation of parser3/src/types/pa_value.h, revision 1.64
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.64 ! parser 8: $Id: pa_value.h,v 1.63 2001/06/27 12:44:33 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.7 paf 166: virtual void put_element(const String& name, Value *value) { bark("(%s) does not accept elements"); }
1.59 parser 167:
1.23 paf 168: /** extract VStateless_class
1.22 paf 169: @return for
1.25 paf 170: - VStateless_class: this
171: - VStateless_object: fclass_real
1.26 paf 172: - WContext: none yet | transparent
1.57 paf 173: - VHash: 0
1.22 paf 174: */
1.9 paf 175: virtual VStateless_class *get_class() { return 0; }
1.59 parser 176:
1.23 paf 177: /** extract VAliased
1.22 paf 178: @return for
1.33 paf 179: - VAliased: this
1.26 paf 180: - WContext: transparent
1.51 paf 181: - VMethodFrame: self_transparent
1.22 paf 182: */
1.1 paf 183: virtual VAliased *get_aliased() { return 0; }
1.32 paf 184:
185: /** extract VTable
186: @return for
1.35 paf 187: - VTable: ftable
1.32 paf 188: */
1.35 paf 189: virtual Table *get_table() { return 0; }
1.1 paf 190:
191: public: // usage
192:
193: Value(Pool& apool) : Pooled(apool), fname(unnamed_name) {
194: }
195:
1.22 paf 196: /// set's the name which is used in error messages
1.1 paf 197: void set_name(const String& aname) { fname=&aname; }
198:
1.26 paf 199: /// @return sure String. if it doesn't have string value barks
1.1 paf 200: const String& as_string() {
201: const String *result=get_string();
202: if(!result)
1.35 paf 203: bark("(%s) has no string representation");
1.6 paf 204:
1.1 paf 205: return *result;
206: }
207:
208: private:
209:
210: const String *fname;
211:
1.6 paf 212: protected:
1.1 paf 213:
1.22 paf 214: /// throws exception specifying bark-reason and name() type() of problematic value
215: void bark(char *reason) const {
1.44 paf 216: THROW(0, 0,
1.1 paf 217: &name(),
1.22 paf 218: reason, type());
1.1 paf 219: }
220:
1.17 paf 221: };
222:
1.23 paf 223: /** \b junction is some code joined with context of it's evaluation.
1.22 paf 224:
225: there are code-junctions and method-junctions
226: - code-junctions are used when some parameter passed in cury brackets
227: - method-junctions used in ^method[] calls or $method references
228: */
1.17 paf 229: class Junction : public Pooled {
230: public:
231:
232: Junction(Pool& apool,
233: Value& aself,
234: VStateless_class *avclass, const Method *amethod,
235: Value *aroot,
236: Value *arcontext,
237: WContext *awcontext,
238: const Array *acode) : Pooled(apool),
239:
240: self(aself),
241: vclass(avclass), method(amethod),
242: root(aroot),
243: rcontext(arcontext),
244: wcontext(awcontext),
245: code(acode) {
246: }
247:
1.22 paf 248: /// always present
1.17 paf 249: Value& self;
1.22 paf 250: //@{
251: /// @name either these // so called 'method-junction'
1.17 paf 252: VStateless_class *vclass; const Method *method;
1.22 paf 253: //@}
254: //@{
255: /// @name or these are present // so called 'code-junction'
1.17 paf 256: Value *root;
257: Value *rcontext;
258: WContext *wcontext;
259: const Array *code;
1.22 paf 260: //@}
1.48 paf 261: };
262:
263: /**
264: native code method
265: params can be NULL when
1.53 paf 266: method min&max params (see VStateless_class::add_native_method)
1.48 paf 267: counts are zero.
268: */
1.39 paf 269: typedef void (*Native_code_ptr)(Request& request,
270: const String& method_name,
1.48 paf 271: MethodParams *params);
1.39 paf 272:
1.23 paf 273: /**
1.22 paf 274: class method.
275:
276: methods can have
277: - named or
278: - numbered parameters
279:
280: methods can be
281: - parser or
282: - native onces
283:
1.40 paf 284: holds
1.22 paf 285: - parameter names or number limits
286: - local names
287: - code [parser or native]
288: */
1.17 paf 289: class Method : public Pooled {
290: public:
1.39 paf 291:
1.41 paf 292: /// allowed method call types
1.39 paf 293: enum Call_type {
1.41 paf 294: CT_ANY, ///< method can be called either statically or dynamically
295: CT_STATIC, ///< method can be called only statically
296: CT_DYNAMIC ///< method can be called only dynamically
1.39 paf 297: };
298:
299: /// name for error reporting
1.17 paf 300: const String& name;
1.39 paf 301: ///
302: Call_type call_type;
1.22 paf 303: //@{
304: /// @name either numbered params // for native-code methods = operators
1.17 paf 305: int min_numbered_params_count, max_numbered_params_count;
1.22 paf 306: //@}
307: //@{
308: /// @name or named params&locals // for parser-code methods
1.17 paf 309: Array *params_names; Array *locals_names;
1.22 paf 310: //@}
311: //@{
312: /// @name the Code
1.17 paf 313: const Array *parser_code;/*OR*/Native_code_ptr native_code;
1.22 paf 314: //@}
1.17 paf 315:
316: Method(
317: Pool& apool,
318: const String& aname,
1.39 paf 319: Call_type call_type,
1.17 paf 320: int amin_numbered_params_count, int amax_numbered_params_count,
321: Array *aparams_names, Array *alocals_names,
322: const Array *aparser_code, Native_code_ptr anative_code) :
323:
324: Pooled(apool),
325: name(aname),
1.39 paf 326: call_type(call_type),
1.17 paf 327: min_numbered_params_count(amin_numbered_params_count),
328: max_numbered_params_count(amax_numbered_params_count),
329: params_names(aparams_names), locals_names(alocals_names),
330: parser_code(aparser_code), native_code(anative_code) {
331: }
332:
1.22 paf 333: /// call this before invoking to ensure proper actual numbered params count
1.55 paf 334: void check_actual_numbered_params(Pool& pool,
1.17 paf 335: Value& self, const String& actual_name, Array *actual_numbered_params) const {
1.54 paf 336:
1.17 paf 337: int actual_count=actual_numbered_params?actual_numbered_params->size():0;
338: if(actual_count<min_numbered_params_count) // not proper count? bark
1.54 paf 339: PTHROW(0, 0,
1.17 paf 340: &actual_name,
1.47 paf 341: "native method of %s (%s) accepts minimum %d parameter(s) (%d present)",
1.17 paf 342: self.name().cstr(),
343: self.type(),
1.47 paf 344: min_numbered_params_count,
345: actual_count);
1.17 paf 346:
347: }
1.1 paf 348: };
349:
350: #endif
E-mail: