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

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: 
1.84    ! paf         7:        $Id: pa_value.h,v 1.83 2002/04/15 13:39:54 paf Exp $
1.1       paf         8: */
                      9: 
                     10: #ifndef PA_VALUE_H
                     11: #define PA_VALUE_H
                     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 VAliased;
                     22: class Request;
1.35      paf        23: class Table;
1.17      paf        24: class Junction;
                     25: class Method;
1.20      paf        26: class Hash;
1.42      paf        27: class VFile;
1.64      parser     28: class MethodParams;
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.23      paf        37:        /** is this value defined?
1.22      paf        38:                @return for
1.63      parser     39:                - VVoid: false
1.69      parser     40:                - VString: eq ''=false, ne ''=true
1.72      parser     41:                - VHash: count!=0
1.71      parser     42:                - VTable: count!=0
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.70      parser     57:                - VBool: clone
                     58:                - VDouble: clone
                     59:                - VInt: clone
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.72      parser     64:                - VTable: count
                     65:                - VHash: count
1.22      paf        66:        */
1.75      parser     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.76      parser     76:        virtual Hash *get_hash(const String * /*source*/) { 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.72      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.73      parser    107:                - VTable: count
                    108:                - VHash: count
1.59      parser    109:        */
1.72      parser    110:        virtual int as_int () const { bark("(%s) does not have numerical (int) value"); return 0; }
1.59      parser    111: 
1.23      paf       112:        /** extract bool
1.22      paf       113:                @return for
1.63      parser    114:                - VVoid: false
1.25      paf       115:                - VBool: value
                    116:                - VInt: 0 or !0
                    117:                - VDouble: 0 or !0
1.27      paf       118:                - VFile: true
1.64      parser    119:                - VDate: 0 or !0
1.73      parser    120:                - VTable: count
                    121:                - VHash: count
1.22      paf       122:        */
1.72      parser    123:        virtual bool as_bool() const { bark("(%s) does not have logical value"); return 0; }
1.59      parser    124:        
1.42      paf       125:        /** extract file
                    126:                @return for
                    127:                - VFile: this
                    128:                - VString: vfile
1.45      paf       129:                - VImage: true
1.42      paf       130:        */
1.75      parser    131:        virtual VFile *as_vfile(String::Untaint_lang /*lang*/=String::UL_UNSPECIFIED,
                    132:                bool /*origins_mode*/=false) { 
1.42      paf       133:                bark("(%s) does not have file value"); return 0; 
                    134:        }
1.59      parser    135:        
1.23      paf       136:        /** extract Junction
1.22      paf       137:                @return for
                    138:                - junction: itself
                    139:        */
1.1       paf       140:        virtual Junction *get_junction() { return 0; }
1.59      parser    141:        
1.23      paf       142:        /** extract Value element
1.22      paf       143:                @return for
1.25      paf       144:                - VHash: (key)=value
1.67      parser    145:                - VAliased: sometimes $CLASS [see VAliased::hide_class()]
1.29      paf       146:                - VStateless_class: +$method
                    147:                - VStateless_object: +$method
1.58      paf       148:                - VClass: (field)=STATIC value;(method)=method_ref with self=object_class
1.51      paf       149:                - VCodeFrame: wcontext_transparent
                    150:                - VMethodFrame: my or self_transparent
1.52      paf       151:                - VTable: columns,methods
1.28      paf       152:                - VEnv: field
1.67      parser    153:                - VForm: CLASS,method,field
1.45      paf       154:                - VString: $method
1.30      paf       155:                - VRequest: fields
1.45      paf       156:                - VResponse: method,fields
1.28      paf       157:                - VCookie: field
1.45      paf       158:                - VFile: method,field
1.67      parser    159:                - VDate: CLASS,method,field
1.26      paf       160:                */
1.75      parser    161:        virtual Value *get_element(const String& /*name*/) { bark("(%s) has no elements"); return 0; }
1.59      parser    162:        
1.31      paf       163:        /** store Value element under @a name
1.22      paf       164:                @return for
1.25      paf       165:                - VHash: (key)=value
1.67      parser    166:                - VStateless_object: (CLASS)=vclass;(method)=method_ref
1.26      paf       167:                - VStateless_class: (field)=value - static values only
                    168:                - VStateless_object: (field)=value
1.51      paf       169:                - VCodeFrame: wcontext_transparent
                    170:                - VMethodFrame: my or self_transparent
1.25      paf       171:                - VResponse: (attribute)=value
                    172:                - VCookie: field
1.22      paf       173:        */
1.75      parser    174:        virtual void put_element(const String& name, Value * /*value*/) { 
1.65      parser    175:                // to prevent modification of system classes,
                    176:                // created at system startup, and not having exception
                    177:                // handler installed, we neet to bark using request.pool
                    178:                bark("(%s) does not accept elements", 
                    179:                        "element can not be stored to %s", &name); 
                    180:        }
1.59      parser    181:        
1.23      paf       182:        /** extract VStateless_class
1.22      paf       183:                @return for
1.25      paf       184:                - VStateless_class: this
                    185:                - VStateless_object: fclass_real
1.26      paf       186:                - WContext: none yet | transparent
1.57      paf       187:                - VHash: 0
1.22      paf       188:        */
1.9       paf       189:        virtual VStateless_class *get_class() { return 0; }
1.59      parser    190:        
1.23      paf       191:        /** extract VAliased
1.22      paf       192:                @return for
1.33      paf       193:                - VAliased: this
1.26      paf       194:                - WContext: transparent
1.51      paf       195:                - VMethodFrame: self_transparent
1.22      paf       196:        */
1.1       paf       197:        virtual VAliased *get_aliased() { return 0; }
1.32      paf       198: 
                    199:        /** extract VTable
                    200:                @return for
1.35      paf       201:                - VTable: ftable
1.32      paf       202:        */
1.35      paf       203:        virtual Table *get_table() { return 0; }
1.1       paf       204: 
                    205: public: // usage
                    206: 
1.84    ! paf       207:        Value(Pool& apool) : Pooled(apool) {
1.68      parser    208:        }
1.1       paf       209: 
1.26      paf       210:        /// @return sure String. if it doesn't have string value barks
1.1       paf       211:        const String& as_string() {
                    212:                const String *result=get_string(); 
                    213:                if(!result)
1.35      paf       214:                        bark("(%s) has no string representation");
1.6       paf       215: 
1.1       paf       216:                return *result;
                    217:        }
                    218: 
1.6       paf       219: protected: 
1.1       paf       220: 
1.22      paf       221:        /// throws exception specifying bark-reason and name() type() of problematic value
1.65      parser    222:        void bark(char *reason, 
                    223:                const char *alt_reason=0, const String *problem_source=0) const {
1.80      paf       224:                throw Exception("parser.runtime",
1.84    ! paf       225:                        problem_source,
        !           226:                        alt_reason?alt_reason:reason, type());
1.1       paf       227:        }
                    228: 
1.17      paf       229: };
                    230: 
1.23      paf       231: /** \b junction is some code joined with context of it's evaluation.
1.22      paf       232: 
                    233:        there are code-junctions and method-junctions
                    234:        - code-junctions are used when some parameter passed in cury brackets
                    235:        - method-junctions used in ^method[] calls or $method references
                    236: */
1.17      paf       237: class Junction : public Pooled {
                    238: public:
                    239: 
                    240:        Junction(Pool& apool,
                    241:                Value& aself,
                    242:                VStateless_class *avclass, const Method *amethod,
                    243:                Value *aroot,
                    244:                Value *arcontext,
                    245:                WContext *awcontext,
                    246:                const Array *acode) : Pooled(apool),
                    247:                
                    248:                self(aself),
                    249:                vclass(avclass), method(amethod),
                    250:                root(aroot),
                    251:                rcontext(arcontext),
                    252:                wcontext(awcontext),
                    253:                code(acode) {
1.81      paf       254:        }
                    255: 
                    256:        void change_context(Junction *source) {
                    257:                if(source) {
                    258:                        root=source->root;
                    259:                        rcontext=source->rcontext;
                    260:                        wcontext=source->wcontext;
                    261:                } else {
                    262:                        root=rcontext=0;
                    263:                        wcontext=0;
                    264:                }
1.17      paf       265:        }
                    266: 
1.22      paf       267:        /// always present
1.17      paf       268:        Value& self;
1.22      paf       269:        //@{
                    270:        /// @name either these // so called 'method-junction'
1.17      paf       271:        VStateless_class *vclass;  const Method *method;
1.22      paf       272:        //@}
                    273:        //@{
                    274:        /// @name or these are present // so called 'code-junction'
1.17      paf       275:        Value *root;
                    276:        Value *rcontext;
                    277:        WContext *wcontext;
                    278:        const Array *code;
1.22      paf       279:        //@}
1.48      paf       280: };
                    281: 
                    282: /**
                    283:        native code method
                    284:        params can be NULL when 
1.53      paf       285:        method min&max params (see VStateless_class::add_native_method)
1.48      paf       286:        counts are zero.        
                    287: */
1.39      paf       288: typedef void (*Native_code_ptr)(Request& request, 
                    289:                                                                const String& method_name, 
1.48      paf       290:                                                                MethodParams *params);
1.39      paf       291: 
1.23      paf       292: /** 
1.22      paf       293:        class method.
                    294: 
                    295:        methods can have 
                    296:        - named or
                    297:        - numbered parameters
                    298: 
                    299:        methods can be
                    300:        - parser or 
                    301:        - native onces
                    302: 
1.40      paf       303:        holds
1.22      paf       304:        - parameter names or number limits
                    305:        - local names
                    306:        - code [parser or native]
                    307: */
1.17      paf       308: class Method : public Pooled {
                    309: public:
1.39      paf       310: 
1.41      paf       311:        /// allowed method call types
1.39      paf       312:        enum Call_type {
1.41      paf       313:                CT_ANY, ///< method can be called either statically or dynamically
                    314:                CT_STATIC, ///< method can be called only statically
                    315:                CT_DYNAMIC ///< method can be called only dynamically
1.39      paf       316:        };
                    317:        
                    318:        /// name for error reporting
1.17      paf       319:        const String& name;
1.39      paf       320:        ///
                    321:        Call_type call_type;
1.22      paf       322:        //@{
                    323:        /// @name either numbered params // for native-code methods = operators
1.17      paf       324:        int min_numbered_params_count, max_numbered_params_count;
1.22      paf       325:        //@}
                    326:        //@{
                    327:        /// @name or named params&locals // for parser-code methods
1.17      paf       328:        Array *params_names;  Array *locals_names;
1.22      paf       329:        //@}
                    330:        //@{
                    331:        /// @name the Code
1.17      paf       332:        const Array *parser_code;/*OR*/Native_code_ptr native_code;
1.22      paf       333:        //@}
1.17      paf       334: 
                    335:        Method(
                    336:                Pool& apool,
                    337:                const String& aname,
1.39      paf       338:                Call_type call_type,
1.17      paf       339:                int amin_numbered_params_count, int amax_numbered_params_count,
                    340:                Array *aparams_names, Array *alocals_names,
                    341:                const Array *aparser_code, Native_code_ptr anative_code) : 
                    342: 
                    343:                Pooled(apool),
                    344:                name(aname),
1.39      paf       345:                call_type(call_type),
1.17      paf       346:                min_numbered_params_count(amin_numbered_params_count),
                    347:                max_numbered_params_count(amax_numbered_params_count),
                    348:                params_names(aparams_names), locals_names(alocals_names),
                    349:                parser_code(aparser_code), native_code(anative_code) {
                    350:        }
                    351: 
1.22      paf       352:        /// call this before invoking to ensure proper actual numbered params count
1.75      parser    353:        void check_actual_numbered_params(
1.84    ! paf       354:                Value& self, const String& actual_name, Array *actual_numbered_params) const;
1.1       paf       355: };
                    356: 
                    357: #endif

E-mail: