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