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

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

E-mail: