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

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

E-mail: