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

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

E-mail: