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

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

E-mail: