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