Annotation of parser3/src/types/pa_value.h, revision 1.43
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.43 ! paf 8: $Id: pa_value.h,v 1.42 2001/04/03 06:23:07 paf 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.1 paf 29:
1.31 paf 30: /// grandfather of all @a values in @b Parser
1.1 paf 31: class Value : public Pooled {
32: public: // Value
33:
1.22 paf 34: /// - all: for error reporting after fail(), etc
1.1 paf 35: virtual const char *type() const =0;
1.22 paf 36: /// - all: for error reporting after fail(), etc
1.1 paf 37: const String& name() const { return *fname; }
1.23 paf 38: /** is this value defined?
1.22 paf 39: @return for
1.25 paf 40: - VUnknown: false
1.22 paf 41: - others: true
42: */
1.38 paf 43: virtual bool is_defined() const { return true; }
1.35 paf 44: /** is this value string?
45: @return for
46: - VString: true
47: - others: false
48: */
1.38 paf 49: virtual bool is_string() const { return false; }
1.23 paf 50: /** what's the meaning of this value in context of expression?
1.22 paf 51: @return for
1.34 paf 52: - VString: fstring as VDouble or this depending on return_string_as_is
1.25 paf 53: - VBool: this
54: - VDouble: this
55: - VInt: this
56: - VUnknown: this
1.27 paf 57: - VFile: this
1.22 paf 58: */
1.35 paf 59: virtual Value *as_expr_result(bool return_string_as_is=false) {
1.34 paf 60: bark("(%s) can not be used in expression"); return 0;
61: }
1.23 paf 62: /** extract Hash
1.22 paf 63: @return for
1.25 paf 64: - VHash: fhash
65: - VResponse: ffields
1.22 paf 66: */
1.20 paf 67: virtual Hash *get_hash() { return 0; }
1.23 paf 68: /** extract const String
1.22 paf 69: @return for
1.25 paf 70: - VString: value
71: - VUnknown: ""
72: - VDouble: value
73: - VBool: must be 0: so in ^if(1>2) it would'nt become "FALSE" string which is 'true'
1.22 paf 74: - others: 0
75: - WContext: accumulated fstring
76: */
1.1 paf 77: virtual const String *get_string() { return 0; }
1.23 paf 78: /** extract double
1.22 paf 79: @return for
1.25 paf 80: - VString: value
81: - VDouble: value
82: - VInt: finteger
83: - VBool: value
1.37 paf 84: - VUnknown: 0
1.22 paf 85: */
1.35 paf 86: virtual double as_double() { bark("(%s) does not have numerical value"); return 0; }
1.23 paf 87: /** extract bool
1.22 paf 88: @return for
1.25 paf 89: - VUnknown: false
90: - VBool: value
91: - VInt: 0 or !0
92: - VDouble: 0 or !0
1.27 paf 93: - VFile: true
1.22 paf 94: */
1.35 paf 95: virtual bool as_bool() { bark("(%s) does not have logical value"); return 0; }
1.42 paf 96: /** extract file
97: @return for
98: - VFile: this
99: - VString: vfile
100: */
101: virtual const VFile *as_vfile() const {
102: bark("(%s) does not have file value"); return 0;
103: }
1.23 paf 104: /** extract Junction
1.22 paf 105: @return for
106: - junction: itself
107: */
1.1 paf 108: virtual Junction *get_junction() { return 0; }
1.23 paf 109: /** extract Value element
1.22 paf 110: @return for
1.25 paf 111: - VHash: (key)=value
1.29 paf 112: - VAliased: $CLASS, $BASE
113: - VStateless_class: +$method
114: - VStateless_object: +$method
1.25 paf 115: - VCode_frame: wcontext_transparent
1.26 paf 116: - VMethod_frame: my or self_transparent
1.25 paf 117: - VTable: column
1.28 paf 118: - VEnv: field
1.25 paf 119: - VForm: CLASS,BASE,method,field
120: - VString: $CLASS,$BASE,$method
1.30 paf 121: - VRequest: fields
1.25 paf 122: - VResponse: CLASS,BASE,method,fields
1.28 paf 123: - VCookie: field
1.27 paf 124: - VFile: CLASS,BASE,method,field
1.26 paf 125: */
1.36 paf 126: virtual Value *get_element(const String& name) { bark("(%s) has no elements"); return 0; }
1.31 paf 127: /** store Value element under @a name
1.22 paf 128: @return for
1.25 paf 129: - VHash: (key)=value
130: - VStateless_object: (CLASS)=vclass;(BASE)=base;(method)=method_ref
1.26 paf 131: - VStateless_class: (field)=value - static values only
132: - VStateless_object: (field)=value
1.25 paf 133: - VCode_frame: wcontext_transparent
1.26 paf 134: - VMethod_frame: my or self_transparent
1.25 paf 135: - VResponse: (attribute)=value
136: - VCookie: field
1.22 paf 137: */
1.7 paf 138: virtual void put_element(const String& name, Value *value) { bark("(%s) does not accept elements"); }
1.23 paf 139: /** extract VStateless_class
1.22 paf 140: @return for
1.25 paf 141: - VStateless_class: this
142: - VStateless_object: fclass_real
1.26 paf 143: - WContext: none yet | transparent
1.22 paf 144: */
1.9 paf 145: virtual VStateless_class *get_class() { return 0; }
1.23 paf 146: /** extract VAliased
1.22 paf 147: @return for
1.33 paf 148: - VAliased: this
1.26 paf 149: - WContext: transparent
150: - VMethod_frame: self_transparent
1.22 paf 151: */
1.1 paf 152: virtual VAliased *get_aliased() { return 0; }
1.32 paf 153:
154: /** extract VTable
155: @return for
1.35 paf 156: - VTable: ftable
1.32 paf 157: */
1.35 paf 158: virtual Table *get_table() { return 0; }
1.1 paf 159:
160: public: // usage
161:
162: Value(Pool& apool) : Pooled(apool), fname(unnamed_name) {
163: }
164:
1.22 paf 165: /// set's the name which is used in error messages
1.1 paf 166: void set_name(const String& aname) { fname=&aname; }
167:
1.26 paf 168: /// @return sure String. if it doesn't have string value barks
1.1 paf 169: const String& as_string() {
170: const String *result=get_string();
171: if(!result)
1.35 paf 172: bark("(%s) has no string representation");
1.6 paf 173:
1.1 paf 174: return *result;
175: }
176:
177: private:
178:
179: const String *fname;
180:
1.6 paf 181: protected:
1.1 paf 182:
1.22 paf 183: /// throws exception specifying bark-reason and name() type() of problematic value
184: void bark(char *reason) const {
1.1 paf 185: THROW(0,0,
186: &name(),
1.22 paf 187: reason, type());
1.1 paf 188: }
189:
1.17 paf 190: };
191:
1.23 paf 192: /** \b junction is some code joined with context of it's evaluation.
1.22 paf 193:
194: there are code-junctions and method-junctions
195: - code-junctions are used when some parameter passed in cury brackets
196: - method-junctions used in ^method[] calls or $method references
197: */
1.17 paf 198: class Junction : public Pooled {
199: public:
200:
201: Junction(Pool& apool,
202: Value& aself,
203: VStateless_class *avclass, const Method *amethod,
204: Value *aroot,
205: Value *arcontext,
206: WContext *awcontext,
207: const Array *acode) : Pooled(apool),
208:
209: self(aself),
210: vclass(avclass), method(amethod),
211: root(aroot),
212: rcontext(arcontext),
213: wcontext(awcontext),
214: code(acode) {
215: }
216:
1.22 paf 217: /// always present
1.17 paf 218: Value& self;
1.22 paf 219: //@{
220: /// @name either these // so called 'method-junction'
1.17 paf 221: VStateless_class *vclass; const Method *method;
1.22 paf 222: //@}
223: //@{
224: /// @name or these are present // so called 'code-junction'
1.17 paf 225: Value *root;
226: Value *rcontext;
227: WContext *wcontext;
228: const Array *code;
1.22 paf 229: //@}
1.17 paf 230: };
231:
1.39 paf 232: /// native code method
233: typedef void (*Native_code_ptr)(Request& request,
234: const String& method_name,
235: Array *params);
236:
1.23 paf 237: /**
1.22 paf 238: class method.
239:
240: methods can have
241: - named or
242: - numbered parameters
243:
244: methods can be
245: - parser or
246: - native onces
247:
1.40 paf 248: holds
1.22 paf 249: - parameter names or number limits
250: - local names
251: - code [parser or native]
252: */
1.17 paf 253: class Method : public Pooled {
254: public:
1.39 paf 255:
1.41 paf 256: /// allowed method call types
1.39 paf 257: enum Call_type {
1.41 paf 258: CT_ANY, ///< method can be called either statically or dynamically
259: CT_STATIC, ///< method can be called only statically
260: CT_DYNAMIC ///< method can be called only dynamically
1.39 paf 261: };
262:
263: /// name for error reporting
1.17 paf 264: const String& name;
1.39 paf 265: ///
266: Call_type call_type;
1.22 paf 267: //@{
268: /// @name either numbered params // for native-code methods = operators
1.17 paf 269: int min_numbered_params_count, max_numbered_params_count;
1.22 paf 270: //@}
271: //@{
272: /// @name or named params&locals // for parser-code methods
1.17 paf 273: Array *params_names; Array *locals_names;
1.22 paf 274: //@}
275: //@{
276: /// @name the Code
1.17 paf 277: const Array *parser_code;/*OR*/Native_code_ptr native_code;
1.22 paf 278: //@}
1.17 paf 279:
280: Method(
281: Pool& apool,
282: const String& aname,
1.39 paf 283: Call_type call_type,
1.17 paf 284: int amin_numbered_params_count, int amax_numbered_params_count,
285: Array *aparams_names, Array *alocals_names,
286: const Array *aparser_code, Native_code_ptr anative_code) :
287:
288: Pooled(apool),
289: name(aname),
1.39 paf 290: call_type(call_type),
1.17 paf 291: min_numbered_params_count(amin_numbered_params_count),
292: max_numbered_params_count(amax_numbered_params_count),
293: params_names(aparams_names), locals_names(alocals_names),
294: parser_code(aparser_code), native_code(anative_code) {
295: }
296:
1.22 paf 297: /// call this before invoking to ensure proper actual numbered params count
1.17 paf 298: void check_actual_numbered_params(
299: Value& self, const String& actual_name, Array *actual_numbered_params) const {
300: int actual_count=actual_numbered_params?actual_numbered_params->size():0;
301: if(actual_count<min_numbered_params_count) // not proper count? bark
302: THROW(0, 0,
303: &actual_name,
304: "native method of %s (%s) accepts minimum %d parameter(s)",
305: self.name().cstr(),
306: self.type(),
307: min_numbered_params_count);
308:
309: }
1.1 paf 310: };
311:
312: #endif
E-mail: