Annotation of parser3/src/types/pa_value.h, revision 1.63

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.63    ! parser      8:        $Id: pa_value.h,v 1.62 2001/05/21 17:13:57 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.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.53      paf        34:        /// all: value type, used for error reporting and 'is' expression operator
1.1       paf        35:        virtual const char *type() const =0;
1.59      parser     36:        
1.1       paf        37:        const String& name() const { return *fname; }
1.59      parser     38:        
1.23      paf        39:        /** is this value defined?
1.22      paf        40:                @return for
1.63    ! parser     41:                - VVoid: false
1.22      paf        42:                - others: true
                     43:        */
1.38      paf        44:        virtual bool is_defined() const { return true; }
1.59      parser     45:        
1.35      paf        46:        /** is this value string?
                     47:                @return for
                     48:                - VString: true
                     49:                - others: false
                     50:        */
1.38      paf        51:        virtual bool is_string() const { return false; }
1.59      parser     52:        
1.23      paf        53:        /** what's the meaning of this value in context of expression?
1.22      paf        54:                @return for
1.34      paf        55:                - VString: fstring as VDouble or this depending on return_string_as_is
1.25      paf        56:                - VBool: this
                     57:                - VDouble: this
                     58:                - VInt: this
1.63    ! parser     59:                - VVoid: this
1.27      paf        60:                - VFile: this
1.45      paf        61:                - VImage: this
1.22      paf        62:        */
1.35      paf        63:        virtual Value *as_expr_result(bool return_string_as_is=false) { 
1.34      paf        64:                bark("(%s) can not be used in expression"); return 0; 
                     65:        }
1.59      parser     66:        
1.23      paf        67:        /** extract Hash
1.22      paf        68:                @return for
1.25      paf        69:                - VHash: fhash
                     70:                - VResponse: ffields
1.22      paf        71:        */
1.20      paf        72:        virtual Hash *get_hash() { return 0; }
1.59      parser     73:        
1.23      paf        74:        /** extract const String
1.22      paf        75:                @return for
1.25      paf        76:                - VString: value
1.63    ! parser     77:                - VVoid: ""
1.25      paf        78:                - VDouble: value
                     79:                - VBool: must be 0: so in ^if(1>2) it would'nt become "FALSE" string which is 'true'
1.22      paf        80:                - others: 0
                     81:                - WContext: accumulated fstring
                     82:        */
1.1       paf        83:        virtual const String *get_string() { return 0; }
1.59      parser     84:        
1.23      paf        85:        /** extract double
1.22      paf        86:                @return for
1.25      paf        87:                - VString: value
                     88:                - VDouble: value
1.59      parser     89:                - VInt: value
1.25      paf        90:                - VBool: value
1.63    ! parser     91:                - VVoid: 0
1.22      paf        92:        */
1.59      parser     93:        virtual double as_double() { bark("(%s) does not have numerical (double) value"); return 0; }
                     94:        
                     95:        /**     extract integer
                     96:                - VString: value
                     97:                - VDouble: value
                     98:                - VInt: value
                     99:                - VBool: value
1.63    ! parser    100:                - VVoid: 0
1.59      parser    101:        */
                    102:        virtual int as_int () { bark("(%s) does not have numerical (int) value"); return 0; }
                    103: 
1.23      paf       104:        /** extract bool
1.22      paf       105:                @return for
1.63    ! parser    106:                - VVoid: false
1.25      paf       107:                - VBool: value
                    108:                - VInt: 0 or !0
                    109:                - VDouble: 0 or !0
1.27      paf       110:                - VFile: true
1.22      paf       111:        */
1.35      paf       112:        virtual bool as_bool() { bark("(%s) does not have logical value"); return 0; }
1.59      parser    113:        
1.42      paf       114:        /** extract file
                    115:                @return for
                    116:                - VFile: this
                    117:                - VString: vfile
1.45      paf       118:                - VImage: true
1.42      paf       119:        */
1.46      paf       120:        virtual const VFile *as_vfile(String::Untaint_lang lang=String::UL_UNSPECIFIED) const { 
1.42      paf       121:                bark("(%s) does not have file value"); return 0; 
                    122:        }
1.59      parser    123:        
1.23      paf       124:        /** extract Junction
1.22      paf       125:                @return for
                    126:                - junction: itself
                    127:        */
1.1       paf       128:        virtual Junction *get_junction() { return 0; }
1.59      parser    129:        
1.23      paf       130:        /** extract Value element
1.22      paf       131:                @return for
1.25      paf       132:                - VHash: (key)=value
1.60      parser    133:                - VAliased: sometimes $CLASS, $BASE [see VAliased::hide_class()]
1.29      paf       134:                - VStateless_class: +$method
                    135:                - VStateless_object: +$method
1.58      paf       136:                - VClass: (field)=STATIC value;(method)=method_ref with self=object_class
1.51      paf       137:                - VCodeFrame: wcontext_transparent
                    138:                - VMethodFrame: my or self_transparent
1.52      paf       139:                - VTable: columns,methods
1.28      paf       140:                - VEnv: field
1.25      paf       141:                - VForm: CLASS,BASE,method,field
1.45      paf       142:                - VString: $method
1.30      paf       143:                - VRequest: fields
1.45      paf       144:                - VResponse: method,fields
1.28      paf       145:                - VCookie: field
1.45      paf       146:                - VFile: method,field
1.26      paf       147:                */
1.36      paf       148:        virtual Value *get_element(const String& name) { bark("(%s) has no elements"); return 0; }
1.59      parser    149:        
1.31      paf       150:        /** store Value element under @a name
1.22      paf       151:                @return for
1.25      paf       152:                - VHash: (key)=value
                    153:                - VStateless_object: (CLASS)=vclass;(BASE)=base;(method)=method_ref
1.26      paf       154:                - VStateless_class: (field)=value - static values only
                    155:                - VStateless_object: (field)=value
1.51      paf       156:                - VCodeFrame: wcontext_transparent
                    157:                - VMethodFrame: my or self_transparent
1.25      paf       158:                - VResponse: (attribute)=value
                    159:                - VCookie: field
1.22      paf       160:        */
1.7       paf       161:        virtual void put_element(const String& name, Value *value) { bark("(%s) does not accept elements"); }
1.59      parser    162:        
1.23      paf       163:        /** extract VStateless_class
1.22      paf       164:                @return for
1.25      paf       165:                - VStateless_class: this
                    166:                - VStateless_object: fclass_real
1.26      paf       167:                - WContext: none yet | transparent
1.57      paf       168:                - VHash: 0
1.22      paf       169:        */
1.9       paf       170:        virtual VStateless_class *get_class() { return 0; }
1.59      parser    171:        
1.23      paf       172:        /** extract VAliased
1.22      paf       173:                @return for
1.33      paf       174:                - VAliased: this
1.26      paf       175:                - WContext: transparent
1.51      paf       176:                - VMethodFrame: self_transparent
1.22      paf       177:        */
1.1       paf       178:        virtual VAliased *get_aliased() { return 0; }
1.32      paf       179: 
                    180:        /** extract VTable
                    181:                @return for
1.35      paf       182:                - VTable: ftable
1.32      paf       183:        */
1.35      paf       184:        virtual Table *get_table() { return 0; }
1.1       paf       185: 
                    186: public: // usage
                    187: 
                    188:        Value(Pool& apool) : Pooled(apool), fname(unnamed_name) {
                    189:        }
                    190: 
1.22      paf       191:        /// set's the name which is used in error messages
1.1       paf       192:        void set_name(const String& aname) { fname=&aname; }
                    193: 
1.26      paf       194:        /// @return sure String. if it doesn't have string value barks
1.1       paf       195:        const String& as_string() {
                    196:                const String *result=get_string(); 
                    197:                if(!result)
1.35      paf       198:                        bark("(%s) has no string representation");
1.6       paf       199: 
1.1       paf       200:                return *result;
                    201:        }
                    202: 
                    203: private:
                    204: 
                    205:        const String *fname;
                    206: 
1.6       paf       207: protected: 
1.1       paf       208: 
1.22      paf       209:        /// throws exception specifying bark-reason and name() type() of problematic value
                    210:        void bark(char *reason) const {
1.44      paf       211:                THROW(0, 0,
1.1       paf       212:                        &name(),
1.22      paf       213:                        reason, type());
1.1       paf       214:        }
                    215: 
1.17      paf       216: };
                    217: 
1.23      paf       218: /** \b junction is some code joined with context of it's evaluation.
1.22      paf       219: 
                    220:        there are code-junctions and method-junctions
                    221:        - code-junctions are used when some parameter passed in cury brackets
                    222:        - method-junctions used in ^method[] calls or $method references
                    223: */
1.17      paf       224: class Junction : public Pooled {
                    225: public:
                    226: 
                    227:        Junction(Pool& apool,
                    228:                Value& aself,
                    229:                VStateless_class *avclass, const Method *amethod,
                    230:                Value *aroot,
                    231:                Value *arcontext,
                    232:                WContext *awcontext,
                    233:                const Array *acode) : Pooled(apool),
                    234:                
                    235:                self(aself),
                    236:                vclass(avclass), method(amethod),
                    237:                root(aroot),
                    238:                rcontext(arcontext),
                    239:                wcontext(awcontext),
                    240:                code(acode) {
                    241:        }
                    242: 
1.22      paf       243:        /// always present
1.17      paf       244:        Value& self;
1.22      paf       245:        //@{
                    246:        /// @name either these // so called 'method-junction'
1.17      paf       247:        VStateless_class *vclass;  const Method *method;
1.22      paf       248:        //@}
                    249:        //@{
                    250:        /// @name or these are present // so called 'code-junction'
1.17      paf       251:        Value *root;
                    252:        Value *rcontext;
                    253:        WContext *wcontext;
                    254:        const Array *code;
1.22      paf       255:        //@}
1.17      paf       256: };
                    257: 
1.48      paf       258: /**
                    259:        @b method parameters passed in this array.
                    260:        contains handy typecast ad junction/not junction ensurers
                    261: 
                    262: */
                    263: class MethodParams : public Array {
                    264: public:
                    265:        MethodParams(Pool& pool, const String& amethod_name) : Array(pool),
                    266:                fmethod_name(amethod_name) {
                    267:        }
                    268: 
                    269:        /// handy typecast. I long for templates
1.49      paf       270:        Value& get(int index) { return *static_cast<Value *>(Array::get(index)); }
1.48      paf       271:        /// handy is-value-a-junction ensurer
1.49      paf       272:        Value& get_junction(int index, const char *msg) { return get_as(index, true, msg); }
1.48      paf       273:        /// handy value-is-not-a-junction ensurer
1.49      paf       274:        Value& get_no_junction(int index, const char *msg) { return get_as(index, false, msg); }
1.48      paf       275: 
                    276: private:
                    277: 
                    278:        /// handy value-is/not-a-junction ensurer
1.49      paf       279:        Value& get_as(int index, bool as_junction, const char *msg) { 
1.48      paf       280:                Value& result=get(index);
1.49      paf       281:                if((result.get_junction()!=0) ^ as_junction)
1.48      paf       282:                        THROW(0, 0,
                    283:                                &fmethod_name,
1.61      parser    284:                                "%s (parameter #%d)", msg, 1+index);
1.48      paf       285:                return result;
                    286:        }
                    287: 
                    288: private:
                    289: 
                    290:        const String& fmethod_name;
                    291: 
                    292: };
                    293: 
                    294: /**
                    295:        native code method
                    296:        params can be NULL when 
1.53      paf       297:        method min&max params (see VStateless_class::add_native_method)
1.48      paf       298:        counts are zero.        
                    299: */
1.39      paf       300: typedef void (*Native_code_ptr)(Request& request, 
                    301:                                                                const String& method_name, 
1.48      paf       302:                                                                MethodParams *params);
1.39      paf       303: 
1.23      paf       304: /** 
1.22      paf       305:        class method.
                    306: 
                    307:        methods can have 
                    308:        - named or
                    309:        - numbered parameters
                    310: 
                    311:        methods can be
                    312:        - parser or 
                    313:        - native onces
                    314: 
1.40      paf       315:        holds
1.22      paf       316:        - parameter names or number limits
                    317:        - local names
                    318:        - code [parser or native]
                    319: */
1.17      paf       320: class Method : public Pooled {
                    321: public:
1.39      paf       322: 
1.41      paf       323:        /// allowed method call types
1.39      paf       324:        enum Call_type {
1.41      paf       325:                CT_ANY, ///< method can be called either statically or dynamically
                    326:                CT_STATIC, ///< method can be called only statically
                    327:                CT_DYNAMIC ///< method can be called only dynamically
1.39      paf       328:        };
                    329:        
                    330:        /// name for error reporting
1.17      paf       331:        const String& name;
1.39      paf       332:        ///
                    333:        Call_type call_type;
1.22      paf       334:        //@{
                    335:        /// @name either numbered params // for native-code methods = operators
1.17      paf       336:        int min_numbered_params_count, max_numbered_params_count;
1.22      paf       337:        //@}
                    338:        //@{
                    339:        /// @name or named params&locals // for parser-code methods
1.17      paf       340:        Array *params_names;  Array *locals_names;
1.22      paf       341:        //@}
                    342:        //@{
                    343:        /// @name the Code
1.17      paf       344:        const Array *parser_code;/*OR*/Native_code_ptr native_code;
1.22      paf       345:        //@}
1.17      paf       346: 
                    347:        Method(
                    348:                Pool& apool,
                    349:                const String& aname,
1.39      paf       350:                Call_type call_type,
1.17      paf       351:                int amin_numbered_params_count, int amax_numbered_params_count,
                    352:                Array *aparams_names, Array *alocals_names,
                    353:                const Array *aparser_code, Native_code_ptr anative_code) : 
                    354: 
                    355:                Pooled(apool),
                    356:                name(aname),
1.39      paf       357:                call_type(call_type),
1.17      paf       358:                min_numbered_params_count(amin_numbered_params_count),
                    359:                max_numbered_params_count(amax_numbered_params_count),
                    360:                params_names(aparams_names), locals_names(alocals_names),
                    361:                parser_code(aparser_code), native_code(anative_code) {
                    362:        }
                    363: 
1.22      paf       364:        /// call this before invoking to ensure proper actual numbered params count
1.55      paf       365:        void check_actual_numbered_params(Pool& pool,
1.17      paf       366:                Value& self, const String& actual_name, Array *actual_numbered_params) const {
1.54      paf       367: 
1.17      paf       368:                int actual_count=actual_numbered_params?actual_numbered_params->size():0;
                    369:                if(actual_count<min_numbered_params_count) // not proper count? bark
1.54      paf       370:                        PTHROW(0, 0,
1.17      paf       371:                                &actual_name,
1.47      paf       372:                                "native method of %s (%s) accepts minimum %d parameter(s) (%d present)", 
1.17      paf       373:                                        self.name().cstr(),
                    374:                                        self.type(),
1.47      paf       375:                                        min_numbered_params_count,
                    376:                                        actual_count);
1.17      paf       377: 
                    378:        }
1.1       paf       379: };
                    380: 
                    381: #endif

E-mail: