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