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

1.22      paf         1: /** @file
1.24      paf         2:        Parser: Value, Method, Junction class decls.
                      3: 
1.78      paf         4:        Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.79      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1       paf         6: */
                      7: 
                      8: #ifndef PA_VALUE_H
                      9: #define PA_VALUE_H
1.87      paf        10: 
1.97    ! paf        11: static const char* IDENT_VALUE_H="$Date: 2002/08/15 07:53:07 $";
1.1       paf        12: 
                     13: #include "pa_pool.h"
                     14: #include "pa_string.h"
                     15: #include "pa_array.h"
                     16: #include "pa_exception.h"
1.13      paf        17: #include "pa_globals.h"
1.1       paf        18: 
1.9       paf        19: class VStateless_class;
1.1       paf        20: class WContext;
                     21: class Request;
1.35      paf        22: class Table;
1.17      paf        23: class Junction;
                     24: class Method;
1.20      paf        25: class Hash;
1.42      paf        26: class VFile;
1.64      parser     27: class MethodParams;
1.93      paf        28: class VObject;
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.89      paf        36: 
1.91      paf        37:        /** remember derived class instance 
                     38:            - VObject: the only client
                     39:        */
1.93      paf        40:        virtual VObject *set_derived(VObject * /*aderived*/) { return 0; }
1.91      paf        41: 
1.89      paf        42:        /**
1.95      paf        43:                all except VObject/VClass: this if @atype eq type()
                     44:                VObject/VClass: can locate parent class by it's type
1.89      paf        45:        */
1.96      paf        46:        virtual Value *as(const char *atype, bool looking_up) {
                     47:                return atype && strcmp(type(), atype)==0?this:0;
1.89      paf        48:        }
1.97    ! paf        49:        /// type checking helper, uses Value::as
        !            50:        bool is(const char *atype) { return as(atype, false)!=0; }
1.59      parser     51:        
1.23      paf        52:        /** is this value defined?
1.22      paf        53:                @return for
1.63      parser     54:                - VVoid: false
1.69      parser     55:                - VString: eq ''=false, ne ''=true
1.72      parser     56:                - VHash: count!=0
1.71      parser     57:                - VTable: count!=0
1.22      paf        58:                - others: true
                     59:        */
1.38      paf        60:        virtual bool is_defined() const { return true; }
1.59      parser     61:        
1.35      paf        62:        /** is this value string?
                     63:                @return for
                     64:                - VString: true
                     65:                - others: false
                     66:        */
1.38      paf        67:        virtual bool is_string() const { return false; }
1.59      parser     68:        
1.23      paf        69:        /** what's the meaning of this value in context of expression?
1.22      paf        70:                @return for
1.34      paf        71:                - VString: fstring as VDouble or this depending on return_string_as_is
1.70      parser     72:                - VBool: clone
                     73:                - VDouble: clone
                     74:                - VInt: clone
1.63      parser     75:                - VVoid: this
1.27      paf        76:                - VFile: this
1.45      paf        77:                - VImage: this
1.64      parser     78:                - VDate: ftime -> float days
1.72      parser     79:                - VTable: count
                     80:                - VHash: count
1.22      paf        81:        */
1.75      parser     82:        virtual Value *as_expr_result(bool /*return_string_as_is*/=false) { 
1.34      paf        83:                bark("(%s) can not be used in expression"); return 0; 
                     84:        }
1.59      parser     85:        
1.23      paf        86:        /** extract Hash
1.22      paf        87:                @return for
1.25      paf        88:                - VHash: fhash
                     89:                - VResponse: ffields
1.22      paf        90:        */
1.76      parser     91:        virtual Hash *get_hash(const String * /*source*/) { return 0; }
1.59      parser     92:        
1.23      paf        93:        /** extract const String
1.22      paf        94:                @return for
1.25      paf        95:                - VString: value
1.63      parser     96:                - VVoid: ""
1.25      paf        97:                - VDouble: value
1.66      parser     98:                - VInt: value
1.25      paf        99:                - VBool: must be 0: so in ^if(1>2) it would'nt become "FALSE" string which is 'true'
1.22      paf       100:                - others: 0
                    101:                - WContext: accumulated fstring
                    102:        */
1.1       paf       103:        virtual const String *get_string() { return 0; }
1.59      parser    104:        
1.23      paf       105:        /** extract double
1.22      paf       106:                @return for
1.25      paf       107:                - VString: value
                    108:                - VDouble: value
1.59      parser    109:                - VInt: value
1.25      paf       110:                - VBool: value
1.63      parser    111:                - VVoid: 0
1.64      parser    112:                - VDate: ftime -> float days
1.22      paf       113:        */
1.72      parser    114:        virtual double as_double() const { bark("(%s) does not have numerical (double) value"); return 0; }
1.59      parser    115:        
                    116:        /**     extract integer
                    117:                - VString: value
                    118:                - VDouble: value
                    119:                - VInt: value
                    120:                - VBool: value
1.63      parser    121:                - VVoid: 0
1.73      parser    122:                - VTable: count
                    123:                - VHash: count
1.59      parser    124:        */
1.72      parser    125:        virtual int as_int () const { bark("(%s) does not have numerical (int) value"); return 0; }
1.59      parser    126: 
1.23      paf       127:        /** extract bool
1.22      paf       128:                @return for
1.63      parser    129:                - VVoid: false
1.25      paf       130:                - VBool: value
                    131:                - VInt: 0 or !0
                    132:                - VDouble: 0 or !0
1.27      paf       133:                - VFile: true
1.64      parser    134:                - VDate: 0 or !0
1.73      parser    135:                - VTable: count
                    136:                - VHash: count
1.22      paf       137:        */
1.72      parser    138:        virtual bool as_bool() const { bark("(%s) does not have logical value"); return 0; }
1.59      parser    139:        
1.42      paf       140:        /** extract file
                    141:                @return for
                    142:                - VFile: this
                    143:                - VString: vfile
1.45      paf       144:                - VImage: true
1.42      paf       145:        */
1.75      parser    146:        virtual VFile *as_vfile(String::Untaint_lang /*lang*/=String::UL_UNSPECIFIED,
                    147:                bool /*origins_mode*/=false) { 
1.42      paf       148:                bark("(%s) does not have file value"); return 0; 
                    149:        }
1.59      parser    150:        
1.23      paf       151:        /** extract Junction
1.22      paf       152:                @return for
                    153:                - junction: itself
                    154:        */
1.1       paf       155:        virtual Junction *get_junction() { return 0; }
1.59      parser    156:        
1.95      paf       157:        /** extract Value junction of name @a name, when @a looking_up looks only down
1.93      paf       158:                @return for
                    159:                - VStateless_class: self or parent method junction
                    160:                - VObject: child or self or parent method junction
1.95      paf       161:        virtual Junction *get_junction(const String& /*name* /, bool /*looking_up* /) { bark("(%s) has no junctions"); return 0; }
1.93      paf       162:        */
                    163: 
                    164:        /** extract base object of Value
                    165:                @return for
                    166:                - VObject: fbase
                    167:        */
                    168:        virtual Value *base_object() { bark("(%s) has no base object"); return 0; }
                    169:        
1.23      paf       170:        /** extract Value element
1.93      paf       171:                @a self =0 means =this
1.22      paf       172:                @return for
1.25      paf       173:                - VHash: (key)=value
1.29      paf       174:                - VStateless_class: +$method
                    175:                - VStateless_object: +$method
1.58      paf       176:                - VClass: (field)=STATIC value;(method)=method_ref with self=object_class
1.51      paf       177:                - VCodeFrame: wcontext_transparent
                    178:                - VMethodFrame: my or self_transparent
1.52      paf       179:                - VTable: columns,methods
1.28      paf       180:                - VEnv: field
1.67      parser    181:                - VForm: CLASS,method,field
1.45      paf       182:                - VString: $method
1.30      paf       183:                - VRequest: fields
1.45      paf       184:                - VResponse: method,fields
1.28      paf       185:                - VCookie: field
1.45      paf       186:                - VFile: method,field
1.67      parser    187:                - VDate: CLASS,method,field
1.26      paf       188:                */
1.95      paf       189:        virtual Value *get_element(const String& /*aname*/, Value * /*aself*/, bool /*looking_up*/) { bark("(%s) has no elements"); return 0; }
1.92      paf       190: 
1.31      paf       191:        /** store Value element under @a name
1.22      paf       192:                @return for
1.25      paf       193:                - VHash: (key)=value
1.67      parser    194:                - VStateless_object: (CLASS)=vclass;(method)=method_ref
1.26      paf       195:                - VStateless_class: (field)=value - static values only
                    196:                - VStateless_object: (field)=value
1.51      paf       197:                - VCodeFrame: wcontext_transparent
                    198:                - VMethodFrame: my or self_transparent
1.25      paf       199:                - VResponse: (attribute)=value
                    200:                - VCookie: field
1.22      paf       201:        */
1.93      paf       202:        virtual bool put_element(const String& name, Value * /*value*/, bool /*replace*/) { 
1.65      parser    203:                // to prevent modification of system classes,
                    204:                // created at system startup, and not having exception
                    205:                // handler installed, we neet to bark using request.pool
                    206:                bark("(%s) does not accept elements", 
                    207:                        "element can not be stored to %s", &name); 
1.93      paf       208:                return false;
1.65      parser    209:        }
1.59      parser    210:        
1.23      paf       211:        /** extract VStateless_class
1.22      paf       212:                @return for
1.85      paf       213:                - VX: x_class
1.25      paf       214:                - VStateless_class: this
1.92      paf       215:                - VObject: fclass
1.26      paf       216:                - WContext: none yet | transparent
1.85      paf       217:                these are methodless classes:
                    218:                - VBool: 0
                    219:                - VJunction: 0
                    220:                - VEnv: 0
                    221:                - VRequest: 0
                    222:                - VCookie: 0
1.22      paf       223:        */
1.85      paf       224:        virtual VStateless_class *get_class()=0;
1.32      paf       225: 
                    226:        /** extract VTable
                    227:                @return for
1.35      paf       228:                - VTable: ftable
1.97    ! paf       229:                - VObject: from possible 'table' parent
1.32      paf       230:        */
1.35      paf       231:        virtual Table *get_table() { return 0; }
1.1       paf       232: 
                    233: public: // usage
                    234: 
1.84      paf       235:        Value(Pool& apool) : Pooled(apool) {
1.68      parser    236:        }
1.1       paf       237: 
1.26      paf       238:        /// @return sure String. if it doesn't have string value barks
1.1       paf       239:        const String& as_string() {
                    240:                const String *result=get_string(); 
                    241:                if(!result)
1.35      paf       242:                        bark("(%s) has no string representation");
1.6       paf       243: 
1.1       paf       244:                return *result;
                    245:        }
                    246: 
1.6       paf       247: protected: 
1.1       paf       248: 
1.22      paf       249:        /// throws exception specifying bark-reason and name() type() of problematic value
1.65      parser    250:        void bark(char *reason, 
                    251:                const char *alt_reason=0, const String *problem_source=0) const {
1.80      paf       252:                throw Exception("parser.runtime",
1.84      paf       253:                        problem_source,
                    254:                        alt_reason?alt_reason:reason, type());
1.1       paf       255:        }
                    256: 
1.17      paf       257: };
                    258: 
1.23      paf       259: /** \b junction is some code joined with context of it's evaluation.
1.22      paf       260: 
                    261:        there are code-junctions and method-junctions
                    262:        - code-junctions are used when some parameter passed in cury brackets
                    263:        - method-junctions used in ^method[] calls or $method references
                    264: */
1.17      paf       265: class Junction : public Pooled {
                    266: public:
                    267: 
                    268:        Junction(Pool& apool,
                    269:                Value& aself,
                    270:                VStateless_class *avclass, const Method *amethod,
                    271:                Value *aroot,
                    272:                Value *arcontext,
                    273:                WContext *awcontext,
                    274:                const Array *acode) : Pooled(apool),
                    275:                
                    276:                self(aself),
                    277:                vclass(avclass), method(amethod),
                    278:                root(aroot),
                    279:                rcontext(arcontext),
                    280:                wcontext(awcontext),
                    281:                code(acode) {
1.81      paf       282:        }
                    283: 
                    284:        void change_context(Junction *source) {
                    285:                if(source) {
                    286:                        root=source->root;
                    287:                        rcontext=source->rcontext;
                    288:                        wcontext=source->wcontext;
                    289:                } else {
                    290:                        root=rcontext=0;
                    291:                        wcontext=0;
                    292:                }
1.17      paf       293:        }
                    294: 
1.22      paf       295:        /// always present
1.17      paf       296:        Value& self;
1.22      paf       297:        //@{
                    298:        /// @name either these // so called 'method-junction'
1.17      paf       299:        VStateless_class *vclass;  const Method *method;
1.22      paf       300:        //@}
                    301:        //@{
                    302:        /// @name or these are present // so called 'code-junction'
1.17      paf       303:        Value *root;
                    304:        Value *rcontext;
                    305:        WContext *wcontext;
                    306:        const Array *code;
1.22      paf       307:        //@}
1.48      paf       308: };
                    309: 
                    310: /**
                    311:        native code method
                    312:        params can be NULL when 
1.53      paf       313:        method min&max params (see VStateless_class::add_native_method)
1.48      paf       314:        counts are zero.        
                    315: */
1.39      paf       316: typedef void (*Native_code_ptr)(Request& request, 
                    317:                                                                const String& method_name, 
1.48      paf       318:                                                                MethodParams *params);
1.39      paf       319: 
1.23      paf       320: /** 
1.22      paf       321:        class method.
                    322: 
                    323:        methods can have 
                    324:        - named or
                    325:        - numbered parameters
                    326: 
                    327:        methods can be
                    328:        - parser or 
                    329:        - native onces
                    330: 
1.40      paf       331:        holds
1.22      paf       332:        - parameter names or number limits
                    333:        - local names
                    334:        - code [parser or native]
                    335: */
1.17      paf       336: class Method : public Pooled {
                    337: public:
1.39      paf       338: 
1.41      paf       339:        /// allowed method call types
1.39      paf       340:        enum Call_type {
1.41      paf       341:                CT_ANY, ///< method can be called either statically or dynamically
                    342:                CT_STATIC, ///< method can be called only statically
                    343:                CT_DYNAMIC ///< method can be called only dynamically
1.39      paf       344:        };
                    345:        
                    346:        /// name for error reporting
1.17      paf       347:        const String& name;
1.39      paf       348:        ///
                    349:        Call_type call_type;
1.22      paf       350:        //@{
                    351:        /// @name either numbered params // for native-code methods = operators
1.17      paf       352:        int min_numbered_params_count, max_numbered_params_count;
1.22      paf       353:        //@}
                    354:        //@{
                    355:        /// @name or named params&locals // for parser-code methods
1.17      paf       356:        Array *params_names;  Array *locals_names;
1.22      paf       357:        //@}
                    358:        //@{
                    359:        /// @name the Code
1.17      paf       360:        const Array *parser_code;/*OR*/Native_code_ptr native_code;
1.22      paf       361:        //@}
1.17      paf       362: 
                    363:        Method(
                    364:                Pool& apool,
                    365:                const String& aname,
1.39      paf       366:                Call_type call_type,
1.17      paf       367:                int amin_numbered_params_count, int amax_numbered_params_count,
                    368:                Array *aparams_names, Array *alocals_names,
                    369:                const Array *aparser_code, Native_code_ptr anative_code) : 
                    370: 
                    371:                Pooled(apool),
                    372:                name(aname),
1.39      paf       373:                call_type(call_type),
1.17      paf       374:                min_numbered_params_count(amin_numbered_params_count),
                    375:                max_numbered_params_count(amax_numbered_params_count),
                    376:                params_names(aparams_names), locals_names(alocals_names),
                    377:                parser_code(aparser_code), native_code(anative_code) {
                    378:        }
                    379: 
1.22      paf       380:        /// call this before invoking to ensure proper actual numbered params count
1.75      parser    381:        void check_actual_numbered_params(
1.84      paf       382:                Value& self, const String& actual_name, Array *actual_numbered_params) const;
1.86      paf       383: };
                    384: 
                    385: ///    Auto-object used for temporarily substituting/removing elements
                    386: class Temp_value_element {
                    387:        Value& fwhere;
                    388:        const String& fname;
                    389:        Value *saved;
                    390: public:
                    391:        Temp_value_element(Value& awhere, const String& aname, Value *awhat) : 
                    392:                fwhere(awhere),
                    393:                fname(aname),
1.94      paf       394:                saved(awhere.get_element(aname, &awhere, false)) {
1.93      paf       395:                fwhere.put_element(aname, awhat, false);
1.86      paf       396:        }
                    397:        ~Temp_value_element() { 
1.93      paf       398:                fwhere.put_element(fname, saved, false);
1.86      paf       399:        }
1.1       paf       400: };
                    401: 
                    402: #endif

E-mail: