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

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.68    ! parser      8:        $Id: pa_value.h,v 1.67 2001/07/20 09:40:46 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.68    ! parser    205:        void set_name(const String& aname) {
        !           206:                fname=&aname; 
        !           207:        }
1.1       paf       208: 
1.26      paf       209:        /// @return sure String. if it doesn't have string value barks
1.1       paf       210:        const String& as_string() {
                    211:                const String *result=get_string(); 
                    212:                if(!result)
1.35      paf       213:                        bark("(%s) has no string representation");
1.6       paf       214: 
1.1       paf       215:                return *result;
                    216:        }
                    217: 
                    218: private:
                    219: 
                    220:        const String *fname;
                    221: 
1.6       paf       222: protected: 
1.1       paf       223: 
1.22      paf       224:        /// throws exception specifying bark-reason and name() type() of problematic value
1.65      parser    225:        void bark(char *reason, 
                    226:                const char *alt_reason=0, const String *problem_source=0) const {
                    227:                Pool& pool=problem_source?problem_source->pool():this->pool();
                    228:                PTHROW(0, 0,
                    229:                        problem_source?problem_source:&name(),
                    230:                        problem_source?alt_reason:reason, type());
1.1       paf       231:        }
                    232: 
1.17      paf       233: };
                    234: 
1.23      paf       235: /** \b junction is some code joined with context of it's evaluation.
1.22      paf       236: 
                    237:        there are code-junctions and method-junctions
                    238:        - code-junctions are used when some parameter passed in cury brackets
                    239:        - method-junctions used in ^method[] calls or $method references
                    240: */
1.17      paf       241: class Junction : public Pooled {
                    242: public:
                    243: 
                    244:        Junction(Pool& apool,
                    245:                Value& aself,
                    246:                VStateless_class *avclass, const Method *amethod,
                    247:                Value *aroot,
                    248:                Value *arcontext,
                    249:                WContext *awcontext,
                    250:                const Array *acode) : Pooled(apool),
                    251:                
                    252:                self(aself),
                    253:                vclass(avclass), method(amethod),
                    254:                root(aroot),
                    255:                rcontext(arcontext),
                    256:                wcontext(awcontext),
                    257:                code(acode) {
                    258:        }
                    259: 
1.22      paf       260:        /// always present
1.17      paf       261:        Value& self;
1.22      paf       262:        //@{
                    263:        /// @name either these // so called 'method-junction'
1.17      paf       264:        VStateless_class *vclass;  const Method *method;
1.22      paf       265:        //@}
                    266:        //@{
                    267:        /// @name or these are present // so called 'code-junction'
1.17      paf       268:        Value *root;
                    269:        Value *rcontext;
                    270:        WContext *wcontext;
                    271:        const Array *code;
1.22      paf       272:        //@}
1.48      paf       273: };
                    274: 
                    275: /**
                    276:        native code method
                    277:        params can be NULL when 
1.53      paf       278:        method min&max params (see VStateless_class::add_native_method)
1.48      paf       279:        counts are zero.        
                    280: */
1.39      paf       281: typedef void (*Native_code_ptr)(Request& request, 
                    282:                                                                const String& method_name, 
1.48      paf       283:                                                                MethodParams *params);
1.39      paf       284: 
1.23      paf       285: /** 
1.22      paf       286:        class method.
                    287: 
                    288:        methods can have 
                    289:        - named or
                    290:        - numbered parameters
                    291: 
                    292:        methods can be
                    293:        - parser or 
                    294:        - native onces
                    295: 
1.40      paf       296:        holds
1.22      paf       297:        - parameter names or number limits
                    298:        - local names
                    299:        - code [parser or native]
                    300: */
1.17      paf       301: class Method : public Pooled {
                    302: public:
1.39      paf       303: 
1.41      paf       304:        /// allowed method call types
1.39      paf       305:        enum Call_type {
1.41      paf       306:                CT_ANY, ///< method can be called either statically or dynamically
                    307:                CT_STATIC, ///< method can be called only statically
                    308:                CT_DYNAMIC ///< method can be called only dynamically
1.39      paf       309:        };
                    310:        
                    311:        /// name for error reporting
1.17      paf       312:        const String& name;
1.39      paf       313:        ///
                    314:        Call_type call_type;
1.22      paf       315:        //@{
                    316:        /// @name either numbered params // for native-code methods = operators
1.17      paf       317:        int min_numbered_params_count, max_numbered_params_count;
1.22      paf       318:        //@}
                    319:        //@{
                    320:        /// @name or named params&locals // for parser-code methods
1.17      paf       321:        Array *params_names;  Array *locals_names;
1.22      paf       322:        //@}
                    323:        //@{
                    324:        /// @name the Code
1.17      paf       325:        const Array *parser_code;/*OR*/Native_code_ptr native_code;
1.22      paf       326:        //@}
1.17      paf       327: 
                    328:        Method(
                    329:                Pool& apool,
                    330:                const String& aname,
1.39      paf       331:                Call_type call_type,
1.17      paf       332:                int amin_numbered_params_count, int amax_numbered_params_count,
                    333:                Array *aparams_names, Array *alocals_names,
                    334:                const Array *aparser_code, Native_code_ptr anative_code) : 
                    335: 
                    336:                Pooled(apool),
                    337:                name(aname),
1.39      paf       338:                call_type(call_type),
1.17      paf       339:                min_numbered_params_count(amin_numbered_params_count),
                    340:                max_numbered_params_count(amax_numbered_params_count),
                    341:                params_names(aparams_names), locals_names(alocals_names),
                    342:                parser_code(aparser_code), native_code(anative_code) {
                    343:        }
                    344: 
1.22      paf       345:        /// call this before invoking to ensure proper actual numbered params count
1.55      paf       346:        void check_actual_numbered_params(Pool& pool,
1.17      paf       347:                Value& self, const String& actual_name, Array *actual_numbered_params) const {
1.54      paf       348: 
1.17      paf       349:                int actual_count=actual_numbered_params?actual_numbered_params->size():0;
                    350:                if(actual_count<min_numbered_params_count) // not proper count? bark
1.54      paf       351:                        PTHROW(0, 0,
1.17      paf       352:                                &actual_name,
1.47      paf       353:                                "native method of %s (%s) accepts minimum %d parameter(s) (%d present)", 
1.17      paf       354:                                        self.name().cstr(),
                    355:                                        self.type(),
1.47      paf       356:                                        min_numbered_params_count,
                    357:                                        actual_count);
1.17      paf       358: 
                    359:        }
1.1       paf       360: };
                    361: 
                    362: #endif

E-mail: